|
| 1 | +import UIKit |
| 2 | +import WordPressData |
| 3 | + |
| 4 | +struct AuthTokenIssueSolver { |
| 5 | + private let coreData: CoreDataStack |
| 6 | + |
| 7 | + init(coreData: CoreDataStack = ContextManager.shared) { |
| 8 | + self.coreData = coreData |
| 9 | + } |
| 10 | + |
| 11 | + /// - note: The completion callback is run immediatelly if there is no issue. |
| 12 | + func fixAuthTokenIssueIfNeeded(in window: UIWindow, _ completion: @escaping () -> Void) { |
| 13 | + guard hasAuthTokenIssues() else { |
| 14 | + completion() |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + let signInVC = WordPressAuthenticationManager.signinForWPComFixingAuthToken { cancelled in |
| 19 | + if cancelled { |
| 20 | + // We present asynchronously to prevent an issue where the Login VC would dismiss the |
| 21 | + // alert instead of itself. |
| 22 | + DispatchQueue.main.async { |
| 23 | + showCancelReAuthenticationAlert(in: window, onDeletionConfirmed: { |
| 24 | + let accountService = AccountService(coreDataStack: ContextManager.sharedInstance()) |
| 25 | + accountService.removeDefaultWordPressComAccount() |
| 26 | + completion() |
| 27 | + }) |
| 28 | + } |
| 29 | + } else { |
| 30 | + completion() |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + window.rootViewController = signInVC |
| 35 | + |
| 36 | + showExplanationAlertForReAuthentication(in: signInVC) |
| 37 | + } |
| 38 | + |
| 39 | + /// Call this method to know if the local installation of WPiOS has the |
| 40 | + /// authToken issue this class was designed to solve. |
| 41 | + /// - returns: `true` if the local WPiOS installation needs to be fixed by this class. |
| 42 | + private func hasAuthTokenIssues() -> Bool { |
| 43 | + guard let account = try? WPAccount.lookupDefaultWordPressComAccount(in: coreData.mainContext) else { |
| 44 | + return false |
| 45 | + } |
| 46 | + return account.authToken == nil |
| 47 | + } |
| 48 | + |
| 49 | + private func showCancelReAuthenticationAlert(in window: UIWindow, onDeletionConfirmed: @escaping () -> Void) { |
| 50 | + let title = NSLocalizedString("Careful!", comment: "Title for the warning shown to the user when he refuses to re-login when the authToken is missing.") |
| 51 | + let message = NSLocalizedString("Proceeding will remove all WordPress.com data from this device, and delete any locally saved drafts. You will not lose anything already saved to your WordPress.com blog(s).", comment: "Message for the warning shown to the user when he refuses to re-login when the authToken is missing.") |
| 52 | + |
| 53 | + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) |
| 54 | + |
| 55 | + alert.addAction(UIAlertAction(title: SharedStrings.Button.cancel, style: .cancel) { _ in }) |
| 56 | + alert.addAction(UIAlertAction(title: SharedStrings.Button.delete, style: .destructive) { _ in |
| 57 | + onDeletionConfirmed() |
| 58 | + }) |
| 59 | + |
| 60 | + window.rootViewController?.present(alert, animated: true, completion: nil) |
| 61 | + } |
| 62 | + |
| 63 | + private func showExplanationAlertForReAuthentication(in presentingViewController: UIViewController) { |
| 64 | + let title = NSLocalizedString("Oops!", comment: "Title for the warning shown to the user when the app realizes there should be an auth token but there isn't one.") |
| 65 | + let message = NSLocalizedString("There was a problem connecting to WordPress.com. Please log in again.", comment: "Message for the warning shown to the user when the app realizes there should be an auth token but there isn't one.") |
| 66 | + |
| 67 | + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) |
| 68 | + |
| 69 | + alert.addAction(UIAlertAction(title: SharedStrings.Button.ok, style: .default) { _ in }) |
| 70 | + alert.modalPresentationStyle = .popover |
| 71 | + |
| 72 | + presentingViewController.present(alert, animated: true) |
| 73 | + } |
| 74 | +} |
0 commit comments