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")
}
}
@Silviuvr
Copy link

Awesome thanks! If we take the loaded example, what if there is a button that has a loading inside it so the loading state is exactly as the loadED state but HAS something extra like a loading near the button text. That switch won't work right? Is there a best practice to handle that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment