Refresh homeview automatically

This commit is contained in:
koen 2022-01-09 18:48:31 +01:00
parent 4625e82b83
commit 188e09c22c
2 changed files with 21 additions and 1 deletions

View File

@ -11,9 +11,10 @@ import UIKit
// A more general derivative of
// https://stackoverflow.com/questions/65812080/introspect-library-uirefreshcontrol-with-swiftui-not-working
class RefreshHelper {
final class RefreshHelper {
var refreshControl: UIRefreshControl?
var refreshAction: (() -> Void)?
private var lastAutomaticRefresh = Date()
@objc func didRefresh() {
guard let refreshControl = refreshControl else { return }
@ -21,3 +22,19 @@ class RefreshHelper {
refreshControl.endRefreshing()
}
}
// MARK: - automatic refreshing
extension RefreshHelper {
private static let timeUntilStale = TimeInterval(60)
func refreshStaleData() {
guard isStale else { return }
lastAutomaticRefresh = .now
refreshAction?()
}
private var isStale: Bool {
lastAutomaticRefresh.addingTimeInterval(Self.timeUntilStale) < .now
}
}

View File

@ -119,5 +119,8 @@ struct HomeView: View {
}
}
}
.onAppear {
refreshHelper.refreshStaleData()
}
}
}