2021-02-05 10:53:00 +01:00
|
|
|
//
|
|
|
|
// MastodonRegisterViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-2-5.
|
|
|
|
//
|
|
|
|
|
2021-03-08 12:05:15 +01:00
|
|
|
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
|
2021-03-02 11:21:54 +01:00
|
|
|
import PhotosUI
|
2021-02-20 06:55:06 +01:00
|
|
|
import UIKit
|
2022-04-27 11:37:03 +02:00
|
|
|
import SwiftUI
|
2022-03-30 09:23:22 +02:00
|
|
|
import MastodonUI
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
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)
|
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
let logger = Logger(subsystem: "MastodonRegisterViewController", category: "ViewController")
|
2021-05-11 10:22:29 +02:00
|
|
|
|
2021-02-05 10:53:00 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2022-01-07 11:49:37 +01:00
|
|
|
private var observations = Set<NSKeyValueObservation>()
|
|
|
|
|
2021-02-05 10:53:00 +01:00
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var viewModel: MastodonRegisterViewModel!
|
2022-04-27 11:37:03 +02:00
|
|
|
private(set) lazy var mastodonRegisterView = MastodonRegisterView(viewModel: viewModel)
|
2021-02-20 06:55:06 +01:00
|
|
|
|
2021-03-29 07:37:56 +02:00
|
|
|
// picker
|
|
|
|
private(set) lazy var imagePicker: PHPickerViewController = {
|
2021-03-02 10:24:04 +01:00
|
|
|
var configuration = PHPickerConfiguration()
|
|
|
|
configuration.filter = .images
|
2021-03-29 07:37:56 +02:00
|
|
|
configuration.selectionLimit = 1
|
2021-03-02 10:24:04 +01:00
|
|
|
|
|
|
|
let imagePicker = PHPickerViewController(configuration: configuration)
|
|
|
|
imagePicker.delegate = self
|
|
|
|
return imagePicker
|
|
|
|
}()
|
2021-03-29 07:37:56 +02:00
|
|
|
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])
|
2021-03-29 07:37:56 +02:00
|
|
|
documentPickerController.delegate = self
|
|
|
|
return documentPickerController
|
|
|
|
}()
|
2021-03-02 10:24:04 +01:00
|
|
|
|
2022-01-07 11:49:37 +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-07 11:49:37 +01:00
|
|
|
return navigationActionView
|
2021-02-05 10:53:00 +01:00
|
|
|
}()
|
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
deinit {
|
2021-03-02 11:21:54 +01:00
|
|
|
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
|
|
|
}
|
2022-01-07 11:49:37 +01:00
|
|
|
|
2021-02-05 10:53:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonRegisterViewController {
|
2021-03-05 13:45:48 +01:00
|
|
|
|
2021-02-05 10:53:00 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2022-01-07 12:11:56 +01:00
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem()
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
setupOnboardingAppearance()
|
2022-04-27 11:37:03 +02:00
|
|
|
viewModel.backgroundColor = view.backgroundColor ?? .clear
|
2021-10-08 12:10:06 +02:00
|
|
|
defer {
|
|
|
|
setupNavigationBarBackgroundView()
|
|
|
|
}
|
2021-02-25 08:39:48 +01:00
|
|
|
|
2022-04-27 11:37:03 +02:00
|
|
|
let hostingViewController = UIHostingController(rootView: mastodonRegisterView)
|
|
|
|
hostingViewController.view.preservesSuperviewLayoutMargins = true
|
|
|
|
addChild(hostingViewController)
|
|
|
|
hostingViewController.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(hostingViewController.view)
|
2021-03-04 08:29:46 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2022-04-27 11:37:03 +02:00
|
|
|
hostingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
hostingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
hostingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
hostingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
2021-03-04 08:29:46 +01:00
|
|
|
])
|
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
navigationActionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(navigationActionView)
|
|
|
|
defer {
|
|
|
|
view.bringSubviewToFront(navigationActionView)
|
2021-04-25 05:39:21 +02:00
|
|
|
}
|
2021-02-05 10:53:00 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2022-01-07 11:49:37 +01:00
|
|
|
navigationActionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
navigationActionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: navigationActionView.bottomAnchor),
|
2021-02-05 10:53:00 +01:00
|
|
|
])
|
2021-02-26 11:27:47 +01:00
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
navigationActionView
|
|
|
|
.observe(\.bounds, options: [.initial, .new]) { [weak self] navigationActionView, _ in
|
2021-03-04 08:29:46 +01:00
|
|
|
guard let self = self else { return }
|
2022-01-07 11:49:37 +01:00
|
|
|
let inset = navigationActionView.frame.height
|
2022-04-27 11:37:03 +02:00
|
|
|
self.viewModel.bottomPaddingHeight = inset
|
2021-03-04 08:29:46 +01:00
|
|
|
}
|
2022-01-07 11:49:37 +01:00
|
|
|
.store(in: &observations)
|
|
|
|
|
|
|
|
navigationActionView.backButton.addTarget(self, action: #selector(MastodonRegisterViewController.backButtonPressed(_:)), for: .touchUpInside)
|
|
|
|
navigationActionView.nextButton.addTarget(self, action: #selector(MastodonRegisterViewController.nextButtonPressed(_:)), for: .touchUpInside)
|
2021-02-20 10:13:16 +01:00
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
viewModel.$isAllValid
|
2021-03-01 10:45:02 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] isAllValid in
|
|
|
|
guard let self = self else { return }
|
2022-01-07 11:49:37 +01:00
|
|
|
self.navigationActionView.nextButton.isEnabled = isAllValid
|
2021-03-01 10:45:02 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-02-20 12:54:08 +01:00
|
|
|
|
2022-04-27 11:37:03 +02:00
|
|
|
viewModel.endEditing
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.view.endEditing(true)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-07 11:49:37 +01:00
|
|
|
|
|
|
|
// // return
|
|
|
|
// if viewModel.approvalRequired {
|
|
|
|
// reasonTextField.returnKeyType = .done
|
|
|
|
// } else {
|
|
|
|
// passwordTextField.returnKeyType = .done
|
|
|
|
// }
|
2022-04-26 10:48:06 +02:00
|
|
|
|
|
|
|
viewModel.$error
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] error in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let error = error as? Mastodon.API.Error else { return }
|
|
|
|
let alertController = UIAlertController(for: error, title: "Sign Up Failure", preferredStyle: .alert)
|
|
|
|
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil)
|
|
|
|
alertController.addAction(okAction)
|
|
|
|
self.coordinator.present(
|
|
|
|
scene: .alertController(alertController: alertController),
|
|
|
|
from: nil,
|
|
|
|
transition: .alertController(animated: true, completion: nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-07 11:49:37 +01:00
|
|
|
|
|
|
|
viewModel.avatarMediaMenuActionPublisher
|
2021-03-31 04:37:38 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-01-07 11:49:37 +01:00
|
|
|
.sink { [weak self] action in
|
2021-03-31 04:37:38 +02:00
|
|
|
guard let self = self else { return }
|
2022-01-07 11:49:37 +01:00
|
|
|
switch action {
|
|
|
|
case .photoLibrary:
|
|
|
|
self.present(self.imagePicker, animated: true, completion: nil)
|
|
|
|
case .camera:
|
|
|
|
self.present(self.imagePickerController, animated: true, completion: nil)
|
|
|
|
case .browse:
|
|
|
|
self.present(self.documentPickerController, animated: true, completion: nil)
|
|
|
|
case .delete:
|
|
|
|
self.viewModel.avatarImage = nil
|
2021-03-31 04:37:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-02-22 10:48:44 +01:00
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
viewModel.$isRegistering
|
2021-02-22 10:48:44 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-01-07 11:49:37 +01:00
|
|
|
.sink { [weak self] isRegistering in
|
2021-02-20 06:55:06 +01:00
|
|
|
guard let self = self else { return }
|
2022-01-07 11:49:37 +01:00
|
|
|
isRegistering ? self.navigationActionView.nextButton.showLoading() : self.navigationActionView.nextButton.stopLoading()
|
2021-02-20 06:55:06 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-02-05 10:53:00 +01:00
|
|
|
}
|
2021-03-05 13:45:48 +01:00
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
2021-02-22 10:48:44 +01:00
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
viewModel.viewDidAppear.send()
|
2021-06-16 09:56:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 06:55:06 +01:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:10:06 +02:00
|
|
|
extension MastodonRegisterViewController {
|
|
|
|
|
2022-01-07 11:49:37 +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)
|
|
|
|
}
|
2021-02-22 10:48:44 +01:00
|
|
|
|
2022-01-07 11:49:37 +01:00
|
|
|
@objc private func nextButtonPressed(_ sender: UIButton) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
|
|
|
|
guard viewModel.isAllValid else { return }
|
|
|
|
|
|
|
|
guard !viewModel.isRegistering else { return }
|
|
|
|
viewModel.isRegistering = true
|
|
|
|
|
|
|
|
let username = viewModel.username
|
|
|
|
let email = viewModel.email
|
|
|
|
let password = viewModel.password
|
|
|
|
let reason = viewModel.reason
|
2021-08-03 10:57:42 +02:00
|
|
|
|
|
|
|
let locale: String = {
|
2021-11-04 12:24:06 +01:00
|
|
|
guard let url = Bundle.main.url(forResource: "local-codes", withExtension: "json"),
|
|
|
|
let data = try? Data(contentsOf: url),
|
|
|
|
let localCode = try? JSONDecoder().decode(MastodonLocalCode.self, from: data)
|
|
|
|
else {
|
|
|
|
assertionFailure()
|
|
|
|
return "en"
|
|
|
|
}
|
|
|
|
let fallbackLanguageCode: String = {
|
|
|
|
let code = Locale.current.languageCode ?? "en"
|
|
|
|
guard localCode[code] != nil else { return "en" }
|
|
|
|
return code
|
|
|
|
}()
|
2022-01-07 11:49:37 +01:00
|
|
|
|
2021-11-04 12:24:06 +01:00
|
|
|
// pick device preferred language
|
2021-08-03 10:57:42 +02:00
|
|
|
guard let identifier = Locale.preferredLanguages.first else {
|
|
|
|
return fallbackLanguageCode
|
|
|
|
}
|
2021-11-04 12:24:06 +01:00
|
|
|
// prepare languageCode and validate then return fallback if needs
|
2021-08-03 10:57:42 +02:00
|
|
|
let local = Locale(identifier: identifier)
|
2021-11-04 12:24:06 +01:00
|
|
|
guard let languageCode = local.languageCode,
|
|
|
|
localCode[languageCode] != nil
|
|
|
|
else {
|
2021-08-03 10:57:42 +02:00
|
|
|
return fallbackLanguageCode
|
|
|
|
}
|
2021-11-04 12:24:06 +01:00
|
|
|
// prepare extendCode and validate then return fallback if needs
|
|
|
|
let extendCodes: [String] = {
|
|
|
|
let locales = Locale.preferredLanguages.map { Locale(identifier: $0) }
|
|
|
|
return locales.compactMap { locale in
|
|
|
|
guard let languageCode = locale.languageCode,
|
|
|
|
let regionCode = locale.regionCode
|
|
|
|
else { return nil }
|
|
|
|
return languageCode + "-" + regionCode
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
let _firstMatchExtendCode = extendCodes.first { code in
|
|
|
|
localCode[code] != nil
|
|
|
|
}
|
|
|
|
guard let firstMatchExtendCode = _firstMatchExtendCode else {
|
2021-08-03 10:57:42 +02:00
|
|
|
return languageCode
|
|
|
|
}
|
2021-11-04 12:24:06 +01:00
|
|
|
return firstMatchExtendCode
|
2022-01-07 11:49:37 +01:00
|
|
|
|
2021-08-03 10:57:42 +02:00
|
|
|
}()
|
2021-02-26 11:27:47 +01:00
|
|
|
let query = Mastodon.API.Account.RegisterQuery(
|
2022-01-07 11:49:37 +01:00
|
|
|
reason: reason,
|
2021-02-26 11:27:47 +01:00
|
|
|
username: username,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
2021-03-08 12:05:15 +01:00
|
|
|
agreement: true, // user confirmed in the server rules scene
|
2021-08-03 10:57:42 +02:00
|
|
|
locale: locale
|
2021-02-26 11:27:47 +01:00
|
|
|
)
|
2022-01-07 11:49:37 +01:00
|
|
|
|
2021-11-04 12:24:06 +01:00
|
|
|
var retryCount = 0
|
2022-01-07 11:49:37 +01:00
|
|
|
|
2021-03-01 11:06:37 +01:00
|
|
|
// register without show server rules
|
|
|
|
context.apiService.accountRegister(
|
|
|
|
domain: viewModel.domain,
|
|
|
|
query: query,
|
|
|
|
authorization: viewModel.applicationAuthorization
|
|
|
|
)
|
2021-11-04 12:24:06 +01:00
|
|
|
.tryCatch { [weak self] error -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Token>, Error> in
|
|
|
|
guard let self = self else { throw error }
|
2022-01-07 11:49:37 +01:00
|
|
|
guard let error = self.viewModel.error as? Mastodon.API.Error,
|
2021-11-04 12:24:06 +01:00
|
|
|
case let .generic(errorEntity) = error.mastodonError,
|
|
|
|
errorEntity.error == "Validation failed: Locale is not included in the list"
|
|
|
|
else {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
guard retryCount == 0 else {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
let retryQuery = Mastodon.API.Account.RegisterQuery(
|
|
|
|
reason: query.reason,
|
|
|
|
username: query.username,
|
|
|
|
email: query.email,
|
|
|
|
password: query.password,
|
|
|
|
agreement: query.agreement,
|
|
|
|
locale: self.viewModel.instance.languages?.first ?? "en"
|
|
|
|
)
|
|
|
|
retryCount += 1
|
|
|
|
return self.context.apiService.accountRegister(
|
|
|
|
domain: self.viewModel.domain,
|
|
|
|
query: retryQuery,
|
|
|
|
authorization: self.viewModel.applicationAuthorization
|
|
|
|
)
|
|
|
|
}
|
2021-03-01 11:06:37 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] completion in
|
|
|
|
guard let self = self else { return }
|
2022-01-07 11:49:37 +01:00
|
|
|
self.viewModel.isRegistering = false
|
2021-03-01 11:06:37 +01:00
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
2022-01-07 11:49:37 +01:00
|
|
|
self.viewModel.error = error
|
2021-03-01 11:06:37 +01:00
|
|
|
case .finished:
|
|
|
|
break
|
2021-02-26 11:27:47 +01:00
|
|
|
}
|
2021-03-01 11:06:37 +01:00
|
|
|
} receiveValue: { [weak self] response in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let userToken = response.value
|
2021-03-08 12:05:15 +01:00
|
|
|
let updateCredentialQuery: Mastodon.API.Account.UpdateCredentialQuery = {
|
2022-01-07 11:49:37 +01:00
|
|
|
let displayName: String? = self.viewModel.name.isEmpty ? nil : self.viewModel.name
|
2021-03-08 12:05:15 +01:00
|
|
|
let avatar: Mastodon.Query.MediaAttachment? = {
|
2022-01-07 11:49:37 +01:00
|
|
|
guard let avatarImage = self.viewModel.avatarImage 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-03-08 12:05:15 +01:00
|
|
|
}
|
2021-04-09 11:31:43 +02:00
|
|
|
return .png(avatarImage.pngData())
|
2021-03-08 12:05:15 +01:00
|
|
|
}()
|
|
|
|
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)
|
2021-03-01 11:06:37 +01:00
|
|
|
self.coordinator.present(scene: .mastodonConfirmEmail(viewModel: viewModel), from: self, transition: .show)
|
2021-02-22 09:20:44 +01:00
|
|
|
}
|
2021-03-01 11:06:37 +01:00
|
|
|
.store(in: &disposeBag)
|
2021-02-05 10:53:00 +01:00
|
|
|
}
|
2022-01-07 11:49:37 +01:00
|
|
|
|
|
|
|
}
|