Skip to content

Commit

Permalink
✨ :: Flow 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Cjsghkd committed Sep 8, 2023
1 parent a84ccb7 commit 42ac3f2
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@ package com.msg.presentation.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.msg.domain.model.auth.LoginRequestModel
import com.msg.domain.model.auth.LoginResponseModel
import com.msg.domain.usecase.auth.LoginUseCase
import com.msg.domain.usecase.auth.SaveTokenUseCase
import com.msg.presentation.exception.exceptionHandling
import com.msg.presentation.viewmodel.util.Event
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

class LoginViewModel @Inject constructor(
private val loginUseCase: LoginUseCase,
private val saveTokenUseCase: SaveTokenUseCase
): ViewModel() {

private val _loginState = MutableStateFlow<Event<LoginResponseModel>>(Event.Loading)
var loginState = _loginState.asStateFlow()

fun login(loginRequestModel: LoginRequestModel) {
viewModelScope.launch {
loginUseCase(loginRequestModel)
.onSuccess {
_loginState.value = Event.Success(it)
saveTokenUseCase(
accessToken = it.accessToken,
refreshToken = it.refreshToken,
Expand Down

0 comments on commit 42ac3f2

Please sign in to comment.