From 72a7438e3deebf00b93f88b6cbcc52ebd27ecd1f Mon Sep 17 00:00:00 2001 From: Andrew Haisting <142518658+ahaisting-livefront@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:23:21 -0500 Subject: [PATCH] setup EventsEffect (#17) --- .../bitwarden/ui/base/util/EventsEffect.kt | 22 +++++++++++++++++++ .../createaccount/CreateAccountScreen.kt | 18 +++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/x8bit/bitwarden/ui/base/util/EventsEffect.kt diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/base/util/EventsEffect.kt b/app/src/main/java/com/x8bit/bitwarden/ui/base/util/EventsEffect.kt new file mode 100644 index 000000000..436861396 --- /dev/null +++ b/app/src/main/java/com/x8bit/bitwarden/ui/base/util/EventsEffect.kt @@ -0,0 +1,22 @@ +package com.x8bit.bitwarden.ui.base.util + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import com.x8bit.bitwarden.ui.base.BaseViewModel +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach + +/** + * Convenience method for observing event flow from [BaseViewModel]. + */ +@Composable +fun EventsEffect( + viewModel: BaseViewModel<*, E, *>, + handler: (E) -> Unit, +) { + LaunchedEffect(key1 = Unit) { + viewModel.eventFlow + .onEach { handler.invoke(it) } + .launchIn(this) + } +} diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/feature/createaccount/CreateAccountScreen.kt b/app/src/main/java/com/x8bit/bitwarden/ui/feature/createaccount/CreateAccountScreen.kt index 83b3621bc..ffacf1602 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/feature/createaccount/CreateAccountScreen.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/feature/createaccount/CreateAccountScreen.kt @@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -20,9 +19,8 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import com.x8bit.bitwarden.R +import com.x8bit.bitwarden.ui.base.util.EventsEffect import com.x8bit.bitwarden.ui.components.BitwardenTextField -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach /** * Top level composable for the create account screen. @@ -32,16 +30,12 @@ fun CreateAccountScreen( viewModel: CreateAccountViewModel = viewModel(), ) { val context = LocalContext.current - LaunchedEffect(key1 = Unit) { - viewModel.eventFlow - .onEach { event -> - when (event) { - is CreateAccountEvent.ShowToast -> { - Toast.makeText(context, event.text, Toast.LENGTH_SHORT).show() - } - } + EventsEffect(viewModel) { event -> + when (event) { + is CreateAccountEvent.ShowToast -> { + Toast.makeText(context, event.text, Toast.LENGTH_SHORT).show() } - .launchIn(this) + } } Column(