Move CaldavDaoExtensions to kmp

pull/2945/head
Alex Baker 4 months ago
parent beb44f7634
commit dbff9758ef

@ -1,9 +1,5 @@
package com.todoroo.astrid.service package com.todoroo.astrid.service
import android.content.Context
import org.tasks.filters.CaldavFilter
import org.tasks.filters.GtasksFilter
import dagger.hilt.android.qualifiers.ApplicationContext
import org.tasks.BuildConfig import org.tasks.BuildConfig
import org.tasks.LocalBroadcastManager import org.tasks.LocalBroadcastManager
import org.tasks.caldav.VtodoCache import org.tasks.caldav.VtodoCache
@ -16,14 +12,15 @@ import org.tasks.data.entity.CaldavAccount
import org.tasks.data.entity.CaldavTask import org.tasks.data.entity.CaldavTask
import org.tasks.data.entity.Task import org.tasks.data.entity.Task
import org.tasks.data.getLocalList import org.tasks.data.getLocalList
import org.tasks.filters.CaldavFilter
import org.tasks.filters.Filter import org.tasks.filters.Filter
import org.tasks.filters.GtasksFilter
import org.tasks.preferences.Preferences import org.tasks.preferences.Preferences
import org.tasks.sync.SyncAdapters import org.tasks.sync.SyncAdapters
import org.tasks.time.DateTimeUtils2.currentTimeMillis import org.tasks.time.DateTimeUtils2.currentTimeMillis
import javax.inject.Inject import javax.inject.Inject
class TaskMover @Inject constructor( class TaskMover @Inject constructor(
@param:ApplicationContext private val context: Context,
private val taskDao: TaskDao, private val taskDao: TaskDao,
private val caldavDao: CaldavDao, private val caldavDao: CaldavDao,
private val googleTaskDao: GoogleTaskDao, private val googleTaskDao: GoogleTaskDao,
@ -81,7 +78,7 @@ class TaskMover @Inject constructor(
} }
suspend fun migrateLocalTasks() { suspend fun migrateLocalTasks() {
val list = caldavDao.getLocalList(context) val list = caldavDao.getLocalList()
move(taskDao.getLocalTasks(), CaldavFilter(list)) move(taskDao.getLocalTasks(), CaldavFilter(list))
} }

@ -217,7 +217,7 @@ class FilterProvider @Inject constructor(
forceExpand: Boolean, forceExpand: Boolean,
): List<FilterListItem> = ): List<FilterListItem> =
caldavDao.getAccounts() caldavDao.getAccounts()
.ifEmpty { listOf(caldavDao.setupLocalAccount(context)) } .ifEmpty { listOf(caldavDao.setupLocalAccount()) }
.filter { it.accountType != TYPE_LOCAL || preferences.getBoolean(R.string.p_lists_enabled, true) } .filter { it.accountType != TYPE_LOCAL || preferences.getBoolean(R.string.p_lists_enabled, true) }
.flatMap { .flatMap {
if (it.isGoogleTasks) { if (it.isGoogleTasks) {

@ -1,8 +1,6 @@
package org.tasks.preferences package org.tasks.preferences
import android.content.Context
import com.todoroo.astrid.api.CustomFilter import com.todoroo.astrid.api.CustomFilter
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.tasks.R import org.tasks.R
import org.tasks.Strings.isNullOrEmpty import org.tasks.Strings.isNullOrEmpty
@ -31,8 +29,6 @@ import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class DefaultFilterProvider @Inject constructor( class DefaultFilterProvider @Inject constructor(
// TODO: don't inject context, it breaks built-in filters when overriding language
@param:ApplicationContext private val context: Context,
private val preferences: Preferences, private val preferences: Preferences,
private val filterDao: FilterDao, private val filterDao: FilterDao,
private val tagDataDao: TagDataDao, private val tagDataDao: TagDataDao,
@ -41,7 +37,6 @@ class DefaultFilterProvider @Inject constructor(
private val locationDao: LocationDao, private val locationDao: LocationDao,
private val googleTaskDao: GoogleTaskDao private val googleTaskDao: GoogleTaskDao
) { ) {
var dashclockFilter: Filter var dashclockFilter: Filter
@Deprecated("use coroutines") get() = runBlocking { getFilterFromPreference(R.string.p_dashclock_filter) } @Deprecated("use coroutines") get() = runBlocking { getFilterFromPreference(R.string.p_dashclock_filter) }
set(filter) = setFilterPreference(filter, R.string.p_dashclock_filter) set(filter) = setFilterPreference(filter, R.string.p_dashclock_filter)
@ -92,7 +87,7 @@ class DefaultFilterProvider @Inject constructor(
private suspend fun getAnyList(): Filter { private suspend fun getAnyList(): Filter {
val filter = caldavDao.getGoogleTaskLists().getOrNull(0)?.let(::GtasksFilter) val filter = caldavDao.getGoogleTaskLists().getOrNull(0)?.let(::GtasksFilter)
?: caldavDao.getCalendars().filterNot { it.access == ACCESS_READ_ONLY }.getOrElse(0) { caldavDao.getLocalList(context) }.let(::CaldavFilter) ?: caldavDao.getCalendars().filterNot { it.access == ACCESS_READ_ONLY }.getOrElse(0) { caldavDao.getLocalList() }.let(::CaldavFilter)
defaultList = filter defaultList = filter
return filter return filter
} }

@ -11,4 +11,5 @@
<string name="filter_timer">Tasks Being Timed</string> <string name="filter_timer">Tasks Being Timed</string>
<string name="filter_today">Today</string> <string name="filter_today">Today</string>
<string name="filter_recently_modified">Recently modified</string> <string name="filter_recently_modified">Recently modified</string>
<string name="default_list">Default list</string>
</resources> </resources>

@ -1,23 +1,24 @@
package org.tasks.data package org.tasks.data
import android.content.Context
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import org.tasks.R import org.jetbrains.compose.resources.getString
import org.tasks.data.dao.CaldavDao import org.tasks.data.dao.CaldavDao
import org.tasks.data.entity.CaldavAccount import org.tasks.data.entity.CaldavAccount
import org.tasks.data.entity.CaldavCalendar import org.tasks.data.entity.CaldavCalendar
import tasks.kmp.generated.resources.Res
import tasks.kmp.generated.resources.default_list
private val mutex = Mutex() private val mutex = Mutex()
suspend fun CaldavDao.setupLocalAccount(context: Context): CaldavAccount = mutex.withLock { suspend fun CaldavDao.setupLocalAccount(): CaldavAccount = mutex.withLock {
val account = getLocalAccount() val account = getLocalAccount()
getLocalList(context, account) getLocalList(account)
return account return account
} }
suspend fun CaldavDao.getLocalList(context: Context) = mutex.withLock { suspend fun CaldavDao.getLocalList() = mutex.withLock {
getLocalList(context, getLocalAccount()) getLocalList(getLocalAccount())
} }
private suspend fun CaldavDao.getLocalAccount() = getAccountByUuid(CaldavDao.LOCAL) ?: CaldavAccount( private suspend fun CaldavDao.getLocalAccount() = getAccountByUuid(CaldavDao.LOCAL) ?: CaldavAccount(
@ -27,10 +28,10 @@ private suspend fun CaldavDao.getLocalAccount() = getAccountByUuid(CaldavDao.LOC
it.copy(id = insert(it)) it.copy(id = insert(it))
} }
private suspend fun CaldavDao.getLocalList(context: Context, account: CaldavAccount): CaldavCalendar = private suspend fun CaldavDao.getLocalList(account: CaldavAccount): CaldavCalendar =
getCalendarsByAccount(account.uuid!!).getOrNull(0) getCalendarsByAccount(account.uuid!!).getOrNull(0)
?: CaldavCalendar( ?: CaldavCalendar(
name = context.getString(R.string.default_list), name = getString(Res.string.default_list),
uuid = UUIDHelper.newUUID(), uuid = UUIDHelper.newUUID(),
account = account.uuid, account = account.uuid,
).apply { ).apply {
Loading…
Cancel
Save