Skip to content

Commit 0fc8873

Browse files
authored
Add error retry handler. (#8)
* Add error retry handler. * Delete OnChange. * Minor improvements.
1 parent 945a9b0 commit 0fc8873

5 files changed

Lines changed: 131 additions & 210 deletions

File tree

Examples/ContentView.swift

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,17 @@ struct ContentView: View {
4949
url: url,
5050
placeholder: { progress in
5151
// Create any view for placeholder (optional).
52-
ZStack {
53-
Color.yellow
54-
55-
ProgressView() {
56-
VStack {
57-
Text("Downloading...")
58-
59-
Text("\(progress) %")
60-
}
61-
}
62-
}
52+
placeholder(progress)
6353
},
6454
image: {
6555
// Customize image.
6656
Image(cpImage: $0)
6757
.resizable()
6858
.scaledToFill()
6959
},
70-
error: { error in
60+
error: { error, retry in
7161
// Create any view for error (optional).
72-
ZStack {
73-
Color.yellow
74-
75-
VStack {
76-
Group {
77-
Text("Error:")
78-
.bold()
79-
80-
Text(error)
81-
}
82-
.font(.footnote)
83-
.multilineTextAlignment(.center)
84-
.foregroundStyle(.red)
85-
}
86-
.padding()
87-
}
62+
self.error(error, action: retry)
8863
}
8964
)
9065
.frame(
@@ -120,6 +95,58 @@ struct ContentView: View {
12095
}
12196
}
12297

98+
// MARK: - Ext. Configure views
99+
100+
@available(iOS 15.0, macOS 12.0, *)
101+
extension ContentView {
102+
func placeholder(_ progress: String) -> some View {
103+
ZStack {
104+
Color.yellow
105+
106+
ProgressView() {
107+
VStack {
108+
Text("Downloading...")
109+
110+
Text("\(progress) %")
111+
}
112+
}
113+
}
114+
}
115+
116+
func error(_ error: String, action: (() -> Void)? = nil) -> some View {
117+
ZStack {
118+
Color.yellow
119+
120+
VStack {
121+
Group {
122+
Text("Error:")
123+
.bold()
124+
125+
Text(error)
126+
}
127+
.font(.footnote)
128+
.multilineTextAlignment(.center)
129+
.foregroundStyle(.red)
130+
131+
retry(action: action)
132+
.padding(.top)
133+
}
134+
.padding()
135+
}
136+
}
137+
138+
func retry(action: (() -> Void)?) -> some View {
139+
Button(
140+
action: { action?() },
141+
label: {
142+
Text("Retry")
143+
.foregroundStyle(.black)
144+
.opacity(0.8)
145+
}
146+
)
147+
}
148+
}
149+
123150
// MARK: - Preview Provider
124151

125152
@available(iOS 15.0, macOS 12.0, *)

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ let exampleUrl = "https://example.com/image.jpg"
2828

2929
CachedAsyncImage(
3030
url: exampleUrl,
31-
placeholder: {
31+
placeholder: { progress in
3232
// Create any view for placeholder (optional).
3333
ZStack {
3434
Color.yellow
3535

36-
ProgressView()
36+
ProgressView() {
37+
VStack {
38+
Text("Downloading...")
39+
40+
Text("\(progress) %")
41+
}
42+
}
3743
}
3844
},
3945
image: {
@@ -73,7 +79,7 @@ CachedAsyncImage(
7379
.resizable()
7480
.scaledToFill()
7581
},
76-
error: { error in
82+
error: { error, retry in
7783
// Create any view for error (optional).
7884
ZStack {
7985
Color.yellow
@@ -88,6 +94,16 @@ CachedAsyncImage(
8894
.font(.footnote)
8995
.multilineTextAlignment(.center)
9096
.foregroundStyle(.red)
97+
98+
Button(
99+
action: retry,
100+
label: {
101+
Text("Retry")
102+
.foregroundStyle(.black)
103+
.opacity(0.8)
104+
}
105+
)
106+
.padding(.top)
91107
}
92108
.padding()
93109
}
@@ -120,6 +136,7 @@ struct MyView: View {
120136

121137
## Requirements
122138
- iOS 14.0 +
139+
- macOS 11.0 +
123140
- [SwiftUI](https://developer.apple.com/xcode/swiftui/)
124141

125142
## License

Sources/CachedAsyncImage/Modifiers/OnChange.swift

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)