-
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.
some classes set deprecated & new structural created
- Loading branch information
1 parent
c59292f
commit 79664c8
Showing
6 changed files
with
307 additions
and
63 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,97 @@ | ||
// | ||
// ErrorableView.swift | ||
// | ||
// | ||
// Created by Mehmet Ateş on 24.11.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public protocol ErrorableView: View where ViewModel: ErrorableBaseViewModel { | ||
associatedtype ViewModel | ||
associatedtype Content: View | ||
associatedtype LoadingContent: View | ||
associatedtype ErrorContent: View | ||
|
||
var viewModel: ViewModel { 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 extension ErrorableView { | ||
var body: some View { | ||
ZStack { | ||
switch errorPresentType { | ||
case .onPage: | ||
onPageConfiguration() | ||
case .fullScreen: | ||
fullScreenConfiguration() | ||
case .sheet: | ||
sheetConfiguration() | ||
} | ||
}.animation(.spring, value: viewModel.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 ErrorableView { | ||
@ViewBuilder func onPageConfiguration() -> some View { | ||
Group { | ||
if viewModel.pageState == .loading { | ||
loadingContent | ||
} else if viewModel.pageState == .failure { | ||
errorContent | ||
} else { | ||
content | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder func fullScreenConfiguration() -> some View { | ||
Group { | ||
if viewModel.pageState == .successful { | ||
content | ||
} else { | ||
loadingContent | ||
} | ||
}.fullScreenCover(isPresented: .init(get: { viewModel.sheetsOpen }, set: { newValue in viewModel.sheetsOpen = newValue })) { | ||
errorContent | ||
} | ||
.onChange(of: viewModel.pageState) { newValue in | ||
viewModel.sheetsOpen = newValue == .failure | ||
} | ||
} | ||
|
||
@ViewBuilder func sheetConfiguration() -> some View { | ||
Group { | ||
if viewModel.pageState == .successful { | ||
content | ||
} else { | ||
loadingContent | ||
} | ||
}.sheet(isPresented: .init(get: { viewModel.sheetsOpen }, set: { newValue in viewModel.sheetsOpen = newValue }), onDismiss: errorStateConfigModel.buttonAction) { | ||
errorContent | ||
} | ||
.onChange(of: viewModel.pageState) { newValue in | ||
viewModel.sheetsOpen = newValue == .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 @@ | ||
// | ||
// ErrorPresentTypes.swift | ||
// | ||
// | ||
// Created by Mehmet Ateş on 24.11.2023. | ||
// | ||
|
||
@frozen public enum ErrorPresentTypes { | ||
case onPage | ||
case fullScreen | ||
case sheet | ||
} |
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 24.11.2023. | ||
// | ||
|
||
@frozen 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
Oops, something went wrong.