From 1ca25c87bd3e2954d4aadc5a6c1758e9cd105e01 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sat, 5 Mar 2022 08:35:22 -0800 Subject: [PATCH] add pull to refresh for ipad grid view --- .../App/Views/Home/HomeFeedViewIOS.swift | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index f4a82b029..43126d025 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -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() + } +}