From adb56cdacf4de79d976bc28ddbcc9020a0c3f053 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Wed, 21 Feb 2024 23:59:01 +0100 Subject: [PATCH] Add mark all read items action for some filters in TimelineTab --- .../app/compose/repositories/ARepository.kt | 16 +++++++++ .../app/compose/timelime/TimelineTab.kt | 26 +++++++++++++- .../app/compose/timelime/TimelineViewModel.kt | 36 ++++++++++++++++++- .../com/readrops/db/dao/newdao/NewItemDao.kt | 14 ++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/appcompose/src/main/java/com/readrops/app/compose/repositories/ARepository.kt b/appcompose/src/main/java/com/readrops/app/compose/repositories/ARepository.kt index 72e8c299..8b133d8d 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/repositories/ARepository.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/repositories/ARepository.kt @@ -64,4 +64,20 @@ abstract class BaseRepository( open suspend fun setItemStarState(item: Item) { database.newItemDao().updateStarState(item.id, item.isStarred) } + + open suspend fun setAllItemsRead(accountId: Int) { + database.newItemDao().setAllItemsRead(accountId) + } + + open suspend fun setAllStarredItemsRead(accountId: Int) { + database.newItemDao().setAllStarredItemsRead(accountId) + } + + open suspend fun setAllItemsReadByFeed(feedId: Int, accountId: Int) { + database.newItemDao().setAllItemsReadByFeed(feedId, accountId) + } + + open suspend fun setAllItemsReadByFolder(folderId: Int, accountId: Int) { + database.newItemDao().setAllItemsReadByFolder(folderId, accountId) + } } \ No newline at end of file diff --git a/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt b/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt index 0fcb7339..ea421bf0 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt @@ -42,6 +42,7 @@ import com.readrops.app.compose.R import com.readrops.app.compose.item.ItemScreen import com.readrops.app.compose.timelime.drawer.TimelineDrawer import com.readrops.app.compose.util.components.CenteredColumn +import com.readrops.app.compose.util.components.TwoChoicesDialog import com.readrops.app.compose.util.theme.spacing import com.readrops.db.filters.FilterType import org.koin.androidx.compose.getViewModel @@ -96,6 +97,21 @@ object TimelineTab : Tab { } } + if (state.confirmDialog) { + TwoChoicesDialog( + title = "Mark all items as read", + text = "Do you really want to mark all items as read?", + icon = painterResource(id = R.drawable.ic_rss_feed_grey), + confirmText = "Validate", + dismissText = "Cancel", + onDismiss = { viewModel.closeConfirmDialog() }, + onConfirm = { + viewModel.closeConfirmDialog() + viewModel.setAllItemsRead() + } + ) + } + ModalNavigationDrawer( drawerState = drawerState, drawerContent = { @@ -158,7 +174,15 @@ object TimelineTab : Tab { ) }, floatingActionButton = { - FloatingActionButton(onClick = { }) { + FloatingActionButton( + onClick = { + if (state.filters.filterType == FilterType.NO_FILTER) { + viewModel.openConfirmDialog() + } else { + viewModel.setAllItemsRead() + } + } + ) { Icon( painter = painterResource(id = R.drawable.ic_done_all), contentDescription = null diff --git a/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineViewModel.kt b/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineViewModel.kt index b6a220f2..e0433b6a 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineViewModel.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineViewModel.kt @@ -183,6 +183,39 @@ class TimelineViewModel( context.startActivity(Intent.createChooser(it, null)) } } + + fun setAllItemsRead() { + viewModelScope.launch(dispatcher) { + when (_timelineState.value.filters.filterType) { + FilterType.FEED_FILTER -> + repository?.setAllItemsReadByFeed( + _timelineState.value.filters.filterFeedId, + currentAccount!!.id + ) + + FilterType.FOLDER_FILER -> repository?.setAllItemsReadByFolder( + _timelineState.value.filters.filterFolderId, + currentAccount!!.id + ) + FilterType.READ_IT_LATER_FILTER -> TODO() + FilterType.STARS_FILTER -> repository?.setAllStarredItemsRead(currentAccount!!.id) + FilterType.NO_FILTER -> repository?.setAllItemsRead(currentAccount!!.id) + FilterType.NEW -> TODO() + } + } + } + + fun openConfirmDialog() { + _timelineState.value = _timelineState.value.copy( + confirmDialog = true + ) + } + + fun closeConfirmDialog() { + _timelineState.value = _timelineState.value.copy( + confirmDialog = false + ) + } } @Immutable @@ -194,5 +227,6 @@ data class TimelineState( val filterFeedName: String = "", val filterFolderName: String = "", val foldersAndFeeds: Map> = emptyMap(), - val itemState: Flow> = emptyFlow() + val itemState: Flow> = emptyFlow(), + val confirmDialog: Boolean = false ) diff --git a/db/src/main/java/com/readrops/db/dao/newdao/NewItemDao.kt b/db/src/main/java/com/readrops/db/dao/newdao/NewItemDao.kt index ed9aaa05..ad83af9e 100644 --- a/db/src/main/java/com/readrops/db/dao/newdao/NewItemDao.kt +++ b/db/src/main/java/com/readrops/db/dao/newdao/NewItemDao.kt @@ -22,4 +22,18 @@ abstract class NewItemDao : NewBaseDao { @Query("Update Item Set starred = :starred Where id = :itemId") abstract suspend fun updateStarState(itemId: Int, starred: Boolean) + + @Query("Update Item set read = 1 Where feed_id IN (Select id From Feed Where account_id = :accountId)") + abstract suspend fun setAllItemsRead(accountId: Int) + + @Query("Update Item set read = 1 Where starred = 1 And feed_id IN (Select id From Feed Where account_id = :accountId)") + abstract suspend fun setAllStarredItemsRead(accountId: Int) + + @Query("Update Item set read = 1 Where feed_id IN " + + "(Select id From Feed Where id = :feedId And account_id = :accountId)") + abstract suspend fun setAllItemsReadByFeed(feedId: Int, accountId: Int) + + @Query("Update Item set read = 1 Where feed_id IN (Select Feed.id From Feed Inner Join Folder " + + "On Feed.folder_id = Folder.id Where Folder.id = :folderId And Folder.account_id = :accountId)") + abstract suspend fun setAllItemsReadByFolder(folderId: Int, accountId: Int) } \ No newline at end of file