Skip to content

Commit

Permalink
refactor/#9 : LoginViewModel에 코루틴 및 UiState 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed Jun 6, 2024
1 parent a0154ee commit d7e4a53
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 64 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/test/data/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.io.IOException
object ApiFactory {
private const val AUTH_BASE_URL: String = BuildConfig.AUTH_BASE_URL
private const val FRIEND_BASE_URL: String = BuildConfig.FRIEND_BASE_URL
private lateinit var userPreference: UserPreference // UserPreference 추가
lateinit var userPreference: UserPreference // UserPreference 추가

// UserPreference 초기화
fun initializeUserPreference(userPreference: UserPreference) {
Expand Down
63 changes: 0 additions & 63 deletions app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.sopt.now.test.presentation.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.test.core.view.UiState
import com.sopt.now.test.data.ApiFactory
import com.sopt.now.test.data.ServicePool
import com.sopt.now.test.data.dto.request.RequestLoginDto
import com.sopt.now.test.data.dto.response.ResponseAuthDto
import kotlinx.coroutines.launch

class LoginViewModel : ViewModel() {
private val _postLoginLiveData: MutableLiveData<UiState<ResponseAuthDto>> = MutableLiveData()
val postLoginLiveData: MutableLiveData<UiState<ResponseAuthDto>> = _postLoginLiveData

fun postLogin(request: RequestLoginDto) {
viewModelScope.launch {
runCatching {
ServicePool.authService.postLogin(request)
}.fold(
{
val response = ServicePool.authService.postLogin(request)
val userId = response.headers()["location"]
if (userId != null) {
ApiFactory.userPreference.saveUserId(userId)
}
_postLoginLiveData.value = UiState.Success(
response.body() ?: ResponseAuthDto(
-1,
"Empty response body"
)
)
},
{ _postLoginLiveData.value = UiState.Failure(it.message.toString()) }
)
}
}
}

0 comments on commit d7e4a53

Please sign in to comment.