make all model classes immutable (#2686)

This commit is contained in:
Konrad Pozniak 2022-08-31 18:54:40 +02:00 committed by GitHub
parent 0041acf2d4
commit 4665637086
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 54 deletions

View file

@ -24,7 +24,6 @@ import com.keylesspalace.tusky.components.search.adapter.SearchPagingSourceFacto
import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.entity.DeletedStatus import com.keylesspalace.tusky.entity.DeletedStatus
import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.usecase.TimelineCases import com.keylesspalace.tusky.usecase.TimelineCases
@ -113,11 +112,7 @@ class SearchViewModel @Inject constructor(
} }
fun expandedChange(statusViewData: StatusViewData.Concrete, expanded: Boolean) { fun expandedChange(statusViewData: StatusViewData.Concrete, expanded: Boolean) {
val idx = loadedStatuses.indexOf(statusViewData) updateStatusViewData(statusViewData.copy(isExpanded = expanded))
if (idx >= 0) {
loadedStatuses[idx] = statusViewData.copy(isExpanded = expanded)
statusesPagingSourceFactory.invalidate()
}
} }
fun reblog(statusViewData: StatusViewData.Concrete, reblog: Boolean) { fun reblog(statusViewData: StatusViewData.Concrete, reblog: Boolean) {
@ -131,51 +126,34 @@ class SearchViewModel @Inject constructor(
} }
private fun setRebloggedForStatus(statusViewData: StatusViewData.Concrete, reblog: Boolean) { private fun setRebloggedForStatus(statusViewData: StatusViewData.Concrete, reblog: Boolean) {
statusViewData.status.reblogged = reblog updateStatus(
statusViewData.status.reblog?.reblogged = reblog statusViewData.status.copy(
statusesPagingSourceFactory.invalidate() reblogged = reblog,
reblog = statusViewData.status.reblog?.copy(reblogged = reblog)
)
)
} }
fun contentHiddenChange(statusViewData: StatusViewData.Concrete, isShowing: Boolean) { fun contentHiddenChange(statusViewData: StatusViewData.Concrete, isShowing: Boolean) {
val idx = loadedStatuses.indexOf(statusViewData) updateStatusViewData(statusViewData.copy(isShowingContent = isShowing))
if (idx >= 0) {
loadedStatuses[idx] = statusViewData.copy(isShowingContent = isShowing)
statusesPagingSourceFactory.invalidate()
}
} }
fun collapsedChange(statusViewData: StatusViewData.Concrete, collapsed: Boolean) { fun collapsedChange(statusViewData: StatusViewData.Concrete, collapsed: Boolean) {
val idx = loadedStatuses.indexOf(statusViewData) updateStatusViewData(statusViewData.copy(isCollapsed = collapsed))
if (idx >= 0) {
loadedStatuses[idx] = statusViewData.copy(isCollapsed = collapsed)
statusesPagingSourceFactory.invalidate()
}
} }
fun voteInPoll(statusViewData: StatusViewData.Concrete, choices: MutableList<Int>) { fun voteInPoll(statusViewData: StatusViewData.Concrete, choices: MutableList<Int>) {
val votedPoll = statusViewData.status.actionableStatus.poll!!.votedCopy(choices) val votedPoll = statusViewData.status.actionableStatus.poll!!.votedCopy(choices)
updateStatus(statusViewData, votedPoll) updateStatus(statusViewData.status.copy(poll = votedPoll))
timelineCases.voteInPoll(statusViewData.id, votedPoll.id, choices) timelineCases.voteInPoll(statusViewData.id, votedPoll.id, choices)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .doOnError { t -> Log.d(TAG, "Failed to vote in poll: ${statusViewData.id}", t) }
{ newPoll -> updateStatus(statusViewData, newPoll) }, .subscribe()
{ t -> Log.d(TAG, "Failed to vote in poll: ${statusViewData.id}", t) }
)
.autoDispose() .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) { fun favorite(statusViewData: StatusViewData.Concrete, isFavorited: Boolean) {
statusViewData.status.favourited = isFavorited updateStatus(statusViewData.status.copy(favourited = isFavorited))
statusesPagingSourceFactory.invalidate()
timelineCases.favourite(statusViewData.id, isFavorited) timelineCases.favourite(statusViewData.id, isFavorited)
.onErrorReturnItem(statusViewData.status) .onErrorReturnItem(statusViewData.status)
.subscribe() .subscribe()
@ -183,8 +161,7 @@ class SearchViewModel @Inject constructor(
} }
fun bookmark(statusViewData: StatusViewData.Concrete, isBookmarked: Boolean) { fun bookmark(statusViewData: StatusViewData.Concrete, isBookmarked: Boolean) {
statusViewData.status.bookmarked = isBookmarked updateStatus(statusViewData.status.copy(bookmarked = isBookmarked))
statusesPagingSourceFactory.invalidate()
timelineCases.bookmark(statusViewData.id, isBookmarked) timelineCases.bookmark(statusViewData.id, isBookmarked)
.onErrorReturnItem(statusViewData.status) .onErrorReturnItem(statusViewData.status)
.subscribe() .subscribe()
@ -208,18 +185,28 @@ class SearchViewModel @Inject constructor(
} }
fun muteConversation(statusViewData: StatusViewData.Concrete, mute: Boolean) { fun muteConversation(statusViewData: StatusViewData.Concrete, mute: Boolean) {
val idx = loadedStatuses.indexOf(statusViewData) updateStatus(statusViewData.status.copy(muted = mute))
if (idx >= 0) {
val newStatus = statusViewData.status.copy(muted = mute)
loadedStatuses[idx] = statusViewData.copy(status = newStatus)
statusesPagingSourceFactory.invalidate()
}
timelineCases.muteConversation(statusViewData.id, mute) timelineCases.muteConversation(statusViewData.id, mute)
.onErrorReturnItem(statusViewData.status) .onErrorReturnItem(statusViewData.status)
.subscribe() .subscribe()
.autoDispose() .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 { companion object {
private const val TAG = "SearchViewModel" private const val TAG = "SearchViewModel"
private const val DEFAULT_LOAD_SIZE = 20 private const val DEFAULT_LOAD_SIZE = 20

View file

@ -48,8 +48,8 @@ data class Announcement(
data class Reaction( data class Reaction(
val name: String, val name: String,
var count: Int, val count: Int,
var me: Boolean, val me: Boolean,
val url: String?, val url: String?,
@SerializedName("static_url") val staticUrl: String? @SerializedName("static_url") val staticUrl: String?
) )

View file

@ -20,12 +20,12 @@ import java.util.ArrayList
import java.util.Date import java.util.Date
data class DeletedStatus( data class DeletedStatus(
var text: String?, val text: String?,
@SerializedName("in_reply_to_id") var inReplyToId: String?, @SerializedName("in_reply_to_id") val inReplyToId: String?,
@SerializedName("spoiler_text") val spoilerText: String, @SerializedName("spoiler_text") val spoilerText: String,
val visibility: Status.Visibility, val visibility: Status.Visibility,
val sensitive: Boolean, val sensitive: Boolean,
@SerializedName("media_attachments") var attachments: ArrayList<Attachment>?, @SerializedName("media_attachments") val attachments: ArrayList<Attachment>?,
val poll: Poll?, val poll: Poll?,
@SerializedName("created_at") val createdAt: Date, @SerializedName("created_at") val createdAt: Date,
val language: String?, val language: String?,

View file

@ -26,7 +26,7 @@ data class Status(
val id: String, val id: String,
val url: String?, // not present if it's reblog val url: String?, // not present if it's reblog
val account: TimelineAccount, 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?, @SerializedName("in_reply_to_account_id") val inReplyToAccountId: String?,
val reblog: Status?, val reblog: Status?,
val content: String, val content: String,
@ -35,13 +35,13 @@ data class Status(
@SerializedName("reblogs_count") val reblogsCount: Int, @SerializedName("reblogs_count") val reblogsCount: Int,
@SerializedName("favourites_count") val favouritesCount: Int, @SerializedName("favourites_count") val favouritesCount: Int,
@SerializedName("replies_count") val repliesCount: Int, @SerializedName("replies_count") val repliesCount: Int,
var reblogged: Boolean, val reblogged: Boolean,
var favourited: Boolean, val favourited: Boolean,
var bookmarked: Boolean, val bookmarked: Boolean,
var sensitive: Boolean, val sensitive: Boolean,
@SerializedName("spoiler_text") val spoilerText: String, @SerializedName("spoiler_text") val spoilerText: String,
val visibility: Visibility, val visibility: Visibility,
@SerializedName("media_attachments") var attachments: ArrayList<Attachment>, @SerializedName("media_attachments") val attachments: ArrayList<Attachment>,
val mentions: List<Mention>, val mentions: List<Mention>,
val tags: List<HashTag>?, val tags: List<HashTag>?,
val application: Application?, val application: Application?,