change: Simplify mapBoth operations to mapEither and map (#746)

This commit is contained in:
Nik Clayton 2024-06-13 22:55:16 +02:00 committed by GitHub
parent 6d266bce6d
commit d8d15ab5b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 17 deletions

View File

@ -29,11 +29,10 @@ import app.pachli.core.network.model.MastoList
import app.pachli.core.network.model.TimelineAccount import app.pachli.core.network.model.TimelineAccount
import app.pachli.core.network.model.UserListRepliesPolicy import app.pachli.core.network.model.UserListRepliesPolicy
import app.pachli.core.network.retrofit.MastodonApi import app.pachli.core.network.retrofit.MastodonApi
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.coroutines.binding.binding import com.github.michaelbull.result.coroutines.binding.binding
import com.github.michaelbull.result.mapBoth import com.github.michaelbull.result.mapEither
import com.github.michaelbull.result.mapError import com.github.michaelbull.result.mapError
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -61,12 +60,12 @@ class NetworkListsRepository @Inject constructor(
externalScope.launch { externalScope.launch {
_lists.value = Ok(Lists.Loading) _lists.value = Ok(Lists.Loading)
_lists.value = api.getLists() _lists.value = api.getLists()
.mapBoth( .mapEither(
{ {
updateTabPreferences(it.body.associateBy { it.id }) updateTabPreferences(it.body.associateBy { it.id })
Ok(Lists.Loaded(it.body)) Lists.Loaded(it.body)
}, },
{ Err(Retrieve(it)) }, { Retrieve(it) },
) )
} }
} }

View File

@ -24,10 +24,10 @@ import app.pachli.core.data.repository.ListsRepository
import app.pachli.core.network.model.TimelineAccount import app.pachli.core.network.model.TimelineAccount
import app.pachli.core.network.retrofit.MastodonApi import app.pachli.core.network.retrofit.MastodonApi
import app.pachli.core.network.retrofit.apiresult.ApiError import app.pachli.core.network.retrofit.apiresult.ApiError
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.mapBoth import com.github.michaelbull.result.map
import com.github.michaelbull.result.mapEither
import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess import com.github.michaelbull.result.onSuccess
import dagger.assisted.Assisted import dagger.assisted.Assisted
@ -82,11 +82,10 @@ class AccountsInListViewModel @AssistedInject constructor(
fun refresh() = viewModelScope.launch { fun refresh() = viewModelScope.launch {
_accountsInList.value = Ok(Accounts.Loading) _accountsInList.value = Ok(Accounts.Loading)
_accountsInList.value = listsRepository.getAccountsInList(listId) _accountsInList.value = listsRepository.getAccountsInList(listId)
.mapBoth({ .mapEither(
Ok(Accounts.Loaded(it)) { Accounts.Loaded(it) },
}, { { FlowError.GetAccounts(it) },
Err(FlowError.GetAccounts(it)) )
})
} }
/** /**
@ -120,11 +119,8 @@ class AccountsInListViewModel @AssistedInject constructor(
query.isEmpty() -> _searchResults.value = Ok(SearchResults.Empty) query.isEmpty() -> _searchResults.value = Ok(SearchResults.Empty)
query.isBlank() -> _searchResults.value = Ok(SearchResults.Loaded(emptyList())) query.isBlank() -> _searchResults.value = Ok(SearchResults.Loaded(emptyList()))
else -> viewModelScope.launch { else -> viewModelScope.launch {
_searchResults.value = api.searchAccounts(query, null, 10, true).mapBoth({ _searchResults.value = api.searchAccounts(query, null, 10, true)
Ok(SearchResults.Loaded(it.body)) .map { SearchResults.Loaded(it.body) }
}, {
Err(it)
})
} }
} }
} }