diff --git a/feature/signup/build.gradle.kts b/feature/signup/build.gradle.kts index b0cffe2..bfbc4a4 100644 --- a/feature/signup/build.gradle.kts +++ b/feature/signup/build.gradle.kts @@ -4,6 +4,7 @@ plugins { alias(libs.plugins.kotlin.compose.compiler) alias(libs.plugins.kotlin.serialization) alias(libs.plugins.kapt) + alias(libs.plugins.parcelable) } android { diff --git a/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/SignUpViewModel.kt b/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/SignUpViewModel.kt index c585721..0aa0698 100644 --- a/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/SignUpViewModel.kt +++ b/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/SignUpViewModel.kt @@ -1,20 +1,34 @@ package nl.jovmit.androiddevs.feature.signup -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.update +import androidx.lifecycle.SavedStateHandle +import nl.jovmit.androiddevs.core.view.extensions.update import nl.jovmit.androiddevs.feature.signup.state.SignUpScreenState -class SignUpViewModel { +class SignUpViewModel( + private val savedStateHandle: SavedStateHandle +) { - private val _screenState = MutableStateFlow(SignUpScreenState()) - val screenState = _screenState.asStateFlow() + val screenState = savedStateHandle.getStateFlow(SIGN_UP, SignUpScreenState()) fun updateEmail(value: String) { - _screenState.update { it.copy(email = value) } + savedStateHandle.update(SIGN_UP) { + it.copy(email = value) + } } fun updatePassword(value: String) { - _screenState.update { it.copy(password = value) } + savedStateHandle.update(SIGN_UP) { + it.copy(password = value) + } + } + + fun updateAbout(value: String) { + savedStateHandle.update(SIGN_UP) { + it.copy(about = value) + } + } + + companion object { + private const val SIGN_UP = "signUpKey" } } \ No newline at end of file diff --git a/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/state/SignUpScreenState.kt b/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/state/SignUpScreenState.kt index c8db5af..6a40793 100644 --- a/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/state/SignUpScreenState.kt +++ b/feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/state/SignUpScreenState.kt @@ -1,7 +1,11 @@ package nl.jovmit.androiddevs.feature.signup.state +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize data class SignUpScreenState( val email: String = "", val password: String = "", val about: String = "" -) \ No newline at end of file +) : Parcelable \ No newline at end of file diff --git a/feature/signup/src/test/java/nl/jovmit/androiddevs/feature/signup/SignUpScreenStateTest.kt b/feature/signup/src/test/java/nl/jovmit/androiddevs/feature/signup/SignUpScreenStateTest.kt index 8b02b06..25a0e36 100644 --- a/feature/signup/src/test/java/nl/jovmit/androiddevs/feature/signup/SignUpScreenStateTest.kt +++ b/feature/signup/src/test/java/nl/jovmit/androiddevs/feature/signup/SignUpScreenStateTest.kt @@ -1,5 +1,6 @@ package nl.jovmit.androiddevs.feature.signup +import androidx.lifecycle.SavedStateHandle import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.test.runTest import nl.jovmit.androiddevs.feature.signup.state.SignUpScreenState @@ -7,10 +8,12 @@ import org.junit.jupiter.api.Test class SignUpScreenStateTest { + private val savedStateHandle = SavedStateHandle() + @Test fun `email value updating`() { val newEmailValue = "email@" - val viewModel = SignUpViewModel() + val viewModel = SignUpViewModel(savedStateHandle) viewModel.updateEmail(newEmailValue) @@ -22,7 +25,7 @@ class SignUpScreenStateTest { @Test fun `password value updating`() = runTest { val newValue = ":irrelevant:" - val viewModel = SignUpViewModel() + val viewModel = SignUpViewModel(savedStateHandle) viewModel.updatePassword(newValue) @@ -30,4 +33,16 @@ class SignUpScreenStateTest { SignUpScreenState(password = newValue) ) } + + @Test + fun `about value updating`() { + val newValue = ":dunno:" + val viewModel = SignUpViewModel(savedStateHandle) + + viewModel.updateAbout(newValue) + + assertThat(viewModel.screenState.value).isEqualTo( + SignUpScreenState(about = newValue) + ) + } } \ No newline at end of file