Skip to content

Commit 8d29a83

Browse files
committed
Fix usage of refreshable
1 parent 077adbf commit 8d29a83

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

Modules/Sources/Yosemite/Stores/BookingStore.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ private extension BookingStore {
5757
let bookings = try await remote.loadAllBookings(for: siteID,
5858
pageNumber: pageNumber,
5959
pageSize: pageSize)
60-
await upsertStoredBookingsInBackground(readOnlyBookings: bookings, siteID: siteID)
60+
let shouldDeleteExistingBookings = pageNumber == Default.firstPageNumber
61+
await upsertStoredBookingsInBackground(
62+
readOnlyBookings: bookings,
63+
siteID: siteID,
64+
shouldDeleteExistingBookings: shouldDeleteExistingBookings
65+
)
6166
let hasNextPage = bookings.count == pageSize
6267
onCompletion(.success(hasNextPage))
6368
} catch {

WooCommerce/Classes/Bookings/BookingList/BookingListView.swift

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ struct BookingListView: View {
55
@ObservedObject private var viewModel: BookingListViewModel
66
@State private var selectedTabIndex = 0
77

8+
@Namespace var topID
9+
810
private let tabs = [Localization.today, Localization.upcoming, Localization.all]
911

1012
init(viewModel: BookingListViewModel) {
@@ -28,6 +30,7 @@ struct BookingListView: View {
2830
}
2931
}
3032
.navigationTitle(Localization.viewTitle)
33+
.toolbarBackground(Color(.listForeground(modal: false)), for: .navigationBar)
3134
.toolbar {
3235
ToolbarItem(placement: .confirmationAction) {
3336
Button {
@@ -99,26 +102,30 @@ private extension BookingListView {
99102
}
100103

101104
var bookingList: some View {
102-
ScrollView {
103-
LazyVStack(spacing: 0, pinnedViews: .sectionHeaders) {
104-
Section {
105-
ForEach(viewModel.bookings) { item in
106-
bookingItem(item)
105+
ScrollViewReader { proxy in
106+
ScrollView {
107+
LazyVStack(spacing: 0, pinnedViews: .sectionHeaders) {
108+
Section {
109+
ForEach(viewModel.bookings) { item in
110+
bookingItem(item)
111+
}
112+
} header: {
113+
headerView
107114
}
108-
} header: {
109-
headerView
115+
.id(topID)
116+
117+
InfiniteScrollIndicator(showContent: viewModel.shouldShowBottomActivityIndicator)
118+
.padding(.top, Layout.viewPadding)
119+
.onAppear {
120+
viewModel.onLoadNextPageAction()
121+
}
110122
}
111-
InfiniteScrollIndicator(showContent: viewModel.shouldShowBottomActivityIndicator)
112-
.padding(.top, Layout.viewPadding)
113-
.onAppear {
114-
viewModel.onLoadNextPageAction()
115-
}
116123
}
117-
}
118-
.refreshable {
119-
viewModel.onRefreshAction(completion: {
120-
// TODO: show/hide ghost animation if needed
121-
})
124+
.refreshable {
125+
await viewModel.onRefreshAction()
126+
// workaround as navigation bar is not snapped back after refreshing
127+
proxy.scrollTo(topID, anchor: .top)
128+
}
122129
}
123130
}
124131

WooCommerce/Classes/Bookings/BookingList/BookingListViewModel.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ final class BookingListViewModel: ObservableObject {
5555
}
5656

5757
/// Called when the user pulls down the list to refresh.
58-
/// - Parameter completion: called when the refresh completes.
59-
func onRefreshAction(completion: @escaping () -> Void) {
60-
paginationTracker.resync(reason: nil) {
61-
completion()
58+
@MainActor
59+
func onRefreshAction() async {
60+
await withCheckedContinuation { continuation in
61+
paginationTracker.resync(reason: nil) {
62+
continuation.resume(returning: ())
63+
}
6264
}
6365
}
6466
}

0 commit comments

Comments
 (0)