2021-02-20 13:23:29 +01:00
|
|
|
//
|
2021-02-26 11:27:47 +01:00
|
|
|
// MastodonPickServerViewController.swift
|
2021-02-20 13:23:29 +01:00
|
|
|
// Mastodon
|
|
|
|
//
|
2021-02-23 15:14:10 +01:00
|
|
|
// Created by BradGao on 2021/2/20.
|
2021-02-20 13:23:29 +01:00
|
|
|
//
|
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
import os.log
|
2021-02-20 13:23:29 +01:00
|
|
|
import UIKit
|
2021-02-23 15:14:10 +01:00
|
|
|
import Combine
|
2021-05-19 11:41:25 +02:00
|
|
|
import GameController
|
2021-06-04 12:31:57 +02:00
|
|
|
import AuthenticationServices
|
2021-02-20 13:23:29 +01:00
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
final class MastodonPickServerViewController: UIViewController, NeedsDependency {
|
2021-02-23 15:14:10 +01:00
|
|
|
|
|
|
|
private var disposeBag = Set<AnyCancellable>()
|
2021-03-06 05:55:52 +01:00
|
|
|
private var tableViewObservation: NSKeyValueObservation?
|
2021-02-23 15:14:10 +01:00
|
|
|
|
|
|
|
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-06-04 12:31:57 +02:00
|
|
|
private(set) lazy var authenticationViewModel = AuthenticationViewModel(
|
|
|
|
context: context,
|
|
|
|
coordinator: coordinator,
|
|
|
|
isAuthenticationExist: false
|
|
|
|
)
|
2021-02-23 15:14:10 +01:00
|
|
|
|
2021-02-25 09:38:24 +01:00
|
|
|
private var expandServerDomainSet = Set<String>()
|
2021-03-06 05:55:52 +01:00
|
|
|
|
2021-03-29 16:02:27 +02:00
|
|
|
private let emptyStateView = PickServerEmptyStateView()
|
2021-03-06 05:55:52 +01:00
|
|
|
let tableViewTopPaddingView = UIView() // fix empty state view background display when tableView bounce scrolling
|
|
|
|
var tableViewTopPaddingViewHeightLayoutConstraint: NSLayoutConstraint!
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
let tableView: UITableView = {
|
|
|
|
let tableView = ControlContainableTableView()
|
|
|
|
tableView.register(PickServerTitleCell.self, forCellReuseIdentifier: String(describing: PickServerTitleCell.self))
|
|
|
|
tableView.register(PickServerCategoriesCell.self, forCellReuseIdentifier: String(describing: PickServerCategoriesCell.self))
|
2021-02-24 15:47:42 +01:00
|
|
|
tableView.register(PickServerSearchCell.self, forCellReuseIdentifier: String(describing: PickServerSearchCell.self))
|
|
|
|
tableView.register(PickServerCell.self, forCellReuseIdentifier: String(describing: PickServerCell.self))
|
2021-05-13 11:50:37 +02:00
|
|
|
tableView.register(PickServerLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: PickServerLoaderTableViewCell.self))
|
2021-02-23 15:14:10 +01:00
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.backgroundColor = .clear
|
2021-02-25 07:09:19 +01:00
|
|
|
tableView.keyboardDismissMode = .onDrag
|
2021-02-23 15:14:10 +01:00
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return tableView
|
|
|
|
}()
|
|
|
|
|
|
|
|
let nextStepButton: PrimaryActionButton = {
|
2021-02-26 09:43:59 +01:00
|
|
|
let button = PrimaryActionButton()
|
2021-02-25 07:41:41 +01:00
|
|
|
button.setTitle(L10n.Common.Controls.Actions.signUp, for: .normal)
|
2021-02-23 15:14:10 +01:00
|
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return button
|
|
|
|
}()
|
2021-05-19 11:41:25 +02:00
|
|
|
var nextStepButtonBottomLayoutConstraint: NSLayoutConstraint!
|
2021-02-26 09:43:59 +01:00
|
|
|
|
2021-06-04 12:31:57 +02:00
|
|
|
var mastodonAuthenticationController: MastodonAuthenticationController?
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
deinit {
|
2021-03-06 05:55:52 +01:00
|
|
|
tableViewObservation = nil
|
2021-02-26 09:43:59 +01:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
extension MastodonPickServerViewController {
|
2021-02-23 15:14:10 +01:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
setupOnboardingAppearance()
|
|
|
|
defer { setupNavigationBarBackgroundView() }
|
2021-04-09 05:05:10 +02:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "ellipsis.circle"), style: .plain, target: nil, action: nil)
|
|
|
|
let children: [UIMenuElement] = [
|
|
|
|
UIAction(title: "Dismiss", image: nil, identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off, handler: { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.dismiss(animated: true, completion: nil)
|
|
|
|
})
|
|
|
|
]
|
|
|
|
navigationItem.rightBarButtonItem?.menu = UIMenu(title: "Debug Tool", image: nil, identifier: nil, options: [], children: children)
|
|
|
|
#endif
|
2021-02-23 15:14:10 +01:00
|
|
|
|
|
|
|
view.addSubview(nextStepButton)
|
2021-05-19 11:41:25 +02:00
|
|
|
nextStepButtonBottomLayoutConstraint = view.bottomAnchor.constraint(equalTo: nextStepButton.bottomAnchor, constant: 0).priority(.defaultHigh)
|
2021-02-23 15:14:10 +01:00
|
|
|
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),
|
2021-05-19 11:41:25 +02:00
|
|
|
view.safeAreaLayoutGuide.bottomAnchor.constraint(greaterThanOrEqualTo: nextStepButton.bottomAnchor, constant: WelcomeViewController.viewBottomPaddingHeight),
|
|
|
|
nextStepButtonBottomLayoutConstraint,
|
2021-02-23 15:14:10 +01:00
|
|
|
])
|
2021-03-06 05:55:52 +01:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
])
|
2021-07-20 13:24:24 +02:00
|
|
|
tableViewTopPaddingView.backgroundColor = Asset.Theme.Mastodon.systemGroupedBackground.color
|
2021-03-06 05:55:52 +01:00
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
view.addSubview(tableView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
2021-03-06 06:29:45 +01:00
|
|
|
nextStepButton.topAnchor.constraint(equalTo: tableView.bottomAnchor, constant: 7),
|
2021-02-23 15:14:10 +01:00
|
|
|
])
|
|
|
|
|
2021-04-25 06:50:14 +02:00
|
|
|
emptyStateView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(emptyStateView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
emptyStateView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
emptyStateView.leadingAnchor.constraint(equalTo: tableView.readableContentGuide.leadingAnchor),
|
|
|
|
emptyStateView.trailingAnchor.constraint(equalTo: tableView.readableContentGuide.trailingAnchor),
|
|
|
|
nextStepButton.topAnchor.constraint(equalTo: emptyStateView.bottomAnchor, constant: 21),
|
|
|
|
])
|
|
|
|
view.sendSubviewToBack(emptyStateView)
|
|
|
|
|
2021-05-19 11:41:25 +02:00
|
|
|
// update layout when keyboard show/dismiss
|
|
|
|
let keyboardEventPublishers = Publishers.CombineLatest3(
|
|
|
|
KeyboardResponderService.shared.isShow,
|
|
|
|
KeyboardResponderService.shared.state,
|
|
|
|
KeyboardResponderService.shared.endFrame
|
|
|
|
)
|
|
|
|
|
|
|
|
keyboardEventPublishers
|
|
|
|
.sink { [weak self] keyboardEvents in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let (isShow, state, endFrame) = keyboardEvents
|
|
|
|
|
|
|
|
// guard external keyboard connected
|
|
|
|
guard isShow, state == .dock, GCKeyboard.coalesced != nil else {
|
|
|
|
self.nextStepButtonBottomLayoutConstraint.constant = WelcomeViewController.viewBottomPaddingHeight
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let externalKeyboardToolbarHeight = self.view.frame.maxY - endFrame.minY
|
|
|
|
guard externalKeyboardToolbarHeight > 0 else {
|
|
|
|
self.nextStepButtonBottomLayoutConstraint.constant = WelcomeViewController.viewBottomPaddingHeight
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
|
|
self.nextStepButtonBottomLayoutConstraint.constant = externalKeyboardToolbarHeight + 16
|
|
|
|
self.view.layoutIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
switch viewModel.mode {
|
2021-02-25 09:38:24 +01:00
|
|
|
case .signIn:
|
2021-02-23 15:14:10 +01:00
|
|
|
nextStepButton.setTitle(L10n.Common.Controls.Actions.signIn, for: .normal)
|
2021-02-25 09:38:24 +01:00
|
|
|
case .signUp:
|
2021-02-23 15:14:10 +01:00
|
|
|
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-23 15:14:10 +01:00
|
|
|
|
2021-02-25 09:38:24 +01:00
|
|
|
tableView.delegate = self
|
2021-03-05 15:50:20 +01:00
|
|
|
viewModel.setupDiffableDataSource(
|
|
|
|
for: tableView,
|
|
|
|
dependency: self,
|
2021-03-06 07:46:04 +01:00
|
|
|
pickServerCategoriesCellDelegate: self,
|
2021-03-05 15:50:20 +01:00
|
|
|
pickServerSearchCellDelegate: self,
|
|
|
|
pickServerCellDelegate: self
|
|
|
|
)
|
2021-02-25 07:09:19 +01:00
|
|
|
|
|
|
|
viewModel
|
|
|
|
.selectedServer
|
2021-03-05 15:50:20 +01:00
|
|
|
.map { $0 != nil }
|
2021-02-25 07:09:19 +01:00
|
|
|
.assign(to: \.isEnabled, on: nextStepButton)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-06-04 12:31:57 +02:00
|
|
|
Publishers.Merge(
|
|
|
|
viewModel.error,
|
|
|
|
authenticationViewModel.error
|
|
|
|
)
|
|
|
|
.compactMap { $0 }
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] error in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let alertController = UIAlertController(for: error, title: "Error", preferredStyle: .alert)
|
|
|
|
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)
|
2021-02-25 07:09:19 +01:00
|
|
|
|
2021-06-04 12:31:57 +02:00
|
|
|
authenticationViewModel
|
2021-02-25 07:09:19 +01:00
|
|
|
.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)
|
|
|
|
|
2021-06-04 12:31:57 +02:00
|
|
|
authenticationViewModel.isAuthenticating
|
2021-02-25 09:38:24 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
2021-02-26 09:43:59 +01:00
|
|
|
.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)
|
2021-03-06 05:55:52 +01:00
|
|
|
|
|
|
|
viewModel.emptyStateViewState
|
2021-06-16 08:49:00 +02:00
|
|
|
.receive(on: RunLoop.main)
|
2021-03-06 05:55:52 +01:00
|
|
|
.sink { [weak self] state in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch state {
|
|
|
|
case .none:
|
2021-06-16 07:54:25 +02:00
|
|
|
UIView.animate(withDuration: 0.3) {
|
|
|
|
self.emptyStateView.alpha = 0
|
|
|
|
}
|
2021-03-06 05:55:52 +01:00
|
|
|
case .loading:
|
2021-06-16 07:54:25 +02:00
|
|
|
self.emptyStateView.alpha = 1
|
2021-03-06 05:55:52 +01:00
|
|
|
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:
|
2021-06-16 07:54:25 +02:00
|
|
|
self.emptyStateView.alpha = 1
|
2021-03-06 05:55:52 +01:00
|
|
|
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-23 15:14:10 +01:00
|
|
|
}
|
2021-02-25 07:09:19 +01:00
|
|
|
|
2021-03-06 05:55:52 +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 }
|
2021-06-04 12:31:57 +02:00
|
|
|
authenticationViewModel.isAuthenticating.send(true)
|
2021-02-25 07:09:19 +01:00
|
|
|
context.apiService.createApplication(domain: server.domain)
|
2021-06-04 12:31:57 +02:00
|
|
|
.tryMap { response -> AuthenticationViewModel.AuthenticateInfo in
|
2021-02-25 07:09:19 +01:00
|
|
|
let application = response.value
|
2021-06-04 12:31:57 +02:00
|
|
|
guard let info = AuthenticationViewModel.AuthenticateInfo(
|
|
|
|
domain: server.domain,
|
|
|
|
application: application,
|
|
|
|
redirectURI: response.value.redirectURI ?? MastodonAuthenticationController.callbackURL
|
|
|
|
) 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 }
|
2021-06-04 12:31:57 +02:00
|
|
|
self.authenticationViewModel.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 }
|
2021-06-04 12:31:57 +02:00
|
|
|
let authenticationController = MastodonAuthenticationController(
|
|
|
|
context: self.context,
|
|
|
|
authenticateURL: info.authorizeURL
|
2021-02-25 07:09:19 +01:00
|
|
|
)
|
2021-06-04 12:31:57 +02:00
|
|
|
|
|
|
|
self.mastodonAuthenticationController = authenticationController
|
|
|
|
authenticationController.authenticationSession?.presentationContextProvider = self
|
|
|
|
authenticationController.authenticationSession?.start()
|
|
|
|
|
|
|
|
self.authenticationViewModel.authenticate(
|
|
|
|
info: info,
|
|
|
|
pinCodePublisher: authenticationController.pinCodePublisher
|
2021-02-25 07:09:19 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
.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 }
|
2021-06-04 12:31:57 +02:00
|
|
|
authenticationViewModel.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
|
2021-06-04 12:31:57 +02:00
|
|
|
guard let authenticateInfo = AuthenticationViewModel.AuthenticateInfo(
|
|
|
|
domain: server.domain,
|
|
|
|
application: application
|
|
|
|
) else {
|
2021-02-25 07:09:19 +01:00
|
|
|
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,
|
2021-06-04 12:31:57 +02:00
|
|
|
clientSecret: authenticateInfo.clientSecret,
|
|
|
|
redirectURI: authenticateInfo.redirectURI
|
2021-02-25 07:09:19 +01:00
|
|
|
)
|
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 }
|
2021-06-04 12:31:57 +02:00
|
|
|
self.authenticationViewModel.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 }
|
2021-03-01 11:06:37 +01:00
|
|
|
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,
|
2021-03-02 06:27:53 +01:00
|
|
|
context: self.context,
|
2021-03-01 11:06:37 +01:00
|
|
|
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-20 13:23:29 +01:00
|
|
|
}
|
2021-02-25 09:38:24 +01:00
|
|
|
|
2021-03-06 05:55:52 +01:00
|
|
|
// MARK: - UITableViewDelegate
|
2021-02-26 11:27:47 +01:00
|
|
|
extension MastodonPickServerViewController: UITableViewDelegate {
|
2021-03-05 15:50:20 +01:00
|
|
|
|
2021-03-06 05:55:52 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
2021-03-06 05:55:52 +01:00
|
|
|
let headerView = UIView()
|
2021-07-20 13:24:24 +02:00
|
|
|
headerView.backgroundColor = Asset.Theme.Mastodon.systemGroupedBackground.color
|
2021-03-06 05:55:52 +01:00
|
|
|
return headerView
|
2021-03-05 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 09:38:24 +01:00
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
2021-03-05 15:50:20 +01:00
|
|
|
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
|
2021-03-05 15:50:20 +01:00
|
|
|
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
|
2021-03-05 15:50:20 +01:00
|
|
|
case .servers:
|
2021-03-01 10:08:31 +01:00
|
|
|
return 0
|
2021-02-25 09:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
2021-03-05 15:50:20 +01:00
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return nil }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return nil }
|
2021-03-10 12:12:53 +01:00
|
|
|
guard case .server = item else { return nil }
|
2021-03-05 15:50:20 +01:00
|
|
|
|
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-03-05 15:50:20 +01:00
|
|
|
|
2021-02-25 09:38:24 +01:00
|
|
|
return indexPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2021-03-05 15:50:20 +01:00
|
|
|
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)
|
2021-03-05 15:50:20 +01:00
|
|
|
viewModel.selectedServer.send(server)
|
2021-02-25 09:38:24 +01:00
|
|
|
}
|
2021-03-05 15:50:20 +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-03-05 15:50:20 +01:00
|
|
|
|
2021-03-06 07:46:04 +01:00
|
|
|
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .categoryPicker:
|
|
|
|
guard let cell = cell as? PickServerCategoriesCell else { return }
|
|
|
|
guard let diffableDataSource = cell.diffableDataSource else { return }
|
|
|
|
let snapshot = diffableDataSource.snapshot()
|
|
|
|
|
|
|
|
let item = viewModel.selectCategoryItem.value
|
|
|
|
guard let section = snapshot.indexOfSection(.main),
|
|
|
|
let row = snapshot.indexOfItem(item) else { return }
|
|
|
|
cell.collectionView.selectItem(at: IndexPath(item: row, section: section), animated: false, scrollPosition: .centeredHorizontally)
|
|
|
|
case .search:
|
|
|
|
guard let cell = cell as? PickServerSearchCell else { return }
|
|
|
|
cell.searchTextField.text = viewModel.searchText.value
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 09:38:24 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 05:55:52 +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
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 15:50:20 +01:00
|
|
|
|
2021-03-06 07:46:04 +01:00
|
|
|
// MARK: - PickServerCategoriesCellDelegate
|
|
|
|
extension MastodonPickServerViewController: PickServerCategoriesCellDelegate {
|
|
|
|
func pickServerCategoriesCell(_ cell: PickServerCategoriesCell, collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
guard let diffableDataSource = cell.diffableDataSource else { return }
|
|
|
|
let item = diffableDataSource.itemIdentifier(for: indexPath)
|
|
|
|
viewModel.selectCategoryItem.value = item ?? .all
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 15:50:20 +01:00
|
|
|
|
|
|
|
// MARK: - PickServerSearchCellDelegate
|
|
|
|
extension MastodonPickServerViewController: PickServerSearchCellDelegate {
|
|
|
|
func pickServerSearchCell(_ cell: PickServerSearchCell, searchTextDidChange searchText: String?) {
|
2021-03-06 07:21:52 +01:00
|
|
|
viewModel.searchText.send(searchText ?? "")
|
2021-02-25 09:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
// MARK: - PickServerCellDelegate
|
2021-02-26 11:27:47 +01:00
|
|
|
extension MastodonPickServerViewController: PickServerCellDelegate {
|
2021-03-05 15:50:20 +01:00
|
|
|
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
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
attribute.isExpand.toggle()
|
2021-02-25 10:40:11 +01:00
|
|
|
tableView.beginUpdates()
|
2021-03-05 15:50:20 +01:00
|
|
|
cell.updateExpandMode(mode: attribute.isExpand ? .expand : .collapse)
|
2021-02-25 10:40:11 +01:00
|
|
|
tableView.endUpdates()
|
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
// MARK: - OnboardingViewControllerAppearance
|
2021-02-26 11:27:47 +01:00
|
|
|
extension MastodonPickServerViewController: OnboardingViewControllerAppearance { }
|
2021-06-04 12:31:57 +02:00
|
|
|
|
|
|
|
// MARK: - ASWebAuthenticationPresentationContextProviding
|
|
|
|
extension MastodonPickServerViewController: ASWebAuthenticationPresentationContextProviding {
|
|
|
|
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
|
|
|
return view.window!
|
|
|
|
}
|
|
|
|
}
|