-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathServiceLocator.swift
438 lines (344 loc) · 13.6 KB
/
ServiceLocator.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import Foundation
import CocoaLumberjack
import Experiments
import Storage
import Yosemite
import Hardware
import class Networking.MediaUploadSessionManager
import WooFoundation
import WordPressShared
/// Provides global dependencies.
///
final class ServiceLocator {
// MARK: - Private properties
/// WooAnalytics Wrapper
///
private static var _analytics: Analytics = WooAnalytics(analyticsProvider: TracksProvider())
/// StoresManager
///
private static var _stores: StoresManager = DefaultStoresManager(sessionManager: SessionManager.standard)
/// WordPressAuthenticator Wrapper
///
private static var _authenticationManager: Authentication = AuthenticationManager()
/// FeatureFlagService
///
private static var _featureFlagService: FeatureFlagService = DefaultFeatureFlagService()
/// ImageService
///
private static var _imageService: ImageService = DefaultImageService()
/// In-App Notifications Presenter
///
private static var _noticePresenter: NoticePresenter = DefaultNoticePresenter()
/// Product image uploader
///
private static var _productImageUploader: ProductImageUploaderProtocol = ProductImageUploader()
/// Push Notifications Manager
///
private static var _pushNotesManager: PushNotesManager = PushNotificationsManager()
/// Shipping Settings
///
private static var _shippingSettingsService: ShippingSettingsService?
/// Selected Site Settings
///
private static var _selectedSiteSettings: SelectedSiteSettings = SelectedSiteSettings()
/// Currency Settings
///
private static var _currencySettings: CurrencySettings = CurrencySettings()
/// CoreData Stack
///
private static var _storageManager = CoreDataManager(name: WooConstants.databaseStackName, crashLogger: crashLogging)
/// Cocoalumberjack DDLog
///
private static var _fileLogger: Logs = DDFileLogger()
/// Crash Logging Stack
///
private static var _crashLogging: CrashLoggingStack = WooCrashLoggingStack(
featureFlagService: featureFlagService
)
/// Support for external Card Readers
///
#if !targetEnvironment(macCatalyst)
private static var _cardReader: CardReaderService = StripeCardReaderService()
#else
private static var _cardReader: CardReaderService = NoOpCardReaderService()
#endif
private static var _cardReaderConfigProvider: CommonReaderConfigProviding = CommonReaderConfigProvider()
/// Support for printing receipts
///
private static var _receiptPrinter: PrinterService = AirPrintReceiptPrinterService()
/// Observer for network connectivity
///
private static var _connectivityObserver: ConnectivityObserver = DefaultConnectivityObserver()
/// Store WPCom plan synchronizer.
///
private static var _storePlanSynchronizer: StorePlanSynchronizing = StorePlanSynchronizer()
/// Storage for general app settings
///
private static var _generalAppSettings: GeneralAppSettingsStorage = GeneralAppSettingsStorage()
/// Background image service
///
private static var _backgroundMediaUploadSessionManager = MediaUploadSessionManager()
private static var _cardPresentPaymentsOnboardingIPPUsersRefresher: CardPresentPaymentsOnboardingIPPUsersRefresher =
CardPresentPaymentsOnboardingIPPUsersRefresher()
private static var _tapToPayReconnectionController = TapToPayReconnectionController<BuiltInReaderConnectionAlertsProvider, CardPresentPaymentAlertsPresenter>(
connectionControllerFactory: BuiltInCardReaderConnectionControllerFactory(
alertProvider: BuiltInReaderConnectionAlertsProvider()))
/// Tracker for app startup waiting time
///
private static var _startupWaitingTimeTracker: AppStartupWaitingTimeTracker = AppStartupWaitingTimeTracker()
// MARK: - Getters
/// Provides the access point to the analytics.
/// - Returns: An implementation of the Analytics protocol. It defaults to WooAnalytics
static var analytics: Analytics {
return _analytics
}
/// Provides the access point to the feature flag service.
/// - Returns: An implementation of the FeatureFlagService protocol. It defaults to DefaultFeatureFlagService
static var featureFlagService: FeatureFlagService {
return _featureFlagService
}
/// Provides the access point to the image service.
/// - Returns: An implementation of the ImageService protocol. It defaults to DefaultImageService
static var imageService: ImageService {
return _imageService
}
/// Provides the access point to the stores.
/// - Returns: An implementation of the StoresManager protocol. It defaults to DefaultStoresManager
static var stores: StoresManager {
return _stores
}
/// Provides the access point to the NoticePresenter.
/// - Returns: An implementation of the NoticePresenter protocol. It defaults to DefaultNoticePresenter
static var noticePresenter: NoticePresenter {
return _noticePresenter
}
/// Provides the access point to the ProductImageUploaderProtocol.
/// - Returns: An implementation of the ProductImageUploaderProtocol. It defaults to ProductImageUploader
static var productImageUploader: ProductImageUploaderProtocol {
return _productImageUploader
}
/// Provides the access point to the PushNotesManager.
/// - Returns: An implementation of the PushNotesManager protocol. It defaults to PushNotificationsManager
static var pushNotesManager: PushNotesManager {
return _pushNotesManager
}
/// Provides the access point to the AuthenticationManager.
/// - Returns: An implementation of the AuthenticationManager protocol. It defaults to DefaultAuthenticationManager
static var authenticationManager: Authentication {
return _authenticationManager
}
/// Shipping Settings
///
static var shippingSettingsService: ShippingSettingsService {
guard let shippingSettingsService = _shippingSettingsService else {
let siteID = stores.sessionManager.defaultStoreID ?? Int64.min
let service = StorageShippingSettingsService(siteID: siteID,
storageManager: storageManager)
_shippingSettingsService = service
return service
}
return shippingSettingsService
}
/// Provides the access point to the Site Settings for the current Site.
/// - Returns: An instance of SelectedSiteSettings.
static var selectedSiteSettings: SelectedSiteSettings {
return _selectedSiteSettings
}
/// Provides the access point to the Currency Settings for the current Site.
/// - Returns: An instance of CurrencySettings.
static var currencySettings: CurrencySettings {
return _currencySettings
}
/// Provides the access point to the StorageManager.
/// - Returns: An instance of CoreDataManager. Notice how we break the pattern we
/// use in all other properties provided by the ServiceLocator. Mocked implementations
/// of the CoreDataManager should be subclasses
static var storageManager: CoreDataManager {
return _storageManager
}
/// Provides the access point to the FileLogger.
/// - Returns: An implementation of the Logs protocol. It defaults to DDFileLogger
static var fileLogger: Logs {
return _fileLogger
}
/// Provides an instance of `WordPressLoggingDelegate` for logging in WordPress libraries.
static let wordPressLibraryLogger: WordPressLoggingDelegate = WordPressLibraryLogger()
/// Provides the access point to the CrashLogger
/// - Returns: An implementation
static var crashLogging: CrashLoggingStack {
return _crashLogging
}
/// Provides the last known `KeyboardState`.
///
/// Because `static let` is lazy, this should be accessed when the app is started
/// (i.e. AppDelegate) for it to accurately provide the last known state.
///
static let keyboardStateProvider: KeyboardStateProviding = KeyboardStateProvider()
/// The main object to use for presenting SMS (`MessageUI`) ViewControllers.
///
static let messageComposerPresenter = MessageComposerPresenter()
/// Provides the access point to the CardReaderService.
/// - Returns: An implementation of the CardReaderService protocol.
static var cardReaderService: CardReaderService {
_cardReader
}
static var cardReaderConfigProvider: CommonReaderConfigProviding {
_cardReaderConfigProvider
}
/// Provides the access point to the ReceiptPrinterService.
/// - Returns: An implementation of the ReceiptPrinterService protocol.
static var receiptPrinterService: PrinterService {
_receiptPrinter
}
/// Provides access point to the ConnectivityObserver.
/// - Returns: An implementation of the ConnectivityObserver protocol.
static var connectivityObserver: ConnectivityObserver {
_connectivityObserver
}
/// Provides access point to the `StorePlanSynchronizer`.
///
static var storePlanSynchronizer: StorePlanSynchronizing {
_storePlanSynchronizer
}
/// Provides access point to GeneralAppSettingsStorage
/// - Returns: An instance of GeneralAppSetingsStorage
static var generalAppSettings: GeneralAppSettingsStorage {
_generalAppSettings
}
static var cardPresentPaymentsOnboardingIPPUsersRefresher: CardPresentPaymentsOnboardingIPPUsersRefresher {
_cardPresentPaymentsOnboardingIPPUsersRefresher
}
static var tapToPayReconnectionController: TapToPayReconnectionController<BuiltInReaderConnectionAlertsProvider,
CardPresentPaymentAlertsPresenter> {
_tapToPayReconnectionController
}
/// Provides access point to the `AppStartupWaitingTimeTracker`.
///
static var startupWaitingTimeTracker: AppStartupWaitingTimeTracker {
_startupWaitingTimeTracker
}
/// Provides access point to the `MediaUploadSessionManager`.
///
static var backgroundMediaUploadSessionManager: MediaUploadSessionManager {
return _backgroundMediaUploadSessionManager
}
}
// MARK: - Testability
/// The setters declared in this extension are meant to be used only from the test bundle
extension ServiceLocator {
static func setAnalytics(_ mock: Analytics) {
guard isRunningTests() else {
return
}
_analytics = mock
}
static func setFeatureFlagService(_ mock: FeatureFlagService) {
guard isRunningTests() else {
return
}
_featureFlagService = mock
}
static func setStores(_ mock: StoresManager) {
guard isRunningTests() || ProcessConfiguration.shouldUseScreenshotsNetworkLayer else {
return
}
_stores = mock
}
static func setNoticePresenter(_ mock: NoticePresenter) {
guard isRunningTests() else {
return
}
_noticePresenter = mock
}
static func setPushNotesManager(_ mock: PushNotesManager) {
guard isRunningTests() else {
return
}
_pushNotesManager = mock
}
static func setAuthenticationManager(_ mock: Authentication) {
guard isRunningTests() else {
return
}
_authenticationManager = mock
}
static func setShippingSettingsService(_ mock: ShippingSettingsService) {
guard isRunningTests() else {
return
}
_shippingSettingsService = mock
}
static func setSelectedSiteSettings(_ mock: SelectedSiteSettings) {
guard isRunningTests() else {
return
}
_selectedSiteSettings = mock
}
static func setCurrencySettings(_ mock: CurrencySettings) {
guard isRunningTests() else {
return
}
_currencySettings = mock
}
static func setStorageManager(_ mock: CoreDataManager) {
guard isRunningTests() else {
return
}
_storageManager = mock
}
static func setFileLogger(_ mock: Logs) {
guard isRunningTests() else {
return
}
_fileLogger = mock
}
static func setCrashLogging(_ mock: CrashLoggingStack) {
guard isRunningTests() else {
return
}
_crashLogging = mock
}
static func setCardReader(_ mock: CardReaderService) {
guard isRunningTests() else {
return
}
#if !targetEnvironment(macCatalyst)
_cardReader = mock
#endif
}
static func setCardReaderConfigProvider(_ mock: CommonReaderConfigProviding) {
guard isRunningTests() else {
return
}
_cardReaderConfigProvider = mock
}
static func setReceiptPrinter(_ mock: PrinterService) {
guard isRunningTests() else {
return
}
_receiptPrinter = mock
}
static func setConnectivityObserver(_ mock: ConnectivityObserver) {
guard isRunningTests() else {
return
}
_connectivityObserver = mock
}
static func setGeneralAppSettingsStorage(_ mock: GeneralAppSettingsStorage) {
guard isRunningTests() else {
return
}
_generalAppSettings = mock
}
static func setProductImageUploader(_ mock: ProductImageUploaderProtocol) {
guard isRunningTests() else {
return
}
_productImageUploader = mock
}
}
private extension ServiceLocator {
static func isRunningTests() -> Bool {
return NSClassFromString("XCTestCase") != nil
}
}