diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt index 738c662a..84a8b032 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt @@ -24,7 +24,6 @@ import com.keylesspalace.tusky.components.search.adapter.SearchPagingSourceFacto import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.entity.DeletedStatus -import com.keylesspalace.tusky.entity.Poll import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.usecase.TimelineCases @@ -113,11 +112,7 @@ class SearchViewModel @Inject constructor( } fun expandedChange(statusViewData: StatusViewData.Concrete, expanded: Boolean) { - val idx = loadedStatuses.indexOf(statusViewData) - if (idx >= 0) { - loadedStatuses[idx] = statusViewData.copy(isExpanded = expanded) - statusesPagingSourceFactory.invalidate() - } + updateStatusViewData(statusViewData.copy(isExpanded = expanded)) } fun reblog(statusViewData: StatusViewData.Concrete, reblog: Boolean) { @@ -131,51 +126,34 @@ class SearchViewModel @Inject constructor( } private fun setRebloggedForStatus(statusViewData: StatusViewData.Concrete, reblog: Boolean) { - statusViewData.status.reblogged = reblog - statusViewData.status.reblog?.reblogged = reblog - statusesPagingSourceFactory.invalidate() + updateStatus( + statusViewData.status.copy( + reblogged = reblog, + reblog = statusViewData.status.reblog?.copy(reblogged = reblog) + ) + ) } fun contentHiddenChange(statusViewData: StatusViewData.Concrete, isShowing: Boolean) { - val idx = loadedStatuses.indexOf(statusViewData) - if (idx >= 0) { - loadedStatuses[idx] = statusViewData.copy(isShowingContent = isShowing) - statusesPagingSourceFactory.invalidate() - } + updateStatusViewData(statusViewData.copy(isShowingContent = isShowing)) } fun collapsedChange(statusViewData: StatusViewData.Concrete, collapsed: Boolean) { - val idx = loadedStatuses.indexOf(statusViewData) - if (idx >= 0) { - loadedStatuses[idx] = statusViewData.copy(isCollapsed = collapsed) - statusesPagingSourceFactory.invalidate() - } + updateStatusViewData(statusViewData.copy(isCollapsed = collapsed)) } fun voteInPoll(statusViewData: StatusViewData.Concrete, choices: MutableList) { val votedPoll = statusViewData.status.actionableStatus.poll!!.votedCopy(choices) - updateStatus(statusViewData, votedPoll) + updateStatus(statusViewData.status.copy(poll = votedPoll)) timelineCases.voteInPoll(statusViewData.id, votedPoll.id, choices) .observeOn(AndroidSchedulers.mainThread()) - .subscribe( - { newPoll -> updateStatus(statusViewData, newPoll) }, - { t -> Log.d(TAG, "Failed to vote in poll: ${statusViewData.id}", t) } - ) + .doOnError { t -> Log.d(TAG, "Failed to vote in poll: ${statusViewData.id}", t) } + .subscribe() .autoDispose() } - private fun updateStatus(statusViewData: StatusViewData.Concrete, newPoll: Poll) { - val idx = loadedStatuses.indexOf(statusViewData) - if (idx >= 0) { - val newStatus = statusViewData.status.copy(poll = newPoll) - loadedStatuses[idx] = statusViewData.copy(status = newStatus) - statusesPagingSourceFactory.invalidate() - } - } - fun favorite(statusViewData: StatusViewData.Concrete, isFavorited: Boolean) { - statusViewData.status.favourited = isFavorited - statusesPagingSourceFactory.invalidate() + updateStatus(statusViewData.status.copy(favourited = isFavorited)) timelineCases.favourite(statusViewData.id, isFavorited) .onErrorReturnItem(statusViewData.status) .subscribe() @@ -183,8 +161,7 @@ class SearchViewModel @Inject constructor( } fun bookmark(statusViewData: StatusViewData.Concrete, isBookmarked: Boolean) { - statusViewData.status.bookmarked = isBookmarked - statusesPagingSourceFactory.invalidate() + updateStatus(statusViewData.status.copy(bookmarked = isBookmarked)) timelineCases.bookmark(statusViewData.id, isBookmarked) .onErrorReturnItem(statusViewData.status) .subscribe() @@ -208,18 +185,28 @@ class SearchViewModel @Inject constructor( } fun muteConversation(statusViewData: StatusViewData.Concrete, mute: Boolean) { - val idx = loadedStatuses.indexOf(statusViewData) - if (idx >= 0) { - val newStatus = statusViewData.status.copy(muted = mute) - loadedStatuses[idx] = statusViewData.copy(status = newStatus) - statusesPagingSourceFactory.invalidate() - } + updateStatus(statusViewData.status.copy(muted = mute)) timelineCases.muteConversation(statusViewData.id, mute) .onErrorReturnItem(statusViewData.status) .subscribe() .autoDispose() } + private fun updateStatusViewData(newStatusViewData: StatusViewData.Concrete) { + val idx = loadedStatuses.indexOfFirst { it.id == newStatusViewData.id } + if (idx >= 0) { + loadedStatuses[idx] = newStatusViewData + statusesPagingSourceFactory.invalidate() + } + } + + private fun updateStatus(newStatus: Status) { + val statusViewData = loadedStatuses.find { it.id == newStatus.id } + if (statusViewData != null) { + updateStatusViewData(statusViewData.copy(status = newStatus)) + } + } + companion object { private const val TAG = "SearchViewModel" private const val DEFAULT_LOAD_SIZE = 20 diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Announcement.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Announcement.kt index 00d5659d..5837815b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Announcement.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Announcement.kt @@ -48,8 +48,8 @@ data class Announcement( data class Reaction( val name: String, - var count: Int, - var me: Boolean, + val count: Int, + val me: Boolean, val url: String?, @SerializedName("static_url") val staticUrl: String? ) diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/DeletedStatus.kt b/app/src/main/java/com/keylesspalace/tusky/entity/DeletedStatus.kt index a417f316..a653cc58 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/DeletedStatus.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/DeletedStatus.kt @@ -20,12 +20,12 @@ import java.util.ArrayList import java.util.Date data class DeletedStatus( - var text: String?, - @SerializedName("in_reply_to_id") var inReplyToId: String?, + val text: String?, + @SerializedName("in_reply_to_id") val inReplyToId: String?, @SerializedName("spoiler_text") val spoilerText: String, val visibility: Status.Visibility, val sensitive: Boolean, - @SerializedName("media_attachments") var attachments: ArrayList?, + @SerializedName("media_attachments") val attachments: ArrayList?, val poll: Poll?, @SerializedName("created_at") val createdAt: Date, val language: String?, diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt index 93386713..c147ae30 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt @@ -26,7 +26,7 @@ data class Status( val id: String, val url: String?, // not present if it's reblog val account: TimelineAccount, - @SerializedName("in_reply_to_id") var inReplyToId: String?, + @SerializedName("in_reply_to_id") val inReplyToId: String?, @SerializedName("in_reply_to_account_id") val inReplyToAccountId: String?, val reblog: Status?, val content: String, @@ -35,13 +35,13 @@ data class Status( @SerializedName("reblogs_count") val reblogsCount: Int, @SerializedName("favourites_count") val favouritesCount: Int, @SerializedName("replies_count") val repliesCount: Int, - var reblogged: Boolean, - var favourited: Boolean, - var bookmarked: Boolean, - var sensitive: Boolean, + val reblogged: Boolean, + val favourited: Boolean, + val bookmarked: Boolean, + val sensitive: Boolean, @SerializedName("spoiler_text") val spoilerText: String, val visibility: Visibility, - @SerializedName("media_attachments") var attachments: ArrayList, + @SerializedName("media_attachments") val attachments: ArrayList, val mentions: List, val tags: List?, val application: Application?,