Skip to content

Commit b81dce1

Browse files
authored
fix(authenticator): Don't use viewModelScope from the state holder (#83)
1 parent e06e82e commit b81dce1

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

authenticator/src/main/java/com/amplifyframework/ui/authenticator/AuthenticatorState.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ import androidx.compose.runtime.Stable
2020
import androidx.compose.runtime.getValue
2121
import androidx.compose.runtime.mutableStateOf
2222
import androidx.compose.runtime.remember
23+
import androidx.compose.runtime.rememberCoroutineScope
2324
import androidx.compose.runtime.setValue
24-
import androidx.lifecycle.viewModelScope
2525
import androidx.lifecycle.viewmodel.compose.viewModel
2626
import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep
2727
import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
2828
import com.amplifyframework.ui.authenticator.forms.SignUpFormBuilder
2929
import com.amplifyframework.ui.authenticator.util.AuthenticatorMessage
3030
import 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

Comments
 (0)