Skip to content

Commit

Permalink
[MOD/#19] enqueue로 통신 했던 것을 코루틴 suspend로 대체 - 로그인 성공, response 받아오는 건 못함
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyuri1114 committed Dec 6, 2023
1 parent d00240b commit 363da8f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface AuthService {

// 로그인
@POST("api/v1/members/sign-in")
fun login(
suspend fun login(
@Body request: RequestLoginDto,
): Call<ResponseLoginDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import android.view.MotionEvent
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import org.sopt.dosopttemplate.R
import org.sopt.dosopttemplate.data.User
import org.sopt.dosopttemplate.databinding.ActivityLoginBinding
import org.sopt.dosopttemplate.di.UserSharedPreferences
import org.sopt.dosopttemplate.presentation.main.BnvActivity
import org.sopt.dosopttemplate.util.BackPressedUtil
import org.sopt.dosopttemplate.util.UiState
import org.sopt.dosopttemplate.util.hideKeyboard
import org.sopt.dosopttemplate.util.showShortSnackBar

class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
Expand Down Expand Up @@ -48,26 +51,31 @@ class LoginActivity : AppCompatActivity() {
val inputId = binding.etSignupId.text.toString()
val inputPw = binding.etSignupPw.text.toString()

loginViewModel.loginUser(inputId, inputPw, this)
loginViewModel.loginUser(inputId, inputPw)

loginViewModel.loginResult.observe(
this,
) { loginSuccessful ->
if (loginSuccessful) {
// showShortToast("getString(R.string.login_success)")

if (binding.cbLoginAutologin.isChecked) {
signUpUser?.let {
loginViewModel.saveUserForAutoLogin(this, it)
) { uiState ->
when (uiState) {
is UiState.Success -> {
if (binding.cbLoginAutologin.isChecked) {
signUpUser?.let {
loginViewModel.saveUserForAutoLogin(this, it)
}
}
val intent = Intent(this, BnvActivity::class.java)
intent.putExtra("signUpUser", signUpUser)
startActivity(intent)
finish()
}

is UiState.Failure -> {
//showShortSnackBar(binding.root, "로그인 성공, 유저 아이디 : ${loginViewModel.}")
}

val intent = Intent(this, BnvActivity::class.java)
intent.putExtra("signUpUser", signUpUser)
startActivity(intent)
finish()
} else {
// showShortSnackBar(binding.root, getString(R.string.login_fail))
is UiState.Loading -> {
showShortSnackBar(binding.root, "로딩중")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,38 @@
package org.sopt.dosopttemplate.presentation.auth

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.data.User
import org.sopt.dosopttemplate.data.remote.ServicePool.authService
import org.sopt.dosopttemplate.data.remote.ServicePool
import org.sopt.dosopttemplate.data.remote.request.RequestLoginDto
import org.sopt.dosopttemplate.data.remote.response.ResponseLoginDto
import org.sopt.dosopttemplate.di.UserSharedPreferences
import org.sopt.dosopttemplate.util.UiState
import retrofit2.Call
import retrofit2.Response

class LoginViewModel : ViewModel() {
private val _loginResult = MutableLiveData<Boolean>()
val loginResult: LiveData<Boolean> get() = _loginResult

fun loginUser(inputId: String, inputPw: String, context: Context) {
// _loginResult.value =
// signUpUser != null && signUpUser.userId == inputId && signUpUser.userPw == inputPw

authService.login(RequestLoginDto(inputId, inputPw))
.enqueue(object : retrofit2.Callback<ResponseLoginDto> {
override fun onResponse(
call: Call<ResponseLoginDto>,
response: Response<ResponseLoginDto>,
) {
if (response.isSuccessful) {
val data: ResponseLoginDto =
response.body() ?: ResponseLoginDto(-1, "null", "null")
val userId = data.id

Toast.makeText(
context,
"로그인이 성공하였고 유저의 ID는 $userId 입니둥",
Toast.LENGTH_SHORT,
).show()
_loginResult.value = true
}
}

override fun onFailure(call: Call<ResponseLoginDto>, t: Throwable) {
Toast.makeText(
context,
"ㅜ ㅜ 서버 에러 발생 ㅜ ㅜ",
Toast.LENGTH_SHORT,
).show()

_loginResult.value = false
}
})
private val _loginResult = MutableLiveData<UiState<Boolean>>()
val loginResult: LiveData<UiState<Boolean>> get() = _loginResult

fun loginUser(inputId: String, inputPw: String) = viewModelScope.launch {
_loginResult.value = UiState.Loading
lateinit var getUserInfo: Call<ResponseLoginDto>

runCatching {
ServicePool.authService.login(
RequestLoginDto(inputId, inputPw),
)
}.onSuccess {
// getUserInfo = it
}.onFailure {
_loginResult.value = UiState.Failure(it.message.toString())
}

_loginResult.value = UiState.Success(true)
}

fun saveUserForAutoLogin(context: Context, signUpUser: User) {
Expand Down

0 comments on commit 363da8f

Please sign in to comment.