mastodon-ios/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewControl...

795 lines
37 KiB
Swift
Raw Normal View History

2021-02-05 10:53:00 +01:00
//
// MastodonRegisterViewController.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-2-5.
//
import AlamofireImage
2021-02-05 10:53:00 +01:00
import Combine
import MastodonSDK
2021-02-20 06:55:06 +01:00
import os.log
import PhotosUI
2021-02-20 06:55:06 +01:00
import UIKit
2021-02-05 10:53:00 +01:00
2021-02-25 08:39:48 +01:00
final class MastodonRegisterViewController: UIViewController, NeedsDependency, OnboardingViewControllerAppearance {
2021-04-09 11:31:43 +02:00
static let avatarImageMaxSizeInPixel = CGSize(width: 400, height: 400)
static let textFieldLabelFont = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 22)
static let errorPromptLabelFont = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 13, weight: .semibold), maximumPointSize: 18)
2021-02-05 10:53:00 +01:00
var disposeBag = Set<AnyCancellable>()
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
var viewModel: MastodonRegisterViewModel!
2021-02-20 06:55:06 +01:00
// picker
private(set) lazy var imagePicker: PHPickerViewController = {
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 1
let imagePicker = PHPickerViewController(configuration: configuration)
imagePicker.delegate = self
return imagePicker
}()
private(set) lazy var imagePickerController: UIImagePickerController = {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .camera
imagePickerController.delegate = self
return imagePickerController
}()
private(set) lazy var documentPickerController: UIDocumentPickerViewController = {
2021-03-31 04:37:38 +02:00
let documentPickerController = UIDocumentPickerViewController(forOpeningContentTypes: [.image])
documentPickerController.delegate = self
return documentPickerController
}()
2021-02-20 06:55:06 +01:00
let tapGestureRecognizer = UITapGestureRecognizer.singleTapGestureRecognizer
2021-02-22 09:20:44 +01:00
let scrollView: UIScrollView = {
2021-02-20 06:55:06 +01:00
let scrollview = UIScrollView()
scrollview.showsVerticalScrollIndicator = false
scrollview.keyboardDismissMode = .interactive
scrollview.alwaysBounceVertical = true
scrollview.clipsToBounds = false // make content could display over bleeding
scrollview.translatesAutoresizingMaskIntoConstraints = false
2021-02-20 06:55:06 +01:00
return scrollview
}()
2021-02-05 10:53:00 +01:00
2021-02-20 06:55:06 +01:00
let largeTitleLabel: UILabel = {
let label = UILabel()
2021-06-16 09:29:57 +02:00
label.font = UIFontMetrics(forTextStyle: .largeTitle).scaledFont(for: .systemFont(ofSize: 34, weight: .bold))
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 = L10n.Scene.Register.title
label.numberOfLines = 0
2021-02-20 06:55:06 +01:00
return label
}()
let avatarView: UIView = {
2021-02-20 06:55:06 +01:00
let view = UIView()
view.backgroundColor = .clear
return view
}()
let avatarButton: UIButton = {
let button = HighlightDimmableButton()
2021-02-20 06:55:06 +01:00
let boldFont = UIFont.systemFont(ofSize: 42)
let configuration = UIImage.SymbolConfiguration(font: boldFont)
let image = UIImage(systemName: "person.fill.viewfinder", withConfiguration: configuration)
button.setImage(image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: UIControl.State.normal)
2021-04-25 11:19:47 +02:00
button.imageView?.tintColor = Asset.Colors.Label.secondary.color
button.backgroundColor = Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-04-25 11:19:47 +02:00
button.layer.cornerRadius = 10
2021-02-20 06:55:06 +01:00
button.clipsToBounds = true
2021-03-02 09:19:20 +01:00
2021-02-20 06:55:06 +01:00
return button
}()
let plusIconImageView: UIImageView = {
2021-02-20 06:55:06 +01:00
let icon = UIImageView()
2021-03-03 05:26:14 +01:00
let image = Asset.Circles.plusCircleFill.image.withRenderingMode(.alwaysTemplate)
2021-02-20 06:55:06 +01:00
icon.image = image
icon.tintColor = Asset.Colors.Icon.plus.color
2021-06-30 14:11:38 +02:00
icon.backgroundColor = UIColor(dynamicProvider: { collection in
switch collection.userInterfaceStyle {
case .dark:
return Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-06-30 14:11:38 +02:00
default:
return .white
}
})
2021-02-20 06:55:06 +01:00
return icon
}()
let domainLabel: UILabel = {
2021-02-05 10:53:00 +01:00
let label = UILabel()
label.font = MastodonRegisterViewController.textFieldLabelFont
2021-04-02 10:50:07 +02:00
label.textColor = Asset.Colors.Label.primary.color
2021-02-05 10:53:00 +01:00
return label
}()
let usernameTextField: UITextField = {
let textField = UITextField()
textField.returnKeyType = .next
2021-02-05 10:53:00 +01:00
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.backgroundColor = Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-02-26 11:27:47 +01:00
textField.textColor = Asset.Colors.Label.primary.color
2021-02-22 09:20:44 +01:00
textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Username.placeholder,
2021-02-26 11:27:47 +01:00
attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.Label.secondary.color,
NSAttributedString.Key.font: MastodonRegisterViewController.textFieldLabelFont])
2021-02-20 06:55:06 +01:00
textField.borderStyle = UITextField.BorderStyle.roundedRect
textField.font = MastodonRegisterViewController.textFieldLabelFont
textField.leftView = {
let containerView = UIView()
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: textField.frame.height))
paddingView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(paddingView)
NSLayoutConstraint.activate([
paddingView.topAnchor.constraint(equalTo: containerView.topAnchor),
paddingView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
paddingView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
paddingView.widthAnchor.constraint(equalToConstant: 5).priority(.defaultHigh),
])
let label = UILabel()
label.font = MastodonRegisterViewController.textFieldLabelFont
label.textColor = Asset.Colors.Label.primary.color
label.text = " @"
label.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(label)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: containerView.topAnchor),
label.leadingAnchor.constraint(equalTo: paddingView.trailingAnchor),
label.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
label.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
])
return containerView
}()
2021-02-20 06:55:06 +01:00
textField.leftViewMode = .always
2021-02-05 10:53:00 +01:00
return textField
}()
let usernameErrorPromptLabel: UILabel = {
2021-02-05 10:53:00 +01:00
let label = UILabel()
let color = Asset.Colors.danger.color
let font = MastodonRegisterViewController.errorPromptLabelFont
2021-02-05 10:53:00 +01:00
return label
}()
2021-02-20 06:55:06 +01:00
let displayNameTextField: UITextField = {
let textField = UITextField()
textField.returnKeyType = .next
2021-02-20 06:55:06 +01:00
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.backgroundColor = Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-02-26 11:27:47 +01:00
textField.textColor = Asset.Colors.Label.primary.color
2021-02-22 09:20:44 +01:00
textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.DisplayName.placeholder,
2021-02-26 11:27:47 +01:00
attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.Label.secondary.color,
NSAttributedString.Key.font: MastodonRegisterViewController.textFieldLabelFont])
2021-02-20 06:55:06 +01:00
textField.borderStyle = UITextField.BorderStyle.roundedRect
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: textField.frame.height))
textField.leftView = paddingView
textField.leftViewMode = .always
textField.font = MastodonRegisterViewController.textFieldLabelFont
2021-02-20 06:55:06 +01:00
return textField
}()
2021-02-05 10:53:00 +01:00
let emailTextField: UITextField = {
let textField = UITextField()
textField.returnKeyType = .next
2021-02-05 10:53:00 +01:00
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.keyboardType = .emailAddress
textField.backgroundColor = Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-02-26 11:27:47 +01:00
textField.textColor = Asset.Colors.Label.primary.color
2021-02-22 09:20:44 +01:00
textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Email.placeholder,
2021-02-26 11:27:47 +01:00
attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.Label.secondary.color,
NSAttributedString.Key.font: MastodonRegisterViewController.textFieldLabelFont])
2021-02-20 06:55:06 +01:00
textField.borderStyle = UITextField.BorderStyle.roundedRect
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: textField.frame.height))
textField.leftView = paddingView
textField.leftViewMode = .always
textField.font = MastodonRegisterViewController.textFieldLabelFont
2021-02-05 10:53:00 +01:00
return textField
}()
let emailErrorPromptLabel: UILabel = {
2021-02-05 10:53:00 +01:00
let label = UILabel()
let color = Asset.Colors.danger.color
let font = MastodonRegisterViewController.errorPromptLabelFont
2021-02-05 10:53:00 +01:00
return label
}()
let passwordTextField: UITextField = {
let textField = UITextField()
textField.returnKeyType = .next // set to "Return" depends on if the last input field or not
2021-02-05 10:53:00 +01:00
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.keyboardType = .asciiCapable
textField.isSecureTextEntry = true
textField.backgroundColor = Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-02-26 11:27:47 +01:00
textField.textColor = Asset.Colors.Label.primary.color
2021-02-22 09:20:44 +01:00
textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Password.placeholder,
2021-02-26 11:27:47 +01:00
attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.Label.secondary.color,
NSAttributedString.Key.font: MastodonRegisterViewController.textFieldLabelFont])
2021-02-20 06:55:06 +01:00
textField.borderStyle = UITextField.BorderStyle.roundedRect
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: textField.frame.height))
textField.leftView = paddingView
textField.leftViewMode = .always
textField.font = MastodonRegisterViewController.textFieldLabelFont
2021-02-05 10:53:00 +01:00
return textField
}()
let passwordCheckLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
return label
}()
let passwordErrorPromptLabel: UILabel = {
let label = UILabel()
let color = Asset.Colors.danger.color
let font = MastodonRegisterViewController.errorPromptLabelFont
return label
}()
lazy var reasonTextField: UITextField = {
let textField = UITextField()
textField.returnKeyType = .next // set to "Return" depends on if the last input field or not
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.backgroundColor = Asset.Theme.Mastodon.secondaryGroupedSystemBackground.color
2021-02-26 11:27:47 +01:00
textField.textColor = Asset.Colors.Label.primary.color
textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Invite.registrationUserInviteRequest,
2021-02-26 11:27:47 +01:00
attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.Label.secondary.color,
NSAttributedString.Key.font: MastodonRegisterViewController.textFieldLabelFont])
textField.borderStyle = UITextField.BorderStyle.roundedRect
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: textField.frame.height))
textField.leftView = paddingView
textField.leftViewMode = .always
textField.font = MastodonRegisterViewController.textFieldLabelFont
return textField
}()
let reasonErrorPromptLabel: UILabel = {
let label = UILabel()
let color = Asset.Colors.danger.color
let font = MastodonRegisterViewController.errorPromptLabelFont
return label
}()
2021-02-26 11:27:47 +01:00
let buttonContainer = UIView()
let signUpButton: PrimaryActionButton = {
let button = PrimaryActionButton()
2021-02-20 06:55:06 +01:00
button.isEnabled = false
2021-02-22 09:20:44 +01:00
button.setTitle(L10n.Common.Controls.Actions.continue, for: .normal)
2021-02-05 10:53:00 +01:00
return button
}()
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-26 11:27:47 +01:00
}
2021-02-05 10:53:00 +01:00
}
extension MastodonRegisterViewController {
2021-02-05 10:53:00 +01:00
override func viewDidLoad() {
super.viewDidLoad()
setupOnboardingAppearance()
defer { setupNavigationBarBackgroundView() }
2021-02-25 08:39:48 +01:00
avatarButton.menu = createMediaContextMenu()
avatarButton.showsMenuAsPrimaryAction = true
2021-02-20 06:55:06 +01:00
domainLabel.text = "@" + viewModel.domain + " "
domainLabel.sizeToFit()
2021-03-05 12:30:52 +01:00
passwordCheckLabel.attributedText = MastodonRegisterViewModel.attributeStringForPassword(validateState: .empty)
2021-02-20 06:55:06 +01:00
usernameTextField.rightView = domainLabel
usernameTextField.rightViewMode = .always
usernameTextField.delegate = self
displayNameTextField.delegate = self
emailTextField.delegate = self
passwordTextField.delegate = self
2021-02-05 10:53:00 +01:00
2021-02-20 06:55:06 +01:00
// gesture
view.addGestureRecognizer(tapGestureRecognizer)
2021-02-22 10:48:44 +01:00
tapGestureRecognizer.addTarget(self, action: #selector(tapGestureRecognizerHandler))
2021-02-05 10:53:00 +01:00
2021-02-20 06:55:06 +01:00
// stackview
let stackView = UIStackView()
2021-02-05 10:53:00 +01:00
stackView.axis = .vertical
2021-02-20 06:55:06 +01:00
stackView.distribution = .fill
stackView.spacing = 40
stackView.layoutMargins = UIEdgeInsets(top: 20, left: 0, bottom: 26, right: 0)
2021-02-20 06:55:06 +01:00
stackView.isLayoutMarginsRelativeArrangement = true
stackView.addArrangedSubview(largeTitleLabel)
stackView.addArrangedSubview(avatarView)
2021-02-05 10:53:00 +01:00
stackView.addArrangedSubview(usernameTextField)
2021-02-20 06:55:06 +01:00
stackView.addArrangedSubview(displayNameTextField)
2021-02-05 10:53:00 +01:00
stackView.addArrangedSubview(emailTextField)
stackView.addArrangedSubview(passwordTextField)
2021-02-20 06:55:06 +01:00
stackView.addArrangedSubview(passwordCheckLabel)
if viewModel.approvalRequired {
stackView.addArrangedSubview(reasonTextField)
}
usernameErrorPromptLabel.translatesAutoresizingMaskIntoConstraints = false
stackView.addSubview(usernameErrorPromptLabel)
NSLayoutConstraint.activate([
usernameErrorPromptLabel.topAnchor.constraint(equalTo: usernameTextField.bottomAnchor, constant: 6),
usernameErrorPromptLabel.leadingAnchor.constraint(equalTo: usernameTextField.leadingAnchor),
usernameErrorPromptLabel.trailingAnchor.constraint(equalTo: usernameTextField.trailingAnchor),
])
emailErrorPromptLabel.translatesAutoresizingMaskIntoConstraints = false
stackView.addSubview(emailErrorPromptLabel)
NSLayoutConstraint.activate([
emailErrorPromptLabel.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 6),
emailErrorPromptLabel.leadingAnchor.constraint(equalTo: emailTextField.leadingAnchor),
emailErrorPromptLabel.trailingAnchor.constraint(equalTo: emailTextField.trailingAnchor),
])
passwordErrorPromptLabel.translatesAutoresizingMaskIntoConstraints = false
stackView.addSubview(passwordErrorPromptLabel)
NSLayoutConstraint.activate([
passwordErrorPromptLabel.topAnchor.constraint(equalTo: passwordCheckLabel.bottomAnchor, constant: 2),
passwordErrorPromptLabel.leadingAnchor.constraint(equalTo: passwordTextField.leadingAnchor),
passwordErrorPromptLabel.trailingAnchor.constraint(equalTo: passwordTextField.trailingAnchor),
])
2021-02-22 09:20:44 +01:00
// scrollView
view.addSubview(scrollView)
2021-02-20 06:55:06 +01:00
NSLayoutConstraint.activate([
2021-02-22 09:20:44 +01:00
scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
view.readableContentGuide.trailingAnchor.constraint(equalTo: scrollView.frameLayoutGuide.trailingAnchor),
scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
scrollView.frameLayoutGuide.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
2021-02-20 06:55:06 +01:00
])
2021-02-20 06:55:06 +01:00
// stackview
2021-02-22 09:20:44 +01:00
scrollView.addSubview(stackView)
2021-02-20 06:55:06 +01:00
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
2021-02-22 09:20:44 +01:00
stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
stackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
stackView.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: stackView.bottomAnchor),
])
2021-02-20 06:55:06 +01:00
// photoview
avatarView.translatesAutoresizingMaskIntoConstraints = false
avatarView.addSubview(avatarButton)
2021-02-20 06:55:06 +01:00
NSLayoutConstraint.activate([
avatarView.heightAnchor.constraint(equalToConstant: 90).priority(.defaultHigh),
2021-02-20 06:55:06 +01:00
])
avatarButton.translatesAutoresizingMaskIntoConstraints = false
2021-02-20 06:55:06 +01:00
NSLayoutConstraint.activate([
2021-04-25 11:19:47 +02:00
avatarButton.heightAnchor.constraint(equalToConstant: 92).priority(.defaultHigh),
avatarButton.widthAnchor.constraint(equalToConstant: 92).priority(.defaultHigh),
avatarButton.centerXAnchor.constraint(equalTo: avatarView.centerXAnchor),
avatarButton.centerYAnchor.constraint(equalTo: avatarView.centerYAnchor),
2021-02-20 06:55:06 +01:00
])
2021-03-03 05:26:14 +01:00
plusIconImageView.translatesAutoresizingMaskIntoConstraints = false
avatarView.addSubview(plusIconImageView)
2021-02-20 06:55:06 +01:00
NSLayoutConstraint.activate([
2021-04-25 11:19:47 +02:00
plusIconImageView.centerXAnchor.constraint(equalTo: avatarButton.trailingAnchor),
plusIconImageView.centerYAnchor.constraint(equalTo: avatarButton.bottomAnchor),
2021-02-20 06:55:06 +01:00
])
2021-02-20 06:55:06 +01:00
// textfield
NSLayoutConstraint.activate([
usernameTextField.heightAnchor.constraint(equalToConstant: 50).priority(.defaultHigh),
displayNameTextField.heightAnchor.constraint(equalToConstant: 50).priority(.defaultHigh),
emailTextField.heightAnchor.constraint(equalToConstant: 50).priority(.defaultHigh),
passwordTextField.heightAnchor.constraint(equalToConstant: 50).priority(.defaultHigh),
])
// password
stackView.setCustomSpacing(6, after: passwordTextField)
stackView.setCustomSpacing(32, after: passwordCheckLabel)
// return
if viewModel.approvalRequired {
reasonTextField.returnKeyType = .done
} else {
passwordTextField.returnKeyType = .done
}
2021-02-20 06:55:06 +01:00
// button
2021-02-26 11:27:47 +01:00
stackView.addArrangedSubview(buttonContainer)
2021-02-05 10:53:00 +01:00
signUpButton.translatesAutoresizingMaskIntoConstraints = false
2021-02-26 11:27:47 +01:00
buttonContainer.addSubview(signUpButton)
2021-02-05 10:53:00 +01:00
NSLayoutConstraint.activate([
2021-02-26 11:27:47 +01:00
signUpButton.topAnchor.constraint(equalTo: buttonContainer.topAnchor),
signUpButton.leadingAnchor.constraint(equalTo: buttonContainer.leadingAnchor, constant: MastodonRegisterViewController.actionButtonMargin),
buttonContainer.trailingAnchor.constraint(equalTo: signUpButton.trailingAnchor, constant: MastodonRegisterViewController.actionButtonMargin),
buttonContainer.bottomAnchor.constraint(equalTo: signUpButton.bottomAnchor),
signUpButton.heightAnchor.constraint(equalToConstant: MastodonRegisterViewController.actionButtonHeight).priority(.defaultHigh),
2021-02-05 10:53:00 +01:00
])
2021-02-26 11:27:47 +01:00
Publishers.CombineLatest(
KeyboardResponderService.shared.state.eraseToAnyPublisher(),
2021-03-12 08:23:28 +01:00
KeyboardResponderService.shared.endFrame.eraseToAnyPublisher()
)
.sink(receiveValue: { [weak self] state, endFrame in
guard let self = self else { return }
guard state == .dock else {
2021-02-22 09:20:44 +01:00
self.scrollView.contentInset.bottom = 0.0
self.scrollView.verticalScrollIndicatorInsets.bottom = 0.0
return
2021-02-20 06:55:06 +01:00
}
2021-02-22 09:20:44 +01:00
let contentFrame = self.view.convert(self.scrollView.frame, to: nil)
let padding = contentFrame.maxY - endFrame.minY
guard padding > 0 else {
2021-02-22 09:20:44 +01:00
self.scrollView.contentInset.bottom = 0.0
self.scrollView.verticalScrollIndicatorInsets.bottom = 0.0
return
}
2021-02-22 09:20:44 +01:00
self.scrollView.contentInset.bottom = padding + 16
self.scrollView.verticalScrollIndicatorInsets.bottom = padding + 16
if self.passwordTextField.isFirstResponder {
2021-02-26 11:27:47 +01:00
let contentFrame = self.buttonContainer.convert(self.signUpButton.frame, to: nil)
let labelPadding = contentFrame.maxY - endFrame.minY
let contentOffsetY = self.scrollView.contentOffset.y
DispatchQueue.main.async {
self.scrollView.setContentOffset(CGPoint(x: 0, y: contentOffsetY + labelPadding + 16.0), animated: true)
}
}
})
.store(in: &disposeBag)
avatarButton.publisher(for: \.isHighlighted, options: .new)
.receive(on: DispatchQueue.main)
.sink { [weak self] isHighlighted in
guard let self = self else { return }
let alpha: CGFloat = isHighlighted ? 0.6 : 1
self.plusIconImageView.alpha = alpha
}
.store(in: &disposeBag)
2021-02-05 10:53:00 +01:00
viewModel.isRegistering
.receive(on: DispatchQueue.main)
.sink { [weak self] isRegistering in
guard let self = self else { return }
2021-02-26 11:27:47 +01:00
isRegistering ? self.signUpButton.showLoading() : self.signUpButton.stopLoading()
2021-02-05 10:53:00 +01:00
}
.store(in: &disposeBag)
2021-02-22 10:48:44 +01:00
viewModel.usernameValidateState
2021-02-20 10:13:16 +01:00
.receive(on: DispatchQueue.main)
2021-02-22 10:48:44 +01:00
.sink { [weak self] validateState in
2021-02-20 10:13:16 +01:00
guard let self = self else { return }
2021-02-22 10:48:44 +01:00
self.setTextFieldValidAppearance(self.usernameTextField, validateState: validateState)
2021-02-20 10:13:16 +01:00
}
.store(in: &disposeBag)
viewModel.usernameErrorPrompt
.receive(on: DispatchQueue.main)
.sink { [weak self] prompt in
guard let self = self else { return }
self.usernameErrorPromptLabel.attributedText = prompt
}
.store(in: &disposeBag)
2021-02-22 10:48:44 +01:00
viewModel.displayNameValidateState
2021-02-20 11:24:23 +01:00
.receive(on: DispatchQueue.main)
2021-02-22 10:48:44 +01:00
.sink { [weak self] validateState in
2021-02-20 11:24:23 +01:00
guard let self = self else { return }
2021-02-22 10:48:44 +01:00
self.setTextFieldValidAppearance(self.displayNameTextField, validateState: validateState)
2021-02-20 11:24:23 +01:00
}
.store(in: &disposeBag)
2021-02-22 10:48:44 +01:00
viewModel.emailValidateState
2021-02-20 11:24:23 +01:00
.receive(on: DispatchQueue.main)
2021-02-22 10:48:44 +01:00
.sink { [weak self] validateState in
2021-02-20 11:24:23 +01:00
guard let self = self else { return }
2021-02-22 10:48:44 +01:00
self.setTextFieldValidAppearance(self.emailTextField, validateState: validateState)
2021-02-20 11:24:23 +01:00
}
.store(in: &disposeBag)
viewModel.emailErrorPrompt
.receive(on: DispatchQueue.main)
.sink { [weak self] prompt in
guard let self = self else { return }
self.emailErrorPromptLabel.attributedText = prompt
}
.store(in: &disposeBag)
2021-02-22 10:48:44 +01:00
viewModel.passwordValidateState
2021-02-20 11:24:23 +01:00
.receive(on: DispatchQueue.main)
2021-02-22 10:48:44 +01:00
.sink { [weak self] validateState in
2021-02-20 11:24:23 +01:00
guard let self = self else { return }
2021-02-22 10:48:44 +01:00
self.setTextFieldValidAppearance(self.passwordTextField, validateState: validateState)
2021-03-05 12:30:52 +01:00
self.passwordCheckLabel.attributedText = MastodonRegisterViewModel.attributeStringForPassword(validateState: validateState)
}
.store(in: &disposeBag)
viewModel.passwordErrorPrompt
.receive(on: DispatchQueue.main)
.sink { [weak self] prompt in
guard let self = self else { return }
self.passwordErrorPromptLabel.attributedText = prompt
}
.store(in: &disposeBag)
viewModel.reasonErrorPrompt
.receive(on: DispatchQueue.main)
.sink { [weak self] prompt in
guard let self = self else { return }
self.reasonErrorPromptLabel.attributedText = prompt
2021-02-20 11:24:23 +01:00
}
.store(in: &disposeBag)
2021-02-20 10:13:16 +01:00
2021-02-22 10:48:44 +01:00
viewModel.isAllValid
.receive(on: DispatchQueue.main)
.sink { [weak self] isAllValid in
guard let self = self else { return }
self.signUpButton.isEnabled = isAllValid
}
.store(in: &disposeBag)
2021-02-05 10:53:00 +01:00
viewModel.error
.receive(on: DispatchQueue.main)
.sink { [weak self] error in
guard let self = self else { return }
2021-03-01 09:27:24 +01:00
guard let error = error as? Mastodon.API.Error else { return }
let alertController = UIAlertController(for: error, title: "Sign Up Failure", preferredStyle: .alert)
2021-02-22 09:20:44 +01:00
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil)
2021-02-05 10:53:00 +01:00
alertController.addAction(okAction)
self.coordinator.present(
scene: .alertController(alertController: alertController),
from: nil,
transition: .alertController(animated: true, completion: nil)
)
}
.store(in: &disposeBag)
2021-03-31 04:37:38 +02:00
viewModel.avatarImage
.receive(on: DispatchQueue.main)
.sink{ [weak self] image in
guard let self = self else { return }
self.avatarButton.menu = self.createMediaContextMenu()
if let avatar = image {
self.avatarButton.setImage(avatar, for: .normal)
} else {
let boldFont = UIFont.systemFont(ofSize: 42)
let configuration = UIImage.SymbolConfiguration(font: boldFont)
let image = UIImage(systemName: "person.fill.viewfinder", withConfiguration: configuration)
self.avatarButton.setImage(image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: UIControl.State.normal)
}
}
.store(in: &disposeBag)
2021-02-22 10:48:44 +01:00
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: usernameTextField)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.viewModel.username.value = self.usernameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
.store(in: &disposeBag)
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: displayNameTextField)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.viewModel.displayName.value = self.displayNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
.store(in: &disposeBag)
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: emailTextField)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.viewModel.email.value = self.emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
.store(in: &disposeBag)
2021-02-20 06:55:06 +01:00
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: passwordTextField)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
2021-02-22 10:48:44 +01:00
self.viewModel.password.value = self.passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
2021-02-20 06:55:06 +01:00
}
.store(in: &disposeBag)
if viewModel.approvalRequired {
reasonTextField.delegate = self
NSLayoutConstraint.activate([
reasonTextField.heightAnchor.constraint(equalToConstant: 50).priority(.defaultHigh),
])
reasonErrorPromptLabel.translatesAutoresizingMaskIntoConstraints = false
stackView.addSubview(reasonErrorPromptLabel)
NSLayoutConstraint.activate([
reasonErrorPromptLabel.topAnchor.constraint(equalTo: reasonTextField.bottomAnchor, constant: 6),
reasonErrorPromptLabel.leadingAnchor.constraint(equalTo: reasonTextField.leadingAnchor),
reasonErrorPromptLabel.trailingAnchor.constraint(equalTo: reasonTextField.trailingAnchor),
])
viewModel.reasonValidateState
.receive(on: DispatchQueue.main)
.sink { [weak self] validateState in
guard let self = self else { return }
self.setTextFieldValidAppearance(self.reasonTextField, validateState: validateState)
}
.store(in: &disposeBag)
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: reasonTextField)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.viewModel.reason.value = self.reasonTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
.store(in: &disposeBag)
}
2021-02-05 10:53:00 +01:00
signUpButton.addTarget(self, action: #selector(MastodonRegisterViewController.signUpButtonPressed(_:)), for: .touchUpInside)
}
2021-03-03 05:26:14 +01:00
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
plusIconImageView.layer.cornerRadius = plusIconImageView.frame.width / 2
plusIconImageView.layer.masksToBounds = true
2021-03-03 05:26:14 +01:00
}
2021-02-05 10:53:00 +01:00
}
2021-02-20 06:55:06 +01:00
extension MastodonRegisterViewController: UITextFieldDelegate {
2021-02-22 10:48:44 +01:00
func textFieldDidBeginEditing(_ textField: UITextField) {
let text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
switch textField {
case usernameTextField:
viewModel.username.value = text
case displayNameTextField:
viewModel.displayName.value = text
case emailTextField:
viewModel.email.value = text
case passwordTextField:
viewModel.password.value = text
case reasonTextField:
viewModel.reason.value = text
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
let text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
2021-02-20 10:13:16 +01:00
switch textField {
case usernameTextField:
2021-02-22 10:48:44 +01:00
viewModel.username.value = text
2021-02-20 11:24:23 +01:00
case displayNameTextField:
2021-02-22 10:48:44 +01:00
viewModel.displayName.value = text
2021-02-20 11:24:23 +01:00
case emailTextField:
2021-02-22 10:48:44 +01:00
viewModel.email.value = text
2021-02-20 11:24:23 +01:00
case passwordTextField:
2021-02-22 10:48:44 +01:00
viewModel.password.value = text
case reasonTextField:
2021-02-26 11:27:47 +01:00
viewModel.reason.value = text
2021-02-22 10:48:44 +01:00
default:
break
2021-02-20 06:55:06 +01:00
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
switch textField {
case usernameTextField:
displayNameTextField.becomeFirstResponder()
case displayNameTextField:
emailTextField.becomeFirstResponder()
case emailTextField:
passwordTextField.becomeFirstResponder()
case passwordTextField:
if viewModel.approvalRequired {
reasonTextField.becomeFirstResponder()
} else {
passwordTextField.resignFirstResponder()
}
case reasonTextField:
reasonTextField.resignFirstResponder()
default:
break
}
return true
}
2021-02-20 06:55:06 +01:00
func showShadowWithColor(color: UIColor, textField: UITextField) {
// To apply Shadow
textField.layer.shadowOpacity = 1
textField.layer.shadowRadius = 2.0
2021-02-20 11:24:23 +01:00
textField.layer.shadowOffset = CGSize.zero
2021-02-20 06:55:06 +01:00
textField.layer.shadowColor = color.cgColor
2021-02-20 10:13:16 +01:00
textField.layer.shadowPath = UIBezierPath(roundedRect: textField.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 2.0, height: 2.0)).cgPath
2021-02-20 06:55:06 +01:00
}
2021-02-20 11:24:23 +01:00
2021-02-22 10:48:44 +01:00
private func setTextFieldValidAppearance(_ textField: UITextField, validateState: MastodonRegisterViewModel.ValidateState) {
switch validateState {
case .empty:
2021-06-22 14:52:30 +02:00
showShadowWithColor(color: textField.isFirstResponder ? Asset.Colors.brandBlue.color : .clear, textField: textField)
2021-02-22 10:48:44 +01:00
case .valid:
showShadowWithColor(color: Asset.Colors.TextField.valid.color, textField: textField)
case .invalid:
showShadowWithColor(color: Asset.Colors.TextField.invalid.color, textField: textField)
2021-02-20 10:13:16 +01:00
}
}
2021-02-20 06:55:06 +01:00
}
extension MastodonRegisterViewController {
2021-02-22 10:48:44 +01:00
@objc private func tapGestureRecognizerHandler(_ sender: UITapGestureRecognizer) {
view.endEditing(true)
2021-02-20 06:55:06 +01:00
}
@objc private func signUpButtonPressed(_ sender: UIButton) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function)
2021-02-22 10:48:44 +01:00
guard viewModel.isAllValid.value else { return }
2021-02-05 10:53:00 +01:00
guard !viewModel.isRegistering.value else { return }
viewModel.isRegistering.value = true
2021-02-22 10:48:44 +01:00
let username = viewModel.username.value
let email = viewModel.email.value
let password = viewModel.password.value
2021-02-26 11:27:47 +01:00
let query = Mastodon.API.Account.RegisterQuery(
reason: viewModel.reason.value,
username: username,
email: email,
password: password,
agreement: true, // user confirmed in the server rules scene
locale: Locale.current.languageCode ?? "en"
2021-02-26 11:27:47 +01:00
)
// register without show server rules
context.apiService.accountRegister(
domain: viewModel.domain,
query: query,
authorization: viewModel.applicationAuthorization
)
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
guard let self = self else { return }
self.viewModel.isRegistering.value = false
switch completion {
case .failure(let error):
self.viewModel.error.send(error)
case .finished:
break
2021-02-26 11:27:47 +01:00
}
} receiveValue: { [weak self] response in
guard let self = self else { return }
let userToken = response.value
let updateCredentialQuery: Mastodon.API.Account.UpdateCredentialQuery = {
let displayName: String? = self.viewModel.displayName.value.isEmpty ? nil : self.viewModel.displayName.value
let avatar: Mastodon.Query.MediaAttachment? = {
guard let avatarImage = self.viewModel.avatarImage.value else { return nil }
2021-04-09 11:31:43 +02:00
guard avatarImage.size.width <= MastodonRegisterViewController.avatarImageMaxSizeInPixel.width else {
return .png(avatarImage.af.imageScaled(to: MastodonRegisterViewController.avatarImageMaxSizeInPixel).pngData())
}
2021-04-09 11:31:43 +02:00
return .png(avatarImage.pngData())
}()
return Mastodon.API.Account.UpdateCredentialQuery(
displayName: displayName,
avatar: avatar
)
}()
let viewModel = MastodonConfirmEmailViewModel(context: self.context, email: email, authenticateInfo: self.viewModel.authenticateInfo, userToken: userToken, updateCredentialQuery: updateCredentialQuery)
self.coordinator.present(scene: .mastodonConfirmEmail(viewModel: viewModel), from: self, transition: .show)
2021-02-22 09:20:44 +01:00
}
.store(in: &disposeBag)
2021-02-05 10:53:00 +01:00
}
}