Skip to content

Commit

Permalink
Complete state hoisting for SignUp Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mitrejcevski committed Sep 18, 2024
1 parent eb8863c commit 559b844
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions feature/signup/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SignUpScreenState>(SIGN_UP) {
it.copy(email = value)
}
}

fun updatePassword(value: String) {
_screenState.update { it.copy(password = value) }
savedStateHandle.update<SignUpScreenState>(SIGN_UP) {
it.copy(password = value)
}
}

fun updateAbout(value: String) {
savedStateHandle.update<SignUpScreenState>(SIGN_UP) {
it.copy(about = value)
}
}

companion object {
private const val SIGN_UP = "signUpKey"
}
}
Original file line number Diff line number Diff line change
@@ -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 = ""
)
) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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
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)

Expand All @@ -22,12 +25,24 @@ class SignUpScreenStateTest {
@Test
fun `password value updating`() = runTest {
val newValue = ":irrelevant:"
val viewModel = SignUpViewModel()
val viewModel = SignUpViewModel(savedStateHandle)

viewModel.updatePassword(newValue)

assertThat(viewModel.screenState.value).isEqualTo(
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)
)
}
}

0 comments on commit 559b844

Please sign in to comment.