Skip to content

Commit 6a6d0d0

Browse files
committed
Make synchronizeResources return Bool instead of Void
1 parent 76449d5 commit 6a6d0d0

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

Modules/Sources/Storage/Tools/StorageType+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ public extension StorageType {
973973
let descriptor = NSSortDescriptor(keyPath: \BookingResource.resourceID, ascending: false)
974974
return allObjects(ofType: BookingResource.self, matching: predicate, sortedBy: [descriptor])
975975
}
976-
976+
977977
/// Retrieves the store booking resource
978978
func loadBookingResource(siteID: Int64, resourceID: Int64) -> BookingResource? {
979979
let predicate = \BookingResource.resourceID == resourceID && \BookingResource.siteID == siteID

Modules/Sources/Yosemite/Actions/BookingAction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public enum BookingAction: Action {
5555

5656
/// Synchronizes booking resources matching the specified criteria.
5757
///
58-
/// - Parameter onCompletion: called when sync completes, returns an error in case of a failure or empty in case of success.
58+
/// - Parameter onCompletion: called when sync completes, returns an error or a boolean that indicates whether there might be more resources to sync.
5959
///
6060
case synchronizeResources(siteID: Int64,
6161
pageNumber: Int,
6262
pageSize: Int = BookingsRemote.Default.pageSize,
63-
onCompletion: (Result<Void, Error>) -> Void)
63+
onCompletion: (Result<Bool, Error>) -> Void)
6464

6565
/// Updates a booking attendance status.
6666
///

Modules/Sources/Yosemite/Model/Model.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ public typealias StorageBlazeTargetLanguage = Storage.BlazeTargetLanguage
277277
public typealias StorageBlazeTargetTopic = Storage.BlazeTargetTopic
278278
// periphery: ignore
279279
public typealias StorageBooking = Storage.Booking
280+
public typealias StorageBookingResource = Storage.BookingResource
280281
public typealias StorageCardReaderType = Storage.CardReaderType
281282
public typealias StorageCoupon = Storage.Coupon
282283
public typealias StorageCustomer = Storage.Customer

Modules/Sources/Yosemite/Stores/BookingStore.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private extension BookingStore {
261261
func synchronizeResources(siteID: Int64,
262262
pageNumber: Int,
263263
pageSize: Int,
264-
onCompletion: @escaping (Result<Void, Error>) -> Void) {
264+
onCompletion: @escaping (Result<Bool, Error>) -> Void) {
265265
Task { @MainActor in
266266
do {
267267
let resources = try await remote.fetchResources(
@@ -273,7 +273,8 @@ private extension BookingStore {
273273
await upsertBookingResourcesInBackground(siteID: siteID,
274274
readOnlyBookingResources: resources)
275275

276-
onCompletion(.success(()))
276+
let hasNextPage = resources.count == pageSize
277+
onCompletion(.success(hasNextPage))
277278
} catch {
278279
onCompletion(.failure(error))
279280
}

Modules/Tests/YosemiteTests/Stores/BookingStoreTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,55 @@ struct BookingStoreTests {
626626

627627
// MARK: - synchronizeResources
628628

629+
@Test func synchronizeResources_returns_false_for_hasNextPage_when_number_of_retrieved_results_is_zero() async throws {
630+
// Given
631+
remote.whenFetchingResources(thenReturn: .success([]))
632+
let store = BookingStore(dispatcher: Dispatcher(),
633+
storageManager: storageManager,
634+
network: network,
635+
remote: remote,
636+
ordersRemote: ordersRemote)
637+
638+
// When
639+
let result = await withCheckedContinuation { continuation in
640+
store.onAction(BookingAction.synchronizeResources(siteID: sampleSiteID,
641+
pageNumber: defaultPageNumber,
642+
pageSize: defaultPageSize,
643+
onCompletion: { result in
644+
continuation.resume(returning: result)
645+
}))
646+
}
647+
648+
// Then
649+
let hasNextPage = try result.get()
650+
#expect(hasNextPage == false)
651+
}
652+
653+
@Test func synchronizeResources_returns_true_for_hasNextPage_when_number_of_retrieved_results_equals_pageSize() async throws {
654+
// Given
655+
let resources = Array(repeating: BookingResource.fake(), count: defaultPageSize)
656+
remote.whenFetchingResources(thenReturn: .success(resources))
657+
let store = BookingStore(dispatcher: Dispatcher(),
658+
storageManager: storageManager,
659+
network: network,
660+
remote: remote,
661+
ordersRemote: ordersRemote)
662+
663+
// When
664+
let result = await withCheckedContinuation { continuation in
665+
store.onAction(BookingAction.synchronizeResources(siteID: sampleSiteID,
666+
pageNumber: defaultPageNumber,
667+
pageSize: defaultPageSize,
668+
onCompletion: { result in
669+
continuation.resume(returning: result)
670+
}))
671+
}
672+
673+
// Then
674+
let hasNextPage = try result.get()
675+
#expect(hasNextPage == true)
676+
}
677+
629678
@Test func synchronizeResources_stores_resources_upon_success() async throws {
630679
// Given
631680
let resource = BookingResource.fake().copy(siteID: sampleSiteID, resourceID: 123)

0 commit comments

Comments
 (0)