|
9 | 9 | + The package will be attached to the targeted application
|
10 | 10 |
|
11 | 11 | ## How to use this package
|
12 |
| -### Create a ViewModel conforming to the ErrorableBaseViewModel |
13 |
| -<b>Note:<b> The class includes AnyObject and ObservableObject! |
14 |
| - |
15 |
| -```swift |
16 |
| -private final class ExampleViewModel: ErrorableBaseViewModel { |
17 |
| - // Your actions will come here |
18 |
| -} |
19 |
| -``` |
20 |
| -### Create some SwiftUI view that conforms to the ErrorableView |
| 12 | +### Just use The "ErrorableViewModifier" with an $pageState property |
21 | 13 | ```swift
|
22 |
| -@available(iOS 15.0, *) |
23 |
| -private struct ExampleContentView: View { |
| 14 | +struct TestView: View { |
| 15 | + @State private var pageState: PageStates = .loading |
| 16 | + |
24 | 17 | var body: some View {
|
25 |
| - NavigationView { |
26 |
| - ScrollView { |
27 |
| - ForEach(0..<100, id: \.self) { _ in |
28 |
| - AsyncImage(url: URL(string: "https://picsum.photos/1000")) { phase in |
29 |
| - if let image = phase.image { |
30 |
| - image |
31 |
| - .resizable() |
32 |
| - .scaledToFill() |
33 |
| - } else { |
34 |
| - Color.gray |
35 |
| - } |
36 |
| - }.frame(height: 200, alignment: .center) |
37 |
| - .clipped() |
| 18 | + NavigationView { |
| 19 | + ScrollView { |
| 20 | + ForEach(0..<100, id: \.self) { _ in |
| 21 | + AsyncImage(url: URL(string: "https://picsum.photos/1000")) { phase in |
| 22 | + if let image = phase.image { |
| 23 | + image |
| 24 | + .resizable() |
| 25 | + .scaledToFill() |
| 26 | + } else { |
| 27 | + Color.gray |
| 28 | + } |
| 29 | + }.frame(height: 200, alignment: .center) |
| 30 | + .clipped() |
| 31 | + } |
| 32 | + }.navigationTitle("Example Content") |
| 33 | + } |
| 34 | + .modifier(ErrorableViewModifier(pageState: $pageState) { // Like this |
| 35 | + DefaultErrorView(type: .sheet) { |
| 36 | + pageState = .loading |
| 37 | + DispatchQueue.main.asyncAfter(deadline: .now() + 3) { |
| 38 | + pageState = .successful |
| 39 | + } |
38 | 40 | }
|
39 |
| - }.navigationTitle("Example Content") |
40 |
| - } |
41 |
| - } |
42 |
| -} |
43 |
| - |
44 |
| -@available(iOS 15.0, *) |
45 |
| -private struct OnPageExampleView: ErrorableView { |
46 |
| - @ObservedObject var viewModel: ExampleViewModel = ExampleViewModel() |
47 |
| - |
48 |
| - var content: some View { |
49 |
| - ExampleContentView() |
50 |
| - } |
51 |
| - |
52 |
| - var errorStateConfigModel: ErrorStateConfigureModel { |
53 |
| - ErrorStateConfigureModel.Builder() |
54 |
| - .buttonAction { |
55 |
| - viewModel.refreshPage() |
56 |
| - }.build() |
| 41 | + }) |
| 42 | + .onAppear { |
| 43 | + DispatchQueue.main.asyncAfter(deadline: .now() + 3) { |
| 44 | + pageState = .failure |
| 45 | + } |
| 46 | + } |
57 | 47 | }
|
58 | 48 | }
|
| 49 | +``` |
| 50 | +## Useful Tips |
| 51 | +This package allows you to manage your page error state easily. But actually, it's useful. What do you get to know? |
59 | 52 |
|
60 |
| -@available(iOS 15.0, *) |
61 |
| -private struct SheetExampleView: ErrorableView { |
62 |
| - @ObservedObject var viewModel: ExampleViewModel = ExampleViewModel() |
63 |
| - |
64 |
| - var content: some View { |
65 |
| - ExampleContentView() |
66 |
| - } |
67 |
| - |
68 |
| - var errorStateConfigModel: ErrorStateConfigureModel { |
69 |
| - ErrorStateConfigureModel.Builder() |
70 |
| - .buttonAction { |
71 |
| - viewModel.refreshPage() |
72 |
| - }.build() |
73 |
| - } |
74 |
| - |
75 |
| - var errorPresentType: ErrorPresentTypes { .sheet } |
| 53 | +- Generic Error Page Support: |
| 54 | + The Package includes a DefaultErrorPage but you don't want to use it. Use the ErrorableView protocol, and create your error page. |
| 55 | +```swift |
| 56 | +protocol ErrorableView: View { |
| 57 | + var type: ErrorPresentTypes { get set } |
76 | 58 | }
|
77 |
| - |
78 |
| -@available(iOS 15.0, *) |
79 |
| -private struct FullScreenExampleView: ErrorableView { |
80 |
| - @ObservedObject var viewModel: ExampleViewModel = ExampleViewModel() |
81 |
| - |
82 |
| - var content: some View { |
83 |
| - ExampleContentView() |
84 |
| - } |
85 |
| - |
86 |
| - var errorStateConfigModel: ErrorStateConfigureModel { |
87 |
| - ErrorStateConfigureModel.Builder() |
88 |
| - .buttonAction { |
89 |
| - viewModel.refreshPage() |
90 |
| - }.build() |
| 59 | +``` |
| 60 | +This protocol only wants to create a type property for your error page presentation state. If your view comformed the protocol you'll use this modifier code block under the below. |
| 61 | +```swift |
| 62 | +.modifier(ErrorableViewModifier(pageState: $viewModel.pageState) { // Like this |
| 63 | + YourView() { |
| 64 | + viewModel.reload() |
91 | 65 | }
|
92 |
| - |
93 |
| - var errorPresentType: ErrorPresentTypes { .fullScreen } |
| 66 | +}) |
| 67 | +``` |
| 68 | +- Fully Customisable Error Page: |
| 69 | +The package includes a customizable ErrorPage named DefaultErrorPage. You can use that uiModel to update DefaultErrorPage. |
| 70 | +```swift |
| 71 | +@frozen public struct DefaultErrorPageUIModel { |
| 72 | + var title: LocalizedStringKey |
| 73 | + var subtitle: LocalizedStringKey? |
| 74 | + var icon: String? |
| 75 | + var systemName: String? |
| 76 | + var buttonTitle: LocalizedStringKey? |
94 | 77 | }
|
95 | 78 | ```
|
96 | 79 |
|
97 |
| -## Sheet Type |
| 80 | +## Examples |
| 81 | +### Sheet Type |
98 | 82 | https://github.com/devmehmetates/ErrorableView-SwiftUI/assets/74152011/1fe9e28a-8ba3-48b8-8d85-b2eb4c6aa672
|
99 | 83 |
|
100 |
| -## OnPage Type |
| 84 | +### OnPage Type |
101 | 85 | https://github.com/devmehmetates/ErrorableView-SwiftUI/assets/74152011/2c579c96-adec-4d6e-9739-1892d97666aa
|
102 | 86 |
|
103 |
| -## Fullscreen Type |
| 87 | +### Fullscreen Type |
104 | 88 | https://github.com/devmehmetates/ErrorableView-SwiftUI/assets/74152011/6e34332f-6c24-489d-8bd2-bfd5ab2fb027
|
0 commit comments