generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
175 additions
and
128 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
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
79 changes: 0 additions & 79 deletions
79
app/src/main/java/com/sopt/now/compose/ui/login/LoginScreen.kt
This file was deleted.
Oops, something went wrong.
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
106 changes: 106 additions & 0 deletions
106
app/src/main/java/com/sopt/now/compose/ui/signIn/SignInScreen.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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.sopt.now.compose.ui.signIn | ||
|
||
import android.widget.Toast | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavHostController | ||
import com.sopt.now.compose.R | ||
import com.sopt.now.compose.data.model.RequestSignInDto | ||
import com.sopt.now.compose.ui.base.SoptInputTextField | ||
import com.sopt.now.compose.ui.base.SoptOutlinedButton | ||
import com.sopt.now.compose.ui.base.SoptPasswordTextField | ||
|
||
@Composable | ||
fun SignInScreen( | ||
onNavigateToHome: NavHostController, | ||
onNavigateToSignUp: () -> Unit, | ||
signInViewModel: SignInViewModel = viewModel(), | ||
) { | ||
val context = LocalContext.current | ||
val signUpState by signInViewModel.signInState.collectAsState() | ||
|
||
var id by remember { mutableStateOf("") } | ||
var password by remember { mutableStateOf("") } | ||
|
||
LaunchedEffect(signUpState) { | ||
if (signUpState.isSuccess) { | ||
Toast.makeText(context, signUpState.message, Toast.LENGTH_SHORT).show() | ||
onNavigateToHome.navigate("main") | ||
} else if (signUpState.message.isNotBlank()) { | ||
Toast.makeText(context, signUpState.message, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
|
||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.SpaceBetween | ||
) { | ||
val isSignInButtonEnabled by remember(id, password) { | ||
mutableStateOf( | ||
id.isNotEmpty() && password.isNotEmpty() | ||
) | ||
} | ||
|
||
Text( | ||
text = stringResource(id = R.string.sign_in_screen_title), | ||
fontSize = 24.sp, | ||
fontWeight = FontWeight.Bold, | ||
modifier = Modifier.padding(16.dp) | ||
) | ||
Column { | ||
SoptInputTextField( | ||
text = R.string.label_id, value = id, onValueChange = { id = it } | ||
) | ||
SoptPasswordTextField( | ||
text = R.string.label_pw, value = password, onValueChange = { password = it } | ||
) | ||
} | ||
|
||
Column { | ||
SoptOutlinedButton( | ||
text = R.string.btn_sign_in, | ||
onClick = { | ||
if (isSignInButtonEnabled) { | ||
signInViewModel.signIn( | ||
RequestSignInDto( | ||
authenticationId = id, | ||
password = password, | ||
) | ||
) | ||
} else { | ||
Toast.makeText( | ||
context, | ||
"로그인 실패", | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
}, | ||
enabled = true | ||
) | ||
SoptOutlinedButton( | ||
text = R.string.btn_sign_up, | ||
onClick = onNavigateToSignUp, | ||
enabled = true | ||
) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
app/src/main/java/com/sopt/now/compose/ui/signIn/SignInViewModel.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.sopt.now.compose.ui.signIn | ||
|
||
import androidx.lifecycle.ViewModel | ||
import com.sopt.now.compose.data.model.RequestSignInDto | ||
import com.sopt.now.compose.data.model.ResponseSignInDto | ||
import com.sopt.now.compose.data.model.SignInState | ||
import com.sopt.now.compose.data.module.ServicePool | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
|
||
class SignInViewModel : ViewModel() { | ||
private val authService by lazy { ServicePool.authService } | ||
|
||
private val _signInState = MutableStateFlow(SignInState(isSuccess = false, message = "")) | ||
val signInState = _signInState.asStateFlow() | ||
|
||
fun signIn(request: RequestSignInDto) { | ||
authService.signIn(request).enqueue( | ||
object : Callback<ResponseSignInDto> { | ||
override fun onResponse( | ||
call: Call<ResponseSignInDto>, | ||
response: Response<ResponseSignInDto>, | ||
) { | ||
if (response.isSuccessful) { | ||
val userId = response.headers()["location"] | ||
|
||
_signInState.update { | ||
SignInState( | ||
isSuccess = true, | ||
message = "유저 아이디는 $userId" | ||
) | ||
} | ||
} else { | ||
val error = response.code() | ||
_signInState.update { | ||
SignInState( | ||
isSuccess = false, | ||
message = "로그인 실패 : $error" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseSignInDto>, t: Throwable) { | ||
_signInState.update { SignInState(isSuccess = false, message = "서버 에러") } | ||
} | ||
} | ||
) | ||
} | ||
} |
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
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