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
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.LocalBroadcastManager
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.Task
import org.tasks.data.getLocalList
import org.tasks.filters.CaldavFilter
import org.tasks.filters.Filter
import org.tasks.filters.GtasksFilter
import org.tasks.preferences.Preferences
import org.tasks.sync.SyncAdapters
import org.tasks.time.DateTimeUtils2.currentTimeMillis
import javax.inject.Inject
class TaskMover @Inject constructor(
@param:ApplicationContext private val context: Context,
private val taskDao: TaskDao,
private val caldavDao: CaldavDao,
private val googleTaskDao: GoogleTaskDao,
@ -81,7 +78,7 @@ class TaskMover @Inject constructor(
}
suspend fun migrateLocalTasks() {
val list = caldavDao.getLocalList(context)
val list = caldavDao.getLocalList()
move(taskDao.getLocalTasks(), CaldavFilter(list))
}

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

@ -1,8 +1,6 @@
package org.tasks.preferences
import android.content.Context
import com.todoroo.astrid.api.CustomFilter
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking
import org.tasks.R
import org.tasks.Strings.isNullOrEmpty
@ -31,8 +29,6 @@ import timber.log.Timber
import javax.inject.Inject
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 filterDao: FilterDao,
private val tagDataDao: TagDataDao,
@ -41,7 +37,6 @@ class DefaultFilterProvider @Inject constructor(
private val locationDao: LocationDao,
private val googleTaskDao: GoogleTaskDao
) {
var dashclockFilter: Filter
@Deprecated("use coroutines") get() = runBlocking { getFilterFromPreference(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 {
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
return filter
}

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

@ -1,23 +1,24 @@
package org.tasks.data
import android.content.Context
import kotlinx.coroutines.sync.Mutex
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.entity.CaldavAccount
import org.tasks.data.entity.CaldavCalendar
import tasks.kmp.generated.resources.Res
import tasks.kmp.generated.resources.default_list
private val mutex = Mutex()
suspend fun CaldavDao.setupLocalAccount(context: Context): CaldavAccount = mutex.withLock {
suspend fun CaldavDao.setupLocalAccount(): CaldavAccount = mutex.withLock {
val account = getLocalAccount()
getLocalList(context, account)
getLocalList(account)
return account
}
suspend fun CaldavDao.getLocalList(context: Context) = mutex.withLock {
getLocalList(context, getLocalAccount())
suspend fun CaldavDao.getLocalList() = mutex.withLock {
getLocalList(getLocalAccount())
}
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))
}
private suspend fun CaldavDao.getLocalList(context: Context, account: CaldavAccount): CaldavCalendar =
private suspend fun CaldavDao.getLocalList(account: CaldavAccount): CaldavCalendar =
getCalendarsByAccount(account.uuid!!).getOrNull(0)
?: CaldavCalendar(
name = context.getString(R.string.default_list),
name = getString(Res.string.default_list),
uuid = UUIDHelper.newUUID(),
account = account.uuid,
).apply {
Loading…
Cancel
Save