99 lines
3.3 KiB
Swift
99 lines
3.3 KiB
Swift
//
|
|
// AccountViewController.swift
|
|
// Mastodon
|
|
//
|
|
// Created by Cirno MainasuK on 2021-9-13.
|
|
//
|
|
|
|
#if DEBUG
|
|
|
|
import os.log
|
|
import UIKit
|
|
import Combine
|
|
import CoreDataStack
|
|
|
|
final class AccountListViewController: UIViewController, NeedsDependency {
|
|
|
|
let logger = Logger(subsystem: "AccountListViewController", category: "UI")
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
private(set) lazy var viewModel = AccountListViewModel(context: context)
|
|
|
|
private(set) lazy var addBarButtonItem: UIBarButtonItem = {
|
|
let barButtonItem = UIBarButtonItem(
|
|
image: UIImage(systemName: "plus"),
|
|
style: .plain,
|
|
target: self,
|
|
action: #selector(AccountListViewController.addBarButtonItem(_:))
|
|
)
|
|
return barButtonItem
|
|
}()
|
|
|
|
private(set) lazy var tableView: UITableView = {
|
|
let tableView = UITableView()
|
|
tableView.register(AccountListTableViewCell.self, forCellReuseIdentifier: String(describing: AccountListTableViewCell.self))
|
|
return tableView
|
|
}()
|
|
|
|
}
|
|
|
|
extension AccountListViewController {
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
view.backgroundColor = .systemBackground
|
|
navigationItem.rightBarButtonItem = addBarButtonItem
|
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
view.addSubview(tableView)
|
|
NSLayoutConstraint.activate([
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
])
|
|
|
|
tableView.delegate = self
|
|
viewModel.setupDiffableDataSource(
|
|
tableView: tableView,
|
|
managedObjectContext: context.managedObjectContext
|
|
)
|
|
}
|
|
}
|
|
|
|
extension AccountListViewController {
|
|
|
|
@objc private func addBarButtonItem(_ sender: UIBarButtonItem) {
|
|
logger.debug("\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
coordinator.present(scene: .welcome, from: self, transition: .modal(animated: true, completion: nil))
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - UITableViewDelegate
|
|
extension AccountListViewController: UITableViewDelegate {
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
guard case let .authentication(objectID) = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
assert(Thread.isMainThread)
|
|
|
|
let authentication = context.managedObjectContext.object(with: objectID) as! MastodonAuthentication
|
|
context.authenticationService.activeMastodonUser(domain: authentication.domain, userID: authentication.userID)
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] result in
|
|
guard let self = self else { return }
|
|
self.coordinator.setup()
|
|
}
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
#endif
|