mastodon-ios/Mastodon/Scene/Onboarding/ServerRules/MastodonServerRulesViewCont...

158 lines
5.9 KiB
Swift
Raw Normal View History

2021-02-22 09:20:44 +01:00
//
// MastodonServerRulesViewController.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-2-22.
//
import os.log
import UIKit
2021-02-26 11:27:47 +01:00
import Combine
import MastodonSDK
import SafariServices
import MetaTextKit
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
import MastodonLocalization
2021-02-22 09:20:44 +01:00
final class MastodonServerRulesViewController: UIViewController, NeedsDependency {
2021-02-22 09:20:44 +01:00
2022-01-05 08:11:35 +01:00
let logger = Logger(subsystem: "MastodonServerRulesViewController", category: "ViewController")
2021-02-26 11:27:47 +01:00
2022-01-05 08:11:35 +01:00
var disposeBag = Set<AnyCancellable>()
private var observations = Set<NSKeyValueObservation>()
2021-02-22 09:20:44 +01:00
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
var viewModel: MastodonServerRulesViewModel!
let stackView = UIStackView()
2022-01-05 08:11:35 +01:00
let tableView: UITableView = {
let tableView = UITableView()
tableView.register(OnboardingHeadlineTableViewCell.self, forCellReuseIdentifier: String(describing: OnboardingHeadlineTableViewCell.self))
tableView.register(ServerRulesTableViewCell.self, forCellReuseIdentifier: String(describing: ServerRulesTableViewCell.self))
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none
tableView.backgroundColor = .clear
tableView.keyboardDismissMode = .onDrag
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
} else {
// Fallback on earlier versions
}
return tableView
2021-02-22 09:20:44 +01:00
}()
2022-01-05 08:11:35 +01:00
let navigationActionView: NavigationActionView = {
let navigationActionView = NavigationActionView()
2022-02-15 11:15:58 +01:00
navigationActionView.backgroundColor = Asset.Scene.Onboarding.background.color
2022-01-05 08:11:35 +01:00
return navigationActionView
}()
2021-02-26 11:27:47 +01:00
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
2021-02-22 09:20:44 +01:00
}
extension MastodonServerRulesViewController {
override func viewDidLoad() {
super.viewDidLoad()
2022-01-05 08:11:35 +01:00
navigationItem.leftBarButtonItem = UIBarButtonItem()
setupOnboardingAppearance()
defer { setupNavigationBarBackgroundView() }
2021-02-22 09:20:44 +01:00
2022-01-05 08:11:35 +01:00
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
2021-02-22 09:20:44 +01:00
NSLayoutConstraint.activate([
2022-01-05 08:11:35 +01:00
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
2021-02-22 09:20:44 +01:00
])
2022-01-05 08:11:35 +01:00
navigationActionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(navigationActionView)
defer {
2022-01-05 08:11:35 +01:00
view.bringSubviewToFront(navigationActionView)
}
2021-02-22 09:20:44 +01:00
NSLayoutConstraint.activate([
2022-01-05 08:11:35 +01:00
navigationActionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
navigationActionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
view.bottomAnchor.constraint(equalTo: navigationActionView.bottomAnchor),
2021-02-22 09:20:44 +01:00
])
2022-01-05 08:11:35 +01:00
navigationActionView
.observe(\.bounds, options: [.initial, .new]) { [weak self] navigationActionView, _ in
guard let self = self else { return }
let inset = navigationActionView.frame.height
self.tableView.contentInset.bottom = inset
}
.store(in: &observations)
2021-02-22 09:20:44 +01:00
2022-01-05 08:11:35 +01:00
tableView.delegate = self
viewModel.setupDiffableDataSource(tableView: tableView)
2021-02-22 09:20:44 +01:00
2022-01-05 08:11:35 +01:00
navigationActionView.backButton.addTarget(self, action: #selector(MastodonServerRulesViewController.backButtonPressed(_:)), for: .touchUpInside)
navigationActionView.nextButton.addTarget(self, action: #selector(MastodonServerRulesViewController.nextButtonPressed(_:)), for: .touchUpInside)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
2022-01-05 08:11:35 +01:00
tableView.flashScrollIndicators()
}
}
extension MastodonServerRulesViewController {
2022-01-05 08:11:35 +01:00
@objc private func backButtonPressed(_ sender: UIButton) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
navigationController?.popViewController(animated: true)
}
2022-01-05 08:11:35 +01:00
@objc private func nextButtonPressed(_ sender: UIButton) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
2022-01-07 11:49:37 +01:00
let viewModel = MastodonRegisterViewModel(
context: context,
domain: viewModel.domain,
authenticateInfo: viewModel.authenticateInfo,
instance: viewModel.instance,
applicationToken: viewModel.applicationToken
)
2022-01-05 08:11:35 +01:00
coordinator.present(scene: .mastodonRegister(viewModel: viewModel), from: self, transition: .show)
2021-02-22 09:20:44 +01:00
}
2022-01-05 08:11:35 +01:00
2021-02-22 09:20:44 +01:00
}
// MARK: - OnboardingViewControllerAppearance
extension MastodonServerRulesViewController: OnboardingViewControllerAppearance { }
2022-01-05 08:11:35 +01:00
// MARK: - UITableViewDelegate
extension MastodonServerRulesViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
2021-02-22 09:20:44 +01:00
2022-01-05 08:11:35 +01:00
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let diffableDataSource = viewModel.diffableDataSource,
section < diffableDataSource.snapshot().numberOfSections
else { return .leastNonzeroMagnitude }
let sectionItem = diffableDataSource.snapshot().sectionIdentifiers[section]
switch sectionItem {
case .header:
return .leastNonzeroMagnitude
case .rules:
return 16
2021-02-22 09:20:44 +01:00
}
}
}