Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔀 :: (#256) - passwordchangescreen state manage viewmodel #257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusManager
import androidx.compose.ui.input.pointer.pointerInput
Expand All @@ -21,6 +23,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.my_page.viewmodel.MyPageViewModel
import com.msg.design_system.component.button.BitgoeulButton
import com.msg.design_system.component.icon.GoBackIcon
Expand All @@ -37,6 +40,9 @@ internal fun PasswordChangeRoute(
onBackClicked: () -> Unit,
viewModel: MyPageViewModel = hiltViewModel()
) {
val isCurrentPassword by viewModel.currentPassword.collectAsStateWithLifecycle()
val isNewPassword by viewModel.newPassword.collectAsStateWithLifecycle()
val isCheckPassword by viewModel.checkPassword.collectAsStateWithLifecycle()

PasswordChangeScreen(
onPasswordChangeClicked = { currentPassword, newPassword ->
Expand All @@ -46,24 +52,32 @@ internal fun PasswordChangeRoute(
)
},
onSuccessScreenButtonClicked = onSuccessScreenButtonClicked,
onBackClicked = onBackClicked
onBackClicked = onBackClicked,
currentPassword = isCurrentPassword,
newPassword = isNewPassword,
checkPassword = isCheckPassword,
onNewPasswordChange = viewModel::onNewPasswordChange,
onCurrentPasswordChange = viewModel::onCurrentPasswordChange,
onCheckPasswordChange = viewModel::onCheckPasswordChange
)
}

@Composable
internal fun PasswordChangeScreen(
modifier: Modifier = Modifier,
currentPassword: String,
newPassword: String,
checkPassword: String,
onNewPasswordChange: (String) -> Unit,
onCurrentPasswordChange: (String) -> Unit,
onCheckPasswordChange: (String) -> Unit,
focusManager: FocusManager = LocalFocusManager.current,
onPasswordChangeClicked: (currentPassword: String, newPassword: String) -> Unit,
onSuccessScreenButtonClicked: () -> Unit,
onBackClicked: () -> Unit
) {
val (isCurrentPassword, setIsCurrentPassword) = remember { mutableStateOf("") }
val (isNewPassword, setIsNewPassword) = remember { mutableStateOf("") }
val (isCheckPassword, setIsCheckPassword) = remember { mutableStateOf("") }

val isWrongPassword = remember { mutableStateOf(false) }
val isSamePassword = remember { mutableStateOf(true) }
val isWrongPassword by remember { mutableStateOf(false) }
var isSamePassword by remember { mutableStateOf(true) }

val (isShowSuccessScreen, setIsShowSuccessScreen) = remember { mutableStateOf(false) }

Expand Down Expand Up @@ -103,9 +117,9 @@ internal fun PasswordChangeScreen(
modifier = modifier.fillMaxWidth(),
placeholder = stringResource(R.string.current_password),
errorText = stringResource(R.string.disagree_password),
onValueChange = setIsCurrentPassword,
onValueChange = onCurrentPasswordChange,
onLinkClicked = {},
isError = isWrongPassword.value,
isError = isWrongPassword,
isLinked = false,
isDisabled = false
)
Expand All @@ -114,9 +128,9 @@ internal fun PasswordChangeScreen(
modifier = modifier.fillMaxWidth(),
placeholder = stringResource(R.string.new_password),
errorText = "비밀번호는 8~24 영어 + 숫자 + 특수문자 로 해주세요",
onValueChange = setIsNewPassword,
onValueChange = onNewPasswordChange,
onLinkClicked = {},
isError = isNewPassword.checkPasswordRegex(),
isError = newPassword.checkPasswordRegex(),
isLinked = false,
isDisabled = false
)
Expand All @@ -126,11 +140,11 @@ internal fun PasswordChangeScreen(
placeholder = stringResource(R.string.check_new_password),
errorText = stringResource(R.string.disagree_password),
onValueChange = {
setIsCheckPassword(it)
isSamePassword.value = isNewPassword == isCheckPassword
onCheckPasswordChange(it)
isSamePassword = newPassword == checkPassword
},
onLinkClicked = {},
isError = !isSamePassword.value,
isError = !isSamePassword,
isLinked = false,
isDisabled = false
)
Expand All @@ -143,7 +157,7 @@ internal fun PasswordChangeScreen(
.padding(horizontal = 28.dp),
text = stringResource(R.string.change)
) {
onPasswordChangeClicked(isCurrentPassword, isNewPassword)
onPasswordChangeClicked(currentPassword, newPassword)
setIsShowSuccessScreen(true)
}
Spacer(modifier = modifier.height(56.dp))
Expand All @@ -170,6 +184,12 @@ fun PasswordChangeScreenPre() {
onSuccessScreenButtonClicked = {},
onPasswordChangeClicked = { _, _ -> },
onBackClicked = {},
focusManager = LocalFocusManager.current
focusManager = LocalFocusManager.current,
currentPassword = "",
onCurrentPasswordChange = {},
newPassword = "",
onNewPasswordChange = {},
checkPassword = "",
onCheckPasswordChange = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.my_page.viewmodel

import com.msg.model.enumdata.Authority
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.msg.common.errorhandling.errorHandling
Expand All @@ -24,9 +25,16 @@ class MyPageViewModel @Inject constructor(
private val getMyPageUseCase: GetMyPageUseCase,
private val withdrawUseCase: WithdrawUseCase,
private val logoutUseCase: LogoutUseCase,
private val changePasswordUseCase: ChangePasswordUseCase
private val changePasswordUseCase: ChangePasswordUseCase,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {

companion object {
private const val CURRENT_PASSWORD = "currentPassword"
private const val NEW_PASSWORD = "newPassword"
private const val CHECK_PASSWORD = "checkPassword"
}

private val _getMyPageResponse = MutableStateFlow<Event<GetMyPageEntity>>(Event.Loading)
val getMyPageResponse = _getMyPageResponse.asStateFlow()

Expand All @@ -50,6 +58,12 @@ class MyPageViewModel @Inject constructor(
)
private set

internal var currentPassword = savedStateHandle.getStateFlow(key = CURRENT_PASSWORD, initialValue = "")

internal var newPassword = savedStateHandle.getStateFlow(key = NEW_PASSWORD, initialValue = "")

internal var checkPassword = savedStateHandle.getStateFlow(key = CHECK_PASSWORD, initialValue = "")

internal fun inquiryMyPage() = viewModelScope.launch {
getMyPageUseCase().onSuccess {
it.catch { remoteError ->
Expand Down Expand Up @@ -105,4 +119,10 @@ class MyPageViewModel @Inject constructor(
_getChangePasswordResponse.value = error.errorHandling()
}
}

internal fun onCurrentPasswordChange(value: String) { savedStateHandle[CURRENT_PASSWORD] = value }

internal fun onNewPasswordChange(value: String) { savedStateHandle[NEW_PASSWORD] = value }

internal fun onCheckPasswordChange(value: String) { savedStateHandle[CHECK_PASSWORD] = value }
}
Loading