Add logic to build drawer folders and feeds list
This commit is contained in:
parent
005858b8cd
commit
3f665e6ab5
@ -106,4 +106,6 @@ dependencies {
|
||||
|
||||
implementation "io.coil-kt:coil:2.4.0"
|
||||
implementation "io.coil-kt:coil-compose:2.4.0"
|
||||
|
||||
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4"
|
||||
}
|
@ -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 }
|
||||
|
||||
}
|
||||
}
|
@ -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())
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import androidx.room.TypeConverters
|
||||
import com.readrops.db.dao.*
|
||||
import com.readrops.db.dao.newdao.NewAccountDao
|
||||
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.entities.*
|
||||
import com.readrops.db.entities.account.Account
|
||||
@ -35,4 +36,6 @@ abstract class Database : RoomDatabase() {
|
||||
abstract fun newItemDao(): NewItemDao
|
||||
|
||||
abstract fun newAccountDao(): NewAccountDao
|
||||
|
||||
abstract fun newFolderDao(): NewFolderDao
|
||||
}
|
@ -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")
|
||||
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>
|
||||
}
|
14
db/src/main/java/com/readrops/db/dao/newdao/NewFolderDao.kt
Normal file
14
db/src/main/java/com/readrops/db/dao/newdao/NewFolderDao.kt
Normal 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>
|
||||
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.readrops.db.dao.newdao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.RawQuery
|
||||
import androidx.sqlite.db.SupportSQLiteQuery
|
||||
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])
|
||||
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
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user