Skip to content

Commit

Permalink
default methods add & fix overlay content & ux improvement & drop ios…
Browse files Browse the repository at this point in the history
… 14 support
  • Loading branch information
devmehmetates committed May 14, 2024
1 parent b7cd017 commit 7ab5830
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import PackageDescription
let package = Package(
name: "ErrorableView",
platforms: [
.iOS(.v14),
.macOS(.v12)
.iOS(.v15),
.macOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
Expand Down
44 changes: 36 additions & 8 deletions Sources/ErrorableView/Abstract/ErrorableViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,48 @@
import SwiftUI

public extension View {
@ViewBuilder
func akErrorView<ErrorContent: ErrorableView, LoadingContent: LoadingView>(
pageState: Binding<PageStates>,
@ViewBuilder errorContent: () -> ErrorContent,
@ViewBuilder loadingContent: () -> LoadingContent = { DefaultLoadingView(loadingText: "Loading...") }
) -> some View {
self.modifier(AKErrorViewModifier(pageState: pageState) {
errorContent()
} loadingContent: {
loadingContent()
})
}

@ViewBuilder
func akErrorView<LoadingContent: LoadingView>(
pageState: Binding<PageStates>,
action: @escaping () -> Void,
@ViewBuilder loadingContent: () -> LoadingContent = { DefaultLoadingView(loadingText: "Loading...") }
) -> some View {
self.modifier(AKErrorViewModifier(pageState: pageState) {
DefaultErrorView(state: pageState) {
action()
}
} loadingContent: {
loadingContent()
})
}

@available(*, deprecated, renamed: "akErrorView", message: "")
@ViewBuilder
func errorableView<Content: ErrorableView, LoadingContent: LoadingView>(pageState: Binding<PageStates>,
@ViewBuilder content: () -> Content,
@ViewBuilder loadingContent: (() -> LoadingContent) = { DefaultLoadingView(loadingText: "Loading...") }) -> some View {
self.modifier(ErrorableViewModifier(pageState: pageState) {
self.modifier(AKErrorViewModifier(pageState: pageState) {
content()
} loadingContent: {
loadingContent()
})
}
}

public struct ErrorableViewModifier<ErrorContent: ErrorableView, LoadingContent: LoadingView>: ViewModifier {
public struct AKErrorViewModifier<ErrorContent: ErrorableView, LoadingContent: LoadingView>: ViewModifier {
@State private var sheetTrigger: Bool = false
@Binding var pageState: PageStates
var errorContent: ErrorContent
Expand Down Expand Up @@ -115,6 +144,7 @@ public struct ErrorableViewModifier<ErrorContent: ErrorableView, LoadingContent:
#endif
}

#if DEBUG
@available(iOS 15.0, *)
struct TestView: View {
@State private var pageState: PageStates = .loading
Expand All @@ -136,12 +166,9 @@ struct TestView: View {
}
}.navigationTitle("Example Content")
}
.errorableView(pageState: $pageState) {
DefaultErrorView(type: .sheet) {
pageState = .loading
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
pageState = .successful
}
.akErrorView(pageState: $pageState) {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
pageState = .successful
}
}
.onAppear {
Expand All @@ -159,3 +186,4 @@ struct TestView: View {
EmptyView()
}
}
#endif
22 changes: 16 additions & 6 deletions Sources/ErrorableView/Views/DefaultErrorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ public protocol ErrorableView: View {
}

@frozen public struct DefaultErrorView: ErrorableView {
var uimodel: DefaultErrorPageUIModel
@Environment(\.dismiss) private var dismiss
@Binding private var state: PageStates
private var uimodel: DefaultErrorPageUIModel
private var buttonAction: (() -> Void)?
public var type: ErrorPresentTypes
var buttonAction: (() -> Void)?

public init(uimodel: DefaultErrorPageUIModel = .Builder().build(),
type: ErrorPresentTypes = .sheet,
buttonAction: (() -> Void)? = nil) {
public init(
uimodel: DefaultErrorPageUIModel = .Builder().build(),
type: ErrorPresentTypes = .sheet,
state: Binding<PageStates>,
buttonAction: (() -> Void)? = nil
) {
self.uimodel = uimodel
self.type = type
self.buttonAction = buttonAction
self._state = state
}

public var body: some View {
Expand Down Expand Up @@ -59,6 +65,8 @@ private extension DefaultErrorView {
Spacer()
Button {
buttonAction?()
state = .loading
dismiss()
} label: {
Image(systemName: "xmark.circle.fill")
.font(.title)
Expand Down Expand Up @@ -98,6 +106,8 @@ private extension DefaultErrorView {
if #available(iOS 15.0, *) {
Button {
buttonAction?()
state = .loading
dismiss()
} label: {
Spacer()
Text(buttonTitle)
Expand Down Expand Up @@ -199,5 +209,5 @@ private extension DefaultErrorView {
}

#Preview {
DefaultErrorView()
DefaultErrorView(state: .constant(.loading))
}
58 changes: 35 additions & 23 deletions Sources/ErrorableView/Views/DefaultLoadingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ public protocol LoadingView: View {
}

@frozen public struct DefaultLoadingView: LoadingView {
var loadingText: LocalizedStringKey
var progressViewColor: Color
private let loadingText: LocalizedStringKey
private let progressViewColor: Color
public var type: LoadingPresenterTypes

public init(loadingText: LocalizedStringKey,
progressViewColor: Color = .accentColor,
type: LoadingPresenterTypes = .overlay
public init(
loadingText: LocalizedStringKey,
progressViewColor: Color = .accentColor,
type: LoadingPresenterTypes = .overlay
) {
self.loadingText = loadingText
self.progressViewColor = progressViewColor
self.type = type
}

public var body: some View {
#if os(macOS)
#if os(macOS)
ZStack {
Rectangle()
.opacity(type == .onPage ? 1 : 0.3)
Expand All @@ -45,30 +46,41 @@ public protocol LoadingView: View {
.padding(.top)
}
}.ignoresSafeArea()
#else
ZStack {
Rectangle()
.opacity(type == .onPage ? 1 : 0.3)
#else
switch type {
case .onPage:
VStack {
if #available(iOS 15.0, *) {
ProgressView()
.scaleEffect(1.2)
.tint(progressViewColor)
} else {
ProgressView()
.scaleEffect(1.2)
}
ProgressView()
.scaleEffect(1.2)
.tint(progressViewColor)

Text(loadingText)
.font(.caption)
.foregroundColor(.secondary)
.padding(.top)
}
}.ignoresSafeArea()
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
#endif
case .overlay:
VStack {
HStack {
Spacer()
}
Spacer()
ProgressView()
.scaleEffect(1.2)
.tint(progressViewColor)

Text(loadingText)
.foregroundColor(.secondary)
.padding(.top)
Spacer()
}.background {
Rectangle()
.foregroundStyle(.ultraThinMaterial)
}.ignoresSafeArea()
}
#endif
}
}

#Preview {
DefaultLoadingView(loadingText: "Loading...")
TestView()
}

0 comments on commit 7ab5830

Please sign in to comment.