Skip to content

Commit d128552

Browse files
committed
Add storage for catalog cellular data setting
1 parent 84c555d commit d128552

File tree

9 files changed

+203
-5
lines changed

9 files changed

+203
-5
lines changed

Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ extension Networking.Booking {
436436
allDay: CopiableProp<Bool> = .copy,
437437
cost: CopiableProp<String> = .copy,
438438
customerID: CopiableProp<Int64> = .copy,
439-
dateCreated: CopiableProp<Date> = .copy,
440-
dateModified: CopiableProp<Date> = .copy,
439+
dateCreated: NullableCopiableProp<Date> = .copy,
440+
dateModified: NullableCopiableProp<Date> = .copy,
441441
endDate: CopiableProp<Date> = .copy,
442442
googleCalendarEventID: NullableCopiableProp<String> = .copy,
443443
orderID: CopiableProp<Int64> = .copy,

Modules/Sources/Storage/Model/Copiable/Models+Copiable.generated.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ extension Storage.GeneralStoreSettings {
127127
searchTermsByKey: CopiableProp<[String: [String]]> = .copy,
128128
isPOSTabVisible: NullableCopiableProp<Bool> = .copy,
129129
lastPOSOpenedDate: NullableCopiableProp<Date> = .copy,
130-
firstPOSCatalogSyncDate: NullableCopiableProp<Date> = .copy
130+
firstPOSCatalogSyncDate: NullableCopiableProp<Date> = .copy,
131+
syncPOSCatalogOverCellular: CopiableProp<Bool> = .copy
131132
) -> Storage.GeneralStoreSettings {
132133
let storeID = storeID ?? self.storeID
133134
let isTelemetryAvailable = isTelemetryAvailable ?? self.isTelemetryAvailable
@@ -151,6 +152,7 @@ extension Storage.GeneralStoreSettings {
151152
let isPOSTabVisible = isPOSTabVisible ?? self.isPOSTabVisible
152153
let lastPOSOpenedDate = lastPOSOpenedDate ?? self.lastPOSOpenedDate
153154
let firstPOSCatalogSyncDate = firstPOSCatalogSyncDate ?? self.firstPOSCatalogSyncDate
155+
let syncPOSCatalogOverCellular = syncPOSCatalogOverCellular ?? self.syncPOSCatalogOverCellular
154156

155157
return Storage.GeneralStoreSettings(
156158
storeID: storeID,
@@ -174,7 +176,8 @@ extension Storage.GeneralStoreSettings {
174176
searchTermsByKey: searchTermsByKey,
175177
isPOSTabVisible: isPOSTabVisible,
176178
lastPOSOpenedDate: lastPOSOpenedDate,
177-
firstPOSCatalogSyncDate: firstPOSCatalogSyncDate
179+
firstPOSCatalogSyncDate: firstPOSCatalogSyncDate,
180+
syncPOSCatalogOverCellular: syncPOSCatalogOverCellular
178181
)
179182
}
180183
}

Modules/Sources/Storage/Model/GeneralStoreSettings.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public struct GeneralStoreSettings: Codable, Equatable, GeneratedCopiable {
9494
///
9595
public var firstPOSCatalogSyncDate: Date?
9696

97+
/// Whether we should sync catalog data over cellular connections for this store
98+
///
99+
public var syncPOSCatalogOverCellular: Bool
100+
97101
public init(storeID: String? = nil,
98102
isTelemetryAvailable: Bool = false,
99103
telemetryLastReportedTime: Date? = nil,
@@ -115,7 +119,8 @@ public struct GeneralStoreSettings: Codable, Equatable, GeneratedCopiable {
115119
searchTermsByKey: [String: [String]] = [:],
116120
isPOSTabVisible: Bool? = nil,
117121
lastPOSOpenedDate: Date? = nil,
118-
firstPOSCatalogSyncDate: Date? = nil) {
122+
firstPOSCatalogSyncDate: Date? = nil,
123+
syncPOSCatalogOverCellular: Bool = true) {
119124
self.storeID = storeID
120125
self.isTelemetryAvailable = isTelemetryAvailable
121126
self.telemetryLastReportedTime = telemetryLastReportedTime
@@ -138,6 +143,7 @@ public struct GeneralStoreSettings: Codable, Equatable, GeneratedCopiable {
138143
self.isPOSTabVisible = isPOSTabVisible
139144
self.lastPOSOpenedDate = lastPOSOpenedDate
140145
self.firstPOSCatalogSyncDate = firstPOSCatalogSyncDate
146+
self.syncPOSCatalogOverCellular = syncPOSCatalogOverCellular
141147
}
142148

143149
public func erasingSelectedTaxRateID() -> GeneralStoreSettings {
@@ -198,6 +204,7 @@ extension GeneralStoreSettings {
198204
self.isPOSTabVisible = try container.decodeIfPresent(Bool.self, forKey: .isPOSTabVisible)
199205
self.lastPOSOpenedDate = try container.decodeIfPresent(Date.self, forKey: .lastPOSOpenedDate)
200206
self.firstPOSCatalogSyncDate = try container.decodeIfPresent(Date.self, forKey: .firstPOSCatalogSyncDate)
207+
self.syncPOSCatalogOverCellular = try container.decodeIfPresent(Bool.self, forKey: .syncPOSCatalogOverCellular) ?? true
201208

202209
// Decode new properties with `decodeIfPresent` and provide a default value if necessary.
203210
}

Modules/Sources/Yosemite/Actions/AppSettingsAction.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,11 @@ public enum AppSettingsAction: Action {
414414
/// Gets the date of the first POS catalog sync for a specific site
415415
///
416416
case getFirstPOSCatalogSyncDate(siteID: Int64, onCompletion: (Date?) -> Void)
417+
418+
/// Sets whether we should allow cellular data use downloading POS catalogs for a specific site
419+
///
420+
case setPOSLocalCatalogCellularDataAllowed(siteID: Int64, allowed: Bool, onCompletion: () -> Void)
421+
422+
/// Gets whether we should allow cellular data use downloading POS catalogs for a specific site
423+
case getPOSLocalCatalogCellularDataAllowed(siteID: Int64, onCompletion: (Bool) -> Void)
417424
}

Modules/Sources/Yosemite/Stores/AppSettingsStore.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ public class AppSettingsStore: Store {
315315
setFirstPOSCatalogSyncDate(siteID: siteID, date: date, onCompletion: onCompletion)
316316
case .getFirstPOSCatalogSyncDate(siteID: let siteID, onCompletion: let onCompletion):
317317
getFirstPOSCatalogSyncDate(siteID: siteID, onCompletion: onCompletion)
318+
case .setPOSLocalCatalogCellularDataAllowed(let siteID, let allowed, let onCompletion):
319+
setPOSLocalCatalogCellularDataAllowed(siteID: siteID, allowed: allowed, onCompletion: onCompletion)
320+
case .getPOSLocalCatalogCellularDataAllowed(let siteID, let onCompletion):
321+
getPOSLocalCatalogCellularDataAllowed(siteID: siteID, onCompletion: onCompletion)
318322
}
319323
}
320324
}
@@ -1365,7 +1369,10 @@ private extension AppSettingsStore {
13651369
onCompletion(.failure(error))
13661370
}
13671371
}
1372+
}
13681373

1374+
// MARK: - Point of Sale local catalog settings
1375+
private extension AppSettingsStore {
13691376
func setPOSLastOpenedDate(siteID: Int64, date: Date, onCompletion: () -> Void) {
13701377
siteSpecificAppSettingsStoreMethods.setPOSLastOpenedDate(siteID: siteID, date: date)
13711378
onCompletion()
@@ -1385,6 +1392,16 @@ private extension AppSettingsStore {
13851392
let date = siteSpecificAppSettingsStoreMethods.getFirstPOSCatalogSyncDate(siteID: siteID)
13861393
onCompletion(date)
13871394
}
1395+
1396+
func setPOSLocalCatalogCellularDataAllowed(siteID: Int64, allowed: Bool, onCompletion: () -> Void) {
1397+
siteSpecificAppSettingsStoreMethods.setPOSLocalCatalogCellularDataAllowed(siteID: siteID, allowed: allowed)
1398+
onCompletion()
1399+
}
1400+
1401+
func getPOSLocalCatalogCellularDataAllowed(siteID: Int64, onCompletion: (Bool) -> Void) {
1402+
let allowed = siteSpecificAppSettingsStoreMethods.getPOSLocalCatalogCellularDataAllowed(siteID: siteID)
1403+
onCompletion(allowed)
1404+
}
13881405
}
13891406

13901407
// MARK: - Errors

Modules/Sources/Yosemite/Stores/Helpers/SiteSpecificAppSettingsStoreMethods.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public protocol SiteSpecificAppSettingsStoreMethodsProtocol {
1717
func setPOSLastOpenedDate(siteID: Int64, date: Date)
1818
func getFirstPOSCatalogSyncDate(siteID: Int64) -> Date?
1919
func setFirstPOSCatalogSyncDate(siteID: Int64, date: Date)
20+
21+
// POS local catalog cellular data
22+
func setPOSLocalCatalogCellularDataAllowed(siteID: Int64, allowed: Bool)
23+
func getPOSLocalCatalogCellularDataAllowed(siteID: Int64) -> Bool
2024
}
2125

2226
/// Methods for managing site-specific app settings
@@ -102,7 +106,10 @@ extension SiteSpecificAppSettingsStoreMethods {
102106
let updatedSettings = storeSettings.copy(searchTermsByKey: updatedSearchTermsByKey)
103107
setStoreSettings(settings: updatedSettings, for: siteID)
104108
}
109+
}
105110

111+
// MARK: - POS sync eligibility tracking
112+
extension SiteSpecificAppSettingsStoreMethods{
106113
func getPOSLastOpenedDate(siteID: Int64) -> Date? {
107114
getStoreSettings(for: siteID).lastPOSOpenedDate
108115
}
@@ -124,6 +131,19 @@ extension SiteSpecificAppSettingsStoreMethods {
124131
}
125132
}
126133

134+
// MARK: - POS local catalog cellular data
135+
extension SiteSpecificAppSettingsStoreMethods {
136+
func setPOSLocalCatalogCellularDataAllowed(siteID: Int64, allowed: Bool) {
137+
let storeSettings = getStoreSettings(for: siteID)
138+
let updatedSettings = storeSettings.copy(syncPOSCatalogOverCellular: allowed)
139+
setStoreSettings(settings: updatedSettings, for: siteID)
140+
}
141+
142+
func getPOSLocalCatalogCellularDataAllowed(siteID: Int64) -> Bool {
143+
getStoreSettings(for: siteID).syncPOSCatalogOverCellular
144+
}
145+
}
146+
127147
// MARK: - Constants
128148
private enum Constants {
129149
static let generalStoreSettingsFileName = "general-store-settings.plist"

Modules/Tests/YosemiteTests/Mocks/MockSiteSpecificAppSettingsStoreMethods.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ final class MockSiteSpecificAppSettingsStoreMethods: SiteSpecificAppSettingsStor
3535
var setFirstPOSCatalogSyncDateCalled = false
3636
var mockPOSLastOpenedDate: Date?
3737
var mockFirstPOSCatalogSyncDate: Date?
38+
var getPOSLocalCatalogCellularDataAllowedCalled = false
39+
var setPOSLocalCatalogCellularDataAllowedCalled = false
40+
var mockPOSLocalCatalogCellularDataAllowed: Bool?
3841

3942

4043
func getStoreSettings(for siteID: Int64) -> GeneralStoreSettings {
@@ -114,4 +117,14 @@ final class MockSiteSpecificAppSettingsStoreMethods: SiteSpecificAppSettingsStor
114117
setFirstPOSCatalogSyncDateCalled = true
115118
mockFirstPOSCatalogSyncDate = date
116119
}
120+
121+
func setPOSLocalCatalogCellularDataAllowed(siteID: Int64, allowed: Bool) {
122+
setPOSLocalCatalogCellularDataAllowedCalled = true
123+
mockPOSLocalCatalogCellularDataAllowed = allowed
124+
}
125+
126+
func getPOSLocalCatalogCellularDataAllowed(siteID: Int64) -> Bool {
127+
getPOSLocalCatalogCellularDataAllowedCalled = true
128+
return mockPOSLocalCatalogCellularDataAllowed ?? false
129+
}
117130
}

Modules/Tests/YosemiteTests/Stores/AppSettingsStoreTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,69 @@ extension AppSettingsStoreTests {
17361736
}
17371737
XCTAssertFalse(checkPOSOpenedAfterReset)
17381738
}
1739+
1740+
// MARK: - POS Local Catalog Cellular Data Tests
1741+
1742+
func test_getPOSLocalCatalogCellularDataAllowed_returns_false_by_default() throws {
1743+
// When
1744+
let isAllowed: Bool = waitFor { promise in
1745+
let action = AppSettingsAction.getPOSLocalCatalogCellularDataAllowed(siteID: TestConstants.siteID) { isAllowed in
1746+
promise(isAllowed)
1747+
}
1748+
self.subject?.onAction(action)
1749+
}
1750+
1751+
// Then
1752+
XCTAssertFalse(isAllowed)
1753+
}
1754+
1755+
func test_getPOSLocalCatalogCellularDataAllowed_returns_saved_value() throws {
1756+
// Given
1757+
mockSiteSpecificAppSettingsStoreMethods.mockPOSLocalCatalogCellularDataAllowed = true
1758+
1759+
// When
1760+
let isAllowed: Bool = waitFor { promise in
1761+
let action = AppSettingsAction.getPOSLocalCatalogCellularDataAllowed(siteID: TestConstants.siteID) { isAllowed in
1762+
promise(isAllowed)
1763+
}
1764+
self.subject?.onAction(action)
1765+
}
1766+
1767+
// Then
1768+
XCTAssertTrue(mockSiteSpecificAppSettingsStoreMethods.getPOSLocalCatalogCellularDataAllowedCalled)
1769+
XCTAssertTrue(isAllowed)
1770+
}
1771+
1772+
func test_setPOSLocalCatalogCellularDataAllowed_saves_value_successfully() throws {
1773+
// When
1774+
waitFor { promise in
1775+
let action = AppSettingsAction.setPOSLocalCatalogCellularDataAllowed(siteID: TestConstants.siteID, allowed: true) {
1776+
promise(())
1777+
}
1778+
self.subject?.onAction(action)
1779+
}
1780+
1781+
// Then
1782+
XCTAssertTrue(mockSiteSpecificAppSettingsStoreMethods.setPOSLocalCatalogCellularDataAllowedCalled)
1783+
XCTAssertEqual(mockSiteSpecificAppSettingsStoreMethods.mockPOSLocalCatalogCellularDataAllowed, true)
1784+
}
1785+
1786+
func test_setPOSLocalCatalogCellularDataAllowed_can_set_false() throws {
1787+
// Given
1788+
mockSiteSpecificAppSettingsStoreMethods.mockPOSLocalCatalogCellularDataAllowed = true
1789+
1790+
// When
1791+
waitFor { promise in
1792+
let action = AppSettingsAction.setPOSLocalCatalogCellularDataAllowed(siteID: TestConstants.siteID, allowed: false) {
1793+
promise(())
1794+
}
1795+
self.subject?.onAction(action)
1796+
}
1797+
1798+
// Then
1799+
XCTAssertTrue(mockSiteSpecificAppSettingsStoreMethods.setPOSLocalCatalogCellularDataAllowedCalled)
1800+
XCTAssertEqual(mockSiteSpecificAppSettingsStoreMethods.mockPOSLocalCatalogCellularDataAllowed, false)
1801+
}
17391802
}
17401803

17411804
// MARK: - Utils

Modules/Tests/YosemiteTests/Stores/Helpers/SiteSpecificAppSettingsStoreMethodsTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,74 @@ struct SiteSpecificAppSettingsStoreMethodsTests {
230230
#expect(retrievedCouponTerms == couponTerms)
231231
}
232232

233+
// MARK: - POS Local Catalog Cellular Data Tests
234+
235+
@Test func getPOSLocalCatalogCellularDataAllowed_returns_true_by_default() {
236+
// When
237+
let isAllowed = sut.getPOSLocalCatalogCellularDataAllowed(siteID: siteID)
238+
239+
// Then
240+
#expect(isAllowed == true)
241+
}
242+
243+
@Test func getPOSLocalCatalogCellularDataAllowed_returns_saved_value() throws {
244+
// Given
245+
let storeSettings = GeneralStoreSettings(syncPOSCatalogOverCellular: false)
246+
let existingData = GeneralStoreSettingsBySite(storeSettingsBySite: [siteID: storeSettings])
247+
try fileStorage.write(existingData, to: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
248+
249+
// When
250+
let isAllowed = sut.getPOSLocalCatalogCellularDataAllowed(siteID: siteID)
251+
252+
// Then
253+
#expect(isAllowed == false)
254+
}
255+
256+
@Test func setPOSLocalCatalogCellularDataAllowed_saves_value_successfully() throws {
257+
// Given
258+
let existingSettings = GeneralStoreSettingsBySite(storeSettingsBySite: [siteID: GeneralStoreSettings()])
259+
try fileStorage.write(existingSettings, to: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
260+
261+
// When
262+
sut.setPOSLocalCatalogCellularDataAllowed(siteID: siteID, allowed: false)
263+
264+
// Then
265+
let savedData: GeneralStoreSettingsBySite = try fileStorage.data(for: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
266+
#expect(savedData.storeSettingsBySite[siteID]?.syncPOSCatalogOverCellular == false)
267+
}
268+
269+
@Test func setPOSLocalCatalogCellularDataAllowed_preserves_existing_settings() throws {
270+
// Given
271+
let existingStoreID = "existing-store-id"
272+
let existingSettings = GeneralStoreSettings(storeID: existingStoreID)
273+
let existingData = GeneralStoreSettingsBySite(storeSettingsBySite: [siteID: existingSettings])
274+
try fileStorage.write(existingData, to: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
275+
276+
// When
277+
sut.setPOSLocalCatalogCellularDataAllowed(siteID: siteID, allowed: false)
278+
279+
// Then
280+
let savedData: GeneralStoreSettingsBySite = try fileStorage.data(for: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
281+
#expect(savedData.storeSettingsBySite[siteID]?.syncPOSCatalogOverCellular == false)
282+
#expect(savedData.storeSettingsBySite[siteID]?.storeID == existingStoreID)
283+
}
284+
285+
@Test func setPOSLocalCatalogCellularDataAllowed_preserves_settings_for_other_sites() throws {
286+
// Given
287+
let otherSiteID: Int64 = 456
288+
let otherSiteSettings = GeneralStoreSettings(syncPOSCatalogOverCellular: true)
289+
let existingData = GeneralStoreSettingsBySite(storeSettingsBySite: [otherSiteID: otherSiteSettings])
290+
try fileStorage.write(existingData, to: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
291+
292+
// When
293+
sut.setPOSLocalCatalogCellularDataAllowed(siteID: siteID, allowed: false)
294+
295+
// Then
296+
let savedData: GeneralStoreSettingsBySite = try fileStorage.data(for: SiteSpecificAppSettingsStoreMethods.defaultGeneralStoreSettingsFileURL)
297+
#expect(savedData.storeSettingsBySite[siteID]?.syncPOSCatalogOverCellular == false)
298+
#expect(savedData.storeSettingsBySite[otherSiteID]?.syncPOSCatalogOverCellular == true)
299+
}
300+
233301
}
234302

235303
// MARK: - Mock FileStorage

0 commit comments

Comments
 (0)