Skip to content

Commit

Permalink
feat/#11: 로그인 서버 통신
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 3, 2024
1 parent 47c12ff commit 1938b2e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.now.compose
package com.sopt.now.compose.presentation

import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -32,9 +32,16 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sopt.now.compose.R
import com.sopt.now.compose.data.ApiFactory
import com.sopt.now.compose.data.UserPreference
import com.sopt.now.compose.data.dto.request.RequestLoginDto
import com.sopt.now.compose.ui.theme.NOWSOPTAndroidTheme

class LoginActivity : ComponentActivity() {

private lateinit var userPreference: UserPreference

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand All @@ -43,12 +50,12 @@ class LoginActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val userId = intent.getStringExtra("userId").toString()
val userPw = intent.getStringExtra("userPw").toString()
val userName = intent.getStringExtra("userName").toString()
val userDescription = intent.getStringExtra("userDescription").toString()

LoginView(userId, userPw, userName ,userDescription)
userPreference = UserPreference(this)
ApiFactory.initializeUserPreference(userPreference)

val viewModel: LoginViewModel = remember { LoginViewModel() }
LoginView(viewModel)
}
}
}
Expand All @@ -57,15 +64,13 @@ class LoginActivity : ComponentActivity() {

@Composable
fun LoginView(
userId: String,
userPw: String,
userName: String,
userDescription: String
viewModel: LoginViewModel
) {
val context = LocalContext.current
var inputId by remember { mutableStateOf("") }
var inputPw by remember { mutableStateOf("") }


Column(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -94,7 +99,7 @@ fun LoginView(
LoginButton(
text = stringResource(id = R.string.btn_login),
onClick = {
checkLogin(context, userId, userPw, userName, userDescription, inputId, inputPw)
checkLogin(context, viewModel, inputId, inputPw)
}
)
Spacer(modifier = Modifier.height(10.dp))
Expand Down Expand Up @@ -155,38 +160,32 @@ fun LoginButton(text: String, onClick: () -> Unit) {

private fun checkLogin(
context: Context,
userId: String,
userPw: String,
userName: String,
userDescription: String,
viewModel: LoginViewModel,
inputId: String,
inputPw: String
) {
if (userId == inputId && userPw == inputPw) {
moveToMain(context, userId, userPw, userName, userDescription)
viewModel.login(RequestLoginDto(inputId, inputPw))

val userId = viewModel.userIdLiveData.value
if(userId != null){
ApiFactory.userPreference.saveUserId(userId)
moveToMain(context)
}
}

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)
}
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)

Toast.makeText(context, "로그인 성공!", Toast.LENGTH_SHORT).show()
}

@Preview(showBackground = true)
@Composable
fun LoginPreview() {
NOWSOPTAndroidTheme {
LoginView("","","","")
LoginView(viewModel = LoginViewModel())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.sopt.now.compose.presentation

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.sopt.now.compose.data.BaseState
import com.sopt.now.compose.data.ServicePool
import com.sopt.now.compose.data.dto.request.RequestLoginDto
import com.sopt.now.compose.data.dto.response.ResponseAuthDto
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginViewModel : ViewModel() {
private val authService by lazy { ServicePool.authService }
val liveData = MutableLiveData<BaseState>()
val userIdLiveData = MutableLiveData<String?>()

fun login(request: RequestLoginDto) {
authService.login(request).enqueue(object : Callback<ResponseAuthDto> {
override fun onResponse(
call: Call<ResponseAuthDto>,
response: Response<ResponseAuthDto>,
) {
if (response.isSuccessful) {
val data: ResponseAuthDto? = response.body()
val userId = response.headers()["location"]
userIdLiveData.value = userId
liveData.value = BaseState(
isSuccess = true,
message = "로그인 성공! 유저의 ID는 $userId 입니다."
)
Log.d("Login", "data: $data, userId: $userId")
} else {
val error = response.message()
liveData.value = BaseState(
isSuccess = false,
message = "로그인 실패 $error"
)
}
}

override fun onFailure(call: Call<ResponseAuthDto>, t: Throwable) {
liveData.value = BaseState(
isSuccess = false,
message = "서버 에러"
)
}
})
}
}

0 comments on commit 1938b2e

Please sign in to comment.