Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created November 26, 2025 23:01
Show Gist options
  • Select an option

  • Save hmlongco/5ea0f8168dcf2e33a2f5ecf41d98a2c5 to your computer and use it in GitHub Desktop.

Select an option

Save hmlongco/5ea0f8168dcf2e33a2f5ecf41d98a2c5 to your computer and use it in GitHub Desktop.
Refreshable.swift
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