11package com.readrops.db.queries
22
3+ import androidx.room.util.newStringBuilder
34import androidx.sqlite.db.SimpleSQLiteQuery
45import androidx.sqlite.db.SupportSQLiteQuery
56import androidx.sqlite.db.SupportSQLiteQueryBuilder
@@ -25,7 +26,9 @@ object FoldersAndFeedsQueryBuilder {
2526 )
2627
2728 private val FEED_JOIN = """ (Select * From Feed Where account_id = :accountId) Feed
28- Left Join Folder On Folder.id = Feed.folder_id """ .trimMargin()
29+ Left Join Folder On Folder.id = Feed.folder_id""" .trimMargin()
30+
31+ private val SEPARATE_STATE = " Left Join ItemState On ItemState.remote_id = Item.remote_id"
2932
3033 private const val FOLDER_JOIN = " Folder Left Join Feed On Folder.id = Feed.folder_id "
3134
@@ -35,12 +38,10 @@ object FoldersAndFeedsQueryBuilder {
3538
3639 private const val FOLDER_SELECTION = " Feed.id is NULL And Folder.account_id = :accountId"
3740
38- private const val ITEM_SELECTION = " And Item.read = 0"
39-
40- fun build (accountId : Int , hideReadFeeds : Boolean = false): SupportSQLiteQuery {
41- return SimpleSQLiteQuery (
41+ fun build (accountId : Int , hideReadFeeds : Boolean , useSeparateState : Boolean ): SupportSQLiteQuery {
42+ val result = SimpleSQLiteQuery (
4243 """
43- ${buildFeedQuery(accountId, hideReadFeeds).sql}
44+ ${buildFeedQuery(accountId, hideReadFeeds, useSeparateState ).sql}
4445 ${
4546 if (! hideReadFeeds) {
4647 """ UNION ALL
@@ -51,11 +52,23 @@ object FoldersAndFeedsQueryBuilder {
5152 }
5253 } """ .trimIndent()
5354 )
55+ return result
5456 }
5557
56- private fun buildFeedQuery (accountId : Int , hideReadFeeds : Boolean ): SupportSQLiteQuery {
57- val tables = if (hideReadFeeds) FEED_JOIN + ITEM_JOIN else FEED_JOIN
58- val selection = if (hideReadFeeds) FEED_SELECTION + ITEM_SELECTION else FEED_SELECTION
58+ private fun buildFeedQuery (accountId : Int , hideReadFeeds : Boolean , useSeparateState : Boolean ): SupportSQLiteQuery {
59+ val tables = buildString {
60+ append(FEED_JOIN )
61+ append(ITEM_JOIN )
62+ if (hideReadFeeds) {
63+ if (useSeparateState) append(SEPARATE_STATE )
64+ }
65+ }
66+ val selection = buildString {
67+ append(FEED_SELECTION )
68+ if (hideReadFeeds) {
69+ if (useSeparateState) append(" And ItemState.read = 0" ) else append(" And Item.read = 0" )
70+ }
71+ }
5972
6073 return SupportSQLiteQueryBuilder .builder(tables.replace(" :accountId" , " $accountId " )).run {
6174 columns(COLUMNS )
0 commit comments