Publish mute/block events from the report activity. (#1945)

Fixes #1870
main
Levi Bard 4 years ago committed by Alibek Omarov
parent dbff0333d7
commit aedf6da586
  1. 22
      app/src/main/java/com/keylesspalace/tusky/components/report/ReportViewModel.kt

@ -19,6 +19,9 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations import androidx.lifecycle.Transformations
import androidx.paging.PagedList import androidx.paging.PagedList
import com.keylesspalace.tusky.appstore.BlockEvent
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.MuteEvent
import com.keylesspalace.tusky.components.report.adapter.StatusesRepository import com.keylesspalace.tusky.components.report.adapter.StatusesRepository
import com.keylesspalace.tusky.components.report.model.StatusViewState import com.keylesspalace.tusky.components.report.model.StatusViewState
import com.keylesspalace.tusky.entity.Relationship import com.keylesspalace.tusky.entity.Relationship
@ -31,6 +34,7 @@ import javax.inject.Inject
class ReportViewModel @Inject constructor( class ReportViewModel @Inject constructor(
private val mastodonApi: MastodonApi, private val mastodonApi: MastodonApi,
private val eventHub: EventHub,
private val statusesRepository: StatusesRepository) : RxAwareViewModel() { private val statusesRepository: StatusesRepository) : RxAwareViewModel() {
private val navigationMutable = MutableLiveData<Screen>() private val navigationMutable = MutableLiveData<Screen>()
@ -123,7 +127,8 @@ class ReportViewModel @Inject constructor(
} }
fun toggleMute() { fun toggleMute() {
if (muteStateMutable.value?.data == true) { val alreadyMuted = muteStateMutable.value?.data == true
if (alreadyMuted) {
mastodonApi.unmuteAccountObservable(accountId) mastodonApi.unmuteAccountObservable(accountId)
} else { } else {
mastodonApi.muteAccountObservable(accountId) mastodonApi.muteAccountObservable(accountId)
@ -132,7 +137,11 @@ class ReportViewModel @Inject constructor(
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
{ relationship -> { relationship ->
muteStateMutable.value = Success(relationship?.muting == true) val muting = relationship?.muting == true
muteStateMutable.value = Success(muting)
if (muting) {
eventHub.dispatch(MuteEvent(accountId))
}
}, },
{ error -> { error ->
muteStateMutable.value = Error(false, error.message) muteStateMutable.value = Error(false, error.message)
@ -143,7 +152,8 @@ class ReportViewModel @Inject constructor(
} }
fun toggleBlock() { fun toggleBlock() {
if (blockStateMutable.value?.data == true) { val alreadyBlocked = blockStateMutable.value?.data == true
if (alreadyBlocked) {
mastodonApi.unblockAccountObservable(accountId) mastodonApi.unblockAccountObservable(accountId)
} else { } else {
mastodonApi.blockAccountObservable(accountId) mastodonApi.blockAccountObservable(accountId)
@ -152,7 +162,11 @@ class ReportViewModel @Inject constructor(
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
{ relationship -> { relationship ->
blockStateMutable.value = Success(relationship?.blocking == true) val blocking = relationship?.blocking == true
blockStateMutable.value = Success(blocking)
if (blocking) {
eventHub.dispatch(BlockEvent(accountId))
}
}, },
{ error -> { error ->
blockStateMutable.value = Error(false, error.message) blockStateMutable.value = Error(false, error.message)

Loading…
Cancel
Save