Skip to content

Commit 90947e6

Browse files
committed
all: refactor isOptOut from display receipt to AppInformationProvider
1 parent 477127a commit 90947e6

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

Sources/Swift/AppInformationProvider.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ protocol AppInformationProvider {
1515
func sharedDirectory() throws -> URL
1616

1717
func sharedDefaults() throws -> UserDefaults
18+
19+
func isOptOut() throws -> Bool
1820
}
1921

2022
public enum AppInformationProviderError: Error, CustomNSError {
@@ -81,6 +83,15 @@ extension AppInformationProvider {
8183
else { throw DisplayReceiptHelperError.appGroupError }
8284
return defaults
8385
}
86+
87+
func isOptOut() throws -> Bool {
88+
let defaults = try self.sharedDefaults()
89+
if defaults.object(forKey: "batch_shared_optout") == nil {
90+
// Key is missing, we don't send display receipt
91+
return true
92+
}
93+
return defaults.bool(forKey: "batch_shared_optout")
94+
}
8495
}
8596

8697

Sources/Swift/Display Receipt/DisplayReceiptCacheHelper.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,4 @@ internal struct DisplayReceiptCacheHelper {
116116
throw DisplayReceiptHelperError.readCacheError(underlyingError: error)
117117
}
118118
}
119-
120-
// MARK: Methods reading user defaults
121-
122-
func isOptOut() throws -> Bool {
123-
let defaults = try appInformationProvider.sharedDefaults()
124-
if defaults.object(forKey: "batch_shared_optout") == nil {
125-
// Key is missing, we don't send display receipt
126-
return true
127-
}
128-
return defaults.bool(forKey: "batch_shared_optout")
129-
}
130119
}

Sources/Swift/Display Receipt/DisplayReceiptHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class DisplayReceiptHelper : NSObject {
4242
public func didReceive(_ request: UNNotificationRequest) {
4343

4444
do {
45-
if try DisplayReceiptCacheHelper().isOptOut() {
45+
if try AppInformationProviderDefaultImpl().isOptOut() {
4646
print("Batch - SDK is opt-out, skipping display receipts")
4747
return
4848
}

Tests/SharedDefaultsTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class SharedDefaultsTests: XCTestCase {
2323
}
2424

2525
func testOptOutReading() throws {
26-
let cacheHelper = DisplayReceiptCacheHelper(appInformationProvider: appInformationProvider)
26+
2727
// Default value, when key is missing, should be "true"
28-
XCTAssertTrue(try cacheHelper.isOptOut())
28+
XCTAssertTrue(try appInformationProvider.isOptOut())
2929

3030
defaults.set(true, forKey: sharedOptOutKey)
31-
XCTAssertTrue(try cacheHelper.isOptOut())
31+
XCTAssertTrue(try appInformationProvider.isOptOut())
3232

3333
defaults.set(false, forKey: sharedOptOutKey)
34-
XCTAssertFalse(try cacheHelper.isOptOut())
34+
XCTAssertFalse(try appInformationProvider.isOptOut())
3535
}
3636
}
3737

0 commit comments

Comments
 (0)