|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | +import Yosemite |
| 4 | + |
| 5 | + |
| 6 | +/// Simplifies and decouples the store picker from the caller |
| 7 | +/// |
| 8 | +final class StorePickerCoordinator: Coordinator { |
| 9 | + |
| 10 | + unowned var navigationController: UINavigationController |
| 11 | + |
| 12 | + /// Determines how the store picker should initialized |
| 13 | + /// |
| 14 | + var selectedConfiguration: StorePickerConfiguration |
| 15 | + |
| 16 | + /// Closure to be executed upon dismissal of the store picker |
| 17 | + /// |
| 18 | + var onDismiss: (() -> Void)? |
| 19 | + |
| 20 | + /// Site Picker VC |
| 21 | + /// |
| 22 | + private lazy var storePicker: StorePickerViewController = { |
| 23 | + let pickerVC = StorePickerViewController() |
| 24 | + pickerVC.delegate = self |
| 25 | + pickerVC.configuration = selectedConfiguration |
| 26 | + return pickerVC |
| 27 | + }() |
| 28 | + |
| 29 | + init(_ navigationController: UINavigationController, config: StorePickerConfiguration) { |
| 30 | + self.navigationController = navigationController |
| 31 | + self.selectedConfiguration = config |
| 32 | + } |
| 33 | + |
| 34 | + func start() { |
| 35 | + showStorePicker() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +// MARK: - StorePickerViewControllerDelegate Conformance |
| 41 | +// |
| 42 | +extension StorePickerCoordinator: StorePickerViewControllerDelegate { |
| 43 | + |
| 44 | + func willSelectStore(with storeID: Int, onCompletion: @escaping SelectStoreClosure) { |
| 45 | + guard selectedConfiguration == .switchingStores else { |
| 46 | + onCompletion() |
| 47 | + return |
| 48 | + } |
| 49 | + guard storeID != StoresManager.shared.sessionManager.defaultStoreID else { |
| 50 | + onCompletion() |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + logOutOfCurrentStore(onCompletion: onCompletion) |
| 55 | + } |
| 56 | + |
| 57 | + func didSelectStore(with storeID: Int) { |
| 58 | + guard storeID != StoresManager.shared.sessionManager.defaultStoreID else { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + finalizeStoreSelection(storeID) |
| 63 | + presentStoreSwitchedNotice() |
| 64 | + onDismiss?() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// MARK: - Private Helpers |
| 69 | +// |
| 70 | +private extension StorePickerCoordinator { |
| 71 | + |
| 72 | + func showStorePicker() { |
| 73 | + if selectedConfiguration == .standard { |
| 74 | + navigationController.present(storePicker, animated: true) |
| 75 | + } else { |
| 76 | + navigationController.pushViewController(storePicker, animated: true) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + func presentStoreSwitchedNotice() { |
| 81 | + guard selectedConfiguration == .switchingStores else { |
| 82 | + return |
| 83 | + } |
| 84 | + guard let newStoreName = StoresManager.shared.sessionManager.defaultSite?.name else { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + let message = NSLocalizedString("Switched to \(newStoreName). You will only receive notifications from this store.", |
| 89 | + comment: "Message presented after users switch to a new store. " |
| 90 | + + "Reads like: Switched to {store name}. You will only receive notifications from this store.") |
| 91 | + let notice = Notice(title: message, feedbackType: .success) |
| 92 | + |
| 93 | + AppDelegate.shared.noticePresenter.enqueue(notice: notice) |
| 94 | + } |
| 95 | + |
| 96 | + func logOutOfCurrentStore(onCompletion: @escaping () -> Void) { |
| 97 | + StoresManager.shared.removeDefaultStore() |
| 98 | + |
| 99 | + let group = DispatchGroup() |
| 100 | + |
| 101 | + group.enter() |
| 102 | + let statsAction = StatsAction.resetStoredStats { |
| 103 | + group.leave() |
| 104 | + } |
| 105 | + StoresManager.shared.dispatch(statsAction) |
| 106 | + |
| 107 | + group.enter() |
| 108 | + let orderAction = OrderAction.resetStoredOrders { |
| 109 | + group.leave() |
| 110 | + } |
| 111 | + StoresManager.shared.dispatch(orderAction) |
| 112 | + |
| 113 | + group.notify(queue: .main) { |
| 114 | + onCompletion() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + func finalizeStoreSelection(_ storeID: Int) { |
| 119 | + StoresManager.shared.updateDefaultStore(storeID: storeID) |
| 120 | + |
| 121 | + // We need to call refreshUserData() here because the user selected |
| 122 | + // their default store and tracks should to know about it. |
| 123 | + WooAnalytics.shared.refreshUserData() |
| 124 | + WooAnalytics.shared.track(.sitePickerContinueTapped, |
| 125 | + withProperties: ["selected_store_id": StoresManager.shared.sessionManager.defaultStoreID ?? String()]) |
| 126 | + |
| 127 | + AppDelegate.shared.authenticatorWasDismissed() |
| 128 | + if selectedConfiguration == .login { |
| 129 | + MainTabBarController.switchToMyStoreTab(animated: true) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments