-
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.
Merge pull request #26 from Team-Ampersand/feature/25_write_auth_pres…
…entation_logic 🔀 :: (#25) write auth presentation logic
- Loading branch information
Showing
11 changed files
with
73 additions
and
24 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
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
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
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
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
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
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
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
Empty file.
41 changes: 41 additions & 0 deletions
41
presentation/src/main/java/com/msg/presentation/viewmodel/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,41 @@ | ||
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) | ||
val loginState = _loginState.asStateFlow() | ||
|
||
fun login(loginRequestModel: LoginRequestModel) = viewModelScope.launch { | ||
loginUseCase(loginRequestModel) | ||
.onSuccess { | ||
_loginState.value = Event.Success(it) | ||
saveTokenUseCase( | ||
accessToken = it.accessToken, | ||
refreshToken = it.refreshToken, | ||
expiresAt = it.expiresAt | ||
) | ||
} | ||
.onFailure { | ||
it.exceptionHandling( | ||
badRequestAction = {}, | ||
notFoundAction = {} | ||
) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
presentation/src/main/java/com/msg/presentation/viewmodel/util/Event.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,15 @@ | ||
package com.msg.presentation.viewmodel.util | ||
|
||
sealed class Event<out T>(val data: T? = null) { | ||
object Loading : Event<Nothing>() | ||
class Success<T>(data: T? = null) : Event<T>(data = data) | ||
object BadRequest : Event<Nothing>() | ||
object Unauthorized : Event<Nothing>() | ||
object ForBidden : Event<Nothing>() | ||
object NotFound : Event<Nothing>() | ||
object NotAcceptable : Event<Nothing>() | ||
object TimeOut : Event<Nothing>() | ||
object Conflict : Event<Nothing>() | ||
object Server : Event<Nothing>() | ||
object UnKnown : Event<Nothing>() | ||
} |