-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ae784e6
Showing
11 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Xcode | ||
# | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## User settings | ||
xcuserdata/ | ||
|
||
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) | ||
*.xcscmblueprint | ||
*.xccheckout | ||
|
||
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) | ||
build/ | ||
DerivedData/ | ||
*.moved-aside | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
|
||
## Obj-C/Swift specific | ||
*.hmap | ||
|
||
## App packaging | ||
*.ipa | ||
*.dSYM.zip | ||
*.dSYM | ||
|
||
## Playgrounds | ||
timeline.xctimeline | ||
playground.xcworkspace | ||
|
||
# Swift Package Manager | ||
# | ||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
# Packages/ | ||
# Package.pins | ||
# Package.resolved | ||
# *.xcodeproj | ||
# | ||
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata | ||
# hence it is not needed unless you have added a package configuration file to your project | ||
# .swiftpm | ||
|
||
.build/ | ||
|
||
# CocoaPods | ||
# | ||
# We recommend against adding the Pods directory to your .gitignore. However | ||
# you should judge for yourself, the pros and cons are mentioned at: | ||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
# | ||
# Pods/ | ||
# | ||
# Add this line if you want to avoid checking in source code from the Xcode workspace | ||
# *.xcworkspace | ||
|
||
# Carthage | ||
# | ||
# Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
# Carthage/Checkouts | ||
|
||
Carthage/Build/ | ||
|
||
# Accio dependency management | ||
Dependencies/ | ||
.accio/ | ||
|
||
# fastlane | ||
# | ||
# It is recommended to not store the screenshots in the git repo. | ||
# Instead, use fastlane to re-generate the screenshots whenever they are needed. | ||
# For more information about the recommended setup visit: | ||
# https://docs.fastlane.tools/best-practices/source-control/#source-control | ||
|
||
fastlane/report.xml | ||
fastlane/Preview.html | ||
fastlane/screenshots/**/*.png | ||
fastlane/test_output | ||
|
||
# Code Injection | ||
# | ||
# After new code Injection tools there's a generated folder /iOSInjectionProject | ||
# https://github.com/johnno1962/injectionforxcode | ||
|
||
iOSInjectionProject/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Mehmet Ateş | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "ErrorableView", | ||
products: [ | ||
// Products define the executables and libraries a package produces, making them visible to other packages. | ||
.library( | ||
name: "ErrorableView", | ||
targets: ["ErrorableView"]), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package, defining a module or a test suite. | ||
// Targets can depend on other targets in this package and products from dependencies. | ||
.target( | ||
name: "ErrorableView"), | ||
.testTarget( | ||
name: "ErrorableViewTests", | ||
dependencies: ["ErrorableView"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# ErrorableView-SwiftUI | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// ErrorableViewModelProtocol.swift | ||
// | ||
// | ||
// Created by Mehmet Ateş on 26.08.2023. | ||
// | ||
|
||
import Combine | ||
|
||
@available(macOS 10.15, *) | ||
@available(iOS 13.0, *) | ||
public protocol ErrorableViewModelProtocol: AnyObject, ObservableObject { | ||
var state: PageStates { get set } | ||
func refresh() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// | ||
// ErrorableViewModelProtocol.swift | ||
// | ||
// | ||
// Created by Mehmet Ateş on 26.08.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@available(macOS 11.0, *) | ||
@available(iOS 14.0, *) | ||
public protocol ErrorableViewProtocol: View where ViewModel: ErrorableViewModelProtocol { | ||
associatedtype ViewModel | ||
associatedtype Content | ||
|
||
var viewModel: ViewModel { get set } | ||
} | ||
|
||
@available(macOS 11.0, *) | ||
@available(iOS 14.0, *) | ||
public extension ErrorableViewProtocol where Content == AnyView { | ||
func createErrorableView( | ||
@ViewBuilder loadingView: () -> Content, | ||
@ViewBuilder failureView: () -> Content, | ||
@ViewBuilder successfulView: () -> Content | ||
) -> Content { | ||
switch viewModel.state { | ||
case .loading: | ||
loadingView() | ||
case .successful: | ||
successfulView() | ||
case .failure: | ||
failureView() | ||
} | ||
} | ||
|
||
func createErrorableView( | ||
@ViewBuilder failureView: () -> Content, | ||
@ViewBuilder successfulView: () -> Content | ||
) -> Content { | ||
switch viewModel.state { | ||
case .loading: | ||
loadingState | ||
case .successful: | ||
successfulView() | ||
case .failure: | ||
failureView() | ||
} | ||
} | ||
|
||
func createErrorableView( | ||
errorTitle: LocalizedStringKey, | ||
errorSubTitle: LocalizedStringKey? = nil, | ||
errorIcon: String? = nil, | ||
errorSystemIcon: String? = nil, | ||
errorButtonTitle: LocalizedStringKey, | ||
@ViewBuilder successfulView: () -> Content | ||
) -> Content { | ||
switch viewModel.state { | ||
case .loading: | ||
loadingState | ||
case .successful: | ||
successfulView() | ||
case .failure: | ||
failuteState(errorTitle: errorTitle, errorSubTitle: errorSubTitle, errorIcon: errorIcon, errorSystemIcon: errorSystemIcon, errorButtonTitle: errorButtonTitle) | ||
} | ||
} | ||
|
||
private var loadingState: Content { | ||
AnyView( | ||
VStack { | ||
Spacer() | ||
ProgressView() | ||
.accentColor(.accentColor) | ||
Spacer() | ||
} | ||
) | ||
} | ||
|
||
private func failuteState( | ||
errorTitle: LocalizedStringKey, | ||
errorSubTitle: LocalizedStringKey?, | ||
errorIcon: String?, | ||
errorSystemIcon: String?, | ||
errorButtonTitle: LocalizedStringKey | ||
) -> Content { | ||
AnyView( | ||
VStack { | ||
Spacer() | ||
if let errorSystemIcon { | ||
Image(systemName: errorSystemIcon) | ||
.font(.system(size: 60)) | ||
.foregroundColor(.red) | ||
} | ||
|
||
if let errorIcon { | ||
Image(errorIcon) | ||
} | ||
|
||
Text(errorTitle) | ||
.font(.title) | ||
.fontWeight(.bold) | ||
.multilineTextAlignment(.center) | ||
.padding(.top) | ||
if let errorSubTitle { | ||
Text(errorSubTitle) | ||
.font(.headline) | ||
.fontWeight(.bold) | ||
.foregroundColor(.secondary) | ||
.multilineTextAlignment(.center) | ||
} | ||
|
||
Spacer() | ||
Button { | ||
viewModel.refresh() | ||
} label: { | ||
Text(errorButtonTitle) | ||
.padding() | ||
.background(Color.red.opacity(0.3)) | ||
}.accentColor(Color.red) | ||
.clipShape(Capsule()) | ||
Spacer() | ||
} | ||
) | ||
} | ||
} | ||
|
||
@available(macOS 11.0, *) | ||
@available(iOS 15.0, *) | ||
private struct Example_Preview: PreviewProvider { | ||
static var previews: some View { | ||
Example() | ||
} | ||
} | ||
|
||
@available(macOS 11.0, *) | ||
@available(iOS 15.0, *) | ||
private struct Example: ErrorableViewProtocol { | ||
typealias Content = AnyView | ||
typealias ViewModel = ExampleViewModel | ||
@ObservedObject var viewModel: ExampleViewModel = ExampleViewModel() | ||
|
||
var body: some View { | ||
NavigationView { | ||
createErrorableView(errorTitle: "Upps!", errorSubTitle: "We encountered an error.\n Please try again later!", errorSystemIcon: "minus.diamond.fill", errorButtonTitle: "Try Again") { | ||
AnyView ( | ||
ScrollView { | ||
ForEach(0..<100, id: \.self) { _ in | ||
AsyncImage(url: URL(string: "https://picsum.photos/200")) { phase in | ||
if let image = phase.image { | ||
image | ||
.resizable() | ||
.scaledToFill() | ||
} else { | ||
Color.gray | ||
} | ||
}.frame(width: 300, height: 200, alignment: .center) | ||
} | ||
} | ||
) | ||
} | ||
.navigationTitle("Example") | ||
}.onAppear { | ||
Task { @MainActor in | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { | ||
viewModel.state = .failure | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@available(macOS 11.0, *) | ||
@available(iOS 15.0, *) | ||
private final class ExampleViewModel: ErrorableViewModelProtocol { | ||
@Published var state: PageStates = .loading | ||
func refresh() { | ||
state = .successful | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// PageStates.swift | ||
// | ||
// | ||
// Created by Mehmet Ateş on 26.08.2023. | ||
// | ||
|
||
public enum PageStates { | ||
case loading | ||
case successful | ||
case failure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import XCTest | ||
@testable import ErrorableView | ||
|
||
final class ErrorableViewTests: XCTestCase { | ||
func testExample() throws { | ||
// XCTest Documentation | ||
// https://developer.apple.com/documentation/xctest | ||
|
||
// Defining Test Cases and Test Methods | ||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods | ||
} | ||
} |