From e97e0d3a22657cfdb2231776c13f8d4bbc6a266d Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 4 Aug 2020 15:00:46 -0500 Subject: [PATCH] Convert ChipListCache to Kotlin --- app/src/main/java/org/tasks/data/CaldavDao.kt | 16 ++-- .../java/org/tasks/data/CaldavDaoBlocking.kt | 5 -- .../tasks/data/GoogleTaskListDaoBlocking.kt | 5 -- .../java/org/tasks/data/TagDataDaoBlocking.kt | 5 -- .../main/java/org/tasks/ui/ChipListCache.java | 76 ------------------- .../main/java/org/tasks/ui/ChipListCache.kt | 64 ++++++++++++++++ 6 files changed, 69 insertions(+), 102 deletions(-) delete mode 100644 app/src/main/java/org/tasks/ui/ChipListCache.java create mode 100644 app/src/main/java/org/tasks/ui/ChipListCache.kt diff --git a/app/src/main/java/org/tasks/data/CaldavDao.kt b/app/src/main/java/org/tasks/data/CaldavDao.kt index 79ea0b646..80d6bd5f8 100644 --- a/app/src/main/java/org/tasks/data/CaldavDao.kt +++ b/app/src/main/java/org/tasks/data/CaldavDao.kt @@ -11,9 +11,8 @@ import com.todoroo.astrid.helper.UUIDHelper import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import org.tasks.R -import org.tasks.data.CaldavAccount.Companion.TYPE_LOCAL -import org.tasks.data.CaldavAccount.Companion.TYPE_OPENTASKS import org.tasks.date.DateTimeUtils.toAppleEpoch +import org.tasks.db.DbUtils.dbchunk import org.tasks.db.SuspendDbUtils.chunkedMap import org.tasks.filters.CaldavFilters import org.tasks.time.DateTimeUtils.currentTimeMillis @@ -169,14 +168,6 @@ SELECT EXISTS(SELECT 1 @Query("SELECT cd_task FROM caldav_tasks WHERE cd_calendar = :calendar AND cd_object IN (:objects)") internal abstract suspend fun getTasksInternal(calendar: String, objects: List): List - @Query(""" -SELECT * -FROM caldav_accounts -WHERE cda_account_type = $TYPE_OPENTASKS - AND cda_uuid NOT IN (:accounts) - """) - abstract suspend fun findDeletedAccounts(accounts: List): List - @Query("SELECT * FROM caldav_lists WHERE cdl_account = :account AND cdl_url NOT IN (:urls)") abstract suspend fun findDeletedCalendars(account: String, urls: List): List @@ -258,7 +249,10 @@ WHERE cda_account_type = $TYPE_OPENTASKS } } update(updated) - touchInternal(updated.map(CaldavTask::task)) + updated + .map(CaldavTask::task) + .dbchunk() + .forEach { touchInternal(it) } } @Query("UPDATE tasks SET modified = :modificationTime WHERE _id in (:ids)") diff --git a/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt b/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt index f039b58a3..cf990bc8d 100644 --- a/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/CaldavDaoBlocking.kt @@ -1,15 +1,10 @@ package org.tasks.data -import androidx.lifecycle.LiveData import kotlinx.coroutines.runBlocking import javax.inject.Inject @Deprecated("use coroutines") class CaldavDaoBlocking @Inject constructor(private val dao: CaldavDao) { - fun subscribeToCalendars(): LiveData> { - return dao.subscribeToCalendars() - } - fun getCalendars(): List = runBlocking { dao.getCalendars() } diff --git a/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt b/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt index 85eed5857..661640fee 100644 --- a/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/GoogleTaskListDaoBlocking.kt @@ -1,6 +1,5 @@ package org.tasks.data -import androidx.lifecycle.LiveData import kotlinx.coroutines.runBlocking import javax.inject.Inject @@ -22,10 +21,6 @@ class GoogleTaskListDaoBlocking @Inject constructor(private val dao: GoogleTaskL dao.getByRemoteId(remoteId) } - fun subscribeToLists(): LiveData> { - return dao.subscribeToLists() - } - fun findExistingList(remoteId: String): GoogleTaskList? = runBlocking { dao.findExistingList(remoteId) } diff --git a/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt b/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt index d4accdab8..0ff353f3a 100644 --- a/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/TagDataDaoBlocking.kt @@ -1,15 +1,10 @@ package org.tasks.data -import androidx.lifecycle.LiveData import kotlinx.coroutines.runBlocking import javax.inject.Inject @Deprecated("use coroutines") class TagDataDaoBlocking @Inject constructor(private val dao: TagDataDao) { - fun subscribeToTags(): LiveData> { - return dao.subscribeToTags() - } - fun tagDataOrderedByName(): List = runBlocking { dao.tagDataOrderedByName() } diff --git a/app/src/main/java/org/tasks/ui/ChipListCache.java b/app/src/main/java/org/tasks/ui/ChipListCache.java deleted file mode 100644 index 74020ca9b..000000000 --- a/app/src/main/java/org/tasks/ui/ChipListCache.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.tasks.ui; - -import com.todoroo.astrid.api.CaldavFilter; -import com.todoroo.astrid.api.Filter; -import com.todoroo.astrid.api.GtasksFilter; -import com.todoroo.astrid.api.TagFilter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.inject.Inject; -import javax.inject.Singleton; -import org.tasks.LocalBroadcastManager; -import org.tasks.data.CaldavCalendar; -import org.tasks.data.CaldavDaoBlocking; -import org.tasks.data.GoogleTaskList; -import org.tasks.data.GoogleTaskListDaoBlocking; -import org.tasks.data.TagData; -import org.tasks.data.TagDataDaoBlocking; - -@Singleton -public class ChipListCache { - - private final Map googleTaskLists = new HashMap<>(); - private final Map caldavCalendars = new HashMap<>(); - private final Map tagDatas = new HashMap<>(); - private final LocalBroadcastManager localBroadcastManager; - - @Inject - ChipListCache( - GoogleTaskListDaoBlocking googleTaskListDao, - CaldavDaoBlocking caldavDao, - TagDataDaoBlocking tagDataDao, - LocalBroadcastManager localBroadcastManager) { - this.localBroadcastManager = localBroadcastManager; - - googleTaskListDao.subscribeToLists().observeForever(this::updateGoogleTaskLists); - caldavDao.subscribeToCalendars().observeForever(this::updateCaldavCalendars); - tagDataDao.subscribeToTags().observeForever(this::updateTags); - } - - private void updateGoogleTaskLists(List updated) { - googleTaskLists.clear(); - for (GoogleTaskList update : updated) { - googleTaskLists.put(update.getRemoteId(), new GtasksFilter(update)); - } - localBroadcastManager.broadcastRefresh(); - } - - private void updateCaldavCalendars(List updated) { - caldavCalendars.clear(); - for (CaldavCalendar update : updated) { - caldavCalendars.put(update.getUuid(), new CaldavFilter(update)); - } - localBroadcastManager.broadcastRefresh(); - } - - private void updateTags(List updated) { - tagDatas.clear(); - for (TagData update : updated) { - tagDatas.put(update.getRemoteId(), new TagFilter(update)); - } - localBroadcastManager.broadcastRefresh(); - } - - Filter getGoogleTaskList(String googleTaskList) { - return googleTaskLists.get(googleTaskList); - } - - Filter getCaldavList(String caldav) { - return caldavCalendars.get(caldav); - } - - TagFilter getTag(String tag) { - return tagDatas.get(tag); - } -} diff --git a/app/src/main/java/org/tasks/ui/ChipListCache.kt b/app/src/main/java/org/tasks/ui/ChipListCache.kt new file mode 100644 index 000000000..c2fa031f8 --- /dev/null +++ b/app/src/main/java/org/tasks/ui/ChipListCache.kt @@ -0,0 +1,64 @@ +package org.tasks.ui + +import com.todoroo.astrid.api.CaldavFilter +import com.todoroo.astrid.api.Filter +import com.todoroo.astrid.api.GtasksFilter +import com.todoroo.astrid.api.TagFilter +import org.tasks.LocalBroadcastManager +import org.tasks.data.* +import java.util.* +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class ChipListCache @Inject internal constructor( + googleTaskListDao: GoogleTaskListDao, + caldavDao: CaldavDao, + tagDataDao: TagDataDao, + private val localBroadcastManager: LocalBroadcastManager) { + + private val googleTaskLists: MutableMap = HashMap() + private val caldavCalendars: MutableMap = HashMap() + private val tagDatas: MutableMap = HashMap() + private fun updateGoogleTaskLists(updated: List) { + googleTaskLists.clear() + for (update in updated) { + googleTaskLists[update.remoteId] = GtasksFilter(update) + } + localBroadcastManager.broadcastRefresh() + } + + private fun updateCaldavCalendars(updated: List) { + caldavCalendars.clear() + for (update in updated) { + caldavCalendars[update.uuid] = CaldavFilter(update) + } + localBroadcastManager.broadcastRefresh() + } + + private fun updateTags(updated: List) { + tagDatas.clear() + for (update in updated) { + tagDatas[update.remoteId] = TagFilter(update) + } + localBroadcastManager.broadcastRefresh() + } + + fun getGoogleTaskList(googleTaskList: String?): Filter? { + return googleTaskLists[googleTaskList] + } + + fun getCaldavList(caldav: String?): Filter? { + return caldavCalendars[caldav] + } + + fun getTag(tag: String?): TagFilter? { + return tagDatas[tag] + } + + init { + googleTaskListDao.subscribeToLists().observeForever { updated: List -> updateGoogleTaskLists(updated) } + caldavDao.subscribeToCalendars().observeForever { updated: List -> updateCaldavCalendars(updated) } + tagDataDao.subscribeToTags().observeForever { updated: List -> updateTags(updated) } + } +} \ No newline at end of file