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
|
|
|
//
|
|
|
|
|
|
|
|
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
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2022-04-18 12:11:31 +02:00
|
|
|
import MastodonUI
|
2022-12-28 13:50:59 +01:00
|
|
|
import MastodonSDK
|
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>()
|
2022-01-04 11:30:21 +01:00
|
|
|
private var observations = Set<NSKeyValueObservation>()
|
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-10-08 12:10:06 +02:00
|
|
|
private var emptyStateViewLeadingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
private var emptyStateViewTrailingLayoutConstraint: NSLayoutConstraint!
|
2021-03-06 05:55:52 +01:00
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
let tableView: UITableView = {
|
|
|
|
let tableView = ControlContainableTableView()
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.backgroundColor = .clear
|
2021-02-25 07:09:19 +01:00
|
|
|
tableView.keyboardDismissMode = .onDrag
|
2022-12-17 20:26:20 +01:00
|
|
|
tableView.sectionHeaderTopPadding = .leastNonzeroMagnitude
|
2021-02-23 15:14:10 +01:00
|
|
|
return tableView
|
|
|
|
}()
|
2022-12-13 23:23:20 +01:00
|
|
|
|
|
|
|
let onboardingNextView: OnboardingNextView = {
|
|
|
|
let onboardingNextView = OnboardingNextView()
|
|
|
|
onboardingNextView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
onboardingNextView.backgroundColor = UIColor.secondarySystemBackground
|
|
|
|
return onboardingNextView
|
2021-02-23 15:14:10 +01:00
|
|
|
}()
|
2021-02-26 09:43:59 +01:00
|
|
|
|
2021-06-04 12:31:57 +02:00
|
|
|
var mastodonAuthenticationController: MastodonAuthenticationController?
|
2022-12-15 21:59:38 +01:00
|
|
|
|
|
|
|
let searchController: UISearchController = {
|
|
|
|
let searchController = UISearchController(searchResultsController: nil)
|
2022-12-23 23:33:30 +01:00
|
|
|
searchController.searchBar.placeholder = L10n.Scene.ServerPicker.Search.placeholder
|
2022-12-15 21:59:38 +01:00
|
|
|
return searchController
|
|
|
|
}()
|
2021-02-23 15:14:10 +01:00
|
|
|
}
|
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
extension MastodonPickServerViewController {
|
2021-04-01 08:39:15 +02:00
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2023-01-06 13:44:11 +01:00
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
setupOnboardingAppearance()
|
|
|
|
defer { setupNavigationBarBackgroundView() }
|
2021-04-09 05:05:10 +02:00
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(tableView)
|
2021-10-08 12:10:06 +02:00
|
|
|
NSLayoutConstraint.activate([
|
2022-01-04 11:30:21 +01:00
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
2022-05-11 13:22:22 +02:00
|
|
|
tableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
|
2021-10-08 12:10:06 +02:00
|
|
|
])
|
|
|
|
|
2022-12-13 23:23:20 +01:00
|
|
|
view.addSubview(onboardingNextView)
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2022-12-13 23:23:20 +01:00
|
|
|
onboardingNextView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
onboardingNextView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: onboardingNextView.bottomAnchor),
|
2021-02-23 15:14:10 +01:00
|
|
|
])
|
2022-01-04 11:30:21 +01:00
|
|
|
|
2022-12-13 23:23:20 +01:00
|
|
|
onboardingNextView
|
2022-05-11 13:22:22 +02:00
|
|
|
.observe(\.bounds, options: [.initial, .new]) { [weak self] _, _ in
|
2022-01-04 11:30:21 +01:00
|
|
|
guard let self = self else { return }
|
2022-12-13 23:23:20 +01:00
|
|
|
let inset = self.onboardingNextView.frame.height
|
2022-05-11 13:22:22 +02:00
|
|
|
self.viewModel.additionalTableViewInsets.bottom = inset
|
2022-01-04 11:30:21 +01:00
|
|
|
}
|
|
|
|
.store(in: &observations)
|
|
|
|
|
2021-04-25 06:50:14 +02:00
|
|
|
emptyStateView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(emptyStateView)
|
2021-10-08 12:10:06 +02:00
|
|
|
emptyStateViewLeadingLayoutConstraint = emptyStateView.leadingAnchor.constraint(equalTo: tableView.leadingAnchor)
|
|
|
|
emptyStateViewTrailingLayoutConstraint = tableView.trailingAnchor.constraint(equalTo: emptyStateView.trailingAnchor)
|
2021-04-25 06:50:14 +02:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
emptyStateView.topAnchor.constraint(equalTo: view.topAnchor),
|
2021-10-08 12:10:06 +02:00
|
|
|
emptyStateViewLeadingLayoutConstraint,
|
|
|
|
emptyStateViewTrailingLayoutConstraint,
|
2022-12-13 23:23:20 +01:00
|
|
|
onboardingNextView.topAnchor.constraint(equalTo: emptyStateView.bottomAnchor, constant: 21),
|
2021-04-25 06:50:14 +02:00
|
|
|
])
|
|
|
|
view.sendSubviewToBack(emptyStateView)
|
2022-01-04 11:30:21 +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,
|
2022-11-09 16:13:05 +01:00
|
|
|
pickServerServerSectionTableHeaderViewDelegate: self
|
2021-03-05 15:50:20 +01:00
|
|
|
)
|
2022-04-18 12:11:31 +02:00
|
|
|
|
|
|
|
KeyboardResponderService
|
|
|
|
.configure(
|
|
|
|
scrollView: tableView,
|
2022-05-11 13:22:22 +02:00
|
|
|
layoutNeedsUpdate: viewModel.viewDidAppear.eraseToAnyPublisher(),
|
|
|
|
additionalSafeAreaInsets: viewModel.$additionalTableViewInsets.eraseToAnyPublisher()
|
2022-04-18 12:11:31 +02:00
|
|
|
)
|
|
|
|
.store(in: &disposeBag)
|
2022-01-04 11:30:21 +01:00
|
|
|
|
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)
|
2022-11-09 14:13:33 +01:00
|
|
|
_ = self.coordinator.present(
|
2021-06-04 12:31:57 +02:00
|
|
|
scene: .alertController(alertController: alertController),
|
|
|
|
from: nil,
|
|
|
|
transition: .alertController(animated: true, completion: nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2023-03-28 11:13:13 +02:00
|
|
|
|
|
|
|
viewModel.scrollToTop
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
self?.tableView.scroll(to: .top, animated: false)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-04 11:30:21 +01:00
|
|
|
|
2021-06-04 12:31:57 +02:00
|
|
|
authenticationViewModel
|
2021-02-25 07:09:19 +01:00
|
|
|
.authenticated
|
2022-10-09 14:07:57 +02:00
|
|
|
.asyncMap { domain, user -> Result<Bool, Error> in
|
|
|
|
do {
|
|
|
|
let result = try await self.context.authenticationService.activeMastodonUser(domain: domain, userID: user.id)
|
|
|
|
return .success(result)
|
|
|
|
} catch {
|
|
|
|
return .failure(error)
|
|
|
|
}
|
2021-02-25 07:09:19 +01:00
|
|
|
}
|
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-09-13 13:14:26 +02:00
|
|
|
// self.dismiss(animated: true, completion: nil)
|
|
|
|
self.coordinator.setup()
|
2021-02-25 07:09:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-04 11:30:21 +01:00
|
|
|
|
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 }
|
2022-12-18 12:20:49 +01:00
|
|
|
if isAuthenticating {
|
|
|
|
self.onboardingNextView.showLoading()
|
|
|
|
} else {
|
|
|
|
self.onboardingNextView.stopLoading()
|
|
|
|
}
|
2021-02-25 09:38:24 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-04 11:30:21 +01:00
|
|
|
|
2021-03-06 05:55:52 +01:00
|
|
|
viewModel.emptyStateViewState
|
2022-01-04 11:30:21 +01:00
|
|
|
.receive(on: DispatchQueue.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)
|
2022-01-04 11:30:21 +01:00
|
|
|
|
2022-12-30 17:59:09 +01:00
|
|
|
onboardingNextView.nextButton.addTarget(self, action: #selector(MastodonPickServerViewController.next(_:)), for: .touchUpInside)
|
2022-12-13 23:37:30 +01:00
|
|
|
|
2023-01-06 14:36:14 +01:00
|
|
|
viewModel.allLanguages
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let snapshot = self?.viewModel.serverSectionHeaderView.diffableDataSource?.snapshot() else { return }
|
|
|
|
|
|
|
|
self?.viewModel.serverSectionHeaderView.diffableDataSource?.applySnapshotUsingReloadData(snapshot) {
|
|
|
|
guard let self = self, let viewModel = self.viewModel else { return }
|
|
|
|
guard let indexPath = viewModel.serverSectionHeaderView.diffableDataSource?.indexPath(for: .category(category: .init(category: Mastodon.Entity.Category.Kind.general.rawValue, serversCount: 0))) else { return }
|
|
|
|
|
|
|
|
viewModel.serverSectionHeaderView.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .right)
|
|
|
|
|
|
|
|
let firstIndex = IndexPath(item: 0, section: 0)
|
|
|
|
viewModel.serverSectionHeaderView.collectionView.scrollToItem(at: firstIndex, at: .left, animated: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2022-12-13 23:37:30 +01:00
|
|
|
title = L10n.Scene.ServerPicker.title
|
2022-12-15 21:59:38 +01:00
|
|
|
|
|
|
|
navigationItem.searchController = searchController
|
|
|
|
searchController.searchResultsUpdater = self
|
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()
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:11:35 +01:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
tableView.flashScrollIndicators()
|
2022-04-18 12:11:31 +02:00
|
|
|
viewModel.viewDidAppear.send()
|
2022-01-05 08:11:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:10:06 +02:00
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
setupNavigationBarAppearance()
|
|
|
|
}
|
2021-03-06 05:55:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-08 12:10:06 +02:00
|
|
|
extension MastodonPickServerViewController {
|
2022-12-11 23:57:58 +01:00
|
|
|
|
2022-12-30 17:59:09 +01:00
|
|
|
@objc private func next(_ sender: UIButton) {
|
|
|
|
|
|
|
|
let server: Mastodon.Entity.Server
|
|
|
|
|
|
|
|
if let selectedServer = viewModel.selectedServer.value {
|
|
|
|
server = selectedServer
|
|
|
|
} else if let randomServer = viewModel.chooseRandomServer() {
|
|
|
|
server = randomServer
|
|
|
|
} 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)
|
|
|
|
}
|
2022-04-27 11:37:03 +02: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
|
|
|
)
|
2022-04-27 11:37:03 +02: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
|
|
|
|
)
|
2022-11-09 14:13:33 +01:00
|
|
|
_ = self.coordinator.present(scene: .mastodonServerRules(viewModel: mastodonServerRulesViewModel), from: self, transition: .show)
|
2021-03-01 11:06:37 +01:00
|
|
|
} else {
|
|
|
|
let mastodonRegisterViewModel = MastodonRegisterViewModel(
|
2021-03-02 06:27:53 +01:00
|
|
|
context: self.context,
|
2022-01-07 11:49:37 +01:00
|
|
|
domain: server.domain,
|
2021-03-01 11:06:37 +01:00
|
|
|
authenticateInfo: response.authenticateInfo,
|
|
|
|
instance: response.instance.value,
|
|
|
|
applicationToken: response.applicationToken.value
|
|
|
|
)
|
2022-11-09 14:13:33 +01:00
|
|
|
_ = self.coordinator.present(scene: .mastodonRegister(viewModel: mastodonRegisterViewModel), from: nil, transition: .show)
|
2021-03-01 11:06:37 +01:00
|
|
|
}
|
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-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)
|
2023-03-28 11:13:13 +02:00
|
|
|
|
|
|
|
// Briefly highlight selected cell
|
|
|
|
guard let cell = tableView.cellForRow(at: indexPath) else { return }
|
|
|
|
cell.backgroundColor = Asset.Colors.selectionHighlight.color
|
|
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
|
|
cell.backgroundColor = .none
|
|
|
|
})
|
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
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return nil }
|
|
|
|
let snapshot = diffableDataSource.snapshot()
|
|
|
|
guard section < snapshot.numberOfSections else { return nil }
|
|
|
|
let section = snapshot.sectionIdentifiers[section]
|
2021-10-08 12:10:06 +02:00
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
switch section {
|
|
|
|
case .servers:
|
|
|
|
return viewModel.serverSectionHeaderView
|
2021-10-08 12:10:06 +02:00
|
|
|
default:
|
2022-01-04 11:30:21 +01:00
|
|
|
return UIView()
|
2021-10-08 12:10:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return .leastNonzeroMagnitude }
|
|
|
|
let snapshot = diffableDataSource.snapshot()
|
|
|
|
guard section < snapshot.numberOfSections else { return .leastNonzeroMagnitude }
|
|
|
|
let section = snapshot.sectionIdentifiers[section]
|
|
|
|
|
|
|
|
switch section {
|
|
|
|
case .servers:
|
|
|
|
return PickServerServerSectionTableHeaderView.height
|
2021-10-08 12:10:06 +02:00
|
|
|
default:
|
2022-01-04 11:30:21 +01:00
|
|
|
return .leastNonzeroMagnitude
|
2021-10-08 12:10:06 +02:00
|
|
|
}
|
2021-03-06 05:55:52 +01:00
|
|
|
}
|
2022-01-04 11:30:21 +01:00
|
|
|
|
2021-03-06 05:55:52 +01:00
|
|
|
}
|
2021-03-05 15:50:20 +01:00
|
|
|
|
2022-01-04 11:30:21 +01:00
|
|
|
// MARK: - PickServerServerSectionTableHeaderViewDelegate
|
|
|
|
extension MastodonPickServerViewController: PickServerServerSectionTableHeaderViewDelegate {
|
|
|
|
func pickServerServerSectionTableHeaderView(_ headerView: PickServerServerSectionTableHeaderView, collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
2022-12-28 13:50:59 +01:00
|
|
|
guard let diffableDataSource = headerView.diffableDataSource,
|
2023-01-06 14:36:14 +01:00
|
|
|
let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
|
2022-12-28 13:50:59 +01:00
|
|
|
|
2023-01-06 14:36:14 +01:00
|
|
|
switch item {
|
|
|
|
case .category(_):
|
2022-12-28 13:50:59 +01:00
|
|
|
viewModel.selectCategoryItem.value = item
|
2023-01-06 14:36:14 +01:00
|
|
|
case .language(_), .signupSpeed(_):
|
|
|
|
break
|
|
|
|
// gets handled by button
|
2022-12-20 22:54:36 +01:00
|
|
|
}
|
2022-01-04 11:30:21 +01:00
|
|
|
}
|
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 { }
|
2022-12-15 21:59:38 +01:00
|
|
|
|
|
|
|
// MARK: - UISearchResultsUpdating
|
|
|
|
|
|
|
|
extension MastodonPickServerViewController: UISearchResultsUpdating {
|
|
|
|
func updateSearchResults(for searchController: UISearchController) {
|
|
|
|
guard let searchText = searchController.searchBar.text else { return }
|
|
|
|
viewModel.searchText.send(searchText)
|
|
|
|
}
|
|
|
|
}
|