-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete state hoisting for SignUp Screen
- Loading branch information
1 parent
eb8863c
commit 559b844
Showing
4 changed files
with
45 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 22 additions & 8 deletions
30
feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/SignUpViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
feature/signup/src/main/java/nl/jovmit/androiddevs/feature/signup/state/SignUpScreenState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters