Skip to content

Commit

Permalink
refactor/#9: LoginLiveData Backing Property
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 24, 2024
1 parent ae98201 commit ef411de
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.sopt.now.test.data.ApiFactory
import com.sopt.now.test.data.ServicePool
import com.sopt.now.test.data.BaseState
import com.sopt.now.test.data.ServicePool
import com.sopt.now.test.data.UserPreference
import com.sopt.now.test.data.dto.request.RequestLoginDto
import com.sopt.now.test.data.dto.response.ResponseAuthDto
Expand All @@ -16,10 +16,11 @@ import retrofit2.Response

class LoginViewModel : ViewModel() {
private val authService by lazy { ServicePool.authService }
val liveData = MutableLiveData<BaseState>()
private val _postLoginLiveData: MutableLiveData<BaseState> = MutableLiveData()
val postLoginLiveData: MutableLiveData<BaseState> = _postLoginLiveData
private lateinit var userPreference: UserPreference

fun login(request: RequestLoginDto) {
fun postLogin(request: RequestLoginDto) {
authService.postLogin(request).enqueue(object : Callback<ResponseAuthDto> {
override fun onResponse(
call: Call<ResponseAuthDto>,
Expand All @@ -31,22 +32,22 @@ class LoginViewModel : ViewModel() {
if (userId != null) {
userPreference.saveUserId(userId)
}
liveData.value = BaseState(
_postLoginLiveData.value = BaseState(
isSuccess = true,
message = "로그인 성공! 유저의 ID는 $userId 입니다."
)
Log.d("Login", "data: $data, userId: $userId")
} else {
val error = response.message()
liveData.value = BaseState(
_postLoginLiveData.value = BaseState(
isSuccess = false,
message = "로그인 실패 $error"
)
}
}

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

0 comments on commit ef411de

Please sign in to comment.