643e012b11
* first setup * network timeline paging / improvements * rename classes / move to correct package * remove unused class TimelineAdapter * some code cleanup * remove TimelineRepository, put mapper functions in TimelineTypeMappers.kt * add db migration * cleanup unused code * bugfix * make default timeline settings work again * fix pinning statuses from timeline * fix network timeline * respect account settings in NetworkTimelineRemoteMediator * respect account settings in NetworkTimelineRemoteMediator * update license headers * show error view when an error occurs * cleanup some todos * fix db migration * fix changing mediaPreviewEnabled setting * fix "load more" button appearing on top of timeline * fix filtering and other bugs * cleanup cache after 14 days * fix TimelineDAOTest * fix code formatting * add NetworkTimeline unit tests * add CachedTimeline unit tests * fix code formatting * move TimelineDaoTest to unit tests * implement removeAllByInstance for CachedTimelineViewModel * fix code formatting * fix bug in TimelineDao.deleteAllFromInstance * improve loading more statuses in NetworkTimelineViewModel * improve loading more statuses in NetworkTimelineViewModel * fix bug where empty state was shown too soon * reload top of cached timeline on app start * improve CachedTimelineRemoteMediator and Tests * improve cached timeline tests * fix some more todos * implement TimelineFragment.removeItem * fix ListStatusAccessibilityDelegate * fix crash in NetworkTimelineViewModel.loadMore * fix default state of collapsible statuses * fix default state of collapsible statuses -tests * fix showing/hiding media in the timeline * get rid of some not-null assertion operators in TimelineTypeMappers * fix tests * error handling in CachedTimelineViewModel.loadMore * keep local status state when refreshing cached statuses * keep local status state when refreshing network timeline statuses * show placeholder loading state in cached timeline * better comments, some code cleanup * add TimelineViewModelTest, improve code, fix bug * fix ktlint * fix voting in boosted polls * code improvement
257 lines
9.1 KiB
Kotlin
257 lines
9.1 KiB
Kotlin
/* Copyright 2021 Tusky Contributors
|
|
*
|
|
* This file is a part of Tusky.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
* Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
package com.keylesspalace.tusky.components.timeline
|
|
|
|
import android.text.SpannedString
|
|
import androidx.core.text.parseAsHtml
|
|
import androidx.core.text.toHtml
|
|
import com.google.gson.Gson
|
|
import com.google.gson.reflect.TypeToken
|
|
import com.keylesspalace.tusky.db.TimelineAccountEntity
|
|
import com.keylesspalace.tusky.db.TimelineStatusEntity
|
|
import com.keylesspalace.tusky.db.TimelineStatusWithAccount
|
|
import com.keylesspalace.tusky.entity.Account
|
|
import com.keylesspalace.tusky.entity.Attachment
|
|
import com.keylesspalace.tusky.entity.Emoji
|
|
import com.keylesspalace.tusky.entity.Poll
|
|
import com.keylesspalace.tusky.entity.Status
|
|
import com.keylesspalace.tusky.util.shouldTrimStatus
|
|
import com.keylesspalace.tusky.util.trimTrailingWhitespace
|
|
import com.keylesspalace.tusky.viewdata.StatusViewData
|
|
import java.util.Date
|
|
|
|
data class Placeholder(
|
|
val id: String,
|
|
val loading: Boolean
|
|
)
|
|
|
|
private val attachmentArrayListType = object : TypeToken<ArrayList<Attachment>>() {}.type
|
|
private val emojisListType = object : TypeToken<List<Emoji>>() {}.type
|
|
private val mentionListType = object : TypeToken<List<Status.Mention>>() {}.type
|
|
|
|
fun Account.toEntity(accountId: Long, gson: Gson): TimelineAccountEntity {
|
|
return TimelineAccountEntity(
|
|
serverId = id,
|
|
timelineUserId = accountId,
|
|
localUsername = localUsername,
|
|
username = username,
|
|
displayName = name,
|
|
url = url,
|
|
avatar = avatar,
|
|
emojis = gson.toJson(emojis),
|
|
bot = bot
|
|
)
|
|
}
|
|
|
|
fun TimelineAccountEntity.toAccount(gson: Gson): Account {
|
|
return Account(
|
|
id = serverId,
|
|
localUsername = localUsername,
|
|
username = username,
|
|
displayName = displayName,
|
|
note = SpannedString(""),
|
|
url = url,
|
|
avatar = avatar,
|
|
header = "",
|
|
locked = false,
|
|
followingCount = 0,
|
|
followersCount = 0,
|
|
statusesCount = 0,
|
|
source = null,
|
|
bot = bot,
|
|
emojis = gson.fromJson(emojis, emojisListType),
|
|
fields = null,
|
|
moved = null
|
|
)
|
|
}
|
|
|
|
fun Placeholder.toEntity(timelineUserId: Long): TimelineStatusEntity {
|
|
return TimelineStatusEntity(
|
|
serverId = this.id,
|
|
url = null,
|
|
timelineUserId = timelineUserId,
|
|
authorServerId = null,
|
|
inReplyToId = null,
|
|
inReplyToAccountId = null,
|
|
content = null,
|
|
createdAt = 0L,
|
|
emojis = null,
|
|
reblogsCount = 0,
|
|
favouritesCount = 0,
|
|
reblogged = false,
|
|
favourited = false,
|
|
bookmarked = false,
|
|
sensitive = false,
|
|
spoilerText = "",
|
|
visibility = Status.Visibility.UNKNOWN,
|
|
attachments = null,
|
|
mentions = null,
|
|
application = null,
|
|
reblogServerId = null,
|
|
reblogAccountId = null,
|
|
poll = null,
|
|
muted = false,
|
|
expanded = loading,
|
|
contentCollapsed = false,
|
|
contentShowing = false,
|
|
pinned = false
|
|
)
|
|
}
|
|
|
|
fun Status.toEntity(
|
|
timelineUserId: Long,
|
|
gson: Gson,
|
|
expanded: Boolean,
|
|
contentShowing: Boolean,
|
|
contentCollapsed: Boolean
|
|
): TimelineStatusEntity {
|
|
return TimelineStatusEntity(
|
|
serverId = this.id,
|
|
url = actionableStatus.url,
|
|
timelineUserId = timelineUserId,
|
|
authorServerId = actionableStatus.account.id,
|
|
inReplyToId = actionableStatus.inReplyToId,
|
|
inReplyToAccountId = actionableStatus.inReplyToAccountId,
|
|
content = actionableStatus.content.toHtml(),
|
|
createdAt = actionableStatus.createdAt.time,
|
|
emojis = actionableStatus.emojis.let(gson::toJson),
|
|
reblogsCount = actionableStatus.reblogsCount,
|
|
favouritesCount = actionableStatus.favouritesCount,
|
|
reblogged = actionableStatus.reblogged,
|
|
favourited = actionableStatus.favourited,
|
|
bookmarked = actionableStatus.bookmarked,
|
|
sensitive = actionableStatus.sensitive,
|
|
spoilerText = actionableStatus.spoilerText,
|
|
visibility = actionableStatus.visibility,
|
|
attachments = actionableStatus.attachments.let(gson::toJson),
|
|
mentions = actionableStatus.mentions.let(gson::toJson),
|
|
application = actionableStatus.application.let(gson::toJson),
|
|
reblogServerId = reblog?.id,
|
|
reblogAccountId = reblog?.let { this.account.id },
|
|
poll = actionableStatus.poll.let(gson::toJson),
|
|
muted = actionableStatus.muted,
|
|
expanded = expanded,
|
|
contentShowing = contentShowing,
|
|
contentCollapsed = contentCollapsed,
|
|
pinned = actionableStatus.pinned == true
|
|
)
|
|
}
|
|
|
|
fun TimelineStatusWithAccount.toViewData(gson: Gson): StatusViewData {
|
|
if (this.status.authorServerId == null) {
|
|
return StatusViewData.Placeholder(this.status.serverId, this.status.expanded)
|
|
}
|
|
|
|
val attachments: ArrayList<Attachment> = gson.fromJson(status.attachments, attachmentArrayListType) ?: arrayListOf()
|
|
val mentions: List<Status.Mention> = gson.fromJson(status.mentions, mentionListType) ?: emptyList()
|
|
val application = gson.fromJson(status.application, Status.Application::class.java)
|
|
val emojis: List<Emoji> = gson.fromJson(status.emojis, emojisListType) ?: emptyList()
|
|
val poll: Poll? = gson.fromJson(status.poll, Poll::class.java)
|
|
|
|
val reblog = status.reblogServerId?.let { id ->
|
|
Status(
|
|
id = id,
|
|
url = status.url,
|
|
account = account.toAccount(gson),
|
|
inReplyToId = status.inReplyToId,
|
|
inReplyToAccountId = status.inReplyToAccountId,
|
|
reblog = null,
|
|
content = status.content?.parseAsHtml()?.trimTrailingWhitespace()
|
|
?: SpannedString(""),
|
|
createdAt = Date(status.createdAt),
|
|
emojis = emojis,
|
|
reblogsCount = status.reblogsCount,
|
|
favouritesCount = status.favouritesCount,
|
|
reblogged = status.reblogged,
|
|
favourited = status.favourited,
|
|
bookmarked = status.bookmarked,
|
|
sensitive = status.sensitive,
|
|
spoilerText = status.spoilerText,
|
|
visibility = status.visibility,
|
|
attachments = attachments,
|
|
mentions = mentions,
|
|
application = application,
|
|
pinned = false,
|
|
muted = status.muted,
|
|
poll = poll,
|
|
card = null
|
|
)
|
|
}
|
|
val status = if (reblog != null) {
|
|
Status(
|
|
id = status.serverId,
|
|
url = null, // no url for reblogs
|
|
account = this.reblogAccount!!.toAccount(gson),
|
|
inReplyToId = null,
|
|
inReplyToAccountId = null,
|
|
reblog = reblog,
|
|
content = SpannedString(""),
|
|
createdAt = Date(status.createdAt), // lie but whatever?
|
|
emojis = listOf(),
|
|
reblogsCount = 0,
|
|
favouritesCount = 0,
|
|
reblogged = false,
|
|
favourited = false,
|
|
bookmarked = false,
|
|
sensitive = false,
|
|
spoilerText = "",
|
|
visibility = status.visibility,
|
|
attachments = ArrayList(),
|
|
mentions = listOf(),
|
|
application = null,
|
|
pinned = status.pinned,
|
|
muted = status.muted,
|
|
poll = null,
|
|
card = null
|
|
)
|
|
} else {
|
|
Status(
|
|
id = status.serverId,
|
|
url = status.url,
|
|
account = account.toAccount(gson),
|
|
inReplyToId = status.inReplyToId,
|
|
inReplyToAccountId = status.inReplyToAccountId,
|
|
reblog = null,
|
|
content = status.content?.parseAsHtml()?.trimTrailingWhitespace()
|
|
?: SpannedString(""),
|
|
createdAt = Date(status.createdAt),
|
|
emojis = emojis,
|
|
reblogsCount = status.reblogsCount,
|
|
favouritesCount = status.favouritesCount,
|
|
reblogged = status.reblogged,
|
|
favourited = status.favourited,
|
|
bookmarked = status.bookmarked,
|
|
sensitive = status.sensitive,
|
|
spoilerText = status.spoilerText,
|
|
visibility = status.visibility,
|
|
attachments = attachments,
|
|
mentions = mentions,
|
|
application = application,
|
|
pinned = status.pinned,
|
|
muted = status.muted,
|
|
poll = poll,
|
|
card = null
|
|
)
|
|
}
|
|
return StatusViewData.Concrete(
|
|
status = status,
|
|
isExpanded = this.status.expanded,
|
|
isShowingContent = this.status.contentShowing,
|
|
isCollapsible = shouldTrimStatus(status.content),
|
|
isCollapsed = this.status.contentCollapsed
|
|
)
|
|
}
|