add pull to refresh for ipad grid view

This commit is contained in:
Satindar Dhillon
2022-03-05 08:35:22 -08:00
parent 9bdc96c567
commit 1ca25c87bd

View File

@ -329,9 +329,21 @@ import Views
}
}
.padding()
.background(Color(.systemGroupedBackground))
.background(
GeometryReader {
Color(.systemGroupedBackground).preference(
key: ScrollViewOffsetPreferenceKey.self,
value: $0.frame(in: .global).origin.y
)
}
)
.onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { offset in
if !viewModel.isLoading, abs(offset) > 240 {
viewModel.loadItems(dataService: dataService, searchQuery: searchQuery, isRefresh: true)
}
}
if viewModel.isLoading {
if viewModel.items.isEmpty, viewModel.isLoading {
LoadingSection()
}
}
@ -339,3 +351,11 @@ import Views
}
#endif
struct ScrollViewOffsetPreferenceKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue = CGFloat.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value += nextValue()
}
}