forked from ijoshsmith/Wizardry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsernameWizardStep.swift
103 lines (78 loc) · 3.63 KB
/
UsernameWizardStep.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// UsernameWizardStep.swift
// WizardryDemo
//
// Created by Joshua Smith on 5/4/16.
// Copyright © 2016 iJoshSmith. All rights reserved.
//
import UIKit
import Wizardry
/// The first step in the Sign Up Wizard, where a username is entered and validated by a service call.
final class UsernameWizardStep {
fileprivate let model: SignUpWizardModel
fileprivate let usernameStepViewController: UsernameStepViewController
init(model: SignUpWizardModel) {
self.model = model
let storyboard = UIStoryboard(name: "SignUpWizard", bundle: nil)
usernameStepViewController = (storyboard.instantiateViewController(withIdentifier: "username") as! UsernameStepViewController)
usernameStepViewController.initialUsername = model.username
}
}
// MARK: - WizardStep conformance
//{
//
///// Returns a view controller that displays this step's user interface.
//var viewController: UIViewController { get }
//
///// Invoked when the user requests a transition to the next wizard step.
///// The completion handler should be invoked when ready to transition, or to cancel the transition.
//func doWorkBeforeWizardGoesToNextStepWithCompletionHandler(_ completionHandler: (_ shouldGoToNextStep: Bool) -> Void)
//
///// Invoked when the user requests a transition to the previous wizard step.
///// The completion handler should be invoked when ready to transition, or to cancel the transition.
//func doWorkBeforeWizardGoesToPreviousStepWithCompletionHandler(_ completionHandler: (_ shouldGoToPreviousStep: Bool) -> Void)
//}
extension UsernameWizardStep: WizardStep {
var viewController: UIViewController {
return usernameStepViewController
}
func doWorkBeforeWizardGoesToNextStepWithCompletionHandler(completionHandler: @escaping (_ shouldGoToNextStep: Bool) -> Void) {
guard let username = usernameStepViewController.currentUsername, isValidUsername(username) else {
completionHandler(false)
return
}
usernameStepViewController.isCheckingUsernameAvailability = true
UsernameService.checkIfAvailable(username) { isUsernameAvailable in
self.usernameStepViewController.isCheckingUsernameAvailability = false
if isUsernameAvailable {
self.model.username = username
}
else {
self.showUsernameUnavailableAlert()
}
completionHandler(isUsernameAvailable)
}
}
func doWorkBeforeWizardGoesToPreviousStepWithCompletionHandler(completionHandler: (_ shouldGoToPreviousStep: Bool) -> Void) {
// There's no need to copy the current username to the data model, because going back from this step cancels the wizard.
// Take the view out of edit mode, to immediately dismiss the keyboard, otherwise it stays around for a moment too long.
usernameStepViewController.view.endEditing(true)
completionHandler(true)
}
}
// MARK: - Private methods
private extension UsernameWizardStep {
func isValidUsername(_ username: String) -> Bool {
return username.characters.count > 0
}
func showUsernameUnavailableAlert() {
let alert = UIAlertController(
title: "Username Unavailable",
message: "Please choose a different username.",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { _ in
alert.dismiss(animated: true, completion: nil)
}))
usernameStepViewController.show(alert, sender: usernameStepViewController)
}
}