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.UserListRepliesPolicy
import app.pachli.core.network.retrofit.MastodonApi
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
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 javax.inject.Inject
import javax.inject.Singleton
@ -61,12 +60,12 @@ class NetworkListsRepository @Inject constructor(
externalScope.launch {
_lists.value = Ok(Lists.Loading)
_lists.value = api.getLists()
.mapBoth(
.mapEither(
{
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.retrofit.MastodonApi
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.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.onSuccess
import dagger.assisted.Assisted
@ -82,11 +82,10 @@ class AccountsInListViewModel @AssistedInject constructor(
fun refresh() = viewModelScope.launch {
_accountsInList.value = Ok(Accounts.Loading)
_accountsInList.value = listsRepository.getAccountsInList(listId)
.mapBoth({
Ok(Accounts.Loaded(it))
}, {
Err(FlowError.GetAccounts(it))
})
.mapEither(
{ Accounts.Loaded(it) },
{ FlowError.GetAccounts(it) },
)
}
/**
@ -120,11 +119,8 @@ class AccountsInListViewModel @AssistedInject constructor(
query.isEmpty() -> _searchResults.value = Ok(SearchResults.Empty)
query.isBlank() -> _searchResults.value = Ok(SearchResults.Loaded(emptyList()))
else -> viewModelScope.launch {
_searchResults.value = api.searchAccounts(query, null, 10, true).mapBoth({
Ok(SearchResults.Loaded(it.body))
}, {
Err(it)
})
_searchResults.value = api.searchAccounts(query, null, 10, true)
.map { SearchResults.Loaded(it.body) }
}
}
}