Add logic to build drawer folders and feeds list

This commit is contained in:
Shinokuni 2023-08-20 12:22:25 +02:00
parent 005858b8cd
commit 3f665e6ab5
7 changed files with 129 additions and 0 deletions

View File

@ -106,4 +106,6 @@ dependencies {
implementation "io.coil-kt:coil:2.4.0" implementation "io.coil-kt:coil:2.4.0"
implementation "io.coil-kt:coil-compose:2.4.0" implementation "io.coil-kt:coil-compose:2.4.0"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4"
} }

View File

@ -0,0 +1,64 @@
package com.readrops.app.compose
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.readrops.app.compose.repositories.GetFoldersWithFeeds
import com.readrops.db.Database
import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account
import com.readrops.db.entities.account.AccountType
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import org.joda.time.LocalDateTime
import org.junit.Before
import org.junit.Test
import kotlin.test.assertTrue
class GetFoldersWithFeedsTest {
private lateinit var database: Database
private lateinit var getFoldersWithFeeds: GetFoldersWithFeeds
private val account = Account(accountType = AccountType.LOCAL)
@Before
fun before() {
val context = ApplicationProvider.getApplicationContext<Context>()
database = Room.inMemoryDatabaseBuilder(context, Database::class.java).build()
runTest {
account.id = database.newAccountDao().insert(account).toInt()
repeat(3) { time ->
database.newFolderDao().insert(Folder(name = "Folder $time", accountId = account.id))
}
repeat(2) { time ->
database.newFeedDao().insert(Feed(name = "Feed $time", accountId = account.id))
}
repeat(2) { time ->
database.newFeedDao().insert(Feed(name = "Feed ${time+2}", folderId = 1, accountId = account.id))
}
repeat(3) { time ->
database.newItemDao().insert(Item(title = "Item $time", feedId = 1, pubDate = LocalDateTime.now()))
}
}
}
@Test
fun getFoldersWithFeedsTest() = runTest {
getFoldersWithFeeds = GetFoldersWithFeeds(database, StandardTestDispatcher(testScheduler))
val foldersAndFeeds = getFoldersWithFeeds.get(account.id)
assertTrue { foldersAndFeeds.size == 4 }
assertTrue { foldersAndFeeds.entries.first().value.size == 2 }
assertTrue { foldersAndFeeds.entries.last().key == null }
assertTrue { foldersAndFeeds[null]!!.size == 2 }
assertTrue { foldersAndFeeds[null]!!.first().unreadCount == 3 }
}
}

View File

@ -0,0 +1,37 @@
package com.readrops.app.compose.repositories
import com.readrops.db.Database
import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class GetFoldersWithFeeds(
private val database: Database,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
) {
suspend fun get(accountId: Int): Map<Folder?, List<Feed>> = withContext(dispatcher) {
val foldersWithFeeds = mutableMapOf<Folder?, List<Feed>>()
val folders = database.newFolderDao().selectFoldersByAccount(accountId)
for (folder in folders) {
val feeds = database.newFeedDao().selectFeedsByFolder(folder.id)
for (feed in feeds) {
feed.unreadCount = database.newItemDao().selectUnreadCount(feed.id)
}
foldersWithFeeds[folder] = feeds
}
val feedsAlone = database.newFeedDao().selectFeedsAlone(accountId)
for (feed in feedsAlone) {
feed.unreadCount = database.newItemDao().selectUnreadCount(feed.id)
}
foldersWithFeeds[null] = feedsAlone
foldersWithFeeds.toSortedMap(nullsLast())
}
}

View File

@ -6,6 +6,7 @@ import androidx.room.TypeConverters
import com.readrops.db.dao.* import com.readrops.db.dao.*
import com.readrops.db.dao.newdao.NewAccountDao import com.readrops.db.dao.newdao.NewAccountDao
import com.readrops.db.dao.newdao.NewFeedDao import com.readrops.db.dao.newdao.NewFeedDao
import com.readrops.db.dao.newdao.NewFolderDao
import com.readrops.db.dao.newdao.NewItemDao import com.readrops.db.dao.newdao.NewItemDao
import com.readrops.db.entities.* import com.readrops.db.entities.*
import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.Account
@ -35,4 +36,6 @@ abstract class Database : RoomDatabase() {
abstract fun newItemDao(): NewItemDao abstract fun newItemDao(): NewItemDao
abstract fun newAccountDao(): NewAccountDao abstract fun newAccountDao(): NewAccountDao
abstract fun newFolderDao(): NewFolderDao
} }

View File

@ -19,4 +19,10 @@ abstract class NewFeedDao : NewBaseDao<Feed> {
@Query("Select case When :feedUrl In (Select url from Feed Where account_id = :accountId) Then 1 else 0 end") @Query("Select case When :feedUrl In (Select url from Feed Where account_id = :accountId) Then 1 else 0 end")
abstract suspend fun feedExists(feedUrl: String, accountId: Int): Boolean abstract suspend fun feedExists(feedUrl: String, accountId: Int): Boolean
@Query("Select * From Feed Where folder_id = :folderId")
abstract suspend fun selectFeedsByFolder(folderId: Int): List<Feed>
@Query("Select * From Feed Where account_id = :accountId And folder_id Is Null")
abstract suspend fun selectFeedsAlone(accountId: Int): List<Feed>
} }

View File

@ -0,0 +1,14 @@
package com.readrops.db.dao.newdao
import androidx.room.Dao
import androidx.room.Query
import com.readrops.db.entities.Folder
@Dao
abstract class NewFolderDao : NewBaseDao<Folder> {
@Query("Select * From Folder Where account_id = :accountId Order By name ASC")
abstract suspend fun selectFoldersByAccount(accountId: Int): List<Folder>
}

View File

@ -1,6 +1,7 @@
package com.readrops.db.dao.newdao package com.readrops.db.dao.newdao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query
import androidx.room.RawQuery import androidx.room.RawQuery
import androidx.sqlite.db.SupportSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
@ -16,5 +17,7 @@ abstract class NewItemDao : NewBaseDao<Item> {
@RawQuery(observedEntities = [Item::class, Feed::class, Folder::class, ItemState::class]) @RawQuery(observedEntities = [Item::class, Feed::class, Folder::class, ItemState::class])
abstract fun selectAll(query: SupportSQLiteQuery): Flow<List<ItemWithFeed>> abstract fun selectAll(query: SupportSQLiteQuery): Flow<List<ItemWithFeed>>
@Query("Select count(*) From Item Where feed_id = :feedId And read = 0")
abstract fun selectUnreadCount(feedId: Int): Int
} }