@@ -7,6 +7,7 @@
77//
88
99import UIKit
10+import TinyConstraints
1011
1112class CodeViewController: UIViewController {
1213
@@ -17,12 +18,16 @@ class CodeViewController: UIViewController {
1718 @IBOutlet weak var f5: UITextField!
1819 @IBOutlet weak var f6: UITextField!
1920
21+ var loading = UIActivityIndicatorView(style: .gray)
22+
2023 var onSuccessLogIn: (()->())?
2124
2225 override func viewDidLoad() {
2326 super.viewDidLoad()
2427 setUpConstraint()
2528
29+ navigationItem.title = DataStorage.standard.getEmail()
30+
2631 f1.addTarget(self, action: #selector(textFiledDidChange(textField:)), for: .editingChanged)
2732 f2.addTarget(self, action: #selector(textFiledDidChange(textField:)), for: .editingChanged)
2833 f3.addTarget(self, action: #selector(textFiledDidChange(textField:)), for: .editingChanged)
@@ -31,6 +36,12 @@ class CodeViewController: UIViewController {
3136 f6.addTarget(self, action: #selector(textFiledDidChange(textField:)), for: .editingChanged)
3237 }
3338
39+ override func viewWillDisappear(_ animated: Bool) {
40+ super.viewWillDisappear(animated)
41+
42+ loading.stopAnimating()
43+ }
44+
3445 override func viewWillAppear(_ animated: Bool) {
3546 super.viewWillAppear(animated)
3647
@@ -62,28 +73,56 @@ class CodeViewController: UIViewController {
6273 }
6374
6475
76+ func presentAlertInvalidData(with text: String)
77+ {
78+ let alert = UIAlertController(title: "Invalid code", message: text, preferredStyle: .alert)
79+ alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
80+ self.present(alert, animated: true, completion: nil)
81+
82+ loading.stopAnimating()
83+ }
84+
6585 @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
6686
6787 @IBAction func whereToGoNext(_ sender: UIButton)
6888 {
89+ guard let f1t = f1.text, let f2t = f2.text, let f3t = f3.text, let f4t = f4.text, let f5t = f5.text, let f6t = f6.text else {
90+ presentAlertInvalidData(with: "Some fields are missing");
91+ return
92+ }
93+
94+ if f1t.isEmpty || f2t.isEmpty || f3t.isEmpty || f4t.isEmpty || f5t.isEmpty || f6t.isEmpty {
95+ presentAlertInvalidData(with: "Some fields are missing");
96+ return
97+ }
98+
6999 let code = "\(f1.text!)\(f2.text!)\(f3.text!)\(f4.text!)\(f5.text!)\(f6.text!)"
70100 if let codeToInt = Int(code)
71101 {
72− Model.verify(with: codeToInt) { _ in
102+ loading.startAnimating()
103+ Model.verify(with: codeToInt) { [weak self] in
73104 // if everything is okay we can authemticicate
74− // Model.authemticiateMe
75− Model.authenticateMe { [weak self] (res) in
76− if res {
77− self?.onSuccessLogIn?()
78− } else {
79− print("sosi")
105+ if !$0 {
106+ self?.presentAlertInvalidData(with: "Wrong code. Try again!")
107+ self?.loading.stopAnimating()
108+ return
109+ } else {
110+ // Model.authemticiateMe
111+ Model.authenticateMe { [weak self] (res) in
112+ if res {
113+ self?.onSuccessLogIn?()
114+ }
80115 }
116+ return
81117 }
82118 }
83119 }
84120 }
85121
86− func setUpConstraint(){
122+ func setUpConstraint()
123+ {
124+ self.view.addSubview(loading)
125+ loading.centerInSuperview()
87126 // for keyboard notifications
88127 NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillShowNotification, object: nil)
89128