Skip to content

Commit

Permalink
add presenter capability
Browse files Browse the repository at this point in the history
  • Loading branch information
devmehmetates committed Feb 17, 2024
1 parent 79664c8 commit 321d18e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
89 changes: 89 additions & 0 deletions Sources/ErrorableView/Abstract/ErrorableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@

import SwiftUI

public protocol ErrorableViewP: View where Presenter: ErrorableBasePresenter {
associatedtype Presenter
associatedtype Content: View
associatedtype LoadingContent: View
associatedtype ErrorContent: View

var presenter: Presenter { get }
@ViewBuilder var content: Self.Content { get }
@ViewBuilder var loadingContent: Self.LoadingContent { get }
@ViewBuilder var errorContent: Self.ErrorContent { get }
var errorStateConfigModel: ErrorStateConfigureModel { get }
var errorPresentType: ErrorPresentTypes { get }
}

public protocol ErrorableView: View where ViewModel: ErrorableBaseViewModel {
associatedtype ViewModel
associatedtype Content: View
Expand Down Expand Up @@ -95,3 +109,78 @@ fileprivate extension ErrorableView {
}
}
}

public extension ErrorableViewP {
var body: some View {
ZStack {
switch errorPresentType {
case .onPage:
onPageConfiguration()
case .fullScreen:
fullScreenConfiguration()
case .sheet:
sheetConfiguration()
}
}.animation(.spring, value: presenter.pageState)
}

@ViewBuilder var loadingContent: some View {
DefaultLoadingView()
}

@ViewBuilder var errorContent: some View {
DefaultErrorView(model: errorStateConfigModel, type: errorPresentType)
}

var errorStateConfigModel: ErrorStateConfigureModel {
ErrorStateConfigureModel
.Builder()
.build()
}

var errorPresentType: ErrorPresentTypes { .onPage }
}

fileprivate extension ErrorableViewP {
@ViewBuilder func onPageConfiguration() -> some View {
Group {
if presenter.pageState == .loading {
loadingContent
} else if presenter.pageState == .failure {
errorContent
} else {
content
}
}
}

@ViewBuilder func fullScreenConfiguration() -> some View {
Group {
if presenter.pageState == .successful {
content
} else {
loadingContent
}
}.fullScreenCover(isPresented: .init(get: { presenter.sheetsOpen }, set: { newValue in presenter.sheetsOpen = newValue })) {
errorContent
}
.onChange(of: presenter.pageState) { newValue in
presenter.sheetsOpen = newValue == .failure
}
}

@ViewBuilder func sheetConfiguration() -> some View {
Group {
if presenter.pageState == .successful {
content
} else {
loadingContent
}
}.sheet(isPresented: .init(get: { presenter.sheetsOpen }, set: { newValue in presenter.sheetsOpen = newValue }), onDismiss: errorStateConfigModel.buttonAction) {
errorContent
}
.onChange(of: presenter.pageState) { newValue in
presenter.sheetsOpen = newValue == .failure
}
}
}
7 changes: 7 additions & 0 deletions Sources/ErrorableView/ErrorableViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ open class ErrorableBaseViewModel: ObservableObject {
public init() {}
}

open class ErrorableBasePresenter: ObservableObject {
@Published public var sheetsOpen: Bool = false
@Published public var pageState: PageStates = .loading

public init() {}
}

@available(*, deprecated, renamed: "ErrorableView", message: "Removed! Please Use`ErrorableView`")
public protocol ErrorableSheetView: ErrorableViewProtocol {}

Expand Down

0 comments on commit 321d18e

Please sign in to comment.