Created
April 13, 2020 10:54
-
-
Save rbsgn/20a9efdbbe75fcb6a5df15c251bdb57a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class WelcomeViewController: UIViewController { | |
override func loadView() { | |
let view = UIView(frame: .zero) | |
view.backgroundColor = .systemBackground | |
let title = makeTitleLabel() | |
let subtitle = makeSubtitleLabel() | |
let buttonHeight: CGFloat = 48 | |
let buttonSpacing: CGFloat = 4 | |
let createDefaultCatalog = makeCreateDefaultCatalogButton() | |
createDefaultCatalog.addTarget( | |
self, | |
action: #selector(didTapCreateDefaultAction(_:)), | |
for: .touchUpInside | |
) | |
let restoreCatalog = makeRestoreCatalogButton() | |
restoreCatalog.addTarget( | |
self, | |
action: #selector(didTapRestoreCatalog), | |
for: .touchUpInside | |
) | |
let skipCatalogConfiguration = makeSkipCatalogConfigurationButton() | |
skipCatalogConfiguration.addTarget( | |
self, | |
action: #selector(didTapSkipCatalogConfiguration(_:)), | |
for: .touchUpInside | |
) | |
let actionsStack = | |
UIStackView( | |
arrangedSubviews: [ | |
skipCatalogConfiguration, | |
restoreCatalog, | |
createDefaultCatalog | |
] | |
) | |
actionsStack.axis = .vertical | |
actionsStack.spacing = buttonSpacing | |
actionsStack.distribution = .fillEqually | |
view.addSubview(title) | |
title.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(subtitle) | |
subtitle.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(actionsStack) | |
actionsStack.translatesAutoresizingMaskIntoConstraints = false | |
let actionsCount = actionsStack.arrangedSubviews.count | |
let actionsStackHeight = | |
CGFloat(actionsCount) * buttonHeight + | |
CGFloat(actionsCount - 1) * buttonSpacing | |
NSLayoutConstraint.activate([ | |
title.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 24), | |
title.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16), | |
title.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16), | |
subtitle.topAnchor.constraint(equalTo: title.lastBaselineAnchor, constant: 12), | |
subtitle.leadingAnchor.constraint(equalTo: title.leadingAnchor), | |
subtitle.trailingAnchor.constraint(equalTo: title.trailingAnchor), | |
actionsStack.leadingAnchor.constraint(equalTo: title.leadingAnchor), | |
actionsStack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16), | |
actionsStack.trailingAnchor.constraint(equalTo: title.trailingAnchor), | |
actionsStack.heightAnchor.constraint(equalToConstant: actionsStackHeight) | |
]) | |
self.view = view | |
} | |
@objc private func didTapCreateDefaultAction(_ sender: Any) { | |
state.didTapCreateDefaultCatalog(self) | |
} | |
@objc private func didTapRestoreCatalog(_ sender: Any) { | |
state.didTapRestoreCatalog(self) | |
} | |
@objc private func didTapSkipCatalogConfiguration(_ sender: Any) { | |
} | |
fileprivate var state: WelcomeViewControllerState = | |
IdleWelcomeViewControllerState() | |
fileprivate enum Action { | |
case createDefaultCatalog | |
case restoreCatalog | |
} | |
fileprivate func disableActionsAnimating(action: Action) { | |
} | |
fileprivate func createDefaultCatalog() { | |
// defaultCatalogCreator.createDefaultCatalog(completion: { | |
// self.state.didCreateDefaultCatalog() | |
// }) | |
state.didCreateDefaultCatalog(self) | |
} | |
fileprivate func restoreCatalog() { | |
// catalogRestorer.restoreCatalog(completion: { | |
// self.state.didRestoreCatalog() | |
// }) | |
state.didRestoreCatalog(self) | |
} | |
fileprivate func enableActionsStoppingAnimation(action: Action) { | |
} | |
fileprivate func finish() { | |
// delegate?.fooViewControllerDidFinish(self) | |
} | |
} | |
// MARK: - State Management | |
class WelcomeViewControllerState { | |
func didTapCreateDefaultCatalog(_ controller: StateBasedWelcomeViewController) { } | |
func didTapRestoreCatalog( _ controller: StateBasedWelcomeViewController) { } | |
func didCreateDefaultCatalog( _ controller: StateBasedWelcomeViewController) { } | |
func didRestoreCatalog( _ controller: StateBasedWelcomeViewController) { } | |
} | |
class IdleWelcomeViewControllerState: WelcomeViewControllerState { | |
override func didTapCreateDefaultCatalog( | |
_ controller: StateBasedWelcomeViewController | |
) { | |
controller.disableActionsAnimating(action: .createDefaultCatalog) | |
controller.state = CreatingDefaultCatalogWelcomeViewControllerState() | |
controller.createDefaultCatalog() | |
} | |
override func didTapRestoreCatalog( | |
_ controller: StateBasedWelcomeViewController | |
) { | |
controller.disableActionsAnimating(action: .restoreCatalog) | |
controller.state = CreatingDefaultCatalogWelcomeViewControllerState() | |
controller.restoreCatalog() | |
} | |
} | |
class CreatingDefaultCatalogWelcomeViewControllerState: WelcomeViewControllerState { | |
override func didTapCreateDefaultCatalog( | |
_ controller: StateBasedWelcomeViewController | |
) { | |
controller.state = IdleWelcomeViewControllerState() | |
controller.enableActionsStoppingAnimation(action: .createDefaultCatalog) | |
controller.finish() | |
} | |
} | |
class RestoringCatalogWelcomeViewControllerState: WelcomeViewControllerState { | |
override func didRestoreCatalog( | |
_ controller: StateBasedWelcomeViewController | |
) { | |
controller.state = IdleWelcomeViewControllerState() | |
controller.enableActionsStoppingAnimation(action: .restoreCatalog) | |
controller.finish() | |
} | |
} | |
// MARK: - Views Making | |
private func makeTitleLabel() -> UILabel { | |
let result = UILabel(frame: .zero) | |
result.text = NSLocalizedString("Добро пожаловать!", comment: "") | |
result.font = | |
UIFont( | |
descriptor: .preferredFontDescriptor(withTextStyle: .largeTitle), | |
size: 24.0 | |
) | |
result.numberOfLines = 1 | |
return result | |
} | |
private func makeSubtitleLabel() -> UILabel { | |
let result = UILabel(frame: .zero) | |
result.text = | |
NSLocalizedString( | |
"Хотите ли вы создать каталог товаров для покупки по-умолчанию? Это упрощает начало работы.\n\nОднако, если вы уже пользовались приложением и имеете резервную копию приложения, то можете восстановить её сейчас.\n\nЕсли же вы не готовы сейчас принять решение, то вы всегда можете создать дефолтный каталог или восстановить его из резервной копии в iCloud в настройках приложения.", | |
comment: "" | |
) | |
result.font = | |
UIFont( | |
descriptor: .preferredFontDescriptor(withTextStyle: .body), | |
size: 15.0 | |
) | |
result.numberOfLines = 0 | |
return result | |
} | |
private func makeCreateDefaultCatalogButton() -> UIButton { | |
let result = | |
makeActionButton( | |
title: NSLocalizedString("Создать по-умолчанию", comment: "") | |
) | |
return result | |
} | |
private func makeRestoreCatalogButton() -> UIButton { | |
let result = | |
makeActionButton( | |
title: NSLocalizedString("Восстановить из резервной копии", comment: "") | |
) | |
return result | |
} | |
private func makeSkipCatalogConfigurationButton() -> UIButton { | |
let result = | |
makeActionButton( | |
title: NSLocalizedString("Пропустить сейчас", comment: "") | |
) | |
return result | |
} | |
private func makeActionButton(title: String) -> UIButton { | |
let result = UIButton(type: .system) | |
result.layer.cornerRadius = 8 | |
result.setTitle(title, for: .normal) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment