Refresh homeview automatically
This commit is contained in:
parent
4625e82b83
commit
188e09c22c
|
@ -11,9 +11,10 @@ import UIKit
|
||||||
|
|
||||||
// A more general derivative of
|
// A more general derivative of
|
||||||
// https://stackoverflow.com/questions/65812080/introspect-library-uirefreshcontrol-with-swiftui-not-working
|
// https://stackoverflow.com/questions/65812080/introspect-library-uirefreshcontrol-with-swiftui-not-working
|
||||||
class RefreshHelper {
|
final class RefreshHelper {
|
||||||
var refreshControl: UIRefreshControl?
|
var refreshControl: UIRefreshControl?
|
||||||
var refreshAction: (() -> Void)?
|
var refreshAction: (() -> Void)?
|
||||||
|
private var lastAutomaticRefresh = Date()
|
||||||
|
|
||||||
@objc func didRefresh() {
|
@objc func didRefresh() {
|
||||||
guard let refreshControl = refreshControl else { return }
|
guard let refreshControl = refreshControl else { return }
|
||||||
|
@ -21,3 +22,19 @@ class RefreshHelper {
|
||||||
refreshControl.endRefreshing()
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -119,5 +119,8 @@ struct HomeView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onAppear {
|
||||||
|
refreshHelper.refreshStaleData()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue