mirror of
https://github.com/bitwarden/android.git
synced 2024-11-24 02:15:53 +03:00
setup EventsEffect (#17)
This commit is contained in:
parent
13e2539f5d
commit
72a7438e3d
2 changed files with 28 additions and 12 deletions
|
@ -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 <E> EventsEffect(
|
||||
viewModel: BaseViewModel<*, E, *>,
|
||||
handler: (E) -> Unit,
|
||||
) {
|
||||
LaunchedEffect(key1 = Unit) {
|
||||
viewModel.eventFlow
|
||||
.onEach { handler.invoke(it) }
|
||||
.launchIn(this)
|
||||
}
|
||||
}
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue