Refreshable
Refreshable copied to clipboard
A backport of the new `refreshable` modifier with support for all SwiftUI versions. (iOS, tvOS, watchOS, macOS)
This backport is now available as
Backport.Refreshable, in a single Backports library, with a LOT more additions. This should simply my efforts and allow me and others to contribute more backports in the near future. SwiftUI Backports
Refreshable
Also available as a part of my SwiftUI+ Collection – just add it to Xcode 13+
A backport of the new refreshable modifier with support for all SwiftUI versions.
This includes support for a refreshAction in the Environment as well as a convenient RefreshableView that makes it easy to build your own trigger's for any refresh action.
Note: This package does not (yet) include a pull-to-refresh like component.
Example
To avoid naming issues, the modifier is called onRefresh and to provide a familiar API for ending the refresh, the closure returns a Refresh instance that can be used (similary to presentationMode), to notify the UI that it should stop refreshing.
ScrollView {
// content
}
.onRefresh { refresh in
URLSession.shared.dataTask(with: url) { _, _, _ in
refresh.wrappedValue.end()
}
}
Then, to provide some UI that triggers the refresh:
RefreshableView { phase in
switch phase {
case let .idle(refresher, action):
Button {
refresher.perform(action)
} label: {
Text(title)
}
case .refreshing:
ProgressView()
case .notSupported:
// `onRefresh` modifier has not been added
Text("Not refreshable")
}
}
Installation
The code is packaged as a framework. You can install manually (by copying the files in the Sources directory) or using Swift Package Manager (preferred)
To install using Swift Package Manager, add this to the dependencies section of your Package.swift file:
.package(url: "https://github.com/SwiftUI-Plus/Refreshable.git", .upToNextMinor(from: "1.0.0"))
Other Packages
If you want easy access to this and more packages, add the following collection to your Xcode 13+ configuration:
https://benkau.com/packages.json