generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/#9 : LoginViewModel에 코루틴 및 UiState 적용
- Loading branch information
1 parent
a0154ee
commit d7e4a53
Showing
3 changed files
with
40 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/sopt/now/test/presentation/login/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) } | ||
) | ||
} | ||
} | ||
} |