Skip to content

Commit 7ed97aa

Browse files
committed
Make reviewable
1 parent f9e340d commit 7ed97aa

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

Modules/Sources/Yosemite/Actions/BookingAction.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Networking
55
/// BookingAction: Defines all of the Actions supported by the BookingStore.
66
///
77
public enum BookingAction: Action {
8-
8+
99
/// Synchronizes the Bookings matching the specified criteria.
1010
///
1111
/// - Parameter onCompletion: called when sync completes, returns an error or a boolean that indicates whether there might be more bookings to sync.
@@ -103,4 +103,12 @@ public enum BookingAction: Action {
103103
bookingID: Int64,
104104
note: String,
105105
onCompletion: (Error?) -> Void)
106+
107+
108+
/// Clears the booking cache.
109+
///
110+
/// - Parameter siteID: The site ID of the booking.
111+
/// - Parameter onCompletion: Called when clear completes.
112+
case clearBookingsCache(siteID: Int64,
113+
onCompletion: () -> Void)
106114
}

Modules/Sources/Yosemite/Stores/BookingStore.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public class BookingStore: Store {
8989
note: note,
9090
onCompletion: onCompletion
9191
)
92+
case .clearBookingsCache(siteID: let siteID, onCompletion: let onCompletion):
93+
clearBookingsCache(siteID: siteID, onCompletion: onCompletion)
9294
}
9395
}
9496
}
@@ -205,12 +207,12 @@ private extension BookingStore {
205207
/// Returns results immediately without saving to storage.
206208
///
207209
func searchBookings(siteID: Int64,
208-
searchQuery: String,
209-
pageNumber: Int,
210-
pageSize: Int,
211-
filters: BookingFilters?,
212-
order: BookingsRemote.Order,
213-
onCompletion: @escaping (Result<[Booking], Error>) -> Void) {
210+
searchQuery: String,
211+
pageNumber: Int,
212+
pageSize: Int,
213+
filters: BookingFilters?,
214+
order: BookingsRemote.Order,
215+
onCompletion: @escaping (Result<[Booking], Error>) -> Void) {
214216
Task { @MainActor in
215217
do {
216218
let bookings = try await remote.loadAllBookings(for: siteID,
@@ -271,9 +273,9 @@ private extension BookingStore {
271273
/// Synchronizes booking resources for the specified site.
272274
///
273275
func synchronizeResources(siteID: Int64,
274-
pageNumber: Int,
275-
pageSize: Int,
276-
onCompletion: @escaping (Result<Bool, Error>) -> Void) {
276+
pageNumber: Int,
277+
pageSize: Int,
278+
onCompletion: @escaping (Result<Bool, Error>) -> Void) {
277279
Task { @MainActor in
278280
do {
279281
let resources = try await remote.fetchResources(
@@ -461,6 +463,12 @@ private extension BookingStore {
461463
}
462464
}
463465
}
466+
467+
func clearBookingsCache(siteID: Int64, onCompletion: @escaping () -> Void) {
468+
storageManager.performAndSave({ storage in
469+
storage.deleteBookings(siteID: siteID)
470+
}, completion: onCompletion, on: .main)
471+
}
464472
}
465473

466474

WooCommerce/Classes/Bookings/BookingList/BookingListContainerViewModel.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ final class BookingListContainerViewModel: ObservableObject {
77
private let siteID: Int64
88
private let stores: StoresManager
99

10+
private lazy var allTabViewModels: [BookingListViewModel] = [
11+
todayListViewModel,
12+
upcomingListViewModel,
13+
allListViewModel
14+
]
1015
private let todayListViewModel: BookingListViewModel
1116
private let upcomingListViewModel: BookingListViewModel
1217
private let allListViewModel: BookingListViewModel
@@ -89,11 +94,25 @@ final class BookingListContainerViewModel: ObservableObject {
8994
restorePersistedFilters()
9095
}
9196

92-
func pullToRefresh() async {
93-
async let today = todayListViewModel.onRefreshAction()
94-
async let upcoming = upcomingListViewModel.onRefreshAction(reason: "pull-to-refresh")
95-
async let all = allListViewModel.onRefreshAction(reason: "pull-to-refresh")
96-
_ = await (today, upcoming, all)
97+
@MainActor
98+
func pullToRefresh(on tab: BookingListTab) async {
99+
await withCheckedContinuation { continuation in
100+
let action = BookingAction.clearBookingsCache(siteID: siteID) {
101+
continuation.resume()
102+
}
103+
stores.dispatch(action)
104+
}
105+
106+
// Launch all tab refreshes in parallel and wait for all to complete
107+
await withTaskGroup(of: Void.self) { group in
108+
for viewModel in allTabViewModels {
109+
group.addTask { @MainActor in
110+
await viewModel.onRefreshSelfAction(
111+
reason: BookingListViewModel.siblingRefreshReason
112+
)
113+
}
114+
}
115+
}
97116
}
98117

99118
func listViewModel(for tab: BookingListTab) -> BookingListViewModel {

WooCommerce/Classes/Bookings/BookingList/BookingListView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ private extension BookingListView {
4343
switch viewModel.syncState {
4444
case .empty:
4545
emptyStateView(isSearching: false) {
46-
await viewModel.onRefreshAllAction()
46+
await viewModel.onRefreshAction()
4747
}
4848
case .syncingFirstPage:
4949
loadingView
5050
case .results:
5151
bookingList(with: viewModel.bookings,
5252
onNextPage: { viewModel.onLoadNextPageAction() },
53-
onRefresh: { await viewModel.onRefreshAllAction() })
53+
onRefresh: { await viewModel.onRefreshAction() })
5454
}
5555
}
5656
.overlay(alignment: .bottom) {

WooCommerce/Classes/Bookings/BookingList/BookingListViewModel.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class BookingListViewModel: ObservableObject {
3434

3535
private static let refreshCacheReason = "refresh-cache"
3636
private static let reorderReason = "reorder"
37+
static let siblingRefreshReason = "sibling-refresh"
3738

3839
/// Keeps track of the current state of the syncing
3940
@Published private(set) var syncState: SyncState = .empty
@@ -98,13 +99,13 @@ final class BookingListViewModel: ObservableObject {
9899
paginationTracker.ensureNextPageIsSynced()
99100
}
100101

101-
func onRefreshAllAction() async {
102-
await parent?.pullToRefresh()
102+
/// Called when the user pulls down the list to refresh.
103+
func onRefreshAction() async {
104+
await parent?.pullToRefresh(on: type)
103105
}
104106

105-
/// Called when the user pulls down the list to refresh.
106107
@MainActor
107-
func onRefreshAction2(reason: String? = nil) async {
108+
func onRefreshSelfAction(reason: String? = nil) async {
108109
await withCheckedContinuation { continuation in
109110
paginationTracker.resync(reason: reason ?? Self.refreshCacheReason) {
110111
continuation.resume(returning: ())

0 commit comments

Comments
 (0)