diff --git a/core/domain/src/commonMain/kotlin/club/nito/core/domain/SignInUseCase.kt b/core/domain/src/commonMain/kotlin/club/nito/core/domain/SignInUseCase.kt index c840fad0..aebc7295 100644 --- a/core/domain/src/commonMain/kotlin/club/nito/core/domain/SignInUseCase.kt +++ b/core/domain/src/commonMain/kotlin/club/nito/core/domain/SignInUseCase.kt @@ -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 } 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 = runExecuting { + authRepository.signIn( + email = email, + password = password, + ) + } } diff --git a/core/model/src/commonMain/kotlin/club/nito/core/model/ExecuteResult.kt b/core/model/src/commonMain/kotlin/club/nito/core/model/ExecuteResult.kt index 76d8df04..dc722e5d 100644 --- a/core/model/src/commonMain/kotlin/club/nito/core/model/ExecuteResult.kt +++ b/core/model/src/commonMain/kotlin/club/nito/core/model/ExecuteResult.kt @@ -14,3 +14,9 @@ sealed interface ExecuteResult { */ data class Failure(val error: NitoError?) : ExecuteResult } + +suspend fun runExecuting(block: suspend () -> T): ExecuteResult = try { + ExecuteResult.Success(block()) +} catch (e: Throwable) { + ExecuteResult.Failure(e.toNitoError()) +} diff --git a/feature/auth/src/androidMain/kotlin/club/nito/feature/auth/SignInViewModel.kt b/feature/auth/src/androidMain/kotlin/club/nito/feature/auth/SignInViewModel.kt index 33af1a83..bc092498 100644 --- a/feature/auth/src/androidMain/kotlin/club/nito/feature/auth/SignInViewModel.kt +++ b/feature/auth/src/androidMain/kotlin/club/nito/feature/auth/SignInViewModel.kt @@ -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 @@ -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 -> {} } }