├── .gitignore
├── .swiftpm
└── xcode
│ └── package.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ └── IDEWorkspaceChecks.plist
├── LICENSE
├── Package.swift
├── README.md
├── Sources
└── SwiftUIPullToRefresh
│ └── SwiftUIPullToRefresh.swift
└── SwiftUI-Pull-To-Refresh.podspec
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## User settings
6 | xcuserdata/
7 |
8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
9 | *.xcscmblueprint
10 | *.xccheckout
11 |
12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
13 | build/
14 | DerivedData/
15 | *.moved-aside
16 | *.pbxuser
17 | !default.pbxuser
18 | *.mode1v3
19 | !default.mode1v3
20 | *.mode2v3
21 | !default.mode2v3
22 | *.perspectivev3
23 | !default.perspectivev3
24 |
25 | ## Obj-C/Swift specific
26 | *.hmap
27 |
28 | ## App packaging
29 | *.ipa
30 | *.dSYM.zip
31 | *.dSYM
32 |
33 | ## Playgrounds
34 | timeline.xctimeline
35 | playground.xcworkspace
36 |
37 | # Swift Package Manager
38 | #
39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
40 | # Packages/
41 | # Package.pins
42 | # Package.resolved
43 | # *.xcodeproj
44 | #
45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
46 | # hence it is not needed unless you have added a package configuration file to your project
47 | # .swiftpm
48 |
49 | .build/
50 |
51 | # CocoaPods
52 | #
53 | # We recommend against adding the Pods directory to your .gitignore. However
54 | # you should judge for yourself, the pros and cons are mentioned at:
55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
56 | #
57 | # Pods/
58 | #
59 | # Add this line if you want to avoid checking in source code from the Xcode workspace
60 | # *.xcworkspace
61 |
62 | # Carthage
63 | #
64 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
65 | # Carthage/Checkouts
66 |
67 | Carthage/Build/
68 |
69 | # Accio dependency management
70 | Dependencies/
71 | .accio/
72 |
73 | # fastlane
74 | #
75 | # It is recommended to not store the screenshots in the git repo.
76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed.
77 | # For more information about the recommended setup visit:
78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control
79 |
80 | fastlane/report.xml
81 | fastlane/Preview.html
82 | fastlane/screenshots/**/*.png
83 | fastlane/test_output
84 |
85 | # Code Injection
86 | #
87 | # After new code Injection tools there's a generated folder /iOSInjectionProject
88 | # https://github.com/johnno1962/injectionforxcode
89 |
90 | iOSInjectionProject/
91 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Gordan Glavaš
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:5.3
2 | // The swift-tools-version declares the minimum version of Swift required to build this package.
3 |
4 | import PackageDescription
5 |
6 | let package = Package(
7 | name: "SwiftUIPullToRefresh",
8 | platforms: [
9 | .iOS(.v13)
10 | ],
11 | products: [
12 | .library(
13 | name: "SwiftUIPullToRefresh",
14 | targets: ["SwiftUIPullToRefresh"]),
15 | ],
16 | dependencies: [
17 | ],
18 | targets: [
19 | .target(
20 | name: "SwiftUIPullToRefresh",
21 | dependencies: [])
22 | ]
23 | )
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SwiftUIPullToRefresh
2 |
3 | Pull to refresh is a common UI pattern, supported in UIKit via UIRefreshControl. (Un)surprisingly, it's also unavailable in SwiftUI prior to version 3, and even then [it's a bit lackluster](https://swiftuirecipes.com/blog/pull-to-refresh-with-swiftui-scrollview#drawbacks).
4 |
5 | This package contains a component - `RefreshableScrollView` - that enables this functionality with **any `ScrollView`**. It also **doesn't rely on `UIViewRepresentable`**, and works with **any iOS version**. The end result looks like this:
6 |
7 | 
8 |
9 | ## Features
10 |
11 | * Works on any `ScrollView`.
12 | * Customizable progress indicator, with a default `RefreshActivityIndicator` spinner that works on any SwiftUI version.
13 | * Specify refresh operation and choose when it ends.
14 | * Support for Swift 5.5 `async` blocks.
15 | * Compatibility `refreshCompat` modifier to deliver a drop-in replacement for iOS 15 `refreshable`.
16 | * Built-in haptic feedback, just like regular `List` with `refreshable` has.
17 | * Additional optional customizations:
18 | + `showsIndicators` to allow for showing/hiding `ScrollView` indicators.
19 | + `loadingViewBackgroundColor` to specify the background color of the progress indicator.
20 | + `threshold` that indicates how much does the user how to pull before triggering refresh.
21 |
22 | ## Installation
23 |
24 | This component is distrubuted as a **Swift package**. Just add this URL to your package list:
25 |
26 | ```text
27 | https://github.com/globulus/swiftui-pull-to-refresh
28 | ```
29 |
30 | You can also use **CocoaPods**:
31 |
32 | ```ruby
33 | pod 'SwiftUI-Pull-To-Refresh', '~> 1.1.9'
34 | ```
35 |
36 | ## Sample usage
37 |
38 | ### Bread & butter
39 |
40 | ```swift
41 | struct TestView: View {
42 | @State private var now = Date()
43 |
44 | var body: some View {
45 | RefreshableScrollView(onRefresh: { done in
46 | DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
47 | self.now = Date()
48 | done()
49 | }
50 | }) {
51 | VStack {
52 | ForEach(1..<20) {
53 | Text("\(Calendar.current.date(byAdding: .hour, value: $0, to: now)!)")
54 | .padding(.bottom, 10)
55 | }
56 | }.padding()
57 | }
58 | }
59 | }
60 | }
61 | ```
62 |
63 | ### Custom progress view
64 |
65 | ```swift
66 | RefreshableScrollView(onRefresh: { done in
67 | DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
68 | self.now = Date()
69 | done()
70 | }
71 | },
72 | progress: { state in // HERE
73 | if state == .waiting {
74 | Text("Pull me down...")
75 | } else if state == .primed {
76 | Text("Now release!")
77 | } else {
78 | Text("Working...")
79 | }
80 | }) {
81 | VStack {
82 | ForEach(1..<20) {
83 | Text("\(Calendar.current.date(byAdding: .hour, value: $0, to: now)!)")
84 | .padding(.bottom, 10)
85 | }
86 | }.padding()
87 | }
88 | ```
89 |
90 | ### Using async block
91 |
92 | ```swift
93 | RefreshableScrollView(action: { // HERE
94 | try? await Task.sleep(nanoseconds: 3_000_000_000)
95 | now = Date()
96 | }, progress: { state in
97 | RefreshActivityIndicator(isAnimating: state == .loading) {
98 | $0.hidesWhenStopped = false
99 | }
100 | }) {
101 | VStack {
102 | ForEach(1..<20) {
103 | Text("\(Calendar.current.date(byAdding: .hour, value: $0, to: now)!)")
104 | .padding(.bottom, 10)
105 | }
106 | }.padding()
107 | }
108 | }
109 | ```
110 |
111 | ### Compatibility mode
112 |
113 | ```swift
114 | VStack {
115 | ForEach(1..<20) {
116 | Text("\(Calendar.current.date(byAdding: .hour, value: $0, to: now)!)")
117 | .padding(.bottom, 10)
118 | }
119 | }
120 | .refreshableCompat { done in // HERE
121 | DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
122 | self.now = Date()
123 | done()
124 | }
125 | } progress: { state in
126 | RefreshActivityIndicator(isAnimating: state == .loading) {
127 | $0.hidesWhenStopped = false
128 | }
129 | }
130 | ```
131 |
132 | ## Recipe
133 |
134 | Check out [this recipe](https://swiftuirecipes.com/blog/pull-to-refresh-with-swiftui-scrollview) for in-depth description of the component and its code. Check out [SwiftUIRecipes.com](https://swiftuirecipes.com) for more **SwiftUI recipes**!
135 |
136 | ## Changelog
137 |
138 | * 1.1.9 - Reworked haptic feedback, added haptic feedback as optional.
139 | * 1.1.8 - Fixed crash when doing two pulls quickly in succession.
140 | * 1.1.7 - Updated haptic feedback. Increased Swift version for Podspec.
141 | * 1.1.6 - Fixed issue where content wouldn't swipe up while in refresh state.
142 | * 1.1.5 - Added smooth animation when loading pull is released.
143 | * 1.1.4 - Added `threshold` and `loadingViewBackgroundColor` customizations.
144 | * 1.1.3 - Add haptic feedback & increase offset a bit to fix indicator being visible on certain iPad Pro models.
145 | * 1.1.2 - Increase offset to fix UI bug occurring on iPhones without notch.
146 | * 1.1.1 - Added `showsIndicators` to allow for showing/hiding `ScrollView` indicators.
147 | * 1.1.0 - Added ability to specify custom progress view, iOS 15 support, async block support and compatibility mode.
148 | * 1.0.0 - Initial release.
149 |
--------------------------------------------------------------------------------
/Sources/SwiftUIPullToRefresh/SwiftUIPullToRefresh.swift:
--------------------------------------------------------------------------------
1 | import SwiftUI
2 |
3 | // There are two type of positioning views - one that scrolls with the content,
4 | // and one that stays fixed
5 | private enum PositionType {
6 | case fixed, moving
7 | }
8 |
9 | // This struct is the currency of the Preferences, and has a type
10 | // (fixed or moving) and the actual Y-axis value.
11 | // It's Equatable because Swift requires it to be.
12 | private struct Position: Equatable {
13 | let type: PositionType
14 | let y: CGFloat
15 | }
16 |
17 | // This might seem weird, but it's necessary due to the funny nature of
18 | // how Preferences work. We can't just store the last position and merge
19 | // it with the next one - instead we have a queue of all the latest positions.
20 | private struct PositionPreferenceKey: PreferenceKey {
21 | typealias Value = [Position]
22 |
23 | static var defaultValue = [Position]()
24 |
25 | static func reduce(value: inout [Position], nextValue: () -> [Position]) {
26 | value.append(contentsOf: nextValue())
27 | }
28 | }
29 |
30 | private struct PositionIndicator: View {
31 | let type: PositionType
32 |
33 | var body: some View {
34 | GeometryReader { proxy in
35 | // the View itself is an invisible Shape that fills as much as possible
36 | Color.clear
37 | // Compute the top Y position and emit it to the Preferences queue
38 | .preference(key: PositionPreferenceKey.self, value: [Position(type: type, y: proxy.frame(in: .global).minY)])
39 | }
40 | }
41 | }
42 |
43 | // Callback that'll trigger once refreshing is done
44 | public typealias RefreshComplete = () -> Void
45 |
46 | // The actual refresh action that's called once refreshing starts. It has the
47 | // RefreshComplete callback to let the refresh action let the View know
48 | // once it's done refreshing.
49 | public typealias OnRefresh = (@escaping RefreshComplete) -> Void
50 |
51 | // The offset threshold. 68 is a good number, but you can play
52 | // with it to your liking.
53 | public let defaultRefreshThreshold: CGFloat = 68
54 |
55 | // Tracks the state of the RefreshableScrollView - it's either:
56 | // 1. waiting for a scroll to happen
57 | // 2. has been primed by pulling down beyond THRESHOLD
58 | // 3. is doing the refreshing.
59 | public enum RefreshState {
60 | case waiting, primed, loading
61 | }
62 |
63 | // ViewBuilder for the custom progress View, that may render itself
64 | // based on the current RefreshState.
65 | public typealias RefreshProgressBuilder = (RefreshState) -> Progress
66 |
67 | // Default color of the rectangle behind the progress spinner
68 | public let defaultLoadingViewBackgroundColor = Color(UIColor.systemBackground)
69 |
70 | public struct RefreshableScrollView