Skip to content

Commit

Permalink
🐛 サインイン失敗時にクラッシュしないよう修正
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsutakein committed Nov 4, 2023
1 parent a190b5c commit 15534bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package club.nito.core.domain

import club.nito.core.data.AuthRepository
import club.nito.core.model.ExecuteResult
import club.nito.core.model.runExecuting

/**
* 直近のスケジュールを取得するユースケース
*/
sealed interface SignInUseCase {
suspend operator fun invoke(email: String, password: String)
suspend operator fun invoke(email: String, password: String): ExecuteResult<Unit>
}

class SignInExecutor(
private val authRepository: AuthRepository,
) : SignInUseCase {
override suspend fun invoke(email: String, password: String) = authRepository.signIn(
email = email,
password = password,
)
override suspend fun invoke(email: String, password: String): ExecuteResult<Unit> = runExecuting {
authRepository.signIn(
email = email,
password = password,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ sealed interface ExecuteResult<out T> {
*/
data class Failure(val error: NitoError?) : ExecuteResult<Nothing>
}

suspend fun <T> runExecuting(block: suspend () -> T): ExecuteResult<T> = try {
ExecuteResult.Success(block())
} catch (e: Throwable) {
ExecuteResult.Failure(e.toNitoError())
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import club.nito.core.domain.ObserveAuthStatusUseCase
import club.nito.core.domain.SignInUseCase
import club.nito.core.model.AuthStatus
import club.nito.core.model.ExecuteResult
import club.nito.core.model.FetchSingleResult
import club.nito.core.ui.buildUiState
import club.nito.core.ui.message.UserMessageStateHolder
Expand Down Expand Up @@ -65,7 +66,13 @@ class SignInViewModel @Inject constructor(
when (intent) {
is SignInIntent.ChangeInputEmail -> email.emit(intent.email)
is SignInIntent.ChangeInputPassword -> password.emit(intent.password)
SignInIntent.ClickSignIn -> signInUseCase(email.value, password.value)
SignInIntent.ClickSignIn -> {
val result = signInUseCase(email.value, password.value)
if (result is ExecuteResult.Failure) {
userMessageStateHolder.showMessage("ログインに失敗しました")
}
}

SignInIntent.ClickRegister -> {}
}
}
Expand Down

0 comments on commit 15534bc

Please sign in to comment.