Skip to content

Commit

Permalink
Merge pull request #267 from School-of-Company/refactor/264-refactor-…
Browse files Browse the repository at this point in the history
…login-module

🔀 :: (#264) - refactor login module
  • Loading branch information
audgns10 committed Aug 14, 2024
2 parents 0c29c57 + 3bdd285 commit 4a865ad
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
94 changes: 50 additions & 44 deletions feature/login/src/main/java/com/bitgoeul/login/LoginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import androidx.compose.foundation.layout.width
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.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusManager
Expand All @@ -28,6 +30,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bitgoeul.login.viewmodel.AuthViewModel
import com.msg.common.event.Event
import com.msg.design_system.R
Expand All @@ -50,10 +53,17 @@ internal fun LoginRoute(
onLoginClicked: () -> Unit,
viewModel: AuthViewModel = hiltViewModel(LocalContext.current as ComponentActivity),
) {
val emailState by viewModel.email.collectAsStateWithLifecycle()
val passwordState by viewModel.password.collectAsStateWithLifecycle()

val coroutineScope = rememberCoroutineScope()
val context = LocalContext.current

LoginScreen(
email = emailState,
password = passwordState,
onEmailChange = viewModel::onEmailChange,
onPasswordChange = viewModel::onPasswordChange,
onSignUpClicked = onSignUpClicked,
onFindPasswordClicked = onFindPasswordClicked,
onLoginClicked = { email, password ->
Expand Down Expand Up @@ -98,23 +108,25 @@ private suspend fun getLoginData(

@Composable
internal fun LoginScreen(
modifier: Modifier = Modifier,
email: String,
password: String,
onEmailChange: (String) -> Unit,
onPasswordChange: (String) -> Unit,
focusManager: FocusManager = LocalFocusManager.current,
onSignUpClicked: () -> Unit,
onLoginClicked: (String, String) -> Unit,
onFindPasswordClicked: () -> Unit,
) {
LockScreenOrientation(orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
val isEmailErrorStatus = remember { mutableStateOf(false) }
val isPasswordErrorStatus = remember { mutableStateOf(false) }
val isErrorTextShow = remember { mutableStateOf(false) }
var isTextStatus = ""
val emailState = remember { mutableStateOf("") }
val passwordState = remember { mutableStateOf("") }
val (isEmailErrorStatus, setIsEmailErrorStatus) = rememberSaveable { mutableStateOf(false) }
val isPasswordErrorStatus by rememberSaveable{ mutableStateOf(false) }
val isErrorTextShow by rememberSaveable { mutableStateOf(false) }

BitgoeulAndroidTheme { color, type ->
Surface {
Column(
modifier = Modifier
modifier = modifier
.fillMaxSize()
.pointerInput(Unit) {
detectTapGestures {
Expand All @@ -123,109 +135,99 @@ internal fun LoginScreen(
},
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(48.dp))
Spacer(modifier = modifier.height(48.dp))

Row(
modifier = Modifier.fillMaxWidth()
modifier = modifier.fillMaxWidth()
) {
Spacer(modifier = Modifier.width(28.dp))
Spacer(modifier = modifier.width(28.dp))
Text(
modifier = Modifier,
modifier = modifier,
text = stringResource(id = R.string.project_name),
color = color.BLACK,
style = type.titleLarge,
fontSize = 30.sp,
)
}

Spacer(modifier = Modifier.height(80.dp))
Spacer(modifier = modifier.height(80.dp))

Row(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 28.dp),
) {
DefaultTextField(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.height(54.dp),
placeholder = stringResource(id = R.string.email),
errorText = stringResource(id = R.string.error_text_email),
onValueChange = {
isTextStatus = it
emailState.value = it
},
isError = isEmailErrorStatus.value,
onButtonClicked = {
isEmailErrorStatus.value = isTextStatus.isNullOrBlank()
},
onValueChange = onEmailChange,
isError = isEmailErrorStatus,
onButtonClicked = { setIsEmailErrorStatus(email.isBlank()) },
isLinked = false,
isDisabled = false,
isReadOnly = false,
isReverseTrailingIcon = false
)
}

if (isErrorTextShow.value) {
Spacer(modifier = Modifier.height(0.dp))
if (isErrorTextShow) {
Spacer(modifier = modifier.height(0.dp))
} else {
Spacer(modifier = Modifier.height(16.dp))
Spacer(modifier = modifier.height(16.dp))
}

Row(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 28.dp),
) {
PasswordTextField(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.height(54.dp),
placeholder = stringResource(id = R.string.password),
errorText = stringResource(id = R.string.wrong_password),
onValueChange = {
passwordState.value = it
},
onLinkClicked = {
onFindPasswordClicked()
},
isError = isPasswordErrorStatus.value,
onValueChange = onPasswordChange,
onLinkClicked = onFindPasswordClicked,
isError = isPasswordErrorStatus,
isLinked = true,
linkText = stringResource(id = R.string.find_password),
isDisabled = false,
)
}

Spacer(modifier = Modifier.height(180.dp))
Spacer(modifier = modifier.height(180.dp))

Row(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 28.dp),
) {
BitgoeulButton(
text = stringResource(id = R.string.login),
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.height(52.dp),
state = if (emailState.value.checkEmailRegex() && passwordState.value.checkPasswordRegex()) ButtonState.Enable else ButtonState.Disable,
text = stringResource(id = R.string.login),
state = if (email.checkEmailRegex() && password.checkPasswordRegex()) ButtonState.Enable else ButtonState.Disable,
onClicked = {
onLoginClicked(emailState.value, passwordState.value)
onLoginClicked(email, password)
}
)
}

Spacer(modifier = Modifier.height(8.dp))
Spacer(modifier = modifier.height(8.dp))

Text(
modifier = Modifier,
text = "또는",
style = type.labelMedium,
color = color.G1,
fontSize = 14.sp,
)

Spacer(modifier = Modifier.height(2.dp))
Spacer(modifier = modifier.height(2.dp))

LinkText(
text = stringResource(id = R.string.sign_up)
Expand All @@ -244,6 +246,10 @@ private fun LoginScreenPre() {
onSignUpClicked = { /*TODO*/ },
onLoginClicked = { _, _ -> },
onFindPasswordClicked = { /*TODO*/ },
focusManager = LocalFocusManager.current
focusManager = LocalFocusManager.current,
email = "",
password = "",
onEmailChange = {},
onPasswordChange = {}
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bitgoeul.login.viewmodel

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.msg.common.errorhandling.errorHandling
Expand All @@ -19,13 +20,22 @@ import javax.inject.Inject
class AuthViewModel @Inject constructor(
private val loginUseCase: LoginUseCase,
private val saveTokenUseCase: SaveTokenUseCase,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
companion object {
private const val EMAIL = "email"
private const val PASSWORD = "password"
}
private val _saveTokenResponse = MutableStateFlow<Event<Nothing>>(Event.Loading)
val saveTokenRequest = _saveTokenResponse.asStateFlow()

private val _loginResponse = MutableStateFlow<Event<AuthTokenEntity>>(Event.Loading)
val loginResponse = _loginResponse.asStateFlow()

internal var email = savedStateHandle.getStateFlow(key = EMAIL, initialValue = "")

internal var password = savedStateHandle.getStateFlow(key = PASSWORD, initialValue = "")

internal fun login(
email: String,
password: String
Expand Down Expand Up @@ -58,4 +68,8 @@ class AuthViewModel @Inject constructor(
_saveTokenResponse.value = it.errorHandling()
}
}

internal fun onEmailChange(value: String) { savedStateHandle[EMAIL] = value }

internal fun onPasswordChange(value: String) { savedStateHandle[PASSWORD] = value }
}

0 comments on commit 4a865ad

Please sign in to comment.