Created
November 26, 2025 23:01
-
-
Save hmlongco/5ea0f8168dcf2e33a2f5ecf41d98a2c5 to your computer and use it in GitHub Desktop.
Refreshable.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Loading { | |
| case loading | |
| case loaded(String) | |
| } | |
| struct LoadingView: View { | |
| @State var state: Loading = .loading | |
| var body: some View { | |
| List { | |
| switch state { | |
| case .loading: | |
| ProgressView() | |
| .task { | |
| await load() | |
| } | |
| case .loaded(let string): | |
| Text(string) | |
| } | |
| } | |
| .refreshable { | |
| await reload() | |
| } | |
| } | |
| func load() async { | |
| try? await Task.sleep(nanoseconds: 1_000_000_000 * 3) | |
| state = .loaded("loaded") | |
| } | |
| func reload() async { | |
| guard case .loaded = state else { | |
| return | |
| } | |
| try? await Task.sleep(nanoseconds: 1_000_000_000 * 3) | |
| state = .loaded("reloaded") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment