Created
December 5, 2021 16:39
-
-
Save enyciaa/6ed341660c807c72dcedd6823e3e3d88 to your computer and use it in GitHub Desktop.
screen-template-9
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
struct JuicyScreenTemplate<ViewState: KotlinBase, Content: View>: View { | |
let screenBackgroundColor: Color | |
let screenState: ScreenState | |
let content: () -> Content | |
let screenToolbarViewState: ScreenToolbarViewState? | |
let screenCallToActionViewState: ScreenCallToActionViewState? | |
let onCallToActionTapped: () -> Void | |
init( | |
screenBackgroundColor: Color = .white, | |
screenState: ScreenState, | |
screenToolbarViewState: ScreenToolbarViewState?, | |
screenCallToActionViewState: ScreenCallToActionViewState? = nil, | |
onCallToActionTapped: @escaping () -> Void = { }, | |
@ViewBuilder content: @escaping () -> Content | |
) { | |
self.screenBackgroundColor = screenBackgroundColor | |
self.screenState = screenState | |
self.screenToolbarViewState = screenToolbarViewState | |
self.screenCallToActionViewState = screenCallToActionViewState | |
self.onCallToActionTapped = onCallToActionTapped | |
self.content = content | |
} | |
var body: some View { | |
ZStack { | |
screenBackgroundColor.ignoresSafeArea() | |
VStack(spacing: 0) { | |
header | |
ZStack(alignment: .bottom) { | |
screenContent | |
if let ctaViewState = screenCallToActionViewState { | |
CallToAction( | |
screenCallToActionViewState: ctaViewState, | |
onCallToActionTapped: onCallToActionTapped | |
) | |
} | |
} | |
} | |
} | |
.navigationBarTitle("") | |
.navigationBarHidden(true) | |
} | |
@ViewBuilder | |
var header: some View { | |
if let viewState = screenToolbarViewState { | |
JuicyHeaderView(title: viewState.title) | |
} | |
} | |
@ViewBuilder | |
var screenContent: some View { | |
switch screenState { | |
case is ScreenStateLoaded: | |
VStack { | |
Spacer() | |
content() | |
Spacer() | |
} | |
case is ScreenStateLoading: | |
LoadingTemplate() | |
case let errorViewState as ScreenStateError: | |
ErrorTemplate( | |
screenErrorViewState: errorViewState.screenErrorViewState | |
) | |
default: | |
EmptyView() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment