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.
#13 [Feat] : State, SideEffect, Event에 따른 LogIn 수정
- Loading branch information
1 parent
637a829
commit 0f3f9bb
Showing
10 changed files
with
152 additions
and
79 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
app/src/main/java/org/sopt/and/presentation/login/LogInContract.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,26 @@ | ||
package org.sopt.and.presentation.login | ||
|
||
import org.sopt.and.presentation.core.UiEvent | ||
import org.sopt.and.presentation.core.UiSideEffect | ||
import org.sopt.and.presentation.core.UiState | ||
|
||
// State 정의 | ||
data class LogInState( | ||
val username: String = "", | ||
val password: String = "", | ||
val isLoading: Boolean = false, | ||
val errorMessage: Int? = null | ||
) : UiState | ||
|
||
// Event 정의 | ||
sealed class LogInEvent : UiEvent { | ||
data class UsernameChanged(val username: String) : LogInEvent() | ||
data class PasswordChanged(val password: String) : LogInEvent() | ||
object LogInClicked : LogInEvent() | ||
} | ||
|
||
// SideEffect 정의 | ||
sealed class LogInEffect : UiSideEffect { | ||
data class ShowSnackBar(val message: Int) : LogInEffect() | ||
object NavigateToMain : LogInEffect() | ||
} |
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
72 changes: 72 additions & 0 deletions
72
app/src/main/java/org/sopt/and/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,72 @@ | ||
package org.sopt.and.presentation.login | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import org.sopt.and.domain.model.UserLoginRequest | ||
import org.sopt.and.domain.repository.UserRepository | ||
import org.sopt.and.presentation.core.BaseViewModel | ||
import javax.inject.Inject | ||
import org.sopt.and.R | ||
import retrofit2.HttpException | ||
|
||
@HiltViewModel | ||
class LogInViewModel @Inject constructor( | ||
private val userRepository: UserRepository | ||
) : BaseViewModel<LogInState, LogInEffect, LogInEvent>() { | ||
|
||
override fun createInitialState(): LogInState = LogInState() | ||
|
||
override suspend fun handleEvent(event: LogInEvent) { | ||
when (event) { | ||
is LogInEvent.UsernameChanged -> { | ||
setState { copy(username = event.username) } | ||
} | ||
|
||
is LogInEvent.PasswordChanged -> { | ||
setState { copy(password = event.password) } | ||
} | ||
|
||
is LogInEvent.LogInClicked -> { | ||
logIn() | ||
} | ||
} | ||
} | ||
|
||
|
||
// 로그인 로직 | ||
fun logIn() { | ||
setState { copy(isLoading = true) } | ||
viewModelScope.launch { | ||
val state = uiState.value | ||
val result = userRepository.postUserLogin( | ||
UserLoginRequest( | ||
username = state.username, | ||
password = state.password | ||
) | ||
) | ||
result.fold( | ||
onSuccess = { | ||
setState { copy(isLoading = false) } | ||
setSideEffect { LogInEffect.NavigateToMain } | ||
}, | ||
onFailure = { error -> | ||
val message = if (error is HttpException) { | ||
Log.d("error", error.code().toString()) | ||
when (error.code()) { | ||
400 -> R.string.log_in_method | ||
403 -> R.string.sign_up_paswd | ||
else -> R.string.unknown_error | ||
} | ||
} else { | ||
R.string.unknown_error | ||
} | ||
setState { copy(isLoading = false) } | ||
setSideEffect { LogInEffect.ShowSnackBar(message) } | ||
} | ||
) | ||
} | ||
} | ||
|
||
} |
8 changes: 0 additions & 8 deletions
8
app/src/main/java/org/sopt/and/presentation/login/LoginState.kt
This file was deleted.
Oops, something went wrong.
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
8 changes: 0 additions & 8 deletions
8
app/src/main/java/org/sopt/and/presentation/signup/RegisterState.kt
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.sopt.and.util | ||
|
||
import android.content.Context | ||
import androidx.compose.material3.SnackbarHostState | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
fun SnackbarHostState.showSnackBar( | ||
context: Context, | ||
scope: CoroutineScope, | ||
message: Int, | ||
actionLabel: String? = "확인" | ||
) { | ||
scope.launch { | ||
this@showSnackBar.showSnackbar(context.getString(message), actionLabel) | ||
} | ||
} |
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