1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-28 10:40:32 +01:00
metatext-app-ios-iphone-ipad/View Controllers/IdentitiesViewController.swift
2021-01-28 15:15:22 -08:00

64 lines
2.3 KiB
Swift

// Copyright © 2021 Metabolist. All rights reserved.
import SwiftUI
import ViewModels
final class IdentitiesViewController: UITableViewController {
private let viewModel: IdentitiesViewModel
private let rootViewModel: RootViewModel
private lazy var dataSource: IdentitiesDataSource = {
.init(tableView: tableView,
publisher: viewModel.$identities.eraseToAnyPublisher(),
viewModelProvider: viewModel.viewModel(identity:),
deleteAction: { [weak self] in self?.rootViewModel.deleteIdentity(id: $0.id) })
}()
init(viewModel: IdentitiesViewModel, rootViewModel: RootViewModel) {
self.viewModel = viewModel
self.rootViewModel = rootViewModel
super.init(style: .insetGrouped)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = dataSource
}
override func didMove(toParent parent: UIViewController?) {
parent?.navigationItem.title = NSLocalizedString("secondary-navigation.accounts", comment: "")
parent?.navigationItem.rightBarButtonItem = editButtonItem
}
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if case let .identitiy(identityViewModel) = dataSource.itemIdentifier(for: indexPath) {
return identityViewModel.id != viewModel.identityContext.identity.id
}
return true
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = dataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .add:
let addIdentityViewModel = rootViewModel.addIdentityViewModel()
let addIdentityView = AddIdentityView(viewModelClosure: { addIdentityViewModel }, displayWelcome: false)
.environmentObject(rootViewModel)
let addIdentityViewController = UIHostingController(rootView: addIdentityView)
show(addIdentityViewController, sender: self)
case let .identitiy(identityViewModel):
rootViewModel.identitySelected(id: identityViewModel.id)
}
}
}