|
| 1 | +// |
| 2 | +// ErrorableViewModelProtocol.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Mehmet Ateş on 26.08.2023. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +@available(macOS 11.0, *) |
| 11 | +@available(iOS 14.0, *) |
| 12 | +public protocol ErrorableViewProtocol: View where ViewModel: ErrorableViewModelProtocol { |
| 13 | + associatedtype ViewModel |
| 14 | + associatedtype Content |
| 15 | + |
| 16 | + var viewModel: ViewModel { get set } |
| 17 | +} |
| 18 | + |
| 19 | +@available(macOS 11.0, *) |
| 20 | +@available(iOS 14.0, *) |
| 21 | +public extension ErrorableViewProtocol where Content == AnyView { |
| 22 | + func createErrorableView( |
| 23 | + @ViewBuilder loadingView: () -> Content, |
| 24 | + @ViewBuilder failureView: () -> Content, |
| 25 | + @ViewBuilder successfulView: () -> Content |
| 26 | + ) -> Content { |
| 27 | + switch viewModel.state { |
| 28 | + case .loading: |
| 29 | + loadingView() |
| 30 | + case .successful: |
| 31 | + successfulView() |
| 32 | + case .failure: |
| 33 | + failureView() |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + func createErrorableView( |
| 38 | + @ViewBuilder failureView: () -> Content, |
| 39 | + @ViewBuilder successfulView: () -> Content |
| 40 | + ) -> Content { |
| 41 | + switch viewModel.state { |
| 42 | + case .loading: |
| 43 | + loadingState |
| 44 | + case .successful: |
| 45 | + successfulView() |
| 46 | + case .failure: |
| 47 | + failureView() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + func createErrorableView( |
| 52 | + errorTitle: LocalizedStringKey, |
| 53 | + errorSubTitle: LocalizedStringKey? = nil, |
| 54 | + errorIcon: String? = nil, |
| 55 | + errorSystemIcon: String? = nil, |
| 56 | + errorButtonTitle: LocalizedStringKey, |
| 57 | + @ViewBuilder successfulView: () -> Content |
| 58 | + ) -> Content { |
| 59 | + switch viewModel.state { |
| 60 | + case .loading: |
| 61 | + loadingState |
| 62 | + case .successful: |
| 63 | + successfulView() |
| 64 | + case .failure: |
| 65 | + failuteState(errorTitle: errorTitle, errorSubTitle: errorSubTitle, errorIcon: errorIcon, errorSystemIcon: errorSystemIcon, errorButtonTitle: errorButtonTitle) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private var loadingState: Content { |
| 70 | + AnyView( |
| 71 | + VStack { |
| 72 | + Spacer() |
| 73 | + ProgressView() |
| 74 | + .accentColor(.accentColor) |
| 75 | + Spacer() |
| 76 | + } |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + private func failuteState( |
| 81 | + errorTitle: LocalizedStringKey, |
| 82 | + errorSubTitle: LocalizedStringKey?, |
| 83 | + errorIcon: String?, |
| 84 | + errorSystemIcon: String?, |
| 85 | + errorButtonTitle: LocalizedStringKey |
| 86 | + ) -> Content { |
| 87 | + AnyView( |
| 88 | + VStack { |
| 89 | + Spacer() |
| 90 | + if let errorSystemIcon { |
| 91 | + Image(systemName: errorSystemIcon) |
| 92 | + .font(.system(size: 60)) |
| 93 | + .foregroundColor(.red) |
| 94 | + } |
| 95 | + |
| 96 | + if let errorIcon { |
| 97 | + Image(errorIcon) |
| 98 | + } |
| 99 | + |
| 100 | + Text(errorTitle) |
| 101 | + .font(.title) |
| 102 | + .fontWeight(.bold) |
| 103 | + .multilineTextAlignment(.center) |
| 104 | + .padding(.top) |
| 105 | + if let errorSubTitle { |
| 106 | + Text(errorSubTitle) |
| 107 | + .font(.headline) |
| 108 | + .fontWeight(.bold) |
| 109 | + .foregroundColor(.secondary) |
| 110 | + .multilineTextAlignment(.center) |
| 111 | + } |
| 112 | + |
| 113 | + Spacer() |
| 114 | + Button { |
| 115 | + viewModel.refresh() |
| 116 | + } label: { |
| 117 | + Text(errorButtonTitle) |
| 118 | + .padding() |
| 119 | + .background(Color.red.opacity(0.3)) |
| 120 | + }.accentColor(Color.red) |
| 121 | + .clipShape(Capsule()) |
| 122 | + Spacer() |
| 123 | + } |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +@available(macOS 11.0, *) |
| 129 | +@available(iOS 15.0, *) |
| 130 | +private struct Example_Preview: PreviewProvider { |
| 131 | + static var previews: some View { |
| 132 | + Example() |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +@available(macOS 11.0, *) |
| 137 | +@available(iOS 15.0, *) |
| 138 | +private struct Example: ErrorableViewProtocol { |
| 139 | + typealias Content = AnyView |
| 140 | + typealias ViewModel = ExampleViewModel |
| 141 | + @ObservedObject var viewModel: ExampleViewModel = ExampleViewModel() |
| 142 | + |
| 143 | + var body: some View { |
| 144 | + NavigationView { |
| 145 | + createErrorableView(errorTitle: "Upps!", errorSubTitle: "We encountered an error.\n Please try again later!", errorSystemIcon: "minus.diamond.fill", errorButtonTitle: "Try Again") { |
| 146 | + AnyView ( |
| 147 | + ScrollView { |
| 148 | + ForEach(0..<100, id: \.self) { _ in |
| 149 | + AsyncImage(url: URL(string: "https://picsum.photos/200")) { phase in |
| 150 | + if let image = phase.image { |
| 151 | + image |
| 152 | + .resizable() |
| 153 | + .scaledToFill() |
| 154 | + } else { |
| 155 | + Color.gray |
| 156 | + } |
| 157 | + }.frame(width: 300, height: 200, alignment: .center) |
| 158 | + } |
| 159 | + } |
| 160 | + ) |
| 161 | + } |
| 162 | + .navigationTitle("Example") |
| 163 | + }.onAppear { |
| 164 | + Task { @MainActor in |
| 165 | + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { |
| 166 | + viewModel.state = .failure |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +@available(macOS 11.0, *) |
| 174 | +@available(iOS 15.0, *) |
| 175 | +private final class ExampleViewModel: ErrorableViewModelProtocol { |
| 176 | + @Published var state: PageStates = .loading |
| 177 | + func refresh() { |
| 178 | + state = .successful |
| 179 | + } |
| 180 | +} |
0 commit comments