Skip to content

Commit

Permalink
Chore #11: edit parameter email to username
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeeum committed Nov 8, 2024
1 parent f68a47e commit 9712368
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/org/sopt/and/feature/signin/SignInState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.and.feature.signin

data class SignInState(
val username: String = "",
val password: String = "",
var isPasswordVisible: Boolean = false,
) {
val isButtonEnabled: Boolean = username.isNotEmpty() && password.isNotEmpty()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.and.signin
package org.sopt.and.feature.signin

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand All @@ -10,10 +10,16 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.sopt.and.domain.entity.request.RequestSignInEntity
import org.sopt.and.domain.repository.WavveRepository
import org.sopt.and.sharedpreference.User
import javax.inject.Inject

@HiltViewModel
class SignInViewModel @Inject constructor() : ViewModel() {
class SignInViewModel @Inject constructor(
private val user:User,
private val wavveRepository: WavveRepository
) : ViewModel() {
private val _state = MutableStateFlow(SignInState())
val state: StateFlow<SignInState>
get() = _state.asStateFlow()
Expand All @@ -22,9 +28,9 @@ class SignInViewModel @Inject constructor() : ViewModel() {
val sideEffect: SharedFlow<SignInSideEffect>
get() = _sideEffect.asSharedFlow()

fun setEmail(email: String) {
fun setUsername(username: String) {
_state.value = _state.value.copy(
email = email
username = username
)
}

Expand All @@ -40,21 +46,23 @@ class SignInViewModel @Inject constructor() : ViewModel() {
)
}

fun showSnackBar(signUpEmail: String, signUpPassword: String) {
fun isSignInValid() {
viewModelScope.launch {
val toastMessage: String

if (isSignInValid(signUpEmail, signUpPassword)) {
var toastMessage: String = ""
wavveRepository.getUserId(
RequestSignInEntity(
username = _state.value.username,
password = _state.value.password
)
).onSuccess { SignInEntity ->
toastMessage = "로그인에 성공했습니다."
user.saveUserToken(SignInEntity.token)
user.setSignInState(true)
_sideEffect.emit(SignInSideEffect.NavigateToHome)
} else {
}.onFailure {
toastMessage = "로그인에 실패했습니다."
}
_sideEffect.emit(SignInSideEffect.ShowSnackBar(toastMessage))
}
}

fun isSignInValid(signUpEmail: String, signUpPassword: String): Boolean {
return (_state.value.email.isNotBlank() && _state.value.password.isNotBlank() && _state.value.email == signUpEmail && _state.value.password == signUpPassword)
}
}

0 comments on commit 9712368

Please sign in to comment.