Skip to content

Commit 8e4ffe9

Browse files
committed
display receipt: split app wide information into a dedicated protocol
this can be shared anyway
1 parent 4477f8c commit 8e4ffe9

File tree

3 files changed

+97
-40
lines changed

3 files changed

+97
-40
lines changed

BatchExtension.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
514E86D02362EBEC00E42FF4 /* BAENotificationServiceExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 514E86CC2362EBEC00E42FF4 /* BAENotificationServiceExtension.m */; };
1414
514E86D12362EBEC00E42FF4 /* BAENotificationServiceExtension.h in Headers */ = {isa = PBXBuildFile; fileRef = 514E86CD2362EBEC00E42FF4 /* BAENotificationServiceExtension.h */; settings = {ATTRIBUTES = (Public, ); }; };
1515
514E86D72362FC1E00E42FF4 /* BAENotificationServiceExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514E86D62362FC1E00E42FF4 /* BAENotificationServiceExtension.swift */; };
16+
5169CF59258B599700F24E9D /* AppInformationProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5169CF58258B599700F24E9D /* AppInformationProvider.swift */; };
1617
51F7ECFC236092FF00E01CD2 /* BatchExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 51F7ECF2236092FF00E01CD2 /* BatchExtension.framework */; };
1718
51F7ED01236092FF00E01CD2 /* BatchExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51F7ED00236092FF00E01CD2 /* BatchExtensionTests.swift */; };
1819
51F7ED03236092FF00E01CD2 /* BatchExtension.h in Headers */ = {isa = PBXBuildFile; fileRef = 51F7ECF5236092FF00E01CD2 /* BatchExtension.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -44,6 +45,7 @@
4445
514E86CC2362EBEC00E42FF4 /* BAENotificationServiceExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BAENotificationServiceExtension.m; sourceTree = "<group>"; };
4546
514E86CD2362EBEC00E42FF4 /* BAENotificationServiceExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BAENotificationServiceExtension.h; sourceTree = "<group>"; };
4647
514E86D62362FC1E00E42FF4 /* BAENotificationServiceExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BAENotificationServiceExtension.swift; sourceTree = "<group>"; };
48+
5169CF58258B599700F24E9D /* AppInformationProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppInformationProvider.swift; sourceTree = "<group>"; };
4749
51F7ECF2236092FF00E01CD2 /* BatchExtension.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BatchExtension.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4850
51F7ECF5236092FF00E01CD2 /* BatchExtension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BatchExtension.h; sourceTree = "<group>"; };
4951
51F7ECF6236092FF00E01CD2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -83,6 +85,7 @@
8385
514E86C72362EBEC00E42FF4 /* Swift */ = {
8486
isa = PBXGroup;
8587
children = (
88+
5169CF58258B599700F24E9D /* AppInformationProvider.swift */,
8689
6224E0E923F43CA000514B9D /* MessagePack */,
8790
6224E0E223F43C8100514B9D /* Display Receipt */,
8891
514E86D52362FAC300E42FF4 /* SwiftPM */,
@@ -303,6 +306,7 @@
303306
6224E0F323F43CA000514B9D /* FlatValue.swift in Sources */,
304307
512540D6254AD54000F01090 /* Writer+Optionals.swift in Sources */,
305308
6224E0E823F43C8100514B9D /* DisplayReceiptHelper.swift in Sources */,
309+
5169CF59258B599700F24E9D /* AppInformationProvider.swift in Sources */,
306310
);
307311
runOnlyForDeploymentPostprocessing = 0;
308312
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// AppInformationProvider.swift
3+
// BatchExtension
4+
//
5+
// Copyright © 2020 Batch. All rights reserved.
6+
//
7+
8+
import Foundation
9+
10+
// Protocol that defines an object that will provide application information
11+
// such as the shared app group id, etc...
12+
protocol AppInformationProvider {
13+
func sharedGroupId() throws -> String
14+
15+
func sharedDirectory() throws -> URL
16+
17+
func sharedDefaults() throws -> UserDefaults
18+
}
19+
20+
public enum AppInformationProviderError: Error, CustomNSError {
21+
case appGroupError
22+
case unknownError
23+
24+
public var localizedDescription: String {
25+
switch self {
26+
case .appGroupError:
27+
return "Could not get app group folder"
28+
case .unknownError:
29+
return "An unknown error occurred"
30+
}
31+
}
32+
33+
public static var errorDomain: String {
34+
return Consts.errorDomain
35+
}
36+
37+
public var errorUserInfo: [String : Any] {
38+
let info: [String : Any] = [
39+
NSLocalizedDescriptionKey: self.localizedDescription
40+
]
41+
42+
return info
43+
}
44+
}
45+
46+
// MARK: Default implementation
47+
extension AppInformationProvider {
48+
func sharedGroupId() throws -> String {
49+
50+
let groupIdOverride = Bundle.main.object(forInfoDictionaryKey: "BATCH_APP_GROUP_ID")
51+
if let groupId = groupIdOverride as? String, !groupId.isEmpty {
52+
return groupId
53+
}
54+
55+
let bundleIdentifer = Bundle.main.bundleIdentifier
56+
if let bundleId = bundleIdentifer, !bundleId.isEmpty {
57+
return "group." + bundleId + ".batch"
58+
}
59+
60+
throw DisplayReceiptHelperError.appGroupError
61+
}
62+
63+
func sharedDirectory() throws -> URL {
64+
do {
65+
guard let sharedDir = FileManager
66+
.default
67+
.containerURL(forSecurityApplicationGroupIdentifier: try self.sharedGroupId())?
68+
.appendingPathComponent(Consts.receiptCacheDirectory)
69+
else { throw DisplayReceiptHelperError.appGroupError }
70+
71+
try FileManager.default.createDirectory(at: sharedDir, withIntermediateDirectories: true, attributes: nil)
72+
return sharedDir
73+
} catch {
74+
throw DisplayReceiptHelperError.writeCacheError(underlyingError: error)
75+
}
76+
}
77+
78+
func sharedDefaults() throws -> UserDefaults {
79+
let groupId = try self.sharedGroupId()
80+
guard let defaults = UserDefaults.init(suiteName: groupId)
81+
else { throw DisplayReceiptHelperError.appGroupError }
82+
return defaults
83+
}
84+
}
85+
86+
87+
struct AppInformationProviderDefaultImpl: AppInformationProvider {
88+
}

Sources/Swift/Display Receipt/DisplayReceiptCacheHelper.swift

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,11 @@ import Foundation
99

1010
internal struct DisplayReceiptCacheHelper {
1111

12+
private var appInformationProvider: AppInformationProvider = AppInformationProviderDefaultImpl()
13+
1214
func makeCoordinator() -> NSFileCoordinator {
1315
return NSFileCoordinator(filePresenter: nil)
1416
}
15-
16-
func sharedGroupId() throws -> String {
17-
18-
let groupIdOverride = Bundle.main.object(forInfoDictionaryKey: "BATCH_APP_GROUP_ID")
19-
if let groupId = groupIdOverride as? String, !groupId.isEmpty {
20-
return groupId
21-
}
22-
23-
let bundleIdentifer = Bundle.main.bundleIdentifier
24-
if let bundleId = bundleIdentifer, !bundleId.isEmpty {
25-
return "group." + bundleId + ".batch"
26-
}
27-
28-
throw DisplayReceiptHelperError.appGroupError
29-
}
30-
31-
func sharedDirectory() throws -> URL {
32-
do {
33-
guard let sharedDir = FileManager
34-
.default
35-
.containerURL(forSecurityApplicationGroupIdentifier: try self.sharedGroupId())?
36-
.appendingPathComponent(Consts.receiptCacheDirectory)
37-
else { throw DisplayReceiptHelperError.appGroupError }
38-
39-
try FileManager.default.createDirectory(at: sharedDir, withIntermediateDirectories: true, attributes: nil)
40-
return sharedDir
41-
} catch {
42-
throw DisplayReceiptHelperError.writeCacheError(underlyingError: error)
43-
}
44-
}
45-
46-
func sharedDefaults() throws -> UserDefaults {
47-
let groupId = try self.sharedGroupId()
48-
guard let defaults = UserDefaults.init(suiteName: groupId)
49-
else { throw DisplayReceiptHelperError.appGroupError }
50-
return defaults
51-
}
5217

5318
// MARK: Methods updating cache files
5419

@@ -74,7 +39,7 @@ internal struct DisplayReceiptCacheHelper {
7439

7540
func write(_ data: Data) throws {
7641
do {
77-
let cacheDir = try sharedDirectory()
42+
let cacheDir = try appInformationProvider.sharedDirectory()
7843
let cacheFile = cacheDir.appendingPathComponent(newFilename())
7944

8045
try write(toFile: cacheFile, data)
@@ -119,7 +84,7 @@ internal struct DisplayReceiptCacheHelper {
11984

12085
func cachedFiles() throws -> [URL] {
12186
do {
122-
let cacheDir = try sharedDirectory()
87+
let cacheDir = try appInformationProvider.sharedDirectory()
12388
let urls = try FileManager.default.contentsOfDirectory(at: cacheDir,
12489
includingPropertiesForKeys: [.isRegularFileKey, .creationDateKey, .isReadableKey],
12590
options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
@@ -151,7 +116,7 @@ internal struct DisplayReceiptCacheHelper {
151116
// MARK: Methods reading user defaults
152117

153118
func isOptOut() throws -> Bool {
154-
let defaults = try self.sharedDefaults()
119+
let defaults = try appInformationProvider.sharedDefaults()
155120
if defaults.object(forKey: "batch_shared_optout") != nil {
156121
// Key is missing, we don't send display receipt
157122
return true

0 commit comments

Comments
 (0)