Merge branch 'main' into tvos-home-screen-big-view

This commit is contained in:
Ethan Pippin 2022-01-09 18:24:05 -07:00
commit 3a5f064952
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

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