Skip to content

Commit

Permalink
[Feat] SignIn API 구현 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed May 3, 2024
1 parent 3094e49 commit 3c9c7c7
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 128 deletions.
6 changes: 1 addition & 5 deletions app/src/main/java/com/sopt/now/compose/ui/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import com.sopt.now.compose.ui.search.SearchScreen

@Composable
fun MainScreen(
id: String,
pw: String,
nickname: String,
mbti: String,
) {
val navController = rememberNavController()
Scaffold(
Expand All @@ -36,7 +32,7 @@ fun MainScreen(
SearchScreen()
}
composable(Screen.MyPage.route) {
MyPageScreen(id, pw, nickname, mbti)
MyPageScreen()
}
}
}
Expand Down
34 changes: 5 additions & 29 deletions app/src/main/java/com/sopt/now/compose/ui/base/SoptAppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package com.sopt.now.compose.ui.base

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.sopt.now.compose.ui.MainScreen
import com.sopt.now.compose.ui.login.LoginScreen
import com.sopt.now.compose.ui.signIn.SignInScreen
import com.sopt.now.compose.ui.signUp.SignUpScreen

@Composable
Expand All @@ -17,38 +15,16 @@ fun SoptAppNavHost() {

NavHost(navController = navController, startDestination = "sign_in") {
composable("sign_in") {
LoginScreen(
SignInScreen(
onNavigateToHome = navController,
onNavigateToSignUp = { navController.navigate("signup") },
)
}

composable(
"main?id={id}&pw={pw}&nickname={nickname}&mbti={mbti}",
arguments = listOf(
navArgument("id") {
type = NavType.StringType
defaultValue = ""
},
navArgument("pw") {
type = NavType.StringType
defaultValue = ""
},
navArgument("nickname") {
type = NavType.StringType
defaultValue = ""
},
navArgument("mbti") {
type = NavType.StringType
defaultValue = ""
},
),
) { backStackEntry ->
val id = backStackEntry.arguments?.getString("id")
val pw = backStackEntry.arguments?.getString("pw")
val nickname = backStackEntry.arguments?.getString("nickname")
val mbti = backStackEntry.arguments?.getString("mbti")
MainScreen(id.toString(), pw.toString(), nickname.toString(), mbti.toString())
"main",
) {
MainScreen()
}
composable("signup") {
SignUpScreen(onNavigateToSignIn = navController)
Expand Down
79 changes: 0 additions & 79 deletions app/src/main/java/com/sopt/now/compose/ui/login/LoginScreen.kt

This file was deleted.

16 changes: 5 additions & 11 deletions app/src/main/java/com/sopt/now/compose/ui/myPage/MyPageScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
Expand All @@ -19,12 +18,7 @@ import androidx.compose.ui.res.painterResource
import com.sopt.now.compose.R

@Composable
fun MyPageScreen(
id: String,
pw: String,
nickname: String,
mbti: String,
) {
fun MyPageScreen() {
var offset by remember { mutableFloatStateOf(0f) }

Column(modifier = Modifier
Expand All @@ -43,10 +37,10 @@ fun MyPageScreen(
modifier = Modifier.aspectRatio(3f / 4f)
)
Column {
Text(text = id)
Text(text = pw)
Text(text = nickname)
Text(text = mbti)
// Text(text = id)
// Text(text = pw)
// Text(text = nickname)
// Text(text = mbti)
}
}
}
106 changes: 106 additions & 0 deletions app/src/main/java/com/sopt/now/compose/ui/signIn/SignInScreen.kt
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
)
}
}
}
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 = "서버 에러") }
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ fun SignUpScreen(
phone = phone
)
)
onNavigateToSignIn.navigate("sign_in")
} else {
Toast.makeText(
context,
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<resources>
<string name="app_name">NOW SOPT Android</string>

<!-- LoginScreen-->
<string name="login_screen_title">Welcome To SOPT!</string>
<!-- SignInScreen-->
<string name="sign_in_screen_title">Welcome To SOPT!</string>
<string name="label_id">ID</string>
<string name="label_pw">Password</string>
<string name="label_nickname">Nickname</string>
<string name="label_phone">전화번호</string>
<string name="btn_login">로그인</string>
<string name="btn_sign_in">로그인</string>
<string name="btn_sign_up">회원가입</string>

<!-- SignUpScreen-->
Expand Down

0 comments on commit 3c9c7c7

Please sign in to comment.