Skip to content

Commit 7ab5830

Browse files
committed
default methods add & fix overlay content & ux improvement & drop ios 14 support
1 parent b7cd017 commit 7ab5830

File tree

4 files changed

+89
-39
lines changed

4 files changed

+89
-39
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import PackageDescription
66
let package = Package(
77
name: "ErrorableView",
88
platforms: [
9-
.iOS(.v14),
10-
.macOS(.v12)
9+
.iOS(.v15),
10+
.macOS(.v13)
1111
],
1212
products: [
1313
// Products define the executables and libraries a package produces, making them visible to other packages.

Sources/ErrorableView/Abstract/ErrorableViewModifier.swift

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,48 @@
88
import SwiftUI
99

1010
public extension View {
11+
@ViewBuilder
12+
func akErrorView<ErrorContent: ErrorableView, LoadingContent: LoadingView>(
13+
pageState: Binding<PageStates>,
14+
@ViewBuilder errorContent: () -> ErrorContent,
15+
@ViewBuilder loadingContent: () -> LoadingContent = { DefaultLoadingView(loadingText: "Loading...") }
16+
) -> some View {
17+
self.modifier(AKErrorViewModifier(pageState: pageState) {
18+
errorContent()
19+
} loadingContent: {
20+
loadingContent()
21+
})
22+
}
23+
24+
@ViewBuilder
25+
func akErrorView<LoadingContent: LoadingView>(
26+
pageState: Binding<PageStates>,
27+
action: @escaping () -> Void,
28+
@ViewBuilder loadingContent: () -> LoadingContent = { DefaultLoadingView(loadingText: "Loading...") }
29+
) -> some View {
30+
self.modifier(AKErrorViewModifier(pageState: pageState) {
31+
DefaultErrorView(state: pageState) {
32+
action()
33+
}
34+
} loadingContent: {
35+
loadingContent()
36+
})
37+
}
38+
39+
@available(*, deprecated, renamed: "akErrorView", message: "")
1140
@ViewBuilder
1241
func errorableView<Content: ErrorableView, LoadingContent: LoadingView>(pageState: Binding<PageStates>,
1342
@ViewBuilder content: () -> Content,
1443
@ViewBuilder loadingContent: (() -> LoadingContent) = { DefaultLoadingView(loadingText: "Loading...") }) -> some View {
15-
self.modifier(ErrorableViewModifier(pageState: pageState) {
44+
self.modifier(AKErrorViewModifier(pageState: pageState) {
1645
content()
1746
} loadingContent: {
1847
loadingContent()
1948
})
2049
}
2150
}
2251

23-
public struct ErrorableViewModifier<ErrorContent: ErrorableView, LoadingContent: LoadingView>: ViewModifier {
52+
public struct AKErrorViewModifier<ErrorContent: ErrorableView, LoadingContent: LoadingView>: ViewModifier {
2453
@State private var sheetTrigger: Bool = false
2554
@Binding var pageState: PageStates
2655
var errorContent: ErrorContent
@@ -115,6 +144,7 @@ public struct ErrorableViewModifier<ErrorContent: ErrorableView, LoadingContent:
115144
#endif
116145
}
117146

147+
#if DEBUG
118148
@available(iOS 15.0, *)
119149
struct TestView: View {
120150
@State private var pageState: PageStates = .loading
@@ -136,12 +166,9 @@ struct TestView: View {
136166
}
137167
}.navigationTitle("Example Content")
138168
}
139-
.errorableView(pageState: $pageState) {
140-
DefaultErrorView(type: .sheet) {
141-
pageState = .loading
142-
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
143-
pageState = .successful
144-
}
169+
.akErrorView(pageState: $pageState) {
170+
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
171+
pageState = .successful
145172
}
146173
}
147174
.onAppear {
@@ -159,3 +186,4 @@ struct TestView: View {
159186
EmptyView()
160187
}
161188
}
189+
#endif

Sources/ErrorableView/Views/DefaultErrorView.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@ public protocol ErrorableView: View {
1212
}
1313

1414
@frozen public struct DefaultErrorView: ErrorableView {
15-
var uimodel: DefaultErrorPageUIModel
15+
@Environment(\.dismiss) private var dismiss
16+
@Binding private var state: PageStates
17+
private var uimodel: DefaultErrorPageUIModel
18+
private var buttonAction: (() -> Void)?
1619
public var type: ErrorPresentTypes
17-
var buttonAction: (() -> Void)?
1820

19-
public init(uimodel: DefaultErrorPageUIModel = .Builder().build(),
20-
type: ErrorPresentTypes = .sheet,
21-
buttonAction: (() -> Void)? = nil) {
21+
public init(
22+
uimodel: DefaultErrorPageUIModel = .Builder().build(),
23+
type: ErrorPresentTypes = .sheet,
24+
state: Binding<PageStates>,
25+
buttonAction: (() -> Void)? = nil
26+
) {
2227
self.uimodel = uimodel
2328
self.type = type
2429
self.buttonAction = buttonAction
30+
self._state = state
2531
}
2632

2733
public var body: some View {
@@ -59,6 +65,8 @@ private extension DefaultErrorView {
5965
Spacer()
6066
Button {
6167
buttonAction?()
68+
state = .loading
69+
dismiss()
6270
} label: {
6371
Image(systemName: "xmark.circle.fill")
6472
.font(.title)
@@ -98,6 +106,8 @@ private extension DefaultErrorView {
98106
if #available(iOS 15.0, *) {
99107
Button {
100108
buttonAction?()
109+
state = .loading
110+
dismiss()
101111
} label: {
102112
Spacer()
103113
Text(buttonTitle)
@@ -199,5 +209,5 @@ private extension DefaultErrorView {
199209
}
200210

201211
#Preview {
202-
DefaultErrorView()
212+
DefaultErrorView(state: .constant(.loading))
203213
}

Sources/ErrorableView/Views/DefaultLoadingView.swift

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@ public protocol LoadingView: View {
1212
}
1313

1414
@frozen public struct DefaultLoadingView: LoadingView {
15-
var loadingText: LocalizedStringKey
16-
var progressViewColor: Color
15+
private let loadingText: LocalizedStringKey
16+
private let progressViewColor: Color
1717
public var type: LoadingPresenterTypes
1818

19-
public init(loadingText: LocalizedStringKey,
20-
progressViewColor: Color = .accentColor,
21-
type: LoadingPresenterTypes = .overlay
19+
public init(
20+
loadingText: LocalizedStringKey,
21+
progressViewColor: Color = .accentColor,
22+
type: LoadingPresenterTypes = .overlay
2223
) {
2324
self.loadingText = loadingText
2425
self.progressViewColor = progressViewColor
2526
self.type = type
2627
}
2728

2829
public var body: some View {
29-
#if os(macOS)
30+
#if os(macOS)
3031
ZStack {
3132
Rectangle()
3233
.opacity(type == .onPage ? 1 : 0.3)
@@ -45,30 +46,41 @@ public protocol LoadingView: View {
4546
.padding(.top)
4647
}
4748
}.ignoresSafeArea()
48-
#else
49-
ZStack {
50-
Rectangle()
51-
.opacity(type == .onPage ? 1 : 0.3)
49+
#else
50+
switch type {
51+
case .onPage:
5252
VStack {
53-
if #available(iOS 15.0, *) {
54-
ProgressView()
55-
.scaleEffect(1.2)
56-
.tint(progressViewColor)
57-
} else {
58-
ProgressView()
59-
.scaleEffect(1.2)
60-
}
53+
ProgressView()
54+
.scaleEffect(1.2)
55+
.tint(progressViewColor)
56+
6157
Text(loadingText)
62-
.font(.caption)
6358
.foregroundColor(.secondary)
6459
.padding(.top)
6560
}
66-
}.ignoresSafeArea()
67-
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
68-
#endif
61+
case .overlay:
62+
VStack {
63+
HStack {
64+
Spacer()
65+
}
66+
Spacer()
67+
ProgressView()
68+
.scaleEffect(1.2)
69+
.tint(progressViewColor)
70+
71+
Text(loadingText)
72+
.foregroundColor(.secondary)
73+
.padding(.top)
74+
Spacer()
75+
}.background {
76+
Rectangle()
77+
.foregroundStyle(.ultraThinMaterial)
78+
}.ignoresSafeArea()
79+
}
80+
#endif
6981
}
7082
}
7183

7284
#Preview {
73-
DefaultLoadingView(loadingText: "Loading...")
85+
TestView()
7486
}

0 commit comments

Comments
 (0)