Add collection count flow to invalidate pagers by recreation

Signed-off-by: Sunik Kupfer <kupfer@bitfire.at>
This commit is contained in:
Sunik Kupfer
2025-12-23 16:51:34 +01:00
parent ae00999d94
commit 319a4c49ec
3 changed files with 22 additions and 6 deletions

View File

@@ -69,6 +69,12 @@ interface CollectionDao {
@Query("SELECT * FROM collection WHERE serviceId=:serviceId AND url=:url")
fun getByServiceAndUrl(serviceId: Long, url: String): Collection?
/**
* Returns the amount of collections currently stored in database
*/
@Query("SELECT COUNT(*) FROM collection")
fun getCountFlow(): Flow<Int>
@Query("SELECT * FROM collection WHERE serviceId=:serviceId AND type='${Collection.TYPE_CALENDAR}' AND supportsVEVENT AND sync ORDER BY displayName COLLATE NOCASE, url COLLATE NOCASE")
fun getSyncCalendars(serviceId: Long): List<Collection>

View File

@@ -27,6 +27,7 @@ import at.bitfire.davdroid.servicedetection.RefreshCollectionsWorker
import at.bitfire.davdroid.util.DavUtils
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runInterruptible
import net.fortuna.ical4j.model.Calendar
import net.fortuna.ical4j.model.Component
@@ -196,6 +197,8 @@ class DavCollectionRepository @Inject constructor(
fun getByServiceAndSync(serviceId: Long) = dao.getByServiceAndSync(serviceId)
fun getCollectionCountFlow(): Flow<Int> = dao.getCountFlow()
fun getSyncCalendars(serviceId: Long) = dao.getSyncCalendars(serviceId)
fun getSyncJtxCollections(serviceId: Long) = dao.getSyncJtxCollections(serviceId)

View File

@@ -23,7 +23,8 @@ import kotlinx.coroutines.flow.map
import javax.inject.Inject
/**
* Gets a list of collections for a service and type, optionally filtered by "show only personal" setting.
* Gets PagingData flow of collections for a service and type, optionally filtered by "show only personal" setting and
* recreates (invalidates) pagers when collection count changes.
*
* Takes the "force read-only address books" setting into account: if set, all address books will have "forceReadOnly" set.
*/
@@ -32,12 +33,9 @@ class GetServiceCollectionPagerUseCase @Inject constructor(
val settings: SettingsManager
) {
companion object {
const val PAGER_SIZE = 20
}
val forceReadOnlyAddressBooksFlow = settings.getBooleanFlow(Settings.FORCE_READ_ONLY_ADDRESSBOOKS, false)
val collectionsCount = collectionRepository.getCollectionCountFlow()
@OptIn(ExperimentalCoroutinesApi::class)
operator fun invoke(
@@ -45,7 +43,12 @@ class GetServiceCollectionPagerUseCase @Inject constructor(
@CollectionType collectionType: String,
showOnlyPersonalFlow: Flow<Boolean>
): Flow<PagingData<Collection>> =
combine(serviceFlow, showOnlyPersonalFlow, forceReadOnlyAddressBooksFlow) { service, onlyPersonal, forceReadOnlyAddressBooks ->
combine(
serviceFlow,
showOnlyPersonalFlow,
forceReadOnlyAddressBooksFlow,
collectionsCount // triggers pager recreation when collection amount changes in DB
) { service, onlyPersonal, forceReadOnlyAddressBooks, _ ->
service?.let { service ->
val dataFlow = Pager(
config = PagingConfig(PAGER_SIZE),
@@ -69,4 +72,8 @@ class GetServiceCollectionPagerUseCase @Inject constructor(
} ?: flowOf(PagingData.empty())
}.flatMapLatest { it }
companion object {
const val PAGER_SIZE = 20
}
}