Skip to content

Commit

Permalink
Setup password via modal flow
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Mar 14, 2018
1 parent 4be265b commit 3391cc5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
26 changes: 20 additions & 6 deletions Trust/Lock/Coordinators/LockCreatePasscodeCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@

import UIKit

protocol LockCreatePasscodeCoordinatorDelegate: class {
func didCancel(in coordinator: LockCreatePasscodeCoordinator)
}

class LockCreatePasscodeCoordinator: Coordinator {
var coordinators: [Coordinator] = []
private let model: LockCreatePasscodeViewModel
private let navigationController: UINavigationController
let navigationController: UINavigationController
weak var delegate: LockCreatePasscodeCoordinatorDelegate?

lazy var lockViewController: LockCreatePasscodeViewController = {
return LockCreatePasscodeViewController(model: model)
let controller = LockCreatePasscodeViewController(model: model)
controller.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismiss))
return controller
}()
init(navigationController: UINavigationController, model: LockCreatePasscodeViewModel) {

init(
navigationController: UINavigationController = NavigationController(),
model: LockCreatePasscodeViewModel
) {
self.navigationController = navigationController
self.model = model
}

func start() {
navigationController.pushViewController(lockViewController, animated: true)
navigationController.viewControllers = [lockViewController]
}
func stop() {
navigationController.popViewController(animated: true)

@objc func dismiss() {
delegate?.didCancel(in: self)
}
}
12 changes: 2 additions & 10 deletions Trust/Lock/ViewControllers/LockPasscodeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class LockPasscodeViewController: UIViewController {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
self.view.backgroundColor = UIColor.white
self.configureInvisiblePasscodeField()
self.configureNavigationItems()
self.configureLockView()
}
override func viewWillAppear(_ animated: Bool) {
Expand All @@ -41,9 +40,7 @@ class LockPasscodeViewController: UIViewController {
invisiblePasscodeField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
view.addSubview(invisiblePasscodeField)
}
private func configureNavigationItems() {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("Cancel", value: "Cancel", comment: ""), style: .plain, target: self, action: #selector(self.userTappedCancel))
}

private func configureLockView() {
lockView = LockView(model)
lockView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -53,12 +50,7 @@ class LockPasscodeViewController: UIViewController {
lockView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
lockView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
}
@objc func userTappedCancel() {
if let finish = willFinishWithResult {
finish(false)
}
dismiss(animated: true, completion: nil)
}

@objc func enteredPasscode(_ passcode: String) {
shouldIgnoreTextFieldDelegateCalls = false
clearPasscode()
Expand Down
25 changes: 19 additions & 6 deletions Trust/Settings/ViewControllers/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ protocol SettingsViewControllerDelegate: class {
func didAction(action: SettingsAction, in viewController: SettingsViewController)
}

class SettingsViewController: FormViewController {
class SettingsViewController: FormViewController, Coordinator {
var coordinators: [Coordinator] = []
struct Values {
static let currencyPopularKey = "0"
static let currencyAllKey = "1"
Expand Down Expand Up @@ -230,12 +231,17 @@ class SettingsViewController: FormViewController {
}

func setPasscode(completion: ((Bool) -> Void)? = .none) {
let lock = LockCreatePasscodeCoordinator(navigationController: self.navigationController!, model: LockCreatePasscodeViewModel())
lock.start()
lock.lockViewController.willFinishWithResult = { result in
let coordinator = LockCreatePasscodeCoordinator(
model: LockCreatePasscodeViewModel()
)
coordinator.delegate = self
coordinator.start()
coordinator.lockViewController.willFinishWithResult = { [weak self] result in
completion?(result)
lock.stop()
self?.navigationController?.dismiss(animated: true, completion: nil)
}
addCoordinator(coordinator)
navigationController?.present(coordinator.navigationController, animated: true, completion: nil)
}

private func linkProvider(
Expand All @@ -251,7 +257,6 @@ class SettingsViewController: FormViewController {
}
}.cellSetup { cell, _ in
cell.imageView?.image = type.image
cell.accessoryType = .disclosureIndicator
}
}

Expand All @@ -270,3 +275,11 @@ class SettingsViewController: FormViewController {
fatalError("init(coder:) has not been implemented")
}
}

extension SettingsViewController: LockCreatePasscodeCoordinatorDelegate {
func didCancel(in coordinator: LockCreatePasscodeCoordinator) {
coordinator.lockViewController.willFinishWithResult?(false)
navigationController?.dismiss(animated: true, completion: nil)
removeCoordinator(coordinator)
}
}

0 comments on commit 3391cc5

Please sign in to comment.