@@ -20,15 +20,16 @@ import androidx.compose.runtime.Stable
2020import androidx.compose.runtime.getValue
2121import androidx.compose.runtime.mutableStateOf
2222import androidx.compose.runtime.remember
23+ import androidx.compose.runtime.rememberCoroutineScope
2324import androidx.compose.runtime.setValue
24- import androidx.lifecycle.viewModelScope
2525import androidx.lifecycle.viewmodel.compose.viewModel
2626import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep
2727import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
2828import com.amplifyframework.ui.authenticator.forms.SignUpFormBuilder
2929import com.amplifyframework.ui.authenticator.util.AuthenticatorMessage
3030import kotlinx.coroutines.flow.Flow
31- import kotlinx.coroutines.launch
31+ import kotlinx.coroutines.flow.launchIn
32+ import kotlinx.coroutines.flow.onEach
3233
3334/* *
3435 * Create the [state holder](https://developer.android.com/jetpack/compose/state#managing-state) for the
@@ -44,14 +45,17 @@ fun rememberAuthenticatorState(
4445 signUpForm : SignUpFormBuilder .() -> Unit = {}
4546): AuthenticatorState {
4647 val viewModel = viewModel<AuthenticatorViewModel >()
48+ val scope = rememberCoroutineScope()
4749 return remember {
4850 val configuration = AuthenticatorConfiguration (
4951 initialStep = initialStep,
5052 signUpForm = signUpForm
5153 )
5254
5355 viewModel.start(configuration)
54- AuthenticatorStateImpl (viewModel)
56+ AuthenticatorStateImpl (viewModel).also { state ->
57+ viewModel.stepState.onEach { state.stepState = it }.launchIn(scope)
58+ }
5559 }
5660}
5761
@@ -82,12 +86,4 @@ internal class AuthenticatorStateImpl constructor(
8286
8387 override val messages: Flow <AuthenticatorMessage >
8488 get() = viewModel.events
85-
86- init {
87- viewModel.viewModelScope.launch {
88- viewModel.stepState.collect {
89- stepState = it
90- }
91- }
92- }
9389}
0 commit comments