diff --git a/app/src/main/java/jp/panta/misskeyandroidclient/ui/main/viewmodel/MainViewModel.kt b/app/src/main/java/jp/panta/misskeyandroidclient/ui/main/viewmodel/MainViewModel.kt index 0713956c9b..987828c0c1 100644 --- a/app/src/main/java/jp/panta/misskeyandroidclient/ui/main/viewmodel/MainViewModel.kt +++ b/app/src/main/java/jp/panta/misskeyandroidclient/ui/main/viewmodel/MainViewModel.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.launch import net.pantasystem.milktea.app_store.account.AccountStore import net.pantasystem.milktea.app_store.setting.SettingStore import net.pantasystem.milktea.common.Logger +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.mapCancellableCatching import net.pantasystem.milktea.common.runCancellableCatching import net.pantasystem.milktea.model.emoji.EmojiEventHandler @@ -118,7 +119,7 @@ class MainViewModel @Inject constructor( fun onPushNotificationConfirmed() { viewModelScope.launch { - configRepository.get().mapCancellableCatching { + configRepository.get().flatMapCancellableCatching{ configRepository.save(it.copy(isConfirmedPostNotification = true)) }.onFailure { logger.error("設定状態の保存に失敗", it) diff --git a/modules/common/src/main/java/net/pantasystem/milktea/common/ResultHelper.kt b/modules/common/src/main/java/net/pantasystem/milktea/common/ResultHelper.kt index 8a1d1f6e91..89a15c33ef 100644 --- a/modules/common/src/main/java/net/pantasystem/milktea/common/ResultHelper.kt +++ b/modules/common/src/main/java/net/pantasystem/milktea/common/ResultHelper.kt @@ -39,4 +39,17 @@ inline fun Result.mapCancellableCatching(transform: (value: T) -> R): } catch (e: Exception) { return Result.failure(e) } +} + +inline fun Result.flatMapCancellableCatching(transform: (value: T) -> Result): Result { + return try { + val result = getOrThrow() + transform(result) + } catch (e: CancellationException) { + throw e + } catch (e: OutOfMemoryError) { + throw e + } catch (e: Exception) { + return Result.failure(e) + } } \ No newline at end of file diff --git a/modules/features/drive/src/main/java/net/pantasystem/milktea/drive/viewmodel/DriveViewModel.kt b/modules/features/drive/src/main/java/net/pantasystem/milktea/drive/viewmodel/DriveViewModel.kt index 84cc6f0f77..f0d915eebe 100644 --- a/modules/features/drive/src/main/java/net/pantasystem/milktea/drive/viewmodel/DriveViewModel.kt +++ b/modules/features/drive/src/main/java/net/pantasystem/milktea/drive/viewmodel/DriveViewModel.kt @@ -26,7 +26,7 @@ import net.pantasystem.milktea.app_store.handler.UserActionAppGlobalErrorStore import net.pantasystem.milktea.common.Logger import net.pantasystem.milktea.common.PageableState import net.pantasystem.milktea.common.convert -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.runCancellableCatching import net.pantasystem.milktea.common_android.resource.StringSource import net.pantasystem.milktea.common_navigation.EXTRA_ACCOUNT_ID @@ -196,7 +196,7 @@ class DriveViewModel @Inject constructor( fun setUsingGridView(value: Boolean) { viewModelScope.launch { - configRepository.get().mapCancellableCatching { config -> + configRepository.get().flatMapCancellableCatching { config -> configRepository.save( config.copy(isDriveUsingGridView = value) ) diff --git a/modules/features/note/src/main/java/net/pantasystem/milktea/note/viewmodel/NotesViewModel.kt b/modules/features/note/src/main/java/net/pantasystem/milktea/note/viewmodel/NotesViewModel.kt index 665e8a5800..a87851f416 100644 --- a/modules/features/note/src/main/java/net/pantasystem/milktea/note/viewmodel/NotesViewModel.kt +++ b/modules/features/note/src/main/java/net/pantasystem/milktea/note/viewmodel/NotesViewModel.kt @@ -12,6 +12,7 @@ import net.pantasystem.milktea.app_store.handler.UserActionAppGlobalErrorAction import net.pantasystem.milktea.app_store.handler.UserActionAppGlobalErrorStore import net.pantasystem.milktea.app_store.notes.NoteTranslationStore import net.pantasystem.milktea.common.Logger +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.mapCancellableCatching import net.pantasystem.milktea.common_android.resource.StringSource import net.pantasystem.milktea.model.note.DeleteAndEditUseCase @@ -316,7 +317,7 @@ class NotesViewModel @Inject constructor( viewModelScope.launch { configRepository.get().mapCancellableCatching { it.copy(isShowWarningDisplayingSensitiveMedia = false) - }.mapCancellableCatching { + }.flatMapCancellableCatching { configRepository.save(it) }.onFailure { logger.error("警告表示の抑制に失敗", it) diff --git a/modules/features/notification/src/main/java/net/pantasystem/milktea/notification/viewmodel/NotificationViewModel.kt b/modules/features/notification/src/main/java/net/pantasystem/milktea/notification/viewmodel/NotificationViewModel.kt index 25d08fcadc..430eaf26ec 100644 --- a/modules/features/notification/src/main/java/net/pantasystem/milktea/notification/viewmodel/NotificationViewModel.kt +++ b/modules/features/notification/src/main/java/net/pantasystem/milktea/notification/viewmodel/NotificationViewModel.kt @@ -152,7 +152,7 @@ class NotificationViewModel @Inject constructor( if (it == 0) { runCancellableCatching { getCurrentAccount() - }.mapCancellableCatching { + }.flatMapCancellableCatching { notificationRepository.markAsRead(it.accountId) }.onFailure { e -> logger.error("failed mark as read", e) @@ -235,7 +235,7 @@ class NotificationViewModel @Inject constructor( viewModelScope.launch { runCancellableCatching { getCurrentAccount() - }.mapCancellableCatching { + }.flatMapCancellableCatching { notificationRepository.markAsRead(it.accountId) }.onSuccess { loadInit() diff --git a/modules/features/setting/src/main/java/net/pantasystem/milktea/setting/viewmodel/muteword/ClientWordFilterSettingViewModel.kt b/modules/features/setting/src/main/java/net/pantasystem/milktea/setting/viewmodel/muteword/ClientWordFilterSettingViewModel.kt index 8ca495c8d7..7293ec2c42 100644 --- a/modules/features/setting/src/main/java/net/pantasystem/milktea/setting/viewmodel/muteword/ClientWordFilterSettingViewModel.kt +++ b/modules/features/setting/src/main/java/net/pantasystem/milktea/setting/viewmodel/muteword/ClientWordFilterSettingViewModel.kt @@ -7,7 +7,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.model.note.muteword.WordFilterConfigRepository import net.pantasystem.milktea.model.note.muteword.WordFilterConfigTextParser import javax.inject.Inject @@ -23,8 +23,8 @@ class ClientWordFilterSettingViewModel @Inject constructor( init { viewModelScope.launch { - repository.get().mapCancellableCatching { - WordFilterConfigTextParser.fromConfig(it).getOrThrow() + repository.get().flatMapCancellableCatching { + WordFilterConfigTextParser.fromConfig(it) }.onSuccess { muteWordsFieldState = it } @@ -37,8 +37,8 @@ class ClientWordFilterSettingViewModel @Inject constructor( fun save() { viewModelScope.launch { - WordFilterConfigTextParser.fromText(muteWordsFieldState).mapCancellableCatching { - repository.save(it).getOrThrow() + WordFilterConfigTextParser.fromText(muteWordsFieldState).flatMapCancellableCatching { + repository.save(it) }.onSuccess { } diff --git a/modules/features/user/src/main/java/net/pantasystem/milktea/user/profile/viewmodel/UserDetailViewModel.kt b/modules/features/user/src/main/java/net/pantasystem/milktea/user/profile/viewmodel/UserDetailViewModel.kt index 67a84a7c82..4ead410324 100644 --- a/modules/features/user/src/main/java/net/pantasystem/milktea/user/profile/viewmodel/UserDetailViewModel.kt +++ b/modules/features/user/src/main/java/net/pantasystem/milktea/user/profile/viewmodel/UserDetailViewModel.kt @@ -16,6 +16,7 @@ import net.pantasystem.milktea.app_store.account.AccountStore import net.pantasystem.milktea.app_store.handler.AppGlobalError import net.pantasystem.milktea.app_store.handler.UserActionAppGlobalErrorStore import net.pantasystem.milktea.common.Logger +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.mapCancellableCatching import net.pantasystem.milktea.common.runCancellableCatching import net.pantasystem.milktea.common_android.resource.StringSource @@ -208,8 +209,8 @@ class UserDetailViewModel @Inject constructor( private fun sync() { viewModelScope.launch { - getUserId().mapCancellableCatching { userId -> - userRepository.sync(userId).getOrThrow() + getUserId().flatMapCancellableCatching { userId -> + userRepository.sync(userId) }.onFailure { logger.error("user sync error", it) userActionAppGlobalErrorStore.dispatch( @@ -358,8 +359,8 @@ class UserDetailViewModel @Inject constructor( fun toggleUserTimelineTab() { viewModelScope.launch { - getUserId().mapCancellableCatching { - toggleUserTimelineAddTabUseCase(it).getOrThrow() + getUserId().flatMapCancellableCatching { + toggleUserTimelineAddTabUseCase(it) }.onFailure { logger.error("toggle user timeline tab failed", it) userActionAppGlobalErrorStore.dispatch( @@ -376,8 +377,8 @@ class UserDetailViewModel @Inject constructor( fun toggleNotifyUserPosts() { viewModelScope.launch { - getUserId().mapCancellableCatching { - toggleNotifyUserPostsUseCase(it).getOrThrow() + getUserId().flatMapCancellableCatching { + toggleNotifyUserPostsUseCase(it) }.onFailure { logger.error("toggle user notify posts failed", it) userActionAppGlobalErrorStore.dispatch( @@ -394,8 +395,8 @@ class UserDetailViewModel @Inject constructor( fun muteRenotes() { viewModelScope.launch { - getUserId().mapCancellableCatching { - renoteMuteRepository.create(it).getOrThrow() + getUserId().flatMapCancellableCatching { + renoteMuteRepository.create(it) }.onFailure { userActionAppGlobalErrorStore.dispatch( AppGlobalError( @@ -411,8 +412,8 @@ class UserDetailViewModel @Inject constructor( fun unMuteRenotes() { viewModelScope.launch { - getUserId().mapCancellableCatching { - renoteMuteRepository.delete(it).getOrThrow() + getUserId().flatMapCancellableCatching { + renoteMuteRepository.delete(it) }.onFailure { userActionAppGlobalErrorStore.dispatch( AppGlobalError( diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/note/NoteRelationGetter.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/note/NoteRelationGetter.kt index 7599ff05f3..bc7a0491ae 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/note/NoteRelationGetter.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/note/NoteRelationGetter.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.note -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.runCancellableCatching import net.pantasystem.milktea.model.drive.FileProperty import net.pantasystem.milktea.model.drive.FilePropertyDataSource @@ -35,14 +35,14 @@ class NoteRelationGetter @Inject constructor( } } - }.mapCancellableCatching { + }.flatMapCancellableCatching { get( it, deep, usersMap = usersMap, notesMap = notesMap, filesMap = filesMap, - ).getOrThrow() + ) } } diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/note/reaction/ToggleReactionUseCase.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/note/reaction/ToggleReactionUseCase.kt index 5e765e1f45..a2a42914ce 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/note/reaction/ToggleReactionUseCase.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/note/reaction/ToggleReactionUseCase.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.note.reaction -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.runCancellableCatching import net.pantasystem.milktea.model.UseCase import net.pantasystem.milktea.model.account.Account @@ -85,8 +85,8 @@ class ToggleReactionUseCase @Inject constructor( // NOTE: Suggestionを表示するためにユーザのデータが必要になるので、キャッシュを更新しておく userRepository.sync(note.userId).getOrThrow() - }.mapCancellableCatching { - noteRepository.sync(noteId).getOrThrow() + }.flatMapCancellableCatching { + noteRepository.sync(noteId) } } diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/user/ToggleFollowUseCase.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/user/ToggleFollowUseCase.kt index 1bbef4993e..b92af7a943 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/user/ToggleFollowUseCase.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/user/ToggleFollowUseCase.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.user -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.common.runCancellableCatching import net.pantasystem.milktea.model.UseCase import net.pantasystem.milktea.model.user.follow.FollowRepository @@ -21,7 +21,7 @@ class ToggleFollowUseCase @Inject constructor( } else { followRepository.create(userId) } - }.mapCancellableCatching { + }.flatMapCancellableCatching { userRepository.sync(userId) } } diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/BlockUserUseCase.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/BlockUserUseCase.kt index bc64909536..d516ed5c89 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/BlockUserUseCase.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/BlockUserUseCase.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.user.block -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.model.UseCase import net.pantasystem.milktea.model.user.User import net.pantasystem.milktea.model.user.UserRepository @@ -12,8 +12,8 @@ class BlockUserUseCase @Inject constructor( ): UseCase { suspend operator fun invoke(userId: User.Id): Result { - return blockRepository.create(userId).mapCancellableCatching { - userRepository.sync(userId).getOrThrow() + return blockRepository.create(userId).flatMapCancellableCatching { + userRepository.sync(userId) } } } \ No newline at end of file diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/UnBlockUserUseCase.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/UnBlockUserUseCase.kt index f4502421ec..d500ba0bb5 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/UnBlockUserUseCase.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/user/block/UnBlockUserUseCase.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.user.block -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.model.UseCase import net.pantasystem.milktea.model.user.User import net.pantasystem.milktea.model.user.UserRepository @@ -12,8 +12,8 @@ class UnBlockUserUseCase @Inject constructor( ) : UseCase { suspend operator fun invoke(userId: User.Id): Result { - return blockRepository.delete(userId).mapCancellableCatching { - userRepository.sync(userId).getOrThrow() + return blockRepository.delete(userId).flatMapCancellableCatching { + userRepository.sync(userId) } } } \ No newline at end of file diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/MuteUserUseCase.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/MuteUserUseCase.kt index 72922f4b83..c0647e4979 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/MuteUserUseCase.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/MuteUserUseCase.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.user.mute -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.model.UseCase import net.pantasystem.milktea.model.user.UserRepository import javax.inject.Inject @@ -11,8 +11,8 @@ class MuteUserUseCase @Inject constructor( ): UseCase { suspend operator fun invoke(createMute: CreateMute): Result { - return muteRepository.create(createMute).mapCancellableCatching { - userRepository.sync(createMute.userId).getOrThrow() + return muteRepository.create(createMute).flatMapCancellableCatching { + userRepository.sync(createMute.userId) } } } \ No newline at end of file diff --git a/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/UnMuteUserUseCase.kt b/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/UnMuteUserUseCase.kt index 511361b1e2..d0d17b2cf3 100644 --- a/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/UnMuteUserUseCase.kt +++ b/modules/model/src/main/java/net/pantasystem/milktea/model/user/mute/UnMuteUserUseCase.kt @@ -1,6 +1,6 @@ package net.pantasystem.milktea.model.user.mute -import net.pantasystem.milktea.common.mapCancellableCatching +import net.pantasystem.milktea.common.flatMapCancellableCatching import net.pantasystem.milktea.model.UseCase import net.pantasystem.milktea.model.user.User import net.pantasystem.milktea.model.user.UserRepository @@ -12,8 +12,8 @@ class UnMuteUserUseCase @Inject constructor( ) : UseCase { suspend operator fun invoke(userId: User.Id): Result { - return muteRepository.delete(userId).mapCancellableCatching { - userRepository.sync(userId).getOrThrow() + return muteRepository.delete(userId).flatMapCancellableCatching { + userRepository.sync(userId) } } } \ No newline at end of file