2021-02-23 08:38:05 +01:00
|
|
|
//
|
|
|
|
// MastodonConfirmEmailViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2021-02-25 08:39:48 +01:00
|
|
|
import MastodonSDK
|
|
|
|
import os.log
|
2021-02-23 08:38:05 +01:00
|
|
|
import ThirdPartyMailer
|
|
|
|
import UIKit
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2021-02-23 08:38:05 +01:00
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
final class MastodonConfirmEmailViewController: UIViewController, NeedsDependency {
|
|
|
|
|
2021-02-23 08:38:05 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var viewModel: MastodonConfirmEmailViewModel!
|
|
|
|
|
2021-10-08 12:10:06 +02:00
|
|
|
let stackView = UIStackView()
|
|
|
|
|
2021-02-23 08:38:05 +01:00
|
|
|
let largeTitleLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
2021-06-16 10:08:17 +02:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .largeTitle).scaledFont(for: UIFont.systemFont(ofSize: 34, weight: .bold))
|
2021-02-23 08:38:05 +01:00
|
|
|
label.textColor = .label
|
|
|
|
label.text = L10n.Scene.ConfirmEmail.title
|
|
|
|
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
|
2022-02-15 07:45:34 +01:00
|
|
|
label.text = L10n.Scene.ConfirmEmail.subtitle
|
2021-02-23 08:38:05 +01:00
|
|
|
label.numberOfLines = 0
|
|
|
|
return label
|
|
|
|
}()
|
2021-04-25 04:50:54 +02:00
|
|
|
|
|
|
|
let emailImageView: UIImageView = {
|
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.image = Asset.Asset.email.image
|
2021-06-30 14:11:38 +02:00
|
|
|
imageView.backgroundColor = .clear
|
2021-04-25 04:50:54 +02:00
|
|
|
imageView.contentMode = .scaleAspectFit
|
|
|
|
return imageView
|
|
|
|
}()
|
2022-01-07 12:11:56 +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 12:11:56 +01:00
|
|
|
return navigationActionView
|
2021-02-23 08:38:05 +01:00
|
|
|
}()
|
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-23 08:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonConfirmEmailViewController {
|
2021-02-25 08:39:48 +01:00
|
|
|
|
2021-02-23 08:38:05 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
2022-01-07 12:11:56 +01:00
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem()
|
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
setupOnboardingAppearance()
|
2021-10-08 12:10:06 +02:00
|
|
|
configureTitleLabel()
|
|
|
|
configureMargin()
|
2021-02-23 08:38:05 +01:00
|
|
|
|
|
|
|
// stackView
|
|
|
|
stackView.axis = .vertical
|
|
|
|
stackView.distribution = .fill
|
|
|
|
stackView.spacing = 10
|
|
|
|
stackView.layoutMargins = UIEdgeInsets(top: 10, left: 0, bottom: 23, right: 0)
|
|
|
|
stackView.isLayoutMarginsRelativeArrangement = true
|
2022-01-07 12:11:56 +01:00
|
|
|
stackView.addArrangedSubview(largeTitleLabel)
|
|
|
|
stackView.addArrangedSubview(subtitleLabel)
|
|
|
|
stackView.addArrangedSubview(emailImageView)
|
2021-04-25 04:50:54 +02:00
|
|
|
emailImageView.setContentHuggingPriority(.defaultLow, for: .vertical)
|
|
|
|
emailImageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
|
2022-01-07 12:11:56 +01:00
|
|
|
stackView.addArrangedSubview(navigationActionView)
|
2021-02-23 08:38:05 +01:00
|
|
|
|
|
|
|
view.addSubview(stackView)
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
stackView.topAnchor.constraint(equalTo: view.readableContentGuide.topAnchor),
|
2021-10-08 12:10:06 +02:00
|
|
|
stackView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
|
2021-02-23 08:38:05 +01:00
|
|
|
stackView.bottomAnchor.constraint(equalTo: view.readableContentGuide.bottomAnchor),
|
|
|
|
])
|
2022-01-07 12:11:56 +01:00
|
|
|
|
2021-02-25 08:39:48 +01:00
|
|
|
self.viewModel.timestampUpdatePublisher
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
AuthenticationViewModel.verifyAndSaveAuthentication(context: self.context, info: self.viewModel.authenticateInfo, userToken: self.viewModel.userToken)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: swap user access token swap fail: %s", (#file as NSString).lastPathComponent, #line, #function, error.localizedDescription)
|
|
|
|
case .finished:
|
2021-03-08 12:05:15 +01:00
|
|
|
// upload avatar and set display name in the background
|
2022-01-27 14:23:39 +01:00
|
|
|
Just(self.viewModel.userToken.accessToken)
|
|
|
|
.asyncMap { token in
|
|
|
|
try await self.context.apiService.accountUpdateCredentials(
|
|
|
|
domain: self.viewModel.authenticateInfo.domain,
|
|
|
|
query: self.viewModel.updateCredentialQuery,
|
|
|
|
authorization: Mastodon.API.OAuth.Authorization(accessToken: token)
|
|
|
|
)
|
2021-03-08 12:05:15 +01:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
.retry(3)
|
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: setup avatar & display name fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
|
|
|
case .finished:
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: setup avatar & display name success", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
} receiveValue: { _ in
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
.store(in: &self.context.disposeBag) // execute in the background
|
|
|
|
} // end switch
|
2021-02-26 11:27:47 +01:00
|
|
|
} receiveValue: { response in
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: user %s's email confirmed", ((#file as NSString).lastPathComponent), #line, #function, response.value.username)
|
2021-09-16 10:58:29 +02:00
|
|
|
self.coordinator.setup()
|
|
|
|
// self.dismiss(animated: true, completion: nil)
|
2021-02-25 08:39:48 +01:00
|
|
|
}
|
|
|
|
.store(in: &self.disposeBag)
|
|
|
|
}
|
|
|
|
.store(in: &self.disposeBag)
|
2022-01-07 12:11:56 +01:00
|
|
|
|
|
|
|
|
2022-02-15 07:45:34 +01:00
|
|
|
navigationActionView.backButton.setTitle(L10n.Scene.ConfirmEmail.Button.resend, for: .normal)
|
2022-01-07 12:11:56 +01:00
|
|
|
navigationActionView.backButton.addTarget(self, action: #selector(MastodonConfirmEmailViewController.resendButtonPressed(_:)), for: .touchUpInside)
|
|
|
|
|
|
|
|
navigationActionView.nextButton.setTitle(L10n.Scene.ConfirmEmail.Button.openEmailApp, for: .normal)
|
|
|
|
navigationActionView.nextButton.addTarget(self, action: #selector(MastodonConfirmEmailViewController.openEmailButtonPressed(_:)), for: .touchUpInside)
|
2021-02-23 08:38:05 +01:00
|
|
|
}
|
2021-02-26 09:43:59 +01:00
|
|
|
|
2021-10-08 12:10:06 +02:00
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
configureTitleLabel()
|
|
|
|
configureMargin()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonConfirmEmailViewController {
|
|
|
|
private func configureTitleLabel() {
|
|
|
|
switch traitCollection.horizontalSizeClass {
|
|
|
|
case .regular:
|
|
|
|
navigationItem.largeTitleDisplayMode = .always
|
|
|
|
navigationItem.title = L10n.Scene.ConfirmEmail.title.replacingOccurrences(of: "\n", with: " ")
|
|
|
|
largeTitleLabel.isHidden = true
|
|
|
|
default:
|
|
|
|
navigationItem.largeTitleDisplayMode = .never
|
|
|
|
navigationItem.title = nil
|
|
|
|
largeTitleLabel.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func configureMargin() {
|
|
|
|
switch traitCollection.horizontalSizeClass {
|
|
|
|
case .regular:
|
|
|
|
let margin = MastodonConfirmEmailViewController.viewEdgeMargin
|
|
|
|
stackView.layoutMargins = UIEdgeInsets(top: 18, left: margin, bottom: 23, right: margin)
|
|
|
|
default:
|
|
|
|
stackView.layoutMargins = UIEdgeInsets(top: 10, left: 0, bottom: 23, right: 0)
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 08:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonConfirmEmailViewController {
|
|
|
|
@objc private func openEmailButtonPressed(_ sender: UIButton) {
|
2021-02-25 08:39:48 +01:00
|
|
|
let alertController = UIAlertController(title: L10n.Scene.ConfirmEmail.OpenEmailApp.title, message: L10n.Scene.ConfirmEmail.OpenEmailApp.description, preferredStyle: .alert)
|
2021-02-23 08:38:05 +01:00
|
|
|
let openEmailAction = UIAlertAction(title: L10n.Scene.ConfirmEmail.Button.openEmailApp, style: .default) { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.showEmailAppAlert()
|
|
|
|
}
|
|
|
|
let cancelAction = UIAlertAction(title: L10n.Common.Controls.Actions.cancel, style: .cancel, handler: nil)
|
|
|
|
alertController.addAction(openEmailAction)
|
|
|
|
alertController.addAction(cancelAction)
|
2021-02-24 03:49:50 +01:00
|
|
|
alertController.preferredAction = openEmailAction
|
2021-02-23 08:38:05 +01:00
|
|
|
self.coordinator.present(scene: .alertController(alertController: alertController), from: self, transition: .alertController(animated: true, completion: nil))
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:11:56 +01:00
|
|
|
@objc private func resendButtonPressed(_ sender: UIButton) {
|
2021-02-25 08:39:48 +01:00
|
|
|
let alertController = UIAlertController(title: L10n.Scene.ConfirmEmail.DontReceiveEmail.title, message: L10n.Scene.ConfirmEmail.DontReceiveEmail.description, preferredStyle: .alert)
|
|
|
|
let resendAction = UIAlertAction(title: L10n.Scene.ConfirmEmail.DontReceiveEmail.resendEmail, style: .default) { _ in
|
|
|
|
let url = Mastodon.API.resendEmailURL(domain: self.viewModel.authenticateInfo.domain)
|
2021-02-24 13:06:28 +01:00
|
|
|
let viewModel = MastodonResendEmailViewModel(resendEmailURL: url, email: self.viewModel.email)
|
2021-02-25 08:39:48 +01:00
|
|
|
self.coordinator.present(scene: .mastodonResendEmail(viewModel: viewModel), from: self, transition: .modal(animated: true, completion: nil))
|
2021-02-24 13:06:28 +01:00
|
|
|
}
|
2021-02-25 08:39:48 +01:00
|
|
|
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default) { _ in
|
|
|
|
}
|
|
|
|
alertController.addAction(resendAction)
|
2021-02-23 08:38:05 +01:00
|
|
|
alertController.addAction(okAction)
|
|
|
|
self.coordinator.present(scene: .alertController(alertController: alertController), from: self, transition: .alertController(animated: true, completion: nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func showEmailAppAlert() {
|
|
|
|
let clients = ThirdPartyMailClient.clients()
|
|
|
|
let application = UIApplication.shared
|
2021-07-01 05:49:30 +02:00
|
|
|
let availableClients = clients.filter { client -> Bool in
|
2021-02-23 08:38:05 +01:00
|
|
|
ThirdPartyMailer.application(application, isMailClientAvailable: client)
|
|
|
|
}
|
2021-02-25 08:39:48 +01:00
|
|
|
let alertController = UIAlertController(title: L10n.Scene.ConfirmEmail.OpenEmailApp.openEmailClient, message: nil, preferredStyle: .alert)
|
2021-02-24 03:49:50 +01:00
|
|
|
|
|
|
|
let alertAction = UIAlertAction(title: L10n.Scene.ConfirmEmail.OpenEmailApp.mail, style: .default) { _ in
|
2021-02-23 08:38:05 +01:00
|
|
|
UIApplication.shared.open(URL(string: "message://")!, options: [:], completionHandler: nil)
|
|
|
|
}
|
|
|
|
alertController.addAction(alertAction)
|
2021-07-01 05:49:30 +02:00
|
|
|
_ = availableClients.compactMap { client -> UIAlertAction in
|
2021-02-23 08:38:05 +01:00
|
|
|
let alertAction = UIAlertAction(title: client.name, style: .default) { _ in
|
2021-07-01 05:49:30 +02:00
|
|
|
_ = ThirdPartyMailer.application(application, openMailClient: client)
|
2021-02-23 08:38:05 +01:00
|
|
|
}
|
|
|
|
alertController.addAction(alertAction)
|
|
|
|
return alertAction
|
|
|
|
}
|
|
|
|
let cancelAction = UIAlertAction(title: L10n.Common.Controls.Actions.cancel, style: .cancel, handler: nil)
|
|
|
|
alertController.addAction(cancelAction)
|
|
|
|
self.coordinator.present(scene: .alertController(alertController: alertController), from: self, transition: .alertController(animated: true, completion: nil))
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 11:27:47 +01:00
|
|
|
|
|
|
|
// MARK: - OnboardingViewControllerAppearance
|
|
|
|
extension MastodonConfirmEmailViewController: OnboardingViewControllerAppearance { }
|
2021-04-25 04:50:54 +02:00
|
|
|
|
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct MastodonConfirmEmailViewController_Previews: PreviewProvider {
|
|
|
|
|
2021-04-25 12:02:31 +02:00
|
|
|
static var controls: some View {
|
2021-04-25 04:50:54 +02:00
|
|
|
UIViewControllerPreview {
|
|
|
|
let viewController = MastodonConfirmEmailViewController()
|
|
|
|
return viewController
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 800))
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:02:31 +02:00
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
controls.colorScheme(.light)
|
|
|
|
controls.colorScheme(.dark)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 04:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|