respect spoiler/sensitive settings in conversations (#2891)

* respect spoiler/sensitive settings in conversations

* fix code formatting
This commit is contained in:
Konrad Pozniak 2022-11-22 20:11:30 +01:00 committed by GitHub
parent a4727716c9
commit f6a5510841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 12 deletions

View File

@ -146,7 +146,11 @@ fun TimelineAccount.toEntity() =
emojis = emojis ?: emptyList() emojis = emojis ?: emptyList()
) )
fun Status.toEntity() = fun Status.toEntity(
expanded: Boolean,
contentShowing: Boolean,
contentCollapsed: Boolean
) =
ConversationStatusEntity( ConversationStatusEntity(
id = id, id = id,
url = url, url = url,
@ -165,20 +169,30 @@ fun Status.toEntity() =
attachments = attachments, attachments = attachments,
mentions = mentions, mentions = mentions,
tags = tags, tags = tags,
showingHiddenContent = false, showingHiddenContent = contentShowing,
expanded = false, expanded = expanded,
collapsed = true, collapsed = contentCollapsed,
muted = muted ?: false, muted = muted ?: false,
poll = poll, poll = poll,
language = language, language = language,
) )
fun Conversation.toEntity(accountId: Long, order: Int) = fun Conversation.toEntity(
accountId: Long,
order: Int,
expanded: Boolean,
contentShowing: Boolean,
contentCollapsed: Boolean
) =
ConversationEntity( ConversationEntity(
accountId = accountId, accountId = accountId,
id = id, id = id,
order = order, order = order,
accounts = accounts.map { it.toEntity() }, accounts = accounts.map { it.toEntity() },
unread = unread, unread = unread,
lastStatus = lastStatus!!.toEntity() lastStatus = lastStatus!!.toEntity(
expanded = expanded,
contentShowing = contentShowing,
contentCollapsed = contentCollapsed
)
) )

View File

@ -5,6 +5,7 @@ import androidx.paging.LoadType
import androidx.paging.PagingState import androidx.paging.PagingState
import androidx.paging.RemoteMediator import androidx.paging.RemoteMediator
import androidx.room.withTransaction import androidx.room.withTransaction
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.db.AppDatabase import com.keylesspalace.tusky.db.AppDatabase
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.HttpHeaderLink import com.keylesspalace.tusky.util.HttpHeaderLink
@ -12,15 +13,17 @@ import retrofit2.HttpException
@OptIn(ExperimentalPagingApi::class) @OptIn(ExperimentalPagingApi::class)
class ConversationsRemoteMediator( class ConversationsRemoteMediator(
private val accountId: Long,
private val api: MastodonApi, private val api: MastodonApi,
private val db: AppDatabase private val db: AppDatabase,
accountManager: AccountManager,
) : RemoteMediator<Int, ConversationEntity>() { ) : RemoteMediator<Int, ConversationEntity>() {
private var nextKey: String? = null private var nextKey: String? = null
private var order: Int = 0 private var order: Int = 0
private val activeAccount = accountManager.activeAccount!!
override suspend fun load( override suspend fun load(
loadType: LoadType, loadType: LoadType,
state: PagingState<Int, ConversationEntity> state: PagingState<Int, ConversationEntity>
@ -46,7 +49,7 @@ class ConversationsRemoteMediator(
db.withTransaction { db.withTransaction {
if (loadType == LoadType.REFRESH) { if (loadType == LoadType.REFRESH) {
db.conversationDao().deleteForAccount(accountId) db.conversationDao().deleteForAccount(activeAccount.id)
} }
val linkHeader = conversationsResponse.headers()["Link"] val linkHeader = conversationsResponse.headers()["Link"]
@ -56,8 +59,19 @@ class ConversationsRemoteMediator(
db.conversationDao().insert( db.conversationDao().insert(
conversations conversations
.filterNot { it.lastStatus == null } .filterNot { it.lastStatus == null }
.map { .map { conversation ->
it.toEntity(accountId, order++)
val expanded = activeAccount.alwaysOpenSpoiler
val contentShowing = activeAccount.alwaysShowSensitiveMedia || !conversation.lastStatus!!.sensitive
val contentCollapsed = true
conversation.toEntity(
accountId = activeAccount.id,
order = order++,
expanded = expanded,
contentShowing = contentShowing,
contentCollapsed = contentCollapsed
)
} }
) )
} }

View File

@ -42,7 +42,7 @@ class ConversationsViewModel @Inject constructor(
@OptIn(ExperimentalPagingApi::class) @OptIn(ExperimentalPagingApi::class)
val conversationFlow = Pager( val conversationFlow = Pager(
config = PagingConfig(pageSize = 30), config = PagingConfig(pageSize = 30),
remoteMediator = ConversationsRemoteMediator(accountManager.activeAccount!!.id, api, database), remoteMediator = ConversationsRemoteMediator(api, database, accountManager),
pagingSourceFactory = { database.conversationDao().conversationsForAccount(accountManager.activeAccount!!.id) } pagingSourceFactory = { database.conversationDao().conversationsForAccount(accountManager.activeAccount!!.id) }
) )
.flow .flow