Skip to content

Commit

Permalink
refactor: Convert account relationship API calls to use ApiResult (#1109
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nikclayton authored Nov 18, 2024
1 parent 88561af commit 654a81a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 119 deletions.
120 changes: 60 additions & 60 deletions app/src/main/java/app/pachli/components/account/AccountViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import app.pachli.util.Error
import app.pachli.util.Loading
import app.pachli.util.Resource
import app.pachli.util.Success
import at.connyduck.calladapter.networkresult.fold
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -71,22 +72,21 @@ class AccountViewModel @Inject constructor(

viewModelScope.launch {
mastodonApi.account(accountId)
.fold(
{ account ->
domain = getDomain(account.url)
accountData.postValue(Success(account))
isDataLoading = false
isRefreshing.postValue(false)

isFromOwnDomain = domain == activeAccount.domain
},
{ t ->
Timber.w(t, "failed obtaining account")
accountData.postValue(Error(cause = t))
isDataLoading = false
isRefreshing.postValue(false)
},
)
.onSuccess { result ->
val account = result.body
domain = getDomain(account.url)
accountData.postValue(Success(account))
isDataLoading = false
isRefreshing.postValue(false)

isFromOwnDomain = domain == activeAccount.domain
}
.onFailure { t ->
Timber.w("failed obtaining account: %s", t)
accountData.postValue(Error(cause = t.throwable))
isDataLoading = false
isRefreshing.postValue(false)
}
}
}
}
Expand All @@ -97,15 +97,14 @@ class AccountViewModel @Inject constructor(

viewModelScope.launch {
mastodonApi.relationships(listOf(accountId))
.fold(
{ relationships ->
relationshipData.postValue(if (relationships.isNotEmpty()) Success(relationships[0]) else Error())
},
{ t ->
Timber.w(t, "failed obtaining relationships")
relationshipData.postValue(Error(cause = t))
},
)
.onSuccess { response ->
val relationships = response.body
relationshipData.postValue(if (relationships.isNotEmpty()) Success(relationships[0]) else Error())
}
.onFailure { t ->
Timber.w("failed obtaining relationships: %s", t)
relationshipData.postValue(Error(cause = t.throwable))
}
}
}
}
Expand Down Expand Up @@ -149,28 +148,32 @@ class AccountViewModel @Inject constructor(

fun blockDomain(instance: String) {
viewModelScope.launch {
mastodonApi.blockDomain(instance).fold({
eventHub.dispatch(DomainMuteEvent(instance))
val relation = relationshipData.value?.data
if (relation != null) {
relationshipData.postValue(Success(relation.copy(blockingDomain = true)))
mastodonApi.blockDomain(instance)
.onSuccess {
eventHub.dispatch(DomainMuteEvent(instance))
val relation = relationshipData.value?.data
if (relation != null) {
relationshipData.postValue(Success(relation.copy(blockingDomain = true)))
}
}
.onFailure { e ->
Timber.e("Error muting %s: %s", instance, e)
}
}, { e ->
Timber.e(e, "Error muting %s", instance)
})
}
}

fun unblockDomain(instance: String) {
viewModelScope.launch {
mastodonApi.unblockDomain(instance).fold({
val relation = relationshipData.value?.data
if (relation != null) {
relationshipData.postValue(Success(relation.copy(blockingDomain = false)))
mastodonApi.unblockDomain(instance)
.onSuccess {
val relation = relationshipData.value?.data
if (relation != null) {
relationshipData.postValue(Success(relation.copy(blockingDomain = false)))
}
}
.onFailure { e ->
Timber.e("Error unmuting %s: %s", instance, e)
}
}, { e ->
Timber.e(e, "Error unmuting %s", instance)
})
}
}

Expand Down Expand Up @@ -258,22 +261,21 @@ class AccountViewModel @Inject constructor(
}
}

relationshipCall.fold(
{ relationship ->
relationshipData.postValue(Success(relationship))
relationshipCall
.onSuccess { response ->
relationshipData.postValue(Success(response.body))

when (relationshipAction) {
RelationShipAction.UNFOLLOW -> eventHub.dispatch(UnfollowEvent(accountId))
RelationShipAction.BLOCK -> eventHub.dispatch(BlockEvent(accountId))
RelationShipAction.MUTE -> eventHub.dispatch(MuteEvent(accountId))
else -> { }
}
},
{ t ->
Timber.w(t, "failed loading relationship")
relationshipData.postValue(Error(relation, cause = t))
},
)
}
.onFailure { e ->
Timber.w("failed loading relationship: %s", e)
relationshipData.postValue(Error(relation, cause = e.throwable))
}
}

fun noteChanged(newNote: String) {
Expand All @@ -282,16 +284,14 @@ class AccountViewModel @Inject constructor(
noteUpdateJob = viewModelScope.launch {
delay(1500)
mastodonApi.updateAccountNote(accountId, newNote)
.fold(
{
noteSaved.postValue(true)
delay(4000)
noteSaved.postValue(false)
},
{ t ->
Timber.w(t, "Error updating note")
},
)
.onSuccess {
noteSaved.postValue(true)
delay(4000)
noteSaved.postValue(false)
}
.onFailure { e ->
Timber.w("Error updating note: %s", e)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import app.pachli.interfaces.AppBarLayoutHost
import app.pachli.view.EndlessOnScrollListener
import at.connyduck.calladapter.networkresult.fold
import com.github.michaelbull.result.getOrElse
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import com.google.android.material.color.MaterialColors
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -229,11 +231,9 @@ class AccountListFragment :
api.blockAccount(id)
} else {
api.unblockAccount(id)
}.fold({
onBlockSuccess(block, id, position)
}, {
onBlockFailure(block, id, it)
})
}
.onSuccess { onBlockSuccess(block, id, position) }
.onFailure { onBlockFailure(block, id, it.throwable) }
}
}

Expand Down Expand Up @@ -379,8 +379,9 @@ class AccountListFragment :
private fun fetchRelationships(ids: List<String>) {
lifecycleScope.launch {
api.relationships(ids)
.fold(::onFetchRelationshipsSuccess) { throwable ->
Timber.e(throwable, "Fetch failure for relationships of accounts: %s", ids)
.onSuccess { onFetchRelationshipsSuccess(it.body) }
.onFailure { throwable ->
Timber.e("Fetch failure for relationships of accounts: %s: %s", ids, throwable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import app.pachli.core.network.retrofit.MastodonApi
import app.pachli.core.ui.BackgroundMessage
import app.pachli.databinding.FragmentInstanceListBinding
import app.pachli.view.EndlessOnScrollListener
import at.connyduck.calladapter.networkresult.fold
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -68,22 +69,22 @@ class InstanceListFragment :
override fun mute(mute: Boolean, instance: String, position: Int) {
viewLifecycleOwner.lifecycleScope.launch {
if (mute) {
api.blockDomain(instance).fold({
adapter.addItem(instance)
}, { e ->
Timber.e(e, "Error muting domain %s", instance)
})
api.blockDomain(instance)
.onSuccess { adapter.addItem(instance) }
.onFailure { Timber.e(it.throwable, "Error muting domain %s", instance) }
} else {
api.unblockDomain(instance).fold({
adapter.removeItem(position)
Snackbar.make(binding.recyclerView, getString(R.string.confirmation_domain_unmuted, instance), Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo) {
mute(true, instance, position)
}
.show()
}, { e ->
Timber.e(e, "Error unmuting domain %s", instance)
})
api.unblockDomain(instance)
.onSuccess {
adapter.removeItem(position)
Snackbar.make(binding.recyclerView, getString(R.string.confirmation_domain_unmuted, instance), Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo) {
mute(true, instance, position)
}
.show()
}
.onFailure { e ->
Timber.e(e.throwable, "Error unmuting domain %s", instance)
}
}
}
}
Expand Down
46 changes: 23 additions & 23 deletions app/src/main/java/app/pachli/components/report/ReportViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import app.pachli.util.Resource
import app.pachli.util.Success
import app.pachli.viewdata.StatusViewData
import at.connyduck.calladapter.networkresult.fold
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.channels.BufferOverflow
Expand Down Expand Up @@ -135,14 +137,9 @@ class ReportViewModel @Inject constructor(
muteStateMutable.value = Loading()
blockStateMutable.value = Loading()
viewModelScope.launch {
mastodonApi.relationships(ids).fold(
{ data ->
updateRelationship(data.getOrNull(0))
},
{
updateRelationship(null)
},
)
mastodonApi.relationships(ids)
.onSuccess { updateRelationship(it.body.firstOrNull()) }
.onFailure { updateRelationship(null) }
}
}

Expand All @@ -163,18 +160,18 @@ class ReportViewModel @Inject constructor(
mastodonApi.unmuteAccount(accountId)
} else {
mastodonApi.muteAccount(accountId)
}.fold(
{ relationship ->
}
.onSuccess { response ->
val relationship = response.body
val muting = relationship.muting
muteStateMutable.value = Success(muting)
if (muting) {
eventHub.dispatch(MuteEvent(accountId))
}
},
{ t ->
muteStateMutable.value = Error(false, t.message)
},
)
}
.onFailure { t ->
muteStateMutable.value = Error(false, t.throwable.message)
}
}

muteStateMutable.value = Loading()
Expand All @@ -187,15 +184,18 @@ class ReportViewModel @Inject constructor(
mastodonApi.unblockAccount(accountId)
} else {
mastodonApi.blockAccount(accountId)
}.fold({ relationship ->
val blocking = relationship.blocking
blockStateMutable.value = Success(blocking)
if (blocking) {
eventHub.dispatch(BlockEvent(accountId))
}
.onSuccess { response ->
val relationship = response.body
val blocking = relationship.blocking
blockStateMutable.value = Success(blocking)
if (blocking) {
eventHub.dispatch(BlockEvent(accountId))
}
}
.onFailure { t ->
blockStateMutable.value = Error(false, t.throwable.message)
}
}, { t ->
blockStateMutable.value = Error(false, t.message)
})
}
blockStateMutable.value = Loading()
}
Expand Down
Loading

0 comments on commit 654a81a

Please sign in to comment.