metatext/View Controllers/AddIdentityViewController.swift

302 lines
13 KiB
Swift
Raw Normal View History

2021-01-27 20:31:32 +00:00
// Copyright © 2021 Metabolist. All rights reserved.
import Combine
import Kingfisher
import Mastodon
import SafariServices
import SwiftUI
import ViewModels
final class AddIdentityViewController: UIViewController {
private let viewModel: AddIdentityViewModel
2021-01-28 03:58:23 +00:00
private let rootViewModel: RootViewModel
2021-01-27 20:31:32 +00:00
private let displayWelcome: Bool
private let scrollView = UIScrollView()
private let stackView = UIStackView()
private let promptLabel = UILabel()
private let urlTextField = UITextField()
private let welcomeLabel = UILabel()
2021-01-30 09:03:05 +00:00
private let instanceAndButtonsStackView = UIStackView()
2021-01-27 20:31:32 +00:00
private let instanceStackView = UIStackView()
2021-01-30 09:03:05 +00:00
private let instanceImageView = AnimatedImageView()
2021-01-27 20:31:32 +00:00
private let instanceTitleLabel = UILabel()
private let instanceURLLabel = UILabel()
2021-01-30 09:03:05 +00:00
private let buttonsStackView = UIStackView()
private let logInButton = CapsuleButton()
private let activityIndicator = UIActivityIndicatorView(style: .large)
private let joinButton = CapsuleButton()
private let browseButton = CapsuleButton()
2021-01-27 20:31:32 +00:00
private let whatIsMastodonButton = UIButton(type: .system)
private var cancellables = Set<AnyCancellable>()
2021-01-28 03:58:23 +00:00
init(viewModel: AddIdentityViewModel, rootViewModel: RootViewModel, displayWelcome: Bool) {
2021-01-27 20:31:32 +00:00
self.viewModel = viewModel
2021-01-28 03:58:23 +00:00
self.rootViewModel = rootViewModel
2021-01-27 20:31:32 +00:00
self.displayWelcome = displayWelcome
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
configureViews()
setupViewHierarchy()
setupConstraints()
setupViewModelBindings()
initialDisplay()
}
}
private extension AddIdentityViewController {
2021-01-30 09:03:05 +00:00
static let verticalSpacing: CGFloat = 20
2021-01-27 20:31:32 +00:00
static let whatIsMastodonURL = URL(string: "https://joinmastodon.org")!
// swiftlint:disable:next function_body_length
func configureViews() {
scrollView.translatesAutoresizingMaskIntoConstraints = false
stackView.translatesAutoresizingMaskIntoConstraints = false
2021-01-30 09:03:05 +00:00
stackView.spacing = Self.verticalSpacing
2021-01-27 20:31:32 +00:00
stackView.axis = .vertical
2021-01-30 09:03:05 +00:00
welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
2021-01-27 20:31:32 +00:00
welcomeLabel.numberOfLines = 0
welcomeLabel.textAlignment = .center
welcomeLabel.adjustsFontForContentSizeCategory = true
welcomeLabel.font = .preferredFont(forTextStyle: .largeTitle)
welcomeLabel.text = NSLocalizedString("add-identity.welcome", comment: "")
promptLabel.numberOfLines = 0
promptLabel.textAlignment = .center
promptLabel.adjustsFontForContentSizeCategory = true
promptLabel.font = .preferredFont(forTextStyle: .callout)
promptLabel.text = NSLocalizedString("add-identity.prompt", comment: "")
urlTextField.borderStyle = .roundedRect
urlTextField.textContentType = .URL
urlTextField.autocapitalizationType = .none
urlTextField.autocorrectionType = .no
urlTextField.keyboardType = .URL
urlTextField.placeholder = NSLocalizedString("add-identity.instance-url", comment: "")
urlTextField.addAction(
UIAction { [weak self] _ in self?.viewModel.urlFieldText = self?.urlTextField.text ?? "" },
for: .editingChanged)
2021-01-30 09:03:05 +00:00
instanceAndButtonsStackView.spacing = .defaultSpacing
instanceAndButtonsStackView.distribution = .fillEqually
2021-01-27 20:31:32 +00:00
instanceStackView.translatesAutoresizingMaskIntoConstraints = false
instanceStackView.axis = .vertical
instanceStackView.spacing = .compactSpacing
2021-01-30 09:03:05 +00:00
instanceStackView.isHidden_stackViewSafe = true
2021-01-27 20:31:32 +00:00
instanceTitleLabel.numberOfLines = 0
instanceTitleLabel.textAlignment = .center
instanceTitleLabel.adjustsFontForContentSizeCategory = true
instanceTitleLabel.font = .preferredFont(forTextStyle: .headline)
instanceURLLabel.numberOfLines = 0
instanceURLLabel.textAlignment = .center
instanceURLLabel.adjustsFontForContentSizeCategory = true
instanceURLLabel.font = .preferredFont(forTextStyle: .subheadline)
instanceURLLabel.textColor = .secondaryLabel
instanceImageView.contentMode = .scaleAspectFill
instanceImageView.layer.cornerRadius = .defaultCornerRadius
instanceImageView.clipsToBounds = true
instanceImageView.kf.indicatorType = .activity
2021-01-30 09:03:05 +00:00
buttonsStackView.axis = .vertical
buttonsStackView.spacing = .defaultSpacing
activityIndicator.hidesWhenStopped = true
logInButton.setTitle(NSLocalizedString("add-identity.log-in", comment: ""), for: .normal)
logInButton.addAction(
UIAction { [weak self] _ in self?.viewModel.logInTapped() },
for: .touchUpInside)
2021-01-27 20:31:32 +00:00
joinButton.addAction(UIAction { [weak self] _ in self?.join() }, for: .touchUpInside)
2021-01-30 09:03:05 +00:00
joinButton.isHidden_stackViewSafe = true
2021-01-27 20:31:32 +00:00
browseButton.setTitle(NSLocalizedString("add-identity.browse", comment: ""), for: .normal)
browseButton.addAction(
UIAction { [weak self] _ in self?.viewModel.browseTapped() },
for: .touchUpInside)
2021-01-30 09:03:05 +00:00
browseButton.isHidden_stackViewSafe = true
2021-01-27 20:31:32 +00:00
whatIsMastodonButton.setTitle(NSLocalizedString("add-identity.what-is-mastodon", comment: ""), for: .normal)
whatIsMastodonButton.addAction(
UIAction { [weak self] _ in
self?.present(SFSafariViewController(url: Self.whatIsMastodonURL), animated: true)
},
for: .touchUpInside)
2021-01-30 09:03:05 +00:00
for button in [logInButton, joinButton, browseButton, whatIsMastodonButton] {
button.setContentCompressionResistancePriority(.required, for: .vertical)
2021-01-27 20:31:32 +00:00
}
}
func setupViewHierarchy() {
2021-01-30 09:03:05 +00:00
view.addSubview(welcomeLabel)
2021-01-27 20:31:32 +00:00
view.addSubview(scrollView)
scrollView.addSubview(stackView)
stackView.addArrangedSubview(promptLabel)
stackView.addArrangedSubview(urlTextField)
2021-01-30 09:03:05 +00:00
stackView.addArrangedSubview(instanceAndButtonsStackView)
instanceStackView.addArrangedSubview(instanceImageView)
2021-01-27 20:31:32 +00:00
instanceStackView.addArrangedSubview(instanceTitleLabel)
instanceStackView.addArrangedSubview(instanceURLLabel)
2021-01-30 09:03:05 +00:00
instanceAndButtonsStackView.addArrangedSubview(instanceStackView)
instanceAndButtonsStackView.addArrangedSubview(buttonsStackView)
buttonsStackView.addArrangedSubview(activityIndicator)
buttonsStackView.addArrangedSubview(logInButton)
buttonsStackView.addArrangedSubview(joinButton)
buttonsStackView.addArrangedSubview(browseButton)
buttonsStackView.addArrangedSubview(UIView())
2021-01-27 20:31:32 +00:00
stackView.addArrangedSubview(whatIsMastodonButton)
}
func setupConstraints() {
let instanceImageViewWidthConstraint = instanceImageView.widthAnchor.constraint(
equalTo: instanceImageView.heightAnchor, multiplier: 16 / 9)
instanceImageViewWidthConstraint.priority = .justBelowMax
NSLayoutConstraint.activate([
2021-01-30 09:03:05 +00:00
welcomeLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),
welcomeLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor),
welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
2021-01-27 20:31:32 +00:00
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: .defaultSpacing),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
stackView.widthAnchor.constraint(equalTo: scrollView.readableContentGuide.widthAnchor),
stackView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
2021-01-30 09:03:05 +00:00
instanceImageViewWidthConstraint
2021-01-27 20:31:32 +00:00
])
}
func setupViewModelBindings() {
2021-01-31 08:12:10 +00:00
viewModel.$instance.combineLatest(viewModel.$isPublicTimelineAvailable, viewModel.$loading)
2021-01-30 09:03:05 +00:00
.throttle(for: .seconds(.defaultAnimationDuration), scheduler: DispatchQueue.main, latest: true)
2021-01-31 08:12:10 +00:00
.sink { [weak self] in self?.configure(instance: $0, isPublicTimelineAvailable: $1, loading: $2) }
2021-01-27 20:31:32 +00:00
.store(in: &cancellables)
viewModel.$alertItem
.compactMap { $0 }
.sink { [weak self] in self?.present(alertItem: $0) }
.store(in: &cancellables)
2021-01-28 03:58:23 +00:00
// There is a situation adding an identity from secondary navigation in which
// setting presentingSecondaryNavigation = false on the navigation view model
// does not work and the old secondary navigation is presented over the new
// main navigation. This is a hack to fix it.
rootViewModel.$navigationViewModel.dropFirst()
.sink { [weak self] _ in self?.dismiss(animated: true) }
.store(in: &cancellables)
2021-01-27 20:31:32 +00:00
}
func initialDisplay() {
if displayWelcome, !UIAccessibility.isVoiceOverRunning {
2021-01-27 20:31:32 +00:00
welcomeLabel.alpha = 0
promptLabel.alpha = 0
urlTextField.alpha = 0
logInButton.alpha = 0
whatIsMastodonButton.alpha = 0
UIView.animate(withDuration: .longAnimationDuration * 2) {
self.welcomeLabel.alpha = 1
} completion: { _ in
UIView.animate(withDuration: .longAnimationDuration * 2) {
self.welcomeLabel.alpha = 0
} completion: { _ in
self.welcomeLabel.isHidden = true
UIView.animate(withDuration: .longAnimationDuration) {
self.promptLabel.alpha = 1
} completion: { _ in
UIView.animate(withDuration: .longAnimationDuration) {
self.urlTextField.alpha = 1
} completion: { _ in
self.urlTextField.becomeFirstResponder()
UIView.animate(withDuration: .longAnimationDuration) {
self.logInButton.alpha = 1
} completion: { _ in
2021-01-30 09:03:05 +00:00
self.whatIsMastodonButton.isHidden_stackViewSafe = false
self.whatIsMastodonButton.alpha = 0
2021-01-27 20:31:32 +00:00
UIView.animate(withDuration: .longAnimationDuration) {
self.whatIsMastodonButton.alpha = 1
}
}
}
}
}
}
} else {
2021-01-30 09:03:05 +00:00
welcomeLabel.isHidden_stackViewSafe = true
whatIsMastodonButton.isHidden_stackViewSafe = !displayWelcome
2021-01-27 20:31:32 +00:00
urlTextField.becomeFirstResponder()
}
}
2021-01-31 08:12:10 +00:00
func configure(instance: Instance?, isPublicTimelineAvailable: Bool, loading: Bool) {
if loading {
self.activityIndicator.startAnimating()
} else {
self.activityIndicator.stopAnimating()
}
2021-01-30 09:03:05 +00:00
UIView.animate(withDuration: .zeroIfReduceMotion(.defaultAnimationDuration)) {
2021-01-31 08:12:10 +00:00
self.logInButton.isHidden_stackViewSafe = loading
2021-01-30 09:03:05 +00:00
if let instance = instance {
self.instanceTitleLabel.text = instance.title
self.instanceURLLabel.text = instance.uri
self.instanceImageView.kf.setImage(with: instance.thumbnail)
self.instanceStackView.isHidden_stackViewSafe = false
if instance.registrations {
let joinButtonTitle: String
if instance.approvalRequired {
joinButtonTitle = NSLocalizedString("add-identity.request-invite", comment: "")
} else {
joinButtonTitle = NSLocalizedString("add-identity.join", comment: "")
}
2021-01-27 20:31:32 +00:00
2021-01-30 09:03:05 +00:00
self.joinButton.setTitle(joinButtonTitle, for: .normal)
2021-01-31 08:12:10 +00:00
self.joinButton.isHidden_stackViewSafe = loading
2021-01-27 20:31:32 +00:00
} else {
2021-01-30 09:03:05 +00:00
self.joinButton.isHidden_stackViewSafe = true
2021-01-27 20:31:32 +00:00
}
2021-01-31 08:12:10 +00:00
self.browseButton.isHidden_stackViewSafe = !isPublicTimelineAvailable || loading
2021-01-30 09:03:05 +00:00
self.whatIsMastodonButton.isHidden_stackViewSafe = true
2021-01-27 20:31:32 +00:00
} else {
2021-01-30 09:03:05 +00:00
self.instanceStackView.isHidden_stackViewSafe = true
self.joinButton.isHidden_stackViewSafe = true
self.browseButton.isHidden_stackViewSafe = true
2021-01-31 08:12:10 +00:00
self.whatIsMastodonButton.isHidden_stackViewSafe =
!self.displayWelcome || self.logInButton.alpha < 1 || loading
2021-01-27 20:31:32 +00:00
}
}
}
func join() {
guard let instance = viewModel.instance, let url = viewModel.url else { return }
let registrationViewModel = viewModel.registrationViewModel(instance: instance, url: url)
let registrationView = RegistrationView(viewModel: registrationViewModel)
let registrationViewController = UIHostingController(rootView: registrationView)
show(registrationViewController, sender: self)
}
}