Skip to content

Instantly share code, notes, and snippets.

@enyciaa
enyciaa / screen-template-9
Created December 5, 2021 16:39
screen-template-9
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,
@enyciaa
enyciaa / screen-template-8
Created December 5, 2021 16:39
screen-template-8
@Composable
fun ScreenTemplate(
screenBackgroundColor: Color = MaterialTheme.colors.surface,
screenState: ScreenState,
loadedState: @Composable () -> Unit,
screenToolbarViewState: ScreenToolbarViewState? = null,
screenCallToActionViewState: ScreenCallToActionViewState? = null,
onCallToActionClicked: () -> Unit = { },
) {
Surface(
@enyciaa
enyciaa / screen-template-7
Created December 5, 2021 16:36
screen-template-7
// Jetpack compose
@Composable
fun ScreenTemplate(
screenBackgroundColor: Color = MaterialTheme.colors.surface,
screenState: ScreenState,
loadedState: @Composable () -> Unit,
screenToolbarViewState: ScreenToolbarViewState? = null,
screenCallToActionViewState: ScreenCallToActionViewState? = null,
onCallToActionClicked: () -> Unit = { },
) {
@enyciaa
enyciaa / screen-template-6
Created December 5, 2021 16:26
screen-template-6
struct CallToAction: View {
let screenCallToActionViewState: ScreenCallToActionViewState
let onCallToActionTapped: () -> Void
var body: some View {
Button(
screenCallToActionViewState.buttonText,
action: onCallToActionTapped
)
}
// Jetpack Compose
@Composable
fun CallToAction(
modifier: Modifier = Modifier,
screenCallToActionViewState: ScreenCallToActionViewState,
onClicked: () -> Unit,
) {
Button(
modifier = modifier,
shape = MaterialTheme.shapes.small,
@enyciaa
enyciaa / screen-template-4
Created December 5, 2021 13:38
screen-template-4
struct MyFabScreenDestination: View {
let myFabScreenViewModel: MyFabScreenViewModel
@State private var viewState: MyFabScreenViewModel.ViewState
init(myFabScreenViewModel: MyFabScreenViewModel) {
self.myFabScreenViewModel = myFabScreenViewModel
_viewState = State(initialValue: myFabScreenViewModel.defaultViewState())
}
var body: some View {
@enyciaa
enyciaa / screen-template-3
Created December 5, 2021 13:37
screen-template-3
@Composable
fun MyFabDestination(
myFabScreenViewModel: MyFabScreenViewModel,
) {
LifecycleHandler(myFabScreenViewModel)
val viewState = myFabScreenViewModel.viewStateStream()
.collectAsState(myFabScreenViewModel.defaultViewState())
val onNextClicked =
@enyciaa
enyciaa / screen-template-2
Created December 5, 2021 13:36
screen-template-2
class MyFabScreenViewModel(
private val navigator: Navigator,
) : MotherViewModel<MyFabScreenViewModel.ViewState, MyFabScreenViewModel.UiAction>() {
override fun onAttach() {
super.onAttach()
flowOf(ScreenState.Loaded)
.onEach { delay(5000) }
.flowOn(Dispatchers.Default)
.onEach { emit(lastViewState.copy(screenState = it)) }
@enyciaa
enyciaa / screen-template-1
Created December 5, 2021 13:35
screen-template-1
sealed interface ScreenState {
object Loading : ScreenState
data class Error(
val message: String
) : ScreenState
object Loaded : ScreenState
}
data class ScreenCallToActionViewState(
class MviViewModel(
private val answerService: AnswerService,
) {
private val coroutineScope = MainScope()
private val _viewState: MutableStateFlow<ViewState> = MutableStateFlow(ViewState())
val viewState = _viewState.asStateFlow()
// See https://proandroiddev.com/android-singleliveevent-redux-with-kotlin-flow-b755c70bb055