Skip to content

Commit

Permalink
refactor/#6: 상태 호이스팅 및 조건문 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 2, 2024
1 parent a9e7d2d commit 2e0c262
Showing 1 changed file with 88 additions and 43 deletions.
131 changes: 88 additions & 43 deletions app/src/main/java/com/sopt/now/compose/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,72 @@ 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)
}
}
}
}
}

@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()
) {
Expand All @@ -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(
Expand All @@ -91,57 +153,40 @@ 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()
}

@Preview(showBackground = true)
@Composable
fun LoginPreview() {
NOWSOPTAndroidTheme {
LoginCompose("","","","")
LoginView("","","","")
}
}

0 comments on commit 2e0c262

Please sign in to comment.