From 2e0c262240d301a67d4401f0f34f4c7dc26433af Mon Sep 17 00:00:00 2001 From: youjin09222 Date: Thu, 2 May 2024 19:56:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor/#6:=20=EC=83=81=ED=83=9C=20=ED=98=B8?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=ED=8C=85=20=EB=B0=8F=20=EC=A1=B0=EA=B1=B4?= =?UTF-8?q?=EB=AC=B8=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sopt/now/compose/LoginActivity.kt | 131 ++++++++++++------ 1 file changed, 88 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/sopt/now/compose/LoginActivity.kt b/app/src/main/java/com/sopt/now/compose/LoginActivity.kt index 8c02e07..d4f42de 100644 --- a/app/src/main/java/com/sopt/now/compose/LoginActivity.kt +++ b/app/src/main/java/com/sopt/now/compose/LoginActivity.kt @@ -48,7 +48,7 @@ class LoginActivity : ComponentActivity() { val userName = intent.getStringExtra("userName").toString() val userDescription = intent.getStringExtra("userDescription").toString() - LoginCompose(userId, userPw, userName ,userDescription) + LoginView(userId, userPw, userName ,userDescription) } } } @@ -56,7 +56,64 @@ class LoginActivity : ComponentActivity() { } @Composable -fun LoginInputField(label: String, value: String, onValueChange: (String) -> Unit) { +fun LoginView( + userId: String, + userPw: String, + userName: String, + userDescription: String +) { + val context = LocalContext.current + var inputId by remember { mutableStateOf("") } + var inputPw by remember { mutableStateOf("") } + + Column( + modifier = Modifier + .fillMaxSize() + .padding(horizontal = 30.dp, vertical = 60.dp), + ) { + Text( + text = stringResource(id = R.string.title_login), + fontSize = 30.sp, + fontWeight = FontWeight.Bold, + textAlign = TextAlign.Center, + modifier = Modifier.fillMaxWidth() + ) + Spacer(modifier = Modifier.height(60.dp)) + LoginInputField( + label = stringResource(id = R.string.tv_id), + inputData = inputId, + isPassword = false + ) { inputId = it } + Spacer(modifier = Modifier.height(60.dp)) + LoginInputField( + label = stringResource(id = R.string.tv_pw), + inputData = inputPw, + isPassword = true + ) { inputPw = it } + Spacer(modifier = Modifier.weight(2f)) + LoginButton( + text = stringResource(id = R.string.btn_login), + onClick = { + checkLogin(context, userId, userPw, userName, userDescription, inputId, inputPw) + } + ) + Spacer(modifier = Modifier.height(10.dp)) + LoginButton( + stringResource(id = R.string.btn_sign_up)) { + Intent(context, SignUpActivity::class.java).apply { + context.startActivity(this) + } + } + } +} + +@Composable +fun LoginInputField( + label: String, + inputData: String, + isPassword: Boolean, + onValueChange: (String) -> Unit +) { Column( modifier = Modifier.fillMaxWidth() ) { @@ -68,17 +125,22 @@ fun LoginInputField(label: String, value: String, onValueChange: (String) -> Uni ) Spacer(modifier = Modifier.height(10.dp)) TextField( - value = value, + value = inputData, onValueChange = onValueChange, modifier = Modifier.fillMaxWidth(), label = { Text(label) }, singleLine = true, - visualTransformation = if (label == "비밀번호") PasswordVisualTransformation() else VisualTransformation.None, - keyboardOptions = if (label == "비밀번호") KeyboardOptions(keyboardType = KeyboardType.Password) else KeyboardOptions.Default + visualTransformation = + if (isPassword) + PasswordVisualTransformation() + else VisualTransformation.None, + keyboardOptions = + if (isPassword) + KeyboardOptions(keyboardType = KeyboardType.Password) + else KeyboardOptions.Default ) } } - @Composable fun LoginButton(text: String, onClick: () -> Unit) { Button( @@ -91,50 +153,33 @@ fun LoginButton(text: String, onClick: () -> Unit) { } } -@Composable -fun LoginCompose(userId: String, userPw: String, userName: String, userDescription: String) { - val context = LocalContext.current - var inputId by remember { mutableStateOf("") } - var inputPw by remember { mutableStateOf("") } - - Column( - modifier = Modifier - .fillMaxSize() - .padding(horizontal = 30.dp, vertical = 60.dp), - ) { - Text( - text = stringResource(id = R.string.title_login), - fontSize = 30.sp, - fontWeight = FontWeight.Bold, - textAlign = TextAlign.Center, - modifier = Modifier.fillMaxWidth() - ) - Spacer(modifier = Modifier.height(60.dp)) - LoginInputField(label = stringResource(id = R.string.tv_id), value = inputId) { inputId = it } - Spacer(modifier = Modifier.height(60.dp)) - LoginInputField(label = stringResource(id = R.string.tv_pw), value = inputPw) { inputPw = it } - Spacer(modifier = Modifier.weight(2f)) - LoginButton(stringResource(id = R.string.btn_login)) { - if (userId == inputId && userPw == inputPw) { - moveToMain(context, userId, userPw, userName, userDescription) - } - } - Spacer(modifier = Modifier.height(10.dp)) - LoginButton(stringResource(id = R.string.btn_sign_up)) { - val intent = Intent(context, SignUpActivity::class.java) - context.startActivity(intent) - } +private fun checkLogin( + context: Context, + userId: String, + userPw: String, + userName: String, + userDescription: String, + inputId: String, + inputPw: String +) { + if (userId == inputId && userPw == inputPw) { + moveToMain(context, userId, userPw, userName, userDescription) } } -private fun moveToMain(context: Context, userId: String, userPw: String, userName: String, userDescription: String) { - val intent = Intent(context, MainActivity::class.java).apply { +private fun moveToMain( + context: Context, + userId: String, + userPw: String, + userName: String, + userDescription: String +) { + Intent(context, MainActivity::class.java).apply { putExtra("userId", userId) putExtra("userPw", userPw) putExtra("userName", userName) putExtra("userDescription", userDescription) } - context.startActivity(intent) Toast.makeText(context, "로그인 성공!", Toast.LENGTH_SHORT).show() } @@ -142,6 +187,6 @@ private fun moveToMain(context: Context, userId: String, userPw: String, userNam @Composable fun LoginPreview() { NOWSOPTAndroidTheme { - LoginCompose("","","","") + LoginView("","","","") } } \ No newline at end of file