Account Activity enhancements (#1196)

* use the "follow" button as an "unblock" button on the profiles of blocked users

* use the "follow" button as an "unblock" button on the profiles of blocked users

* add an icon to the profiles that can be clicked to mute/unmute the user

* add an icon to the profiles that can be clicked to mute/unmute the user

* Fix view issues

* Fix view issues

* Implement swipe to refresh for Account layout

* Implement swipe to refresh handler at the account screen

* Implement swipe to refresh

* Correct account refresh

* Show Progress Bar

* Show Progress Bar

* Move "itSelf" check into the viewModel

* Change methods access level

* Change TimelineFragment newInstance overload

* Change avatarSize type to Float

* Replace ImageButton with MaterialButton

* Update account activity swipe to refresh colors

* Refactor code

* Refactor code

* Fix crash on moved account refresh

* Show moved account stats

* Update mute button behaviour

* Show tabs and content for moved accounts

* Fix crash on tablet
This commit is contained in:
pandasoft0 2019-05-15 13:43:16 +03:00 committed by Konrad Pozniak
commit ae5d8b8633
13 changed files with 890 additions and 580 deletions

View file

@ -3,6 +3,7 @@ package com.keylesspalace.tusky.viewmodel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.keylesspalace.tusky.appstore.*
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.entity.Relationship
import com.keylesspalace.tusky.network.MastodonApi
@ -16,30 +17,36 @@ import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class AccountViewModel @Inject constructor(
private val mastodonApi: MastodonApi,
private val eventHub: EventHub
): ViewModel() {
class AccountViewModel @Inject constructor(
private val mastodonApi: MastodonApi,
private val eventHub: EventHub,
private val accountManager: AccountManager
) : ViewModel() {
val accountData = MutableLiveData<Resource<Account>>()
val relationshipData = MutableLiveData<Resource<Relationship>>()
private val callList: MutableList<Call<*>> = mutableListOf()
private val disposable: Disposable = eventHub.events
.subscribe { event ->
if (event is ProfileEditedEvent && event.newProfileData.id == accountData.value?.data?.id) {
accountData.postValue(Success(event.newProfileData))
}
.subscribe { event ->
if (event is ProfileEditedEvent && event.newProfileData.id == accountData.value?.data?.id) {
accountData.postValue(Success(event.newProfileData))
}
}
val isRefreshing = MutableLiveData<Boolean>().apply { value = false }
private var isDataLoading = false
fun obtainAccount(accountId: String, reload: Boolean = false) {
if(accountData.value == null || reload) {
lateinit var accountId: String
var isSelf = false
private fun obtainAccount(reload: Boolean = false) {
if (accountData.value == null || reload) {
isDataLoading = true
accountData.postValue(Loading())
val call = mastodonApi.account(accountId)
call.enqueue(object : Callback<Account> {
call.enqueue(object : Callback<Account> {
override fun onResponse(call: Call<Account>,
response: Response<Account>) {
if (response.isSuccessful) {
@ -47,10 +54,14 @@ class AccountViewModel @Inject constructor(
} else {
accountData.postValue(Error())
}
isDataLoading = false
isRefreshing.postValue(false)
}
override fun onFailure(call: Call<Account>, t: Throwable) {
accountData.postValue(Error())
isDataLoading = false
isRefreshing.postValue(false)
}
})
@ -58,14 +69,14 @@ class AccountViewModel @Inject constructor(
}
}
fun obtainRelationship(accountId: String, reload: Boolean = false) {
if(relationshipData.value == null || reload) {
private fun obtainRelationship(reload: Boolean = false) {
if (relationshipData.value == null || reload) {
relationshipData.postValue(Loading())
val ids = listOf(accountId)
val call = mastodonApi.relationships(ids)
call.enqueue(object : Callback<List<Relationship>> {
call.enqueue(object : Callback<List<Relationship>> {
override fun onResponse(call: Call<List<Relationship>>,
response: Response<List<Relationship>>) {
val relationships = response.body()
@ -86,47 +97,47 @@ class AccountViewModel @Inject constructor(
}
}
fun changeFollowState(id: String) {
fun changeFollowState() {
val relationship = relationshipData.value?.data
if (relationship?.following == true || relationship?.requested == true) {
changeRelationship(RelationShipAction.UNFOLLOW, id)
changeRelationship(RelationShipAction.UNFOLLOW)
} else {
changeRelationship(RelationShipAction.FOLLOW, id)
changeRelationship(RelationShipAction.FOLLOW)
}
}
fun changeBlockState(id: String) {
fun changeBlockState() {
if (relationshipData.value?.data?.blocking == true) {
changeRelationship(RelationShipAction.UNBLOCK, id)
changeRelationship(RelationShipAction.UNBLOCK)
} else {
changeRelationship(RelationShipAction.BLOCK, id)
changeRelationship(RelationShipAction.BLOCK)
}
}
fun changeMuteState(id: String) {
fun changeMuteState() {
if (relationshipData.value?.data?.muting == true) {
changeRelationship(RelationShipAction.UNMUTE, id)
changeRelationship(RelationShipAction.UNMUTE)
} else {
changeRelationship(RelationShipAction.MUTE, id)
changeRelationship(RelationShipAction.MUTE)
}
}
fun changeShowReblogsState(id: String) {
fun changeShowReblogsState() {
if (relationshipData.value?.data?.showingReblogs == true) {
changeRelationship(RelationShipAction.FOLLOW, id, false)
changeRelationship(RelationShipAction.FOLLOW, false)
} else {
changeRelationship(RelationShipAction.FOLLOW, id, true)
changeRelationship(RelationShipAction.FOLLOW, true)
}
}
private fun changeRelationship(relationshipAction: RelationShipAction, id: String, showReblogs: Boolean = true) {
private fun changeRelationship(relationshipAction: RelationShipAction, showReblogs: Boolean = true) {
val relation = relationshipData.value?.data
val account = accountData.value?.data
if(relation != null && account != null) {
if (relation != null && account != null) {
// optimistically post new state for faster response
val newRelation = when(relationshipAction) {
val newRelation = when (relationshipAction) {
RelationShipAction.FOLLOW -> {
if (account.locked) {
relation.copy(requested = true)
@ -134,11 +145,11 @@ class AccountViewModel @Inject constructor(
relation.copy(following = true)
}
}
RelationShipAction.UNFOLLOW -> relation.copy(following = false)
RelationShipAction.BLOCK -> relation.copy(blocking = true)
RelationShipAction.UNBLOCK -> relation.copy(blocking = false)
RelationShipAction.MUTE -> relation.copy(muting = true)
RelationShipAction.UNMUTE -> relation.copy(muting = false)
RelationShipAction.UNFOLLOW -> relation.copy(following = false)
RelationShipAction.BLOCK -> relation.copy(blocking = true)
RelationShipAction.UNBLOCK -> relation.copy(blocking = false)
RelationShipAction.MUTE -> relation.copy(muting = true)
RelationShipAction.UNMUTE -> relation.copy(muting = false)
}
relationshipData.postValue(Loading(newRelation))
}
@ -151,10 +162,11 @@ class AccountViewModel @Inject constructor(
relationshipData.postValue(Success(relationship))
when (relationshipAction) {
RelationShipAction.UNFOLLOW -> eventHub.dispatch(UnfollowEvent(id))
RelationShipAction.BLOCK -> eventHub.dispatch(BlockEvent(id))
RelationShipAction.MUTE -> eventHub.dispatch(MuteEvent(id))
else -> {}
RelationShipAction.UNFOLLOW -> eventHub.dispatch(UnfollowEvent(accountId))
RelationShipAction.BLOCK -> eventHub.dispatch(BlockEvent(accountId))
RelationShipAction.MUTE -> eventHub.dispatch(MuteEvent(accountId))
else -> {
}
}
} else {
@ -168,13 +180,13 @@ class AccountViewModel @Inject constructor(
}
}
val call = when(relationshipAction) {
RelationShipAction.FOLLOW -> mastodonApi.followAccount(id, showReblogs)
RelationShipAction.UNFOLLOW -> mastodonApi.unfollowAccount(id)
RelationShipAction.BLOCK -> mastodonApi.blockAccount(id)
RelationShipAction.UNBLOCK -> mastodonApi.unblockAccount(id)
RelationShipAction.MUTE -> mastodonApi.muteAccount(id)
RelationShipAction.UNMUTE -> mastodonApi.unmuteAccount(id)
val call = when (relationshipAction) {
RelationShipAction.FOLLOW -> mastodonApi.followAccount(accountId, showReblogs)
RelationShipAction.UNFOLLOW -> mastodonApi.unfollowAccount(accountId)
RelationShipAction.BLOCK -> mastodonApi.blockAccount(accountId)
RelationShipAction.UNBLOCK -> mastodonApi.unblockAccount(accountId)
RelationShipAction.MUTE -> mastodonApi.muteAccount(accountId)
RelationShipAction.UNMUTE -> mastodonApi.unmuteAccount(accountId)
}
call.enqueue(callback)
@ -189,6 +201,27 @@ class AccountViewModel @Inject constructor(
disposable.dispose()
}
fun refresh() {
reload(true)
}
private fun reload(isReload: Boolean = false) {
if (isDataLoading)
return
accountId.let {
obtainAccount(isReload)
if (!isSelf)
obtainRelationship(isReload)
}
}
fun setAccountInfo(accountId: String) {
this.accountId = accountId
this.isSelf = accountManager.activeAccount?.accountId == accountId
reload(false)
}
enum class RelationShipAction {
FOLLOW, UNFOLLOW, BLOCK, UNBLOCK, MUTE, UNMUTE
}