|
1 | 1 | # ErrorableView-SwiftUI |
2 | 2 |
|
| 3 | +## How to install this package |
| 4 | + |
| 5 | ++ Open your project on Xcode |
| 6 | ++ Go to Project Tab and select "Package Dependencies" |
| 7 | ++ Click "+" and search this package with use git clone url |
| 8 | ++ Don't change anything and click Add Package |
| 9 | ++ The package will be attached to the targeted application |
| 10 | + |
| 11 | +## How to use this package |
| 12 | +### Create a ViewModel conforming to the ErrorableViewModelProtocol |
| 13 | +<b>Note:<b> The class includes AnyObject and ObservableObject! |
| 14 | + |
| 15 | +```swift |
| 16 | +final class TestViewModel: ErrorableViewModelProtocol { |
| 17 | + @Published var state: PageStates = .loading |
| 18 | + |
| 19 | + func refresh() { |
| 20 | + // TODO: Write here your refresh action |
| 21 | + // TODO: Don't forget to update the state property in the refresh action |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | +### Create SwiftUI and conform to the ErrorableViewProtocol |
| 26 | +- Set the ViewModel typealias to the view model we created |
| 27 | +- Create the Viewmodel as @ObservedObject. |
| 28 | +- Use the createErrorableView function according to your need. |
| 29 | +```swift |
| 30 | +struct ContentView: ErrorableViewProtocol { |
| 31 | + typealias Content = AnyView |
| 32 | + typealias ViewModel = TestViewModel |
| 33 | + @ObservedObject var viewModel: TestViewModel = TestViewModel() |
| 34 | + |
| 35 | + var body: some View { |
| 36 | + NavigationView { |
| 37 | + createErrorableView(errorTitle: "Upps!", errorSubTitle: "We encountered an error.\n Please try again later!", errorSystemIcon: "minus.diamond.fill", errorButtonTitle: "Try Again") { |
| 38 | + AnyView ( |
| 39 | + ScrollView { |
| 40 | + ForEach(0..<100, id: \.self) { _ in |
| 41 | + AsyncImage(url: URL(string: "https://picsum.photos/200")) { phase in |
| 42 | + if let image = phase.image { |
| 43 | + image |
| 44 | + .resizable() |
| 45 | + .scaledToFill() |
| 46 | + } else { |
| 47 | + Color.gray |
| 48 | + } |
| 49 | + }.frame(width: 300, height: 200, alignment: .center) |
| 50 | + .clipped() |
| 51 | + } |
| 52 | + }.frame(width: UIScreen.main.bounds.width) |
| 53 | + ) |
| 54 | + } |
| 55 | + .navigationTitle("Example") |
| 56 | + }.onAppear { |
| 57 | + Task { @MainActor in |
| 58 | + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { |
| 59 | + viewModel.state = .failure |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Demo Images |
| 68 | +<div> |
| 69 | + <img width = 255 src="https://github.com/devmehmetates/ErrorableView-SwiftUI/assets/74152011/69b5fcd7-dc27-4c84-ab28-f258f3a2d96d"> |
| 70 | + <img width = 255 src="https://github.com/devmehmetates/ErrorableView-SwiftUI/assets/74152011/ea08d457-d440-496f-a84f-9636611854a0"> |
| 71 | + <img width = 255 src="https://github.com/devmehmetates/ErrorableView-SwiftUI/assets/74152011/69a47f0d-95c4-4375-9aea-583c85fc9688"> |
| 72 | +</div> |
0 commit comments