generated from AND-SOPT-ANDROID/and-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.
- Loading branch information
1 parent
5fedc56
commit 2b47925
Showing
7 changed files
with
82 additions
and
56 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
11 changes: 0 additions & 11 deletions
11
app/src/main/java/org/sopt/and/presentation/signin/SignInSideEffect.kt
This file was deleted.
Oops, something went wrong.
56 changes: 29 additions & 27 deletions
56
app/src/main/java/org/sopt/and/presentation/signin/SignInViewModel.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 |
---|---|---|
@@ -1,55 +1,57 @@ | ||
package org.sopt.and.presentation.signin | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import org.sopt.and.core.viewmodel.BaseViewModel | ||
import org.sopt.and.domain.entity.User | ||
import org.sopt.and.domain.usecase.SignInUseCase | ||
import org.sopt.and.presentation.signin.state.SignInUiState | ||
import org.sopt.and.presentation.signin.contract.SignInEvent | ||
import org.sopt.and.presentation.signin.contract.SignInSideEffect | ||
import org.sopt.and.presentation.signin.contract.SignInUiState | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SignInViewModel @Inject constructor( | ||
private val signInUseCase: SignInUseCase | ||
) : ViewModel() { | ||
private var _uiState = MutableStateFlow(SignInUiState()) | ||
val uiState = _uiState.asStateFlow() | ||
) : BaseViewModel<SignInUiState, SignInSideEffect, SignInEvent>() { | ||
|
||
private var _sideEffect = MutableSharedFlow<SignInSideEffect>() | ||
val sideEffect = _sideEffect.asSharedFlow() | ||
override fun createInitialState(): SignInUiState = SignInUiState() | ||
|
||
fun updateId(id: String) = _uiState.update { currentState -> | ||
currentState.copy( | ||
id = id | ||
) | ||
} | ||
override suspend fun handleEvent(event: SignInEvent) { | ||
when (event) { | ||
is SignInEvent.OnIdTextFieldChanged -> { | ||
setState { copy(id = event.id) } | ||
} | ||
|
||
is SignInEvent.OnPasswordTextFieldChanged -> { | ||
setState { copy(password = event.password) } | ||
} | ||
|
||
fun updatePassword(password: String) = _uiState.update { currentState -> | ||
currentState.copy( | ||
password = password | ||
) | ||
SignInEvent.OnSignInButtonClicked -> { | ||
postSignIn() | ||
} | ||
|
||
SignInEvent.OnSignUpButtonClicked -> { | ||
navigateToSignUp() | ||
} | ||
} | ||
} | ||
|
||
fun onSignUpButtonClick() { | ||
private fun navigateToSignUp() { | ||
viewModelScope.launch { | ||
_sideEffect.emit(SignInSideEffect.NavigateToSignUp) | ||
setSideEffect(SignInSideEffect.NavigateToSignUp) | ||
} | ||
} | ||
|
||
fun onSignInButtonClick() = viewModelScope.launch { | ||
val user = with(_uiState.value) { User(id, password, "") } | ||
private fun postSignIn() = viewModelScope.launch { | ||
val user = with(currentState) { User(id, password, "") } | ||
signInUseCase.invoke(user).onSuccess { response -> | ||
response.token?.run { | ||
_sideEffect.emit(SignInSideEffect.NavigateToHome(this)) | ||
setSideEffect(SignInSideEffect.NavigateToHome(this)) | ||
} | ||
}.onFailure { throwable -> | ||
_sideEffect.emit(SignInSideEffect.SnackBar(throwable.message.orEmpty())) | ||
setSideEffect(SignInSideEffect.ShowSnackBar(throwable.message.orEmpty())) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/org/sopt/and/presentation/signin/contract/SignInEvent.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,10 @@ | ||
package org.sopt.and.presentation.signin.contract | ||
|
||
import org.sopt.and.core.viewmodel.UiEvent | ||
|
||
sealed class SignInEvent: UiEvent { | ||
data class OnIdTextFieldChanged(val id: String): SignInEvent() | ||
data class OnPasswordTextFieldChanged(val password: String): SignInEvent() | ||
data object OnSignInButtonClicked: SignInEvent() | ||
data object OnSignUpButtonClicked: SignInEvent() | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/org/sopt/and/presentation/signin/contract/SignInSideEffect.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,10 @@ | ||
package org.sopt.and.presentation.signin.contract | ||
|
||
import org.sopt.and.core.viewmodel.UiSideEffect | ||
|
||
sealed interface SignInSideEffect: UiSideEffect { | ||
data class ShowToast(val message: String) : SignInSideEffect | ||
data class ShowSnackBar(val message: String) : SignInSideEffect | ||
data class NavigateToHome(val token: String) : SignInSideEffect | ||
data object NavigateToSignUp : SignInSideEffect | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/org/sopt/and/presentation/signin/contract/SignInUiState.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,9 @@ | ||
package org.sopt.and.presentation.signin.contract | ||
|
||
import org.sopt.and.core.viewmodel.UiState | ||
|
||
|
||
data class SignInUiState( | ||
val id: String = "", | ||
val password: String = "" | ||
): UiState |
6 changes: 0 additions & 6 deletions
6
app/src/main/java/org/sopt/and/presentation/signin/state/SignInUiState.kt
This file was deleted.
Oops, something went wrong.