Convert ChipListCache to Kotlin

pull/1066/head
Alex Baker 4 years ago
parent 14f46f0688
commit e97e0d3a22

@ -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<String>): List<Long>
@Query("""
SELECT *
FROM caldav_accounts
WHERE cda_account_type = $TYPE_OPENTASKS
AND cda_uuid NOT IN (:accounts)
""")
abstract suspend fun findDeletedAccounts(accounts: List<String>): List<CaldavAccount>
@Query("SELECT * FROM caldav_lists WHERE cdl_account = :account AND cdl_url NOT IN (:urls)")
abstract suspend fun findDeletedCalendars(account: String, urls: List<String>): List<CaldavCalendar>
@ -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)")

@ -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<List<CaldavCalendar>> {
return dao.subscribeToCalendars()
}
fun getCalendars(): List<CaldavCalendar> = runBlocking {
dao.getCalendars()
}

@ -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<List<GoogleTaskList>> {
return dao.subscribeToLists()
}
fun findExistingList(remoteId: String): GoogleTaskList? = runBlocking {
dao.findExistingList(remoteId)
}

@ -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<List<TagData>> {
return dao.subscribeToTags()
}
fun tagDataOrderedByName(): List<TagData> = runBlocking {
dao.tagDataOrderedByName()
}

@ -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<String, GtasksFilter> googleTaskLists = new HashMap<>();
private final Map<String, CaldavFilter> caldavCalendars = new HashMap<>();
private final Map<String, TagFilter> 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<GoogleTaskList> updated) {
googleTaskLists.clear();
for (GoogleTaskList update : updated) {
googleTaskLists.put(update.getRemoteId(), new GtasksFilter(update));
}
localBroadcastManager.broadcastRefresh();
}
private void updateCaldavCalendars(List<CaldavCalendar> updated) {
caldavCalendars.clear();
for (CaldavCalendar update : updated) {
caldavCalendars.put(update.getUuid(), new CaldavFilter(update));
}
localBroadcastManager.broadcastRefresh();
}
private void updateTags(List<TagData> 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);
}
}

@ -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<String?, GtasksFilter> = HashMap()
private val caldavCalendars: MutableMap<String?, CaldavFilter> = HashMap()
private val tagDatas: MutableMap<String?, TagFilter> = HashMap()
private fun updateGoogleTaskLists(updated: List<GoogleTaskList>) {
googleTaskLists.clear()
for (update in updated) {
googleTaskLists[update.remoteId] = GtasksFilter(update)
}
localBroadcastManager.broadcastRefresh()
}
private fun updateCaldavCalendars(updated: List<CaldavCalendar>) {
caldavCalendars.clear()
for (update in updated) {
caldavCalendars[update.uuid] = CaldavFilter(update)
}
localBroadcastManager.broadcastRefresh()
}
private fun updateTags(updated: List<TagData>) {
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<GoogleTaskList> -> updateGoogleTaskLists(updated) }
caldavDao.subscribeToCalendars().observeForever { updated: List<CaldavCalendar> -> updateCaldavCalendars(updated) }
tagDataDao.subscribeToTags().observeForever { updated: List<TagData> -> updateTags(updated) }
}
}
Loading…
Cancel
Save