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
|
2021-03-30 08:31:52 +02:00
|
|
|
import MastodonSDK
|
|
|
|
import SafariServices
|
2021-02-22 09:20:44 +01:00
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
final class MastodonServerRulesViewController: UIViewController, NeedsDependency {
|
2021-02-22 09:20:44 +01:00
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
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 largeTitleLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
2021-02-26 09:43:59 +01:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .largeTitle).scaledFont(for: .systemFont(ofSize: 34, weight: .bold))
|
2021-02-22 09:20:44 +01:00
|
|
|
label.textColor = .label
|
|
|
|
label.text = L10n.Scene.ServerRules.title
|
2021-05-10 12:48:04 +02:00
|
|
|
label.numberOfLines = 0
|
2021-02-22 09:20:44 +01:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
private(set) lazy var subtitleLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .title1).scaledFont(for: UIFont.systemFont(ofSize: 20))
|
|
|
|
label.textColor = .secondaryLabel
|
|
|
|
label.text = L10n.Scene.ServerRules.subtitle(viewModel.domain)
|
|
|
|
label.numberOfLines = 0
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let rulesLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
2021-06-16 09:11:12 +02:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold))
|
2021-04-02 10:50:07 +02:00
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
2021-02-22 09:20:44 +01:00
|
|
|
label.text = "Rules"
|
|
|
|
label.numberOfLines = 0
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
2021-03-30 08:31:52 +02:00
|
|
|
let bottomContainerView: UIView = {
|
2021-02-22 09:20:44 +01:00
|
|
|
let view = UIView()
|
2021-04-25 11:19:47 +02:00
|
|
|
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
2021-02-22 09:20:44 +01:00
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
2021-03-30 08:31:52 +02:00
|
|
|
private(set) lazy var bottomPromptTextView: UITextView = {
|
|
|
|
let textView = UITextView()
|
2021-05-10 12:48:04 +02:00
|
|
|
textView.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22)
|
2021-03-30 08:31:52 +02:00
|
|
|
textView.textColor = .label
|
|
|
|
textView.isSelectable = true
|
|
|
|
textView.isEditable = false
|
2021-04-26 14:36:59 +02:00
|
|
|
textView.isScrollEnabled = false
|
2021-04-25 11:19:47 +02:00
|
|
|
textView.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
2021-03-30 08:31:52 +02:00
|
|
|
return textView
|
2021-02-22 09:20:44 +01:00
|
|
|
}()
|
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
let confirmButton: PrimaryActionButton = {
|
2021-02-26 09:43:59 +01:00
|
|
|
let button = PrimaryActionButton()
|
2021-02-22 09:20:44 +01:00
|
|
|
button.setTitle(L10n.Scene.ServerRules.Button.confirm, for: .normal)
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
let scrollView: UIScrollView = {
|
|
|
|
let scrollView = UIScrollView()
|
|
|
|
scrollView.alwaysBounceVertical = true
|
2021-02-26 12:52:58 +01:00
|
|
|
scrollView.showsVerticalScrollIndicator = false
|
2021-02-26 09:43:59 +01:00
|
|
|
return scrollView
|
|
|
|
}()
|
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()
|
|
|
|
|
2021-02-26 12:52:58 +01:00
|
|
|
setupOnboardingAppearance()
|
2021-03-30 08:31:52 +02:00
|
|
|
configTextView()
|
|
|
|
|
2021-02-26 12:52:58 +01:00
|
|
|
defer { setupNavigationBarBackgroundView() }
|
2021-02-22 09:20:44 +01:00
|
|
|
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomContainerView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(bottomContainerView)
|
2021-02-22 09:20:44 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2021-03-30 08:31:52 +02:00
|
|
|
view.bottomAnchor.constraint(equalTo: bottomContainerView.bottomAnchor),
|
|
|
|
bottomContainerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
bottomContainerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
2021-02-22 09:20:44 +01:00
|
|
|
])
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomContainerView.preservesSuperviewLayoutMargins = true
|
2021-02-26 12:52:58 +01:00
|
|
|
defer {
|
2021-03-30 08:31:52 +02:00
|
|
|
view.bringSubviewToFront(bottomContainerView)
|
2021-02-26 12:52:58 +01:00
|
|
|
}
|
2021-02-22 09:20:44 +01:00
|
|
|
|
|
|
|
confirmButton.translatesAutoresizingMaskIntoConstraints = false
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomContainerView.addSubview(confirmButton)
|
2021-02-22 09:20:44 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomContainerView.layoutMarginsGuide.bottomAnchor.constraint(equalTo: confirmButton.bottomAnchor, constant: MastodonServerRulesViewController.viewBottomPaddingHeight),
|
|
|
|
confirmButton.leadingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.leadingAnchor, constant: MastodonServerRulesViewController.actionButtonMargin),
|
|
|
|
bottomContainerView.readableContentGuide.trailingAnchor.constraint(equalTo: confirmButton.trailingAnchor, constant: MastodonServerRulesViewController.actionButtonMargin),
|
2021-02-26 12:52:58 +01:00
|
|
|
confirmButton.heightAnchor.constraint(equalToConstant: MastodonServerRulesViewController.actionButtonHeight).priority(.defaultHigh),
|
2021-02-22 09:20:44 +01:00
|
|
|
])
|
|
|
|
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomPromptTextView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
bottomContainerView.addSubview(bottomPromptTextView)
|
2021-02-22 09:20:44 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomPromptTextView.frameLayoutGuide.topAnchor.constraint(equalTo: bottomContainerView.topAnchor, constant: 20),
|
|
|
|
bottomPromptTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.leadingAnchor),
|
|
|
|
bottomPromptTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.trailingAnchor),
|
|
|
|
confirmButton.topAnchor.constraint(equalTo: bottomPromptTextView.frameLayoutGuide.bottomAnchor, constant: 20),
|
2021-02-22 09:20:44 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
scrollView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(scrollView)
|
|
|
|
NSLayoutConstraint.activate([
|
2021-02-26 12:52:58 +01:00
|
|
|
scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: view.topAnchor),
|
2021-02-22 09:20:44 +01:00
|
|
|
scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
|
2021-02-26 12:52:58 +01:00
|
|
|
scrollView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.readableContentGuide.trailingAnchor),
|
|
|
|
scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
2021-02-22 09:20:44 +01:00
|
|
|
scrollView.frameLayoutGuide.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
let stackView = UIStackView()
|
|
|
|
stackView.axis = .vertical
|
|
|
|
stackView.distribution = .fill
|
|
|
|
stackView.spacing = 10
|
2021-02-26 09:43:59 +01:00
|
|
|
stackView.layoutMargins = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
|
2021-02-22 09:20:44 +01:00
|
|
|
stackView.addArrangedSubview(largeTitleLabel)
|
|
|
|
stackView.addArrangedSubview(subtitleLabel)
|
|
|
|
stackView.addArrangedSubview(rulesLabel)
|
|
|
|
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
scrollView.addSubview(stackView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
|
|
|
|
stackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
|
|
|
|
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
rulesLabel.attributedText = viewModel.rulesAttributedString
|
|
|
|
confirmButton.addTarget(self, action: #selector(MastodonServerRulesViewController.confirmButtonPressed(_:)), for: .touchUpInside)
|
2021-02-26 12:52:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
updateScrollViewContentInset()
|
2021-02-22 09:20:44 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 12:52:58 +01:00
|
|
|
override func viewSafeAreaInsetsDidChange() {
|
|
|
|
super.viewSafeAreaInsetsDidChange()
|
|
|
|
updateScrollViewContentInset()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonServerRulesViewController {
|
|
|
|
func updateScrollViewContentInset() {
|
|
|
|
view.layoutIfNeeded()
|
2021-03-30 08:31:52 +02:00
|
|
|
scrollView.contentInset.bottom = bottomContainerView.frame.height
|
|
|
|
scrollView.verticalScrollIndicatorInsets.bottom = bottomContainerView.frame.height
|
|
|
|
}
|
|
|
|
|
|
|
|
func configTextView() {
|
|
|
|
let str = NSString(string: L10n.Scene.ServerRules.prompt(viewModel.domain))
|
|
|
|
let termsOfServiceRange = str.range(of: L10n.Scene.ServerRules.termsOfService)
|
|
|
|
let privacyRange = str.range(of: L10n.Scene.ServerRules.privacyPolicy)
|
2021-06-22 14:52:30 +02:00
|
|
|
let attributeString = NSMutableAttributedString(
|
|
|
|
string: L10n.Scene.ServerRules.prompt(viewModel.domain),
|
|
|
|
attributes: [
|
|
|
|
NSAttributedString.Key.font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22),
|
|
|
|
NSAttributedString.Key.foregroundColor: UIColor.label
|
|
|
|
]
|
|
|
|
)
|
2021-03-30 08:31:52 +02:00
|
|
|
attributeString.addAttribute(.link, value: Mastodon.API.serverRulesURL(domain: viewModel.domain), range: termsOfServiceRange)
|
|
|
|
attributeString.addAttribute(.link, value: Mastodon.API.privacyURL(domain: viewModel.domain), range: privacyRange)
|
2021-06-22 14:52:30 +02:00
|
|
|
let linkAttributes = [NSAttributedString.Key.foregroundColor: Asset.Colors.brandBlue.color]
|
2021-03-30 08:31:52 +02:00
|
|
|
bottomPromptTextView.attributedText = attributeString
|
|
|
|
bottomPromptTextView.linkTextAttributes = linkAttributes
|
|
|
|
bottomPromptTextView.delegate = self
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonServerRulesViewController: UITextViewDelegate {
|
|
|
|
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
|
|
|
|
let safariVC = SFSafariViewController(url: URL)
|
|
|
|
self.present(safariVC, animated: true, completion: nil)
|
|
|
|
return false
|
2021-02-26 12:52:58 +01:00
|
|
|
}
|
2021-02-22 09:20:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonServerRulesViewController {
|
|
|
|
@objc private func confirmButtonPressed(_ sender: UIButton) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-03-01 11:06:37 +01:00
|
|
|
|
2021-04-29 10:12:04 +02:00
|
|
|
let viewModel = MastodonRegisterViewModel(domain: self.viewModel.domain, context: self.context, authenticateInfo: self.viewModel.authenticateInfo, instance: self.viewModel.instance, applicationToken: self.viewModel.applicationToken)
|
2021-03-01 11:06:37 +01:00
|
|
|
self.coordinator.present(scene: .mastodonRegister(viewModel: viewModel), from: self, transition: .show)
|
2021-02-22 09:20:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
// MARK: - OnboardingViewControllerAppearance
|
|
|
|
extension MastodonServerRulesViewController: OnboardingViewControllerAppearance { }
|
|
|
|
|
2021-02-22 09:20:44 +01:00
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ServerRulesViewController_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
UIViewControllerPreview {
|
|
|
|
let viewController = MastodonServerRulesViewController()
|
|
|
|
return viewController
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 800))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|