From f6a55108415b799838593b4807062801f20eb699 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Tue, 22 Nov 2022 20:11:30 +0100 Subject: [PATCH] respect spoiler/sensitive settings in conversations (#2891) * respect spoiler/sensitive settings in conversations * fix code formatting --- .../conversation/ConversationEntity.kt | 26 ++++++++++++++----- .../ConversationsRemoteMediator.kt | 24 +++++++++++++---- .../conversation/ConversationsViewModel.kt | 2 +- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt index 6816c13be..48aeb78e7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt @@ -146,7 +146,11 @@ fun TimelineAccount.toEntity() = emojis = emojis ?: emptyList() ) -fun Status.toEntity() = +fun Status.toEntity( + expanded: Boolean, + contentShowing: Boolean, + contentCollapsed: Boolean +) = ConversationStatusEntity( id = id, url = url, @@ -165,20 +169,30 @@ fun Status.toEntity() = attachments = attachments, mentions = mentions, tags = tags, - showingHiddenContent = false, - expanded = false, - collapsed = true, + showingHiddenContent = contentShowing, + expanded = expanded, + collapsed = contentCollapsed, muted = muted ?: false, poll = poll, language = language, ) -fun Conversation.toEntity(accountId: Long, order: Int) = +fun Conversation.toEntity( + accountId: Long, + order: Int, + expanded: Boolean, + contentShowing: Boolean, + contentCollapsed: Boolean +) = ConversationEntity( accountId = accountId, id = id, order = order, accounts = accounts.map { it.toEntity() }, unread = unread, - lastStatus = lastStatus!!.toEntity() + lastStatus = lastStatus!!.toEntity( + expanded = expanded, + contentShowing = contentShowing, + contentCollapsed = contentCollapsed + ) ) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsRemoteMediator.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsRemoteMediator.kt index 02a44f951..921d694b2 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsRemoteMediator.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsRemoteMediator.kt @@ -5,6 +5,7 @@ import androidx.paging.LoadType import androidx.paging.PagingState import androidx.paging.RemoteMediator import androidx.room.withTransaction +import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.db.AppDatabase import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.util.HttpHeaderLink @@ -12,15 +13,17 @@ import retrofit2.HttpException @OptIn(ExperimentalPagingApi::class) class ConversationsRemoteMediator( - private val accountId: Long, private val api: MastodonApi, - private val db: AppDatabase + private val db: AppDatabase, + accountManager: AccountManager, ) : RemoteMediator() { private var nextKey: String? = null private var order: Int = 0 + private val activeAccount = accountManager.activeAccount!! + override suspend fun load( loadType: LoadType, state: PagingState @@ -46,7 +49,7 @@ class ConversationsRemoteMediator( db.withTransaction { if (loadType == LoadType.REFRESH) { - db.conversationDao().deleteForAccount(accountId) + db.conversationDao().deleteForAccount(activeAccount.id) } val linkHeader = conversationsResponse.headers()["Link"] @@ -56,8 +59,19 @@ class ConversationsRemoteMediator( db.conversationDao().insert( conversations .filterNot { it.lastStatus == null } - .map { - it.toEntity(accountId, order++) + .map { conversation -> + + 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 + ) } ) } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt index 735aa26c1..5e9e609e2 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsViewModel.kt @@ -42,7 +42,7 @@ class ConversationsViewModel @Inject constructor( @OptIn(ExperimentalPagingApi::class) val conversationFlow = Pager( config = PagingConfig(pageSize = 30), - remoteMediator = ConversationsRemoteMediator(accountManager.activeAccount!!.id, api, database), + remoteMediator = ConversationsRemoteMediator(api, database, accountManager), pagingSourceFactory = { database.conversationDao().conversationsForAccount(accountManager.activeAccount!!.id) } ) .flow