Skip to content

Commit 2118d3f

Browse files
InAppManager now uses online/offline request handler and not api client directly.
1 parent 6eec87c commit 2118d3f

File tree

9 files changed

+47
-40
lines changed

9 files changed

+47
-40
lines changed

swift-sdk/Internal/DependencyContainer.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ protocol DependencyContainerProtocol {
2020

2121
func createInAppFetcher(apiClient: ApiClientProtocol) -> InAppFetcherProtocol
2222
func createPersistenceContextProvider() -> IterablePersistenceContextProvider?
23-
var offlineMode: Bool { get}
2423
func createRequestHandler(apiKey: String,
2524
config: IterableConfig,
2625
endPoint: String,
@@ -33,8 +32,9 @@ protocol DependencyContainerProtocol {
3332
extension DependencyContainerProtocol {
3433
func createInAppManager(config: IterableConfig,
3534
apiClient: ApiClientProtocol,
35+
requestHandler: RequestHandlerProtocol,
3636
deviceMetadata: DeviceMetadata) -> IterableInternalInAppManagerProtocol {
37-
InAppManager(apiClient: apiClient,
37+
InAppManager(requestHandler: requestHandler,
3838
deviceMetadata: deviceMetadata,
3939
fetcher: createInAppFetcher(apiClient: apiClient),
4040
displayer: inAppDisplayer,
@@ -125,7 +125,6 @@ struct DependencyContainer: DependencyContainerProtocol {
125125
InAppFetcher(apiClient: apiClient)
126126
}
127127

128-
let offlineMode = false
129128
let dateProvider: DateProviderProtocol = SystemDateProvider()
130129
let networkSession: NetworkSessionProtocol = URLSession(configuration: .default)
131130
let notificationStateProvider: NotificationStateProviderProtocol = SystemNotificationStateProvider()

swift-sdk/Internal/InAppManager.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protocol IterableInternalInAppManagerProtocol: IterableInAppManagerProtocol, InA
2727
}
2828

2929
class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
30-
init(apiClient: ApiClientProtocol,
30+
init(requestHandler: RequestHandlerProtocol,
3131
deviceMetadata: DeviceMetadata,
3232
fetcher: InAppFetcherProtocol,
3333
displayer: InAppDisplayerProtocol,
@@ -42,7 +42,7 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
4242
retryInterval: Double) {
4343
ITBInfo()
4444

45-
self.apiClient = apiClient
45+
self.requestHandler = requestHandler
4646
self.deviceMetadata = deviceMetadata
4747
self.fetcher = fetcher
4848
self.displayer = displayer
@@ -233,7 +233,9 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
233233

234234
// track in-app delivery
235235
mergeMessagesResult.deliveredMessages.forEach {
236-
_ = apiClient?.track(inAppDelivery: InAppMessageContext.from(message: $0, location: nil))
236+
requestHandler?.track(inAppDelivery: $0,
237+
onSuccess: nil,
238+
onFailure: nil)
237239
}
238240

239241
finishSync(inboxChanged: mergeMessagesResult.inboxChanged)
@@ -318,7 +320,9 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
318320
self.scheduleNextInAppMessage()
319321

320322
if consume {
321-
self.apiClient?.inAppConsume(messageId: message.messageId)
323+
self.requestHandler?.inAppConsume(message.messageId,
324+
onSuccess: nil,
325+
onFailure: nil)
322326
}
323327
}
324328
}
@@ -481,9 +485,11 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
481485
ITBInfo()
482486

483487
updateMessage(message, didProcessTrigger: true, consumed: true)
484-
let messageContext = InAppMessageContext.from(message: message, location: location, inboxSessionId: inboxSessionId)
485-
apiClient?.inAppConsume(inAppMessageContext: messageContext, source: source)
486-
488+
requestHandler?.inAppConsume(message: message,
489+
location: location,
490+
source: source,
491+
onSuccess: nil,
492+
onFailure: nil)
487493
callbackQueue.async {
488494
self.notificationCenter.post(name: .iterableInboxChanged, object: self, userInfo: nil)
489495
}
@@ -518,7 +524,7 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
518524
}
519525
}
520526

521-
private weak var apiClient: ApiClientProtocol?
527+
private weak var requestHandler: RequestHandlerProtocol?
522528
private let deviceMetadata: DeviceMetadata
523529
private let fetcher: InAppFetcherProtocol
524530
private let displayer: InAppDisplayerProtocol

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ final class IterableAPIInternal: NSObject, PushTrackerProtocol, AuthProvider {
6363
lazy var inAppManager: IterableInternalInAppManagerProtocol = {
6464
self.dependencyContainer.createInAppManager(config: self.config,
6565
apiClient: self.apiClient,
66+
requestHandler: self.requestHandler,
6667
deviceMetadata: deviceMetadata)
6768
}()
6869

swift-sdk/Internal/LegacyRequestHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import Foundation
66

77
/// Request handling pre iOS 10.0
8-
struct LegacyRequestHandler: RequestHandlerProtocol {
8+
class LegacyRequestHandler: RequestHandlerProtocol {
99
init(apiKey: String,
1010
authProvider: AuthProvider?,
1111
authManager: IterableInternalAuthManagerProtocol?,

swift-sdk/Internal/RequestHandlerProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import Foundation
66

77
/// `IterableAPIinternal` will delegate all network related calls to this protocol.
8-
protocol RequestHandlerProtocol {
8+
protocol RequestHandlerProtocol: class {
99
var offlineMode: Bool { get set }
1010

1111
func start()

tests/common/CommonExtensions.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class MockDependencyContainer: DependencyContainerProtocol {
8080
let applicationStateProvider: ApplicationStateProviderProtocol
8181
let notificationCenter: NotificationCenterProtocol
8282
let apnsTypeChecker: APNSTypeCheckerProtocol
83-
let offlineMode: Bool
8483

8584
init(dateProvider: DateProviderProtocol,
8685
networkSession: NetworkSessionProtocol,
@@ -92,8 +91,7 @@ class MockDependencyContainer: DependencyContainerProtocol {
9291
urlOpener: UrlOpenerProtocol,
9392
applicationStateProvider: ApplicationStateProviderProtocol,
9493
notificationCenter: NotificationCenterProtocol,
95-
apnsTypeChecker: APNSTypeCheckerProtocol,
96-
offlineMode: Bool) {
94+
apnsTypeChecker: APNSTypeCheckerProtocol) {
9795
self.dateProvider = dateProvider
9896
self.networkSession = networkSession
9997
self.notificationStateProvider = notificationStateProvider
@@ -105,7 +103,6 @@ class MockDependencyContainer: DependencyContainerProtocol {
105103
self.applicationStateProvider = applicationStateProvider
106104
self.notificationCenter = notificationCenter
107105
self.apnsTypeChecker = apnsTypeChecker
108-
self.offlineMode = offlineMode
109106
}
110107

111108
func createInAppFetcher(apiClient _: ApiClientProtocol) -> InAppFetcherProtocol {
@@ -130,8 +127,7 @@ extension IterableAPI {
130127
urlOpener: UrlOpenerProtocol = MockUrlOpener(),
131128
applicationStateProvider: ApplicationStateProviderProtocol = UIApplication.shared,
132129
notificationCenter: NotificationCenterProtocol = NotificationCenter.default,
133-
apnsTypeChecker: APNSTypeCheckerProtocol = APNSTypeChecker(),
134-
offlineMode: Bool = false) {
130+
apnsTypeChecker: APNSTypeCheckerProtocol = APNSTypeChecker()) {
135131
let mockDependencyContainer = MockDependencyContainer(dateProvider: dateProvider,
136132
networkSession: networkSession,
137133
notificationStateProvider: notificationStateProvider,
@@ -142,8 +138,7 @@ extension IterableAPI {
142138
urlOpener: urlOpener,
143139
applicationStateProvider: applicationStateProvider,
144140
notificationCenter: notificationCenter,
145-
apnsTypeChecker: apnsTypeChecker,
146-
offlineMode: offlineMode)
141+
apnsTypeChecker: apnsTypeChecker)
147142

148143
internalImplementation = IterableAPIInternal(apiKey: apiKey,
149144
launchOptions: launchOptions,
@@ -173,8 +168,7 @@ extension IterableAPIInternal {
173168
urlOpener: UrlOpenerProtocol = MockUrlOpener(),
174169
applicationStateProvider: ApplicationStateProviderProtocol = UIApplication.shared,
175170
notificationCenter: NotificationCenterProtocol = NotificationCenter.default,
176-
apnsTypeChecker: APNSTypeCheckerProtocol = APNSTypeChecker(),
177-
offlineMode: Bool = false) -> IterableAPIInternal {
171+
apnsTypeChecker: APNSTypeCheckerProtocol = APNSTypeChecker()) -> IterableAPIInternal {
178172
let mockDependencyContainer = MockDependencyContainer(dateProvider: dateProvider,
179173
networkSession: networkSession,
180174
notificationStateProvider: notificationStateProvider,
@@ -185,8 +179,7 @@ extension IterableAPIInternal {
185179
urlOpener: urlOpener,
186180
applicationStateProvider: applicationStateProvider,
187181
notificationCenter: notificationCenter,
188-
apnsTypeChecker: apnsTypeChecker,
189-
offlineMode: offlineMode)
182+
apnsTypeChecker: apnsTypeChecker)
190183

191184
let internalImplementation = IterableAPIInternal(apiKey: apiKey,
192185
launchOptions: launchOptions,

tests/endpoint-tests/E2EDependencyContainer.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class E2EDependencyContainer: DependencyContainerProtocol {
1616
let applicationStateProvider: ApplicationStateProviderProtocol
1717
let notificationCenter: NotificationCenterProtocol
1818
let apnsTypeChecker: APNSTypeCheckerProtocol
19-
let offlineMode: Bool
2019

2120
func createInAppFetcher(apiClient: ApiClientProtocol) -> InAppFetcherProtocol {
2221
InAppFetcher(apiClient: apiClient)
@@ -31,8 +30,7 @@ class E2EDependencyContainer: DependencyContainerProtocol {
3130
urlOpener: UrlOpenerProtocol = AppUrlOpener(),
3231
applicationStateProvider: ApplicationStateProviderProtocol = UIApplication.shared,
3332
notificationCenter: NotificationCenterProtocol = NotificationCenter.default,
34-
apnsTypeChecker: APNSTypeCheckerProtocol = APNSTypeChecker(),
35-
offlineMode: Bool = false) {
33+
apnsTypeChecker: APNSTypeCheckerProtocol = APNSTypeChecker()) {
3634
self.dateProvider = dateProvider
3735
self.networkSession = networkSession
3836
self.notificationStateProvider = notificationStateProvider
@@ -43,16 +41,15 @@ class E2EDependencyContainer: DependencyContainerProtocol {
4341
self.applicationStateProvider = applicationStateProvider
4442
self.notificationCenter = notificationCenter
4543
self.apnsTypeChecker = apnsTypeChecker
46-
self.offlineMode = offlineMode
4744
}
4845
}
4946

5047
extension IterableAPIInternal {
5148
@discardableResult static func initializeForE2E(apiKey: String,
5249
launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil,
5350
config: IterableConfig = IterableConfig(),
54-
offlineMode: Bool = false) -> IterableAPIInternal {
55-
let e2eDependencyContainer = E2EDependencyContainer(offlineMode: offlineMode)
51+
localStorage: LocalStorageProtocol = MockLocalStorage()) -> IterableAPIInternal {
52+
let e2eDependencyContainer = E2EDependencyContainer()
5653
let internalImplementation = IterableAPIInternal(apiKey: apiKey,
5754
launchOptions: launchOptions,
5855
config: config,

tests/endpoint-tests/OfflineModeE2ETests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class OfflineModeEndpointTests: XCTestCase {
2222

2323
func test01TrackPurchase() throws {
2424
let expectation1 = expectation(description: #function)
25+
let localStorage = MockLocalStorage()
26+
localStorage.offlineModeBeta = true
2527
let api = IterableAPIInternal.initializeForE2E(apiKey: Self.apiKey,
26-
offlineMode: true)
28+
localStorage: localStorage)
2729
api.email = "[email protected]"
2830

2931
let items = [
@@ -43,8 +45,10 @@ class OfflineModeEndpointTests: XCTestCase {
4345

4446
func test02TrackPushOpen() throws {
4547
let expectation1 = expectation(description: #function)
48+
let localStorage = MockLocalStorage()
49+
localStorage.offlineModeBeta = true
4650
let api = IterableAPIInternal.initializeForE2E(apiKey: Self.apiKey,
47-
offlineMode: true)
51+
localStorage: localStorage)
4852
api.email = "[email protected]"
4953

5054
api.trackPushOpen(Self.pushCampaignId,
@@ -63,8 +67,10 @@ class OfflineModeEndpointTests: XCTestCase {
6367

6468
func test03TrackPushOpenWithPushPayload() throws {
6569
let expectation1 = expectation(description: #function)
70+
let localStorage = MockLocalStorage()
71+
localStorage.offlineModeBeta = true
6672
let api = IterableAPIInternal.initializeForE2E(apiKey: Self.apiKey,
67-
offlineMode: true)
73+
localStorage: localStorage)
6874
api.email = "[email protected]"
6975

7076
let pushPayload = [
@@ -88,8 +94,10 @@ class OfflineModeEndpointTests: XCTestCase {
8894

8995
func test04TrackEvent() throws {
9096
let expectation1 = expectation(description: #function)
97+
let localStorage = MockLocalStorage()
98+
localStorage.offlineModeBeta = true
9199
let api = IterableAPIInternal.initializeForE2E(apiKey: Self.apiKey,
92-
offlineMode: true)
100+
localStorage: localStorage)
93101
api.email = "[email protected]"
94102

95103
api.track("event1",

tests/offline-events-tests/RequestHandlerTests.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,15 +596,18 @@ class RequestHandlerTests: XCTestCase {
596596
}
597597

598598
func testDeleteAllTasksOnLogout() throws {
599-
let internalApi = IterableAPIInternal.initializeForTesting(offlineMode: true)
599+
let localStorage = MockLocalStorage()
600+
localStorage.offlineModeBeta = true
601+
let internalApi = IterableAPIInternal.initializeForTesting(networkSession: MockNetworkSession(),
602+
localStorage: localStorage)
600603
internalApi.email = "[email protected]"
601604

602605
let taskId = IterableUtil.generateUUID()
603606
try persistenceContextProvider.mainQueueContext().create(task: IterableTask(id: taskId,
604-
type: .apiCall,
605-
scheduledAt: Date(),
606-
data: nil,
607-
requestedAt: Date()))
607+
type: .apiCall,
608+
scheduledAt: Date(),
609+
data: nil,
610+
requestedAt: Date()))
608611
try persistenceContextProvider.mainQueueContext().save()
609612

610613
internalApi.logoutUser()

0 commit comments

Comments
 (0)