-
Notifications
You must be signed in to change notification settings - Fork 114
/
AuthenticatedState.swift
201 lines (174 loc) · 10.5 KB
/
AuthenticatedState.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import Foundation
import Yosemite
import Networking
import Storage
// MARK: - AuthenticatedState
//
class AuthenticatedState: StoresManagerState {
/// Dispatcher: Glues all of the Stores!
///
private let dispatcher = Dispatcher()
/// Retains all of the active Services
///
private let services: [ActionsProcessor]
/// NotificationCenter Tokens
///
private var errorObserverToken: NSObjectProtocol?
/// For tracking events from Networking layer
///
private let trackEventRequestNotificationHandler: TrackEventRequestNotificationHandler
/// Designated Initializer
///
init(credentials: Credentials) {
let storageManager = ServiceLocator.storageManager
let network = AlamofireNetwork(credentials: credentials)
var services: [ActionsProcessor] = [
AppSettingsStore(dispatcher: dispatcher,
storageManager: storageManager,
fileStorage: PListFileStorage(),
generalAppSettings: ServiceLocator.generalAppSettings),
AddOnGroupStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
BlazeStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
CommentStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
CouponStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
CustomerStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
DataStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
DomainStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
FeatureFlagStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
InAppPurchaseStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
InboxNotesStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
JetpackSettingsStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
JustInTimeMessageStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
MediaStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
NotificationStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
NotificationCountStore(dispatcher: dispatcher, storageManager: storageManager, fileStorage: PListFileStorage()),
OrderCardPresentPaymentEligibilityStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
OrderNoteStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
OrderStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
OrderStatusStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
PaymentStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
PaymentGatewayStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductAttributeStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductAttributeTermStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductReviewStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductCategoryStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductShippingClassStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductVariationStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ProductTagStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
RefundStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
SettingStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ShipmentStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ShippingLabelStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
ShippingMethodStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
SitePluginStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
SitePostStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
SiteStore(dotcomClientID: ApiCredentials.dotcomAppId,
dotcomClientSecret: ApiCredentials.dotcomSecret,
dispatcher: dispatcher,
storageManager: storageManager,
network: network),
StatsStoreV4(dispatcher: dispatcher, storageManager: storageManager, network: network),
SubscriptionStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
SystemStatusStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
TaxStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
TelemetryStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
UserStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
CardPresentPaymentStore(dispatcher: dispatcher,
storageManager: storageManager,
network: network,
cardReaderService: ServiceLocator.cardReaderService,
cardReaderConfigProvider: ServiceLocator.cardReaderConfigProvider),
ReceiptStore(dispatcher: dispatcher,
storageManager: storageManager,
network: network,
receiptPrinterService: ServiceLocator.receiptPrinterService,
fileStorage: PListFileStorage()),
AnnouncementsStore(dispatcher: dispatcher,
storageManager: storageManager,
network: network,
fileStorage: PListFileStorage()),
WordPressSiteStore(network: network, dispatcher: dispatcher),
StoreOnboardingTasksStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
GoogleAdsStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
MetaDataStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
]
if case .wpcom = credentials {
services.append(contentsOf: [
AccountStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
WordPressThemeStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
])
} else {
DDLogInfo("No WordPress.com auth token found. AccountStore is not initialized.")
}
switch credentials {
case let .wporg(_, _, siteAddress),
let .applicationPassword(_, _, siteAddress):
/// Needs Jetpack connection store to handle Jetpack setup for non-Jetpack sites.
/// `AlamofireNetwork` is used here to handle requests with application password auth.
services.append(JetpackConnectionStore(dispatcher: dispatcher, network: network, siteURL: siteAddress))
case .wpcom:
/// When authenticated with WPCom, the store is used to handle Jetpack setup when a selected site doesn't have Jetpack.
/// The store will require cookie-nonce auth, which is handled by a `WordPressOrgNetwork`
/// injected later through the `authenticate` action before any other action is triggered.
services.append(JetpackConnectionStore(dispatcher: dispatcher))
}
self.services = services
trackEventRequestNotificationHandler = TrackEventRequestNotificationHandler()
startListeningToNotifications()
}
/// Convenience Initializer
///
convenience init?(sessionManager: SessionManagerProtocol) {
guard let credentials = sessionManager.defaultCredentials else {
return nil
}
self.init(credentials: credentials)
}
/// Executed before the current state is deactivated.
///
func willLeave() {
let pushNotesManager = ServiceLocator.pushNotesManager
pushNotesManager.unregisterForRemoteNotifications()
pushNotesManager.resetBadgeCountForAllStores(onCompletion: {})
resetServices()
}
/// Executed whenever the state is activated.
///
func didEnter() { }
/// Forwards the received action to the Actions Dispatcher.
///
func onAction(_ action: Action) {
dispatcher.dispatch(action)
}
}
// MARK: - Private Methods
//
private extension AuthenticatedState {
/// Starts listening for Notifications
///
func startListeningToNotifications() {
let nc = NotificationCenter.default
errorObserverToken = nc.addObserver(forName: .RemoteDidReceiveJetpackTimeoutError, object: nil, queue: .main) { [weak self] note in
self?.tunnelTimeoutWasReceived(note: note)
}
}
/// Executed whenever a DotcomError is received (ApplicationLayer). This allows us to have a *main* error handling flow!
///
func tunnelTimeoutWasReceived(note: Notification) {
ServiceLocator.analytics.track(.jetpackTunnelTimeout)
}
}
private extension AuthenticatedState {
func resetServices() {
let resetStoredProviders = AppSettingsAction.resetStoredProviders(onCompletion: nil)
let resetOrdersSettings = AppSettingsAction.resetOrdersSettings
let resetProductsSettings = AppSettingsAction.resetProductsSettings
let resetGeneralStoreSettings = AppSettingsAction.resetGeneralStoreSettings
ServiceLocator.stores.dispatch([resetStoredProviders,
resetOrdersSettings,
resetProductsSettings,
resetGeneralStoreSettings])
}
}