2021-02-20 06:56:24 +01:00
|
|
|
//
|
|
|
|
// WelcomeViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
2021-02-23 15:14:10 +01:00
|
|
|
// Created by BradGao on 2021/2/20.
|
2021-02-20 06:56:24 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2021-09-15 14:18:19 +02:00
|
|
|
import Combine
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-02-20 06:56:24 +01:00
|
|
|
|
2021-02-23 08:44:59 +01:00
|
|
|
final class WelcomeViewController: UIViewController, NeedsDependency {
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
private enum Constants {
|
2023-01-06 13:44:11 +01:00
|
|
|
static let topAnchorInset: CGFloat = 20
|
2022-12-28 15:28:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var observations = Set<NSKeyValueObservation>()
|
|
|
|
private(set) lazy var viewModel = WelcomeViewModel(context: context)
|
|
|
|
|
|
|
|
let welcomeIllustrationView = WelcomeIllustrationView()
|
|
|
|
|
|
|
|
private(set) lazy var dismissBarButtonItem = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(WelcomeViewController.dismissBarButtonItemDidPressed(_:)))
|
|
|
|
|
|
|
|
let buttonContainer = UIStackView()
|
2023-01-07 16:02:46 +01:00
|
|
|
let educationPages: [WelcomeContentPage] = [.whatIsMastodon, .mastodonIsLikeThat, .howDoIPickAServer]
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
private(set) lazy var signUpButton: PrimaryActionButton = {
|
|
|
|
let button = PrimaryActionButton()
|
|
|
|
button.adjustsBackgroundImageWhenUserInterfaceStyleChanges = false
|
|
|
|
button.contentEdgeInsets = WelcomeViewController.actionButtonPadding
|
|
|
|
button.titleLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
button.titleLabel?.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold))
|
|
|
|
button.setTitle(L10n.Common.Controls.Actions.signUp, for: .normal)
|
|
|
|
let backgroundImageColor: UIColor = .white
|
|
|
|
let backgroundImageHighlightedColor: UIColor = UIColor(white: 0.8, alpha: 1.0)
|
|
|
|
button.setBackgroundImage(.placeholder(color: backgroundImageColor), for: .normal)
|
|
|
|
button.setBackgroundImage(.placeholder(color: backgroundImageHighlightedColor), for: .highlighted)
|
|
|
|
button.setTitleColor(.black, for: .normal)
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
let signUpButtonShadowView = UIView()
|
|
|
|
|
|
|
|
private(set) lazy var signInButton: UIButton = {
|
|
|
|
let button = UIButton()
|
|
|
|
button.contentEdgeInsets = WelcomeViewController.actionButtonPadding
|
|
|
|
button.titleLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
button.titleLabel?.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold))
|
|
|
|
button.setTitle(L10n.Scene.Welcome.logIn, for: .normal)
|
|
|
|
let titleColor: UIColor = UIColor.white.withAlphaComponent(0.9)
|
|
|
|
button.setTitleColor(titleColor, for: .normal)
|
|
|
|
button.setTitleColor(titleColor.withAlphaComponent(0.3), for: .highlighted)
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
private(set) lazy var pageCollectionView: UICollectionView = {
|
|
|
|
let flowLayout = UICollectionViewFlowLayout()
|
|
|
|
flowLayout.scrollDirection = .horizontal
|
|
|
|
flowLayout.minimumLineSpacing = 0
|
2023-01-08 13:35:08 +01:00
|
|
|
flowLayout.itemSize = CGSize(width: self.view.frame.width, height: 400)
|
2023-01-07 16:02:46 +01:00
|
|
|
|
|
|
|
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
|
|
|
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
collectionView.isPagingEnabled = true
|
|
|
|
collectionView.backgroundColor = nil
|
|
|
|
collectionView.showsHorizontalScrollIndicator = false
|
|
|
|
collectionView.bounces = false
|
|
|
|
collectionView.register(WelcomeContentCollectionViewCell.self, forCellWithReuseIdentifier: WelcomeContentCollectionViewCell.identifier)
|
|
|
|
|
|
|
|
return collectionView
|
2022-12-28 15:28:09 +01:00
|
|
|
}()
|
2023-01-08 14:25:22 +01:00
|
|
|
|
|
|
|
private(set) var pageControl: UIPageControl = {
|
|
|
|
let pageControl = UIPageControl(frame: .zero)
|
|
|
|
pageControl.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return pageControl
|
|
|
|
}()
|
2021-02-20 06:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension WelcomeViewController {
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
definesPresentationContext = true
|
|
|
|
preferredContentSize = CGSize(width: 547, height: 678)
|
|
|
|
|
2023-01-06 13:44:11 +01:00
|
|
|
navigationController?.navigationBar.prefersLargeTitles = true
|
2022-12-28 15:28:09 +01:00
|
|
|
view.overrideUserInterfaceStyle = .light
|
|
|
|
|
|
|
|
setupOnboardingAppearance()
|
|
|
|
|
|
|
|
view.addSubview(welcomeIllustrationView)
|
|
|
|
welcomeIllustrationView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
welcomeIllustrationView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
welcomeIllustrationView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: welcomeIllustrationView.trailingAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: welcomeIllustrationView.bottomAnchor)
|
|
|
|
])
|
|
|
|
|
|
|
|
buttonContainer.axis = .vertical
|
|
|
|
buttonContainer.spacing = 12
|
|
|
|
buttonContainer.isLayoutMarginsRelativeArrangement = true
|
|
|
|
|
|
|
|
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(buttonContainer)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
buttonContainer.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
|
|
|
|
buttonContainer.trailingAnchor.constraint(equalTo: view.readableContentGuide.trailingAnchor),
|
|
|
|
view.layoutMarginsGuide.bottomAnchor.constraint(equalTo: buttonContainer.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
signUpButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
buttonContainer.addArrangedSubview(signUpButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
signUpButton.heightAnchor.constraint(greaterThanOrEqualToConstant: WelcomeViewController.actionButtonHeight).priority(.required - 1),
|
|
|
|
])
|
|
|
|
signInButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
buttonContainer.addArrangedSubview(signInButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
signInButton.heightAnchor.constraint(greaterThanOrEqualToConstant: WelcomeViewController.actionButtonHeight).priority(.required - 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
signUpButtonShadowView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
buttonContainer.addSubview(signUpButtonShadowView)
|
|
|
|
buttonContainer.sendSubviewToBack(signUpButtonShadowView)
|
|
|
|
signUpButtonShadowView.pinTo(to: signUpButton)
|
|
|
|
|
|
|
|
signUpButton.addTarget(self, action: #selector(signUpButtonDidClicked(_:)), for: .touchUpInside)
|
|
|
|
signInButton.addTarget(self, action: #selector(signInButtonDidClicked(_:)), for: .touchUpInside)
|
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
pageCollectionView.delegate = self
|
|
|
|
pageCollectionView.dataSource = self
|
|
|
|
view.addSubview(pageCollectionView)
|
|
|
|
|
2023-01-08 14:25:22 +01:00
|
|
|
pageControl.numberOfPages = self.educationPages.count
|
|
|
|
pageControl.addTarget(self, action: #selector(WelcomeViewController.pageControlDidChange(_:)), for: .valueChanged)
|
|
|
|
view.addSubview(pageControl)
|
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
let scrollView = pageCollectionView as UIScrollView
|
|
|
|
scrollView.delegate = self
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
2023-01-07 16:02:46 +01:00
|
|
|
pageCollectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: computedTopAnchorInset),
|
|
|
|
pageCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: pageCollectionView.trailingAnchor),
|
2023-01-08 14:25:22 +01:00
|
|
|
pageControl.topAnchor.constraint(equalTo: pageCollectionView.bottomAnchor, constant: 16),
|
|
|
|
|
|
|
|
pageControl.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: pageControl.trailingAnchor),
|
|
|
|
buttonContainer.topAnchor.constraint(equalTo: pageControl.bottomAnchor, constant: 16),
|
2022-12-28 15:28:09 +01:00
|
|
|
])
|
2023-01-08 14:25:22 +01:00
|
|
|
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
viewModel.$needsShowDismissEntry
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] needsShowDismissEntry in
|
|
|
|
guard let self = self else { return }
|
2023-01-06 13:44:11 +01:00
|
|
|
self.navigationItem.leftBarButtonItem = needsShowDismissEntry ? self.dismissBarButtonItem : nil
|
2022-12-28 15:28:09 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-11-27 14:16:09 +01:00
|
|
|
}
|
2021-02-20 06:56:24 +01:00
|
|
|
|
2022-12-28 15:28:09 +01:00
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
|
|
|
|
setupButtonShadowView()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewSafeAreaInsetsDidChange() {
|
|
|
|
super.viewSafeAreaInsetsDidChange()
|
|
|
|
|
|
|
|
var overlap: CGFloat = 5
|
|
|
|
// shift illustration down for non-notch phone
|
|
|
|
if view.safeAreaInsets.bottom == 0 {
|
|
|
|
overlap += 56
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
view.layoutIfNeeded()
|
|
|
|
|
|
|
|
setupIllustrationLayout()
|
|
|
|
setupButtonShadowView()
|
2023-01-08 13:35:08 +01:00
|
|
|
|
|
|
|
let flowLayout = UICollectionViewFlowLayout()
|
|
|
|
flowLayout.scrollDirection = .horizontal
|
|
|
|
flowLayout.minimumLineSpacing = 0
|
|
|
|
flowLayout.itemSize = CGSize(width: self.view.frame.width, height: 400)
|
|
|
|
|
|
|
|
pageCollectionView.setCollectionViewLayout(flowLayout, animated: true)
|
2022-12-28 15:28:09 +01:00
|
|
|
}
|
2023-01-06 13:44:11 +01:00
|
|
|
|
|
|
|
private var computedTopAnchorInset: CGFloat {
|
|
|
|
(navigationController?.navigationBar.bounds.height ?? UINavigationBar().bounds.height) + Constants.topAnchorInset
|
|
|
|
}
|
2021-02-23 08:44:59 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 11:24:48 +01:00
|
|
|
extension WelcomeViewController {
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
private func setupButtonShadowView() {
|
|
|
|
signUpButtonShadowView.layer.setupShadow(
|
|
|
|
color: .black,
|
|
|
|
alpha: 0.25,
|
|
|
|
x: 0,
|
|
|
|
y: 1,
|
|
|
|
blur: 2,
|
|
|
|
spread: 0,
|
|
|
|
roundedRect: signUpButtonShadowView.bounds,
|
|
|
|
byRoundingCorners: .allCorners,
|
|
|
|
cornerRadii: CGSize(width: 10, height: 10)
|
2021-12-31 10:31:13 +01:00
|
|
|
)
|
|
|
|
}
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
private func updateButtonContainerLayoutMargins(traitCollection: UITraitCollection) {
|
|
|
|
switch traitCollection.userInterfaceIdiom {
|
|
|
|
case .phone:
|
|
|
|
buttonContainer.layoutMargins = UIEdgeInsets(
|
|
|
|
top: 0,
|
|
|
|
left: WelcomeViewController.actionButtonMargin,
|
|
|
|
bottom: WelcomeViewController.viewBottomPaddingHeight,
|
|
|
|
right: WelcomeViewController.actionButtonMargin
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
let margin = traitCollection.horizontalSizeClass == .regular ? WelcomeViewController.actionButtonMarginExtend : WelcomeViewController.actionButtonMargin
|
|
|
|
buttonContainer.layoutMargins = UIEdgeInsets(
|
|
|
|
top: 0,
|
|
|
|
left: margin,
|
|
|
|
bottom: WelcomeViewController.viewBottomPaddingHeightExtend,
|
|
|
|
right: margin
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setupIllustrationLayout() {
|
|
|
|
welcomeIllustrationView.setup()
|
|
|
|
}
|
2021-02-23 08:44:59 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
extension WelcomeViewController {
|
2023-01-08 14:25:22 +01:00
|
|
|
|
|
|
|
//MARK: - Actions
|
2022-12-28 15:28:09 +01:00
|
|
|
@objc
|
|
|
|
private func signUpButtonDidClicked(_ sender: UIButton) {
|
|
|
|
_ = coordinator.present(scene: .mastodonPickServer(viewMode: MastodonPickServerViewModel(context: context)), from: self, transition: .show)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
private func signInButtonDidClicked(_ sender: UIButton) {
|
|
|
|
_ = coordinator.present(scene: .mastodonLogin, from: self, transition: .show)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
private func dismissBarButtonItemDidPressed(_ sender: UIButton) {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2023-01-08 14:25:22 +01:00
|
|
|
|
|
|
|
@objc
|
|
|
|
private func pageControlDidChange(_ sender: UIPageControl) {
|
|
|
|
let item = sender.currentPage
|
|
|
|
let indexPath = IndexPath(item: item, section: 0)
|
|
|
|
|
|
|
|
pageCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
|
|
|
|
}
|
2021-02-23 15:14:10 +01:00
|
|
|
}
|
2021-02-26 09:43:59 +01:00
|
|
|
|
|
|
|
// MARK: - OnboardingViewControllerAppearance
|
2023-01-06 13:44:11 +01:00
|
|
|
extension WelcomeViewController: OnboardingViewControllerAppearance {
|
|
|
|
func setupNavigationBarAppearance() {
|
|
|
|
// always transparent
|
|
|
|
let barAppearance = UINavigationBarAppearance()
|
|
|
|
barAppearance.configureWithTransparentBackground()
|
|
|
|
navigationItem.standardAppearance = barAppearance
|
|
|
|
navigationItem.compactAppearance = barAppearance
|
|
|
|
navigationItem.scrollEdgeAppearance = barAppearance
|
|
|
|
navigationItem.compactScrollEdgeAppearance = barAppearance
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 11:27:47 +01:00
|
|
|
|
|
|
|
// MARK: - UIAdaptivePresentationControllerDelegate
|
|
|
|
extension WelcomeViewController: UIAdaptivePresentationControllerDelegate {
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
|
|
|
|
|
|
|
|
// update button layout
|
|
|
|
updateButtonContainerLayoutMargins(traitCollection: traitCollection)
|
|
|
|
|
|
|
|
let navigationController = navigationController as? OnboardingNavigationController
|
|
|
|
|
|
|
|
switch traitCollection.userInterfaceIdiom {
|
|
|
|
case .phone:
|
2021-12-31 10:31:13 +01:00
|
|
|
navigationController?.gradientBorderView.isHidden = true
|
2022-12-28 15:28:09 +01:00
|
|
|
// make underneath view controller alive to fix layout issue due to view life cycle
|
2021-09-27 10:30:36 +02:00
|
|
|
return .fullScreen
|
2022-12-28 15:28:09 +01:00
|
|
|
default:
|
|
|
|
switch traitCollection.horizontalSizeClass {
|
|
|
|
case .compact:
|
|
|
|
navigationController?.gradientBorderView.isHidden = true
|
|
|
|
return .fullScreen
|
|
|
|
default:
|
|
|
|
navigationController?.gradientBorderView.isHidden = false
|
|
|
|
return .formSheet
|
|
|
|
}
|
2021-09-27 10:30:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-28 15:28:09 +01:00
|
|
|
|
|
|
|
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
|
|
|
|
return false
|
|
|
|
}
|
2022-11-26 15:22:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
//MARK: - UIScrollViewDelegate
|
|
|
|
extension WelcomeViewController: UIScrollViewDelegate {
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
|
let contentOffset = scrollView.contentOffset.x
|
|
|
|
welcomeIllustrationView.update(contentOffset: contentOffset)
|
2022-11-27 22:05:20 +01:00
|
|
|
}
|
2023-01-08 14:25:22 +01:00
|
|
|
|
|
|
|
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
|
|
pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
|
|
|
|
}
|
|
|
|
|
|
|
|
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
|
|
|
|
pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
|
|
|
|
}
|
2022-11-26 15:22:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
//MARK: - UICollectionViewDelegate
|
|
|
|
extension WelcomeViewController: UICollectionViewDelegate { }
|
2022-11-26 15:22:23 +01:00
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
//MARK: - UICollectionViewDataSource
|
|
|
|
extension WelcomeViewController: UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
|
educationPages.count
|
2022-12-28 15:28:09 +01:00
|
|
|
}
|
2022-11-27 14:16:09 +01:00
|
|
|
|
2023-01-07 16:02:46 +01:00
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
|
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WelcomeContentCollectionViewCell.identifier, for: indexPath) as? WelcomeContentCollectionViewCell else { fatalError("WTF? Wrong cell?") }
|
|
|
|
|
|
|
|
let page = educationPages[indexPath.item]
|
|
|
|
cell.update(with: page)
|
|
|
|
|
|
|
|
return cell
|
2022-12-28 15:28:09 +01:00
|
|
|
}
|
2022-11-27 14:16:09 +01:00
|
|
|
}
|