mastodon-ios/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewContr...

506 lines
23 KiB
Swift
Raw Normal View History

//
2021-02-26 11:27:47 +01:00
// MastodonPickServerViewController.swift
// Mastodon
//
// Created by BradGao on 2021/2/20.
//
import os.log
import UIKit
import Combine
2021-02-26 11:27:47 +01:00
final class MastodonPickServerViewController: UIViewController, NeedsDependency {
private var disposeBag = Set<AnyCancellable>()
private var tableViewObservation: NSKeyValueObservation?
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
2021-02-26 11:27:47 +01:00
var viewModel: MastodonPickServerViewModel!
2021-02-25 09:38:24 +01:00
private var expandServerDomainSet = Set<String>()
let emptyStateView = PickServerEmptyStateView()
let tableViewTopPaddingView = UIView() // fix empty state view background display when tableView bounce scrolling
var tableViewTopPaddingViewHeightLayoutConstraint: NSLayoutConstraint!
let tableView: UITableView = {
let tableView = ControlContainableTableView()
tableView.register(PickServerTitleCell.self, forCellReuseIdentifier: String(describing: PickServerTitleCell.self))
tableView.register(PickServerCategoriesCell.self, forCellReuseIdentifier: String(describing: PickServerCategoriesCell.self))
tableView.register(PickServerSearchCell.self, forCellReuseIdentifier: String(describing: PickServerSearchCell.self))
tableView.register(PickServerCell.self, forCellReuseIdentifier: String(describing: PickServerCell.self))
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none
tableView.backgroundColor = .clear
2021-02-25 07:09:19 +01:00
tableView.keyboardDismissMode = .onDrag
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
let nextStepButton: PrimaryActionButton = {
let button = PrimaryActionButton()
2021-02-25 07:41:41 +01:00
button.setTitle(L10n.Common.Controls.Actions.signUp, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
deinit {
tableViewObservation = nil
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
2021-02-26 11:27:47 +01:00
extension MastodonPickServerViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupOnboardingAppearance()
defer { setupNavigationBarBackgroundView() }
view.addSubview(nextStepButton)
NSLayoutConstraint.activate([
2021-02-26 11:27:47 +01:00
nextStepButton.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor, constant: MastodonPickServerViewController.actionButtonMargin),
view.readableContentGuide.trailingAnchor.constraint(equalTo: nextStepButton.trailingAnchor, constant: MastodonPickServerViewController.actionButtonMargin),
nextStepButton.heightAnchor.constraint(equalToConstant: MastodonPickServerViewController.actionButtonHeight).priority(.defaultHigh),
view.layoutMarginsGuide.bottomAnchor.constraint(equalTo: nextStepButton.bottomAnchor, constant: WelcomeViewController.viewBottomPaddingHeight),
])
emptyStateView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(emptyStateView)
NSLayoutConstraint.activate([
emptyStateView.topAnchor.constraint(equalTo: view.topAnchor),
emptyStateView.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
emptyStateView.trailingAnchor.constraint(equalTo: view.readableContentGuide.trailingAnchor),
nextStepButton.topAnchor.constraint(equalTo: emptyStateView.bottomAnchor, constant: 7)
])
// fix AutoLayout warning when observe before view appear
viewModel.viewWillAppear
.receive(on: DispatchQueue.main)
.sink { [weak self] in
guard let self = self else { return }
self.tableViewObservation = self.tableView.observe(\.contentSize, options: [.initial, .new]) { [weak self] tableView, _ in
guard let self = self else { return }
self.updateEmptyStateViewLayout()
}
}
.store(in: &disposeBag)
tableViewTopPaddingView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableViewTopPaddingView)
tableViewTopPaddingViewHeightLayoutConstraint = tableViewTopPaddingView.heightAnchor.constraint(equalToConstant: 0.0).priority(.defaultHigh)
NSLayoutConstraint.activate([
tableViewTopPaddingView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
tableViewTopPaddingView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableViewTopPaddingView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableViewTopPaddingViewHeightLayoutConstraint,
])
tableViewTopPaddingView.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
nextStepButton.topAnchor.constraint(equalTo: tableView.bottomAnchor, constant: 7)
])
switch viewModel.mode {
2021-02-25 09:38:24 +01:00
case .signIn:
nextStepButton.setTitle(L10n.Common.Controls.Actions.signIn, for: .normal)
2021-02-25 09:38:24 +01:00
case .signUp:
nextStepButton.setTitle(L10n.Common.Controls.Actions.continue, for: .normal)
}
2021-02-25 07:09:19 +01:00
nextStepButton.addTarget(self, action: #selector(nextStepButtonDidClicked(_:)), for: .touchUpInside)
2021-02-25 09:38:24 +01:00
tableView.delegate = self
viewModel.setupDiffableDataSource(
for: tableView,
dependency: self,
pickServerSearchCellDelegate: self,
pickServerCellDelegate: self
)
2021-02-25 07:09:19 +01:00
viewModel
.selectedServer
.map { $0 != nil }
2021-02-25 07:09:19 +01:00
.assign(to: \.isEnabled, on: nextStepButton)
.store(in: &disposeBag)
viewModel.error
.compactMap { $0 }
.receive(on: DispatchQueue.main)
.sink { [weak self] error in
guard let self = self else { return }
2021-02-25 09:52:41 +01:00
let alertController = UIAlertController(for: error, title: "Error", preferredStyle: .alert)
2021-02-25 07:09:19 +01:00
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil)
alertController.addAction(okAction)
self.coordinator.present(
scene: .alertController(alertController: alertController),
from: nil,
transition: .alertController(animated: true, completion: nil)
)
}
.store(in: &disposeBag)
viewModel
.authenticated
.flatMap { [weak self] (domain, user) -> AnyPublisher<Result<Bool, Error>, Never> in
guard let self = self else { return Just(.success(false)).eraseToAnyPublisher() }
return self.context.authenticationService.activeMastodonUser(domain: domain, userID: user.id)
}
2021-02-26 11:27:47 +01:00
.receive(on: DispatchQueue.main)
2021-02-25 07:09:19 +01:00
.sink { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
assertionFailure(error.localizedDescription)
case .success(let isActived):
assert(isActived)
2021-02-26 11:27:47 +01:00
self.dismiss(animated: true, completion: nil)
2021-02-25 07:09:19 +01:00
}
}
.store(in: &disposeBag)
viewModel.isAuthenticating
2021-02-25 09:38:24 +01:00
.receive(on: DispatchQueue.main)
.sink { [weak self] isAuthenticating in
guard let self = self else { return }
isAuthenticating ? self.nextStepButton.showLoading() : self.nextStepButton.stopLoading()
2021-02-25 09:38:24 +01:00
}
.store(in: &disposeBag)
viewModel.emptyStateViewState
.receive(on: DispatchQueue.main)
.sink { [weak self] state in
guard let self = self else { return }
switch state {
case .none:
self.emptyStateView.networkIndicatorImageView.isHidden = true
self.emptyStateView.activityIndicatorView.stopAnimating()
self.emptyStateView.infoLabel.isHidden = true
case .loading:
self.emptyStateView.networkIndicatorImageView.isHidden = true
self.emptyStateView.activityIndicatorView.startAnimating()
self.emptyStateView.infoLabel.isHidden = false
self.emptyStateView.infoLabel.text = L10n.Scene.ServerPicker.EmptyState.findingServers
self.emptyStateView.infoLabel.textAlignment = self.traitCollection.layoutDirection == .rightToLeft ? .right : .left
case .badNetwork:
self.emptyStateView.networkIndicatorImageView.isHidden = false
self.emptyStateView.activityIndicatorView.stopAnimating()
self.emptyStateView.infoLabel.isHidden = false
self.emptyStateView.infoLabel.text = L10n.Scene.ServerPicker.EmptyState.badNetwork
self.emptyStateView.infoLabel.textAlignment = .center
}
}
.store(in: &disposeBag)
}
2021-02-25 07:09:19 +01:00
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel.viewWillAppear.send()
}
}
extension MastodonPickServerViewController {
2021-02-25 07:09:19 +01:00
@objc
private func nextStepButtonDidClicked(_ sender: UIButton) {
switch viewModel.mode {
2021-02-25 09:38:24 +01:00
case .signIn:
2021-02-25 07:09:19 +01:00
doSignIn()
2021-02-25 09:38:24 +01:00
case .signUp:
2021-02-25 07:09:19 +01:00
doSignUp()
}
}
private func doSignIn() {
guard let server = viewModel.selectedServer.value else { return }
viewModel.isAuthenticating.send(true)
2021-02-25 07:09:19 +01:00
context.apiService.createApplication(domain: server.domain)
2021-02-26 11:27:47 +01:00
.tryMap { response -> MastodonPickServerViewModel.AuthenticateInfo in
2021-02-25 07:09:19 +01:00
let application = response.value
2021-02-26 11:27:47 +01:00
guard let info = MastodonPickServerViewModel.AuthenticateInfo(domain: server.domain, application: application) else {
2021-02-25 07:09:19 +01:00
throw APIService.APIError.explicit(.badResponse)
}
return info
}
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
guard let self = self else { return }
self.viewModel.isAuthenticating.send(false)
2021-02-25 07:09:19 +01:00
switch completion {
case .failure(let error):
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: sign in fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
self.viewModel.error.send(error)
case .finished:
break
}
} receiveValue: { [weak self] info in
guard let self = self else { return }
let mastodonPinBasedAuthenticationViewModel = MastodonPinBasedAuthenticationViewModel(authenticateURL: info.authorizeURL)
self.viewModel.authenticate(
info: info,
pinCodePublisher: mastodonPinBasedAuthenticationViewModel.pinCodePublisher
)
self.viewModel.mastodonPinBasedAuthenticationViewController = self.coordinator.present(
scene: .mastodonPinBasedAuthentication(viewModel: mastodonPinBasedAuthenticationViewModel),
from: nil,
transition: .modal(animated: true, completion: nil)
)
}
.store(in: &disposeBag)
}
private func doSignUp() {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
guard let server = viewModel.selectedServer.value else { return }
viewModel.isAuthenticating.send(true)
2021-02-25 07:09:19 +01:00
context.apiService.instance(domain: server.domain)
2021-02-26 11:27:47 +01:00
.compactMap { [weak self] response -> AnyPublisher<MastodonPickServerViewModel.SignUpResponseFirst, Error>? in
2021-02-25 07:09:19 +01:00
guard let self = self else { return nil }
guard response.value.registrations != false else {
return Fail(error: AuthenticationViewModel.AuthenticationError.registrationClosed).eraseToAnyPublisher()
}
return self.context.apiService.createApplication(domain: server.domain)
2021-02-26 11:27:47 +01:00
.map { MastodonPickServerViewModel.SignUpResponseFirst(instance: response, application: $0) }
2021-02-25 07:09:19 +01:00
.eraseToAnyPublisher()
}
.switchToLatest()
2021-02-26 11:27:47 +01:00
.tryMap { response -> MastodonPickServerViewModel.SignUpResponseSecond in
2021-02-25 07:09:19 +01:00
let application = response.application.value
guard let authenticateInfo = AuthenticationViewModel.AuthenticateInfo(domain: server.domain, application: application) else {
throw APIService.APIError.explicit(.badResponse)
}
2021-02-26 11:27:47 +01:00
return MastodonPickServerViewModel.SignUpResponseSecond(instance: response.instance, authenticateInfo: authenticateInfo)
2021-02-25 07:09:19 +01:00
}
2021-02-26 11:27:47 +01:00
.compactMap { [weak self] response -> AnyPublisher<MastodonPickServerViewModel.SignUpResponseThird, Error>? in
2021-02-25 07:09:19 +01:00
guard let self = self else { return nil }
let instance = response.instance
let authenticateInfo = response.authenticateInfo
return self.context.apiService.applicationAccessToken(
domain: server.domain,
clientID: authenticateInfo.clientID,
clientSecret: authenticateInfo.clientSecret
)
2021-02-26 11:27:47 +01:00
.map { MastodonPickServerViewModel.SignUpResponseThird(instance: instance, authenticateInfo: authenticateInfo, applicationToken: $0) }
2021-02-25 07:09:19 +01:00
.eraseToAnyPublisher()
}
.switchToLatest()
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
guard let self = self else { return }
self.viewModel.isAuthenticating.send(false)
2021-02-25 07:09:19 +01:00
switch completion {
case .failure(let error):
self.viewModel.error.send(error)
case .finished:
break
}
} receiveValue: { [weak self] response in
guard let self = self else { return }
if let rules = response.instance.value.rules, !rules.isEmpty {
// show server rules before register
let mastodonServerRulesViewModel = MastodonServerRulesViewModel(
domain: server.domain,
authenticateInfo: response.authenticateInfo,
rules: rules,
instance: response.instance.value,
applicationToken: response.applicationToken.value
)
self.coordinator.present(scene: .mastodonServerRules(viewModel: mastodonServerRulesViewModel), from: self, transition: .show)
} else {
let mastodonRegisterViewModel = MastodonRegisterViewModel(
domain: server.domain,
authenticateInfo: response.authenticateInfo,
instance: response.instance.value,
applicationToken: response.applicationToken.value
)
self.coordinator.present(scene: .mastodonRegister(viewModel: mastodonRegisterViewModel), from: nil, transition: .show)
}
2021-02-25 07:09:19 +01:00
}
.store(in: &disposeBag)
}
}
2021-02-25 09:38:24 +01:00
// MARK: - UITableViewDelegate
2021-02-26 11:27:47 +01:00
extension MastodonPickServerViewController: UITableViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView === tableView else { return }
let offsetY = scrollView.contentOffset.y + scrollView.safeAreaInsets.top
if offsetY < 0 {
tableViewTopPaddingViewHeightLayoutConstraint.constant = abs(offsetY)
} else {
tableViewTopPaddingViewHeightLayoutConstraint.constant = 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
return headerView
}
2021-02-25 09:38:24 +01:00
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let diffableDataSource = viewModel.diffableDataSource else { return 0 }
let sections = diffableDataSource.snapshot().sectionIdentifiers
let section = sections[section]
switch section {
case .header:
2021-02-25 09:38:24 +01:00
return 20
case .category:
2021-02-25 09:38:24 +01:00
// Since category view has a blur shadow effect, its height need to be large than the actual height,
// Thus we reduce the section header's height by 10, and make the category cell height 60+20(10 inset for top and bottom)
return 10
case .search:
// Same reason as above
return 10
case .servers:
return 0
2021-02-25 09:38:24 +01:00
}
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard let diffableDataSource = viewModel.diffableDataSource else { return nil }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return nil }
guard case let .server(server) = item else { return nil }
2021-02-25 09:38:24 +01:00
if tableView.indexPathForSelectedRow == indexPath {
tableView.deselectRow(at: indexPath, animated: false)
viewModel.selectedServer.send(nil)
return nil
}
2021-02-25 09:38:24 +01:00
return indexPath
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
guard case let .server(server, _) = item else { return }
2021-02-25 09:38:24 +01:00
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
viewModel.selectedServer.send(server)
2021-02-25 09:38:24 +01:00
}
2021-02-25 09:38:24 +01:00
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
viewModel.selectedServer.send(nil)
}
2021-02-25 09:38:24 +01:00
}
extension MastodonPickServerViewController {
private func updateEmptyStateViewLayout() {
guard let diffableDataSource = self.viewModel.diffableDataSource else { return }
guard let indexPath = diffableDataSource.indexPath(for: .search) else { return }
let rectInTableView = tableView.rectForRow(at: indexPath)
emptyStateView.topPaddingViewTopLayoutConstraint.constant = rectInTableView.maxY
}
}
//extension MastodonPickServerViewController: UITableViewDataSource {
// func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//
// let section = Self.Section.allCases[indexPath.section]
// switch section {
// case .title:
//
// case .categories:
//
// case .search:
//
// case .serverList:
// let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PickServerCell.self), for: indexPath) as! PickServerCell
// let server = viewModel.servers.value[indexPath.row]
// // cell.server = server
//// if expandServerDomainSet.contains(server.domain) {
//// cell.mode = .expand
//// } else {
//// cell.mode = .collapse
//// }
// if server == viewModel.selectedServer.value {
// tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
// } else {
// tableView.deselectRow(at: indexPath, animated: false)
// }
//
// cell.delegate = self
// return cell
// }
// }
//}
// MARK: - PickServerSearchCellDelegate
extension MastodonPickServerViewController: PickServerSearchCellDelegate {
func pickServerSearchCell(_ cell: PickServerSearchCell, searchTextDidChange searchText: String?) {
viewModel.searchText.send(searchText)
2021-02-25 09:38:24 +01:00
}
}
// MARK: - PickServerCellDelegate
2021-02-26 11:27:47 +01:00
extension MastodonPickServerViewController: PickServerCellDelegate {
func pickServerCell(_ cell: PickServerCell, expandButtonPressed button: UIButton) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let indexPath = tableView.indexPath(for: cell) else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
guard case let .server(_, attribute) = item else { return }
2021-02-25 09:38:24 +01:00
attribute.isExpand.toggle()
tableView.beginUpdates()
cell.updateExpandMode(mode: attribute.isExpand ? .expand : .collapse)
tableView.endUpdates()
// expand attribute change do not needs apply snapshot to diffable data source
// but should I block the viewModel data binding during tableView.beginUpdates/endUpdates?
2021-02-25 09:38:24 +01:00
}
// func pickServerCell(modeChange server: Mastodon.Entity.Server, newMode: PickServerCell.Mode, updates: (() -> Void)) {
// if newMode == .collapse {
// expandServerDomainSet.remove(server.domain)
// } else {
// expandServerDomainSet.insert(server.domain)
// }
//
// tableView.beginUpdates()
// updates()
// tableView.endUpdates()
//
// if newMode == .expand, let modeChangeIndex = self.viewModel.servers.value.firstIndex(where: { $0 == server }), self.tableView.indexPathsForVisibleRows?.last?.row == modeChangeIndex {
// self.tableView.scrollToRow(at: IndexPath(row: modeChangeIndex, section: 3), at: .bottom, animated: true)
// }
// }
2021-02-25 09:38:24 +01:00
}
//extension MastodonPickServerViewController: PickServerCategoriesDataSource, PickServerCategoriesCellDelegate {
// func numberOfCategories() -> Int {
// return viewModel.categories.count
// }
//
// func category(at index: Int) -> MastodonPickServerViewModel.Category {
// return viewModel.categories[index]
// }
//
// func selectedIndex() -> Int {
// return viewModel.selectCategoryIndex.value
// }
//
// func pickServerCategoriesCell(_ cell: PickServerCategoriesCell, didSelect index: Int) {
// return viewModel.selectCategoryIndex.send(index)
// }
//}
// MARK: - OnboardingViewControllerAppearance
2021-02-26 11:27:47 +01:00
extension MastodonPickServerViewController: OnboardingViewControllerAppearance { }