2021-09-13 13:14:26 +02:00
|
|
|
//
|
|
|
|
// AccountViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-9-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
2021-09-14 12:21:15 +02:00
|
|
|
import PanModal
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2021-09-13 13:14:26 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
2021-09-14 12:21:15 +02:00
|
|
|
let dragIndicatorView = DragIndicatorView()
|
|
|
|
|
2021-09-17 11:07:46 +02:00
|
|
|
var hasLoaded = false
|
2021-09-13 13:14:26 +02:00
|
|
|
private(set) lazy var tableView: UITableView = {
|
|
|
|
let tableView = UITableView()
|
|
|
|
tableView.register(AccountListTableViewCell.self, forCellReuseIdentifier: String(describing: AccountListTableViewCell.self))
|
2021-09-14 12:21:15 +02:00
|
|
|
tableView.register(AddAccountTableViewCell.self, forCellReuseIdentifier: String(describing: AddAccountTableViewCell.self))
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
tableView.separatorStyle = .none
|
2021-09-16 06:23:47 +02:00
|
|
|
tableView.tableFooterView = UIView()
|
2021-09-13 13:14:26 +02:00
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:21:15 +02:00
|
|
|
// MARK: - PanModalPresentable
|
|
|
|
extension AccountListViewController: PanModalPresentable {
|
|
|
|
var panScrollable: UIScrollView? { tableView }
|
|
|
|
var showDragIndicator: Bool { false }
|
|
|
|
|
|
|
|
var shortFormHeight: PanModalHeight {
|
2021-09-17 11:07:46 +02:00
|
|
|
func calculateHeight(of numberOfItems: Int) -> CGFloat {
|
|
|
|
return CGFloat(numberOfItems * 60 + 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasLoaded {
|
|
|
|
let height = calculateHeight(of: viewModel.diffableDataSource.snapshot().numberOfItems)
|
|
|
|
return .contentHeight(CGFloat(height))
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = viewModel.context.authenticationService.mastodonAuthentications.value.count + 1
|
|
|
|
let height = calculateHeight(of: count)
|
|
|
|
return .contentHeight(height)
|
2021-09-14 12:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var longFormHeight: PanModalHeight {
|
2021-09-17 11:07:46 +02:00
|
|
|
return .maxHeightWithTopInset(0)
|
2021-09-14 12:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:14:26 +02:00
|
|
|
extension AccountListViewController {
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-11-04 08:13:17 +01:00
|
|
|
setupBackgroundColor(theme: ThemeService.shared.currentTheme.value)
|
2021-09-14 12:21:15 +02:00
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackgroundColor(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-09-13 13:14:26 +02:00
|
|
|
navigationItem.rightBarButtonItem = addBarButtonItem
|
|
|
|
|
2021-09-14 12:21:15 +02:00
|
|
|
dragIndicatorView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(dragIndicatorView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
dragIndicatorView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
dragIndicatorView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
dragIndicatorView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
dragIndicatorView.heightAnchor.constraint(equalToConstant: DragIndicatorView.height).priority(.required - 1),
|
|
|
|
])
|
|
|
|
|
2021-09-13 13:14:26 +02:00
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(tableView)
|
|
|
|
NSLayoutConstraint.activate([
|
2021-09-14 12:21:15 +02:00
|
|
|
tableView.topAnchor.constraint(equalTo: dragIndicatorView.bottomAnchor),
|
2021-09-13 13:14:26 +02:00
|
|
|
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
|
|
|
|
)
|
2021-09-15 14:18:19 +02:00
|
|
|
|
2021-09-16 06:23:47 +02:00
|
|
|
viewModel.dataSourceDidUpdate
|
|
|
|
.receive(on: DispatchQueue.main)
|
2021-10-11 13:19:27 +02:00
|
|
|
.sink { [weak self, weak presentingViewController] in
|
2021-09-16 06:23:47 +02:00
|
|
|
guard let self = self else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// the presentingViewController may deinit.
|
|
|
|
// Hold it and check the window to prevent PanModel crash
|
2022-05-07 05:42:10 +02:00
|
|
|
guard let _ = presentingViewController else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
guard self.view.window != nil else { return }
|
|
|
|
|
2021-09-17 11:07:46 +02:00
|
|
|
self.hasLoaded = true
|
2022-01-27 14:23:39 +01:00
|
|
|
self.panModalSetNeedsLayoutUpdate() // <<< may crash the app
|
2021-09-17 11:07:46 +02:00
|
|
|
self.panModalTransition(to: .shortForm)
|
2021-09-16 06:23:47 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-09-15 14:18:19 +02:00
|
|
|
if UIAccessibility.isVoiceOverRunning {
|
|
|
|
let dragIndicatorTapGestureRecognizer = UITapGestureRecognizer.singleTapGestureRecognizer
|
|
|
|
dragIndicatorView.addGestureRecognizer(dragIndicatorTapGestureRecognizer)
|
|
|
|
dragIndicatorTapGestureRecognizer.addTarget(self, action: #selector(AccountListViewController.dragIndicatorTapGestureRecognizerHandler(_:)))
|
|
|
|
dragIndicatorView.isAccessibilityElement = true
|
2021-09-16 12:26:35 +02:00
|
|
|
dragIndicatorView.accessibilityLabel = L10n.Scene.AccountList.dismissAccountSwitcher
|
2021-09-15 14:18:19 +02:00
|
|
|
}
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
2021-09-14 12:21:15 +02:00
|
|
|
|
|
|
|
private func setupBackgroundColor(theme: Theme) {
|
2021-11-04 08:13:17 +01:00
|
|
|
let backgroundColor = UIColor { traitCollection in
|
|
|
|
switch traitCollection.userInterfaceLevel {
|
|
|
|
case .elevated where traitCollection.userInterfaceStyle == .dark:
|
|
|
|
return theme.systemElevatedBackgroundColor
|
|
|
|
default:
|
|
|
|
return theme.systemBackgroundColor.withAlphaComponent(0.9)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view.backgroundColor = backgroundColor
|
2021-09-14 12:21:15 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2021-09-15 14:18:19 +02:00
|
|
|
|
|
|
|
@objc private func dragIndicatorTapGestureRecognizerHandler(_ sender: UITapGestureRecognizer) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2021-09-13 13:14:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2021-09-14 12:21:15 +02:00
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .authentication(let objectID):
|
|
|
|
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)
|
|
|
|
case .addAccount:
|
|
|
|
// TODO: add dismiss entry for welcome scene
|
|
|
|
coordinator.present(scene: .welcome, from: self, transition: .modal(animated: true, completion: nil))
|
|
|
|
}
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
|
|
|
}
|