Skip to content

Commit

Permalink
feat: Result型をよりスマートに扱えるようにするためのオペレーターを作成し使用するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Jan 11, 2024
1 parent 2fe67dd commit 7ec74ac
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ inline fun <R, T> Result<T>.mapCancellableCatching(transform: (value: T) -> R):
} catch (e: Exception) {
return Result.failure(e)
}
}

inline fun<R, T> Result<T>.flatMapCancellableCatching(transform: (value: T) -> Result<R>): Result<R> {
return try {
val result = getOrThrow()
transform(result)
} catch (e: CancellationException) {
throw e
} catch (e: OutOfMemoryError) {
throw e
} catch (e: Exception) {
return Result.failure(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -235,7 +235,7 @@ class NotificationViewModel @Inject constructor(
viewModelScope.launch {
runCancellableCatching {
getCurrentAccount()
}.mapCancellableCatching {
}.flatMapCancellableCatching {
notificationRepository.markAsRead(it.accountId)
}.onSuccess {
loadInit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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 {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,14 +35,14 @@ class NoteRelationGetter @Inject constructor(
}

}
}.mapCancellableCatching {
}.flatMapCancellableCatching {
get(
it,
deep,
usersMap = usersMap,
notesMap = notesMap,
filesMap = filesMap,
).getOrThrow()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -85,8 +85,8 @@ class ToggleReactionUseCase @Inject constructor(

// NOTE: Suggestionを表示するためにユーザのデータが必要になるので、キャッシュを更新しておく
userRepository.sync(note.userId).getOrThrow()
}.mapCancellableCatching {
noteRepository.sync(noteId).getOrThrow()
}.flatMapCancellableCatching {
noteRepository.sync(noteId)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,7 +21,7 @@ class ToggleFollowUseCase @Inject constructor(
} else {
followRepository.create(userId)
}
}.mapCancellableCatching {
}.flatMapCancellableCatching {
userRepository.sync(userId)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,8 +12,8 @@ class BlockUserUseCase @Inject constructor(
): UseCase {

suspend operator fun invoke(userId: User.Id): Result<Unit> {
return blockRepository.create(userId).mapCancellableCatching {
userRepository.sync(userId).getOrThrow()
return blockRepository.create(userId).flatMapCancellableCatching {
userRepository.sync(userId)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,8 +12,8 @@ class UnBlockUserUseCase @Inject constructor(
) : UseCase {

suspend operator fun invoke(userId: User.Id): Result<Unit> {
return blockRepository.delete(userId).mapCancellableCatching {
userRepository.sync(userId).getOrThrow()
return blockRepository.delete(userId).flatMapCancellableCatching {
userRepository.sync(userId)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,8 +11,8 @@ class MuteUserUseCase @Inject constructor(
): UseCase {

suspend operator fun invoke(createMute: CreateMute): Result<Unit> {
return muteRepository.create(createMute).mapCancellableCatching {
userRepository.sync(createMute.userId).getOrThrow()
return muteRepository.create(createMute).flatMapCancellableCatching {
userRepository.sync(createMute.userId)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,8 +12,8 @@ class UnMuteUserUseCase @Inject constructor(
) : UseCase {

suspend operator fun invoke(userId: User.Id): Result<Unit> {
return muteRepository.delete(userId).mapCancellableCatching {
userRepository.sync(userId).getOrThrow()
return muteRepository.delete(userId).flatMapCancellableCatching {
userRepository.sync(userId)
}
}
}

0 comments on commit 7ec74ac

Please sign in to comment.