diff --git a/LockdowniOS/Account/EmailSignInViewController.swift b/LockdowniOS/Account/EmailSignInViewController.swift index ae266ea..7285ab0 100644 --- a/LockdowniOS/Account/EmailSignInViewController.swift +++ b/LockdowniOS/Account/EmailSignInViewController.swift @@ -15,12 +15,6 @@ import CocoaLumberjackSwift class EmailSignInViewController: BaseViewController, UITextFieldDelegate, Loadable { - struct Delegate { - var accountStateDidChange: () -> () = { } - } - - var delegate = Delegate() - @IBOutlet weak var emailField: UITextField! @IBOutlet weak var passwordField: UITextField! @@ -70,9 +64,9 @@ class EmailSignInViewController: BaseViewController, UITextFieldDelegate, Loadab try setAPICredentials(email: email, password: password) setAPICredentialsConfirmed(confirmed: true) self.hideLoadingView() - self.delegate.accountStateDidChange() + NotificationCenter.default.post(name: AccountUI.accountStateDidChange, object: self) self.showPopupDialog(title: "Success! 🎉", message: "You've successfully signed in.", acceptButton: "Okay") { - self.dismiss(animated: true, completion: { + self.presentingViewController?.dismiss(animated: true, completion: { // logged in and confirmed - update this email with the receipt and refresh VPN credentials firstly { () -> Promise in try Client.subscriptionEvent() diff --git a/LockdowniOS/Account/EmailSignUpViewController.swift b/LockdowniOS/Account/EmailSignUpViewController.swift index b11c445..f6e9b0f 100644 --- a/LockdowniOS/Account/EmailSignUpViewController.swift +++ b/LockdowniOS/Account/EmailSignUpViewController.swift @@ -15,7 +15,6 @@ import PromiseKit class EmailSignUpViewController: BaseViewController, UITextFieldDelegate, Loadable { struct Delegate { - var accountStateDidChange: () -> () = { } var showSignIn: () -> () = { } } @@ -120,11 +119,11 @@ class EmailSignUpViewController: BaseViewController, UITextFieldDelegate, Loadab popup.addButtons([ DefaultButton(title: NSLocalizedString("Okay", comment: ""), dismissOnTap: true) { self.hideLoadingView() - self.dismiss(animated: true, completion: nil) + self.presentingViewController?.dismiss(animated: true, completion: nil) } ]) self.present(popup, animated: true, completion: nil) - self.delegate.accountStateDidChange() + NotificationCenter.default.post(name: AccountUI.accountStateDidChange, object: self) } catch { self.showPopupDialog(title: "Error Saving Credentials", message: "Couldn't save credentials to local keychain. Please report this error to team@lockdownprivacy.com.", acceptButton: "Okay") diff --git a/LockdowniOS/AccountUI.swift b/LockdowniOS/AccountUI.swift index 4263f88..9292fff 100644 --- a/LockdowniOS/AccountUI.swift +++ b/LockdowniOS/AccountUI.swift @@ -10,23 +10,23 @@ import UIKit enum AccountUI { - static func presentCreateAccount(on vc: UIViewController, completion: @escaping () -> () = { }) { + static let accountStateDidChange = Notification.Name("AccountUIAccountStateDidChangeNotification") + + static func presentCreateAccount(on vc: UIViewController) { let storyboard = UIStoryboard.main let viewController = storyboard.instantiateViewController(withIdentifier: "emailSignUpViewController") as! EmailSignUpViewController viewController.delegate.showSignIn = { [weak vc] in if let strongVC = vc { - AccountUI.presentSignInToAccount(on: strongVC, completion: completion) + AccountUI.presentSignInToAccount(on: strongVC) } } - viewController.delegate.accountStateDidChange = completion vc.present(viewController, animated: true, completion: nil) } - static func presentSignInToAccount(on vc: UIViewController, completion: @escaping () -> () = { }) { + static func presentSignInToAccount(on vc: UIViewController) { let storyboard = UIStoryboard.main let viewController = storyboard.instantiateViewController(withIdentifier: "emailSignInViewController") as! EmailSignInViewController - viewController.delegate.accountStateDidChange = completion vc.present(viewController, animated: true, completion: nil) } diff --git a/LockdowniOS/AccountVC.swift b/LockdowniOS/AccountVC.swift index f077805..d8e525d 100644 --- a/LockdowniOS/AccountVC.swift +++ b/LockdowniOS/AccountVC.swift @@ -28,17 +28,23 @@ final class AccountViewController: BaseViewController, Loadable { tableView.contentInset.top += 12 tableView.tableFooterView = UIView() + tableView.clear() createTable() } + + do { + NotificationCenter.default.addObserver(self, selector: #selector(accountStateDidChange), name: AccountUI.accountStateDidChange, object: nil) + } + } + + @objc + func accountStateDidChange() { + assert(Thread.isMainThread) + self.reloadTable() } func reloadTable() { - // always reload when called -// guard isViewLoaded else { -// return -// } - - tableView.rows = [] + tableView.clear() createTable() tableView.reloadData() } @@ -49,9 +55,9 @@ final class AccountViewController: BaseViewController, Loadable { var title = "⚠️ Not Signed In" var message: String? = "Sign up below to unlock benefits of a Lockdown account." var firstButton = DefaultCell(title: NSLocalizedString("Sign Up | Sign In", comment: ""), height: buttonHeight, dismissOnTap: true) { - AccountUI.presentCreateAccount(on: self) { [weak self] in - self?.reloadTable() - } + // AccountViewController will update itself by observing + // AccountUI.accountStateDidChange notification + AccountUI.presentCreateAccount(on: self) } firstButton.backgroundView = UIView() firstButton.backgroundView?.backgroundColor = UIColor.tunnelsBlue diff --git a/LockdowniOS/AppDelegate.swift b/LockdowniOS/AppDelegate.swift index 827fdef..7cf1fc6 100644 --- a/LockdowniOS/AppDelegate.swift +++ b/LockdowniOS/AppDelegate.swift @@ -489,7 +489,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ]) self.getCurrentViewController()?.present(popup, animated: true, completion: nil) DispatchQueue.main.async { - (self.window?.rootViewController as? MainTabBarController)?.accountViewController.reloadTable() + NotificationCenter.default.post(name: AccountUI.accountStateDidChange, object: self) } } .catch { error in