Skip to content

Commit

Permalink
Implement LocalTaskListStore
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Nov 9, 2024
1 parent 29312b8 commit fbdf465
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 57 deletions.
36 changes: 0 additions & 36 deletions app/src/main/kotlin/at/bitfire/davdroid/resource/LocalTaskList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import android.annotation.SuppressLint
import android.content.ContentProviderClient
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.SyncState
import at.bitfire.davdroid.util.DavUtils.lastSegment
import at.bitfire.ical4android.DmfsTaskList
import at.bitfire.ical4android.DmfsTaskListFactory
import at.bitfire.ical4android.TaskProvider
Expand All @@ -35,18 +31,6 @@ class LocalTaskList private constructor(

companion object {

fun create(account: Account, provider: ContentProviderClient, providerName: TaskProvider.ProviderName, info: Collection): Uri {
// If the collection doesn't have a color, use a default color.
if (info.color != null)
info.color = Constants.DAVDROID_GREEN_RGBA

val values = valuesFromCollectionInfo(info, withColor = true)
values.put(TaskLists.OWNER, account.name)
values.put(TaskLists.SYNC_ENABLED, 1)
values.put(TaskLists.VISIBLE, 1)
return create(account, provider, providerName, values)
}

@SuppressLint("Recycle")
@Throws(Exception::class)
fun onRenameAccount(context: Context, oldName: String, newName: String) {
Expand All @@ -61,23 +45,6 @@ class LocalTaskList private constructor(
}
}

private fun valuesFromCollectionInfo(info: Collection, withColor: Boolean): ContentValues {
val values = ContentValues(3)
values.put(TaskLists._SYNC_ID, info.url.toString())
values.put(TaskLists.LIST_NAME,
if (info.displayName.isNullOrBlank()) info.url.lastSegment else info.displayName)

if (withColor && info.color != null)
values.put(TaskLists.LIST_COLOR, info.color)

if (info.privWriteContent && !info.forceReadOnly)
values.put(TaskListColumns.ACCESS_LEVEL, TaskListColumns.ACCESS_LEVEL_OWNER)
else
values.put(TaskListColumns.ACCESS_LEVEL, TaskListColumns.ACCESS_LEVEL_READ)

return values
}

}

private val logger = Logger.getGlobal()
Expand Down Expand Up @@ -124,9 +91,6 @@ class LocalTaskList private constructor(
accessLevel = values.getAsInteger(TaskListColumns.ACCESS_LEVEL)
}

fun update(info: Collection, updateColor: Boolean) =
update(valuesFromCollectionInfo(info, updateColor))


override fun findDeleted() = queryTasks(Tasks._DELETED, null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,97 @@ package at.bitfire.davdroid.resource

import android.accounts.Account
import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.util.DavUtils.lastSegment
import at.bitfire.ical4android.DmfsTaskList
import at.bitfire.ical4android.TaskProvider
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.qualifiers.ApplicationContext
import org.dmfs.tasks.contract.TaskContract.TaskListColumns
import org.dmfs.tasks.contract.TaskContract.TaskLists
import java.util.logging.Level
import java.util.logging.Logger

class LocalTaskListStore @AssistedInject constructor(
@Assisted authority: String,
val accountSettingsFactory: AccountSettings.Factory,
@ApplicationContext val context: Context,
val db: AppDatabase,
val logger: Logger
): LocalDataStore<LocalTaskList> {

@AssistedFactory
interface Factory {
fun create(authority: String): LocalTaskListStore
}

private val serviceDao = db.serviceDao()

private val providerName = TaskProvider.ProviderName.fromAuthority(authority)

class LocalTaskListStore: LocalDataStore<LocalTaskList> {

override fun create(provider: ContentProviderClient, fromCollection: Collection): LocalTaskList? {
TODO("Not yet implemented")
val service = serviceDao.get(fromCollection.serviceId) ?: throw IllegalArgumentException("Couldn't fetch DB service from collection")
val account = Account(service.accountName, context.getString(R.string.account_type))

logger.log(Level.INFO, "Adding local task list", fromCollection)
val uri = create(account, provider, providerName, fromCollection)
return DmfsTaskList.findByID(account, provider, providerName, LocalTaskList.Factory, ContentUris.parseId(uri))
}

private fun create(account: Account, provider: ContentProviderClient, providerName: TaskProvider.ProviderName, info: Collection): Uri {
// If the collection doesn't have a color, use a default color.
if (info.color != null)
info.color = Constants.DAVDROID_GREEN_RGBA

val values = valuesFromCollectionInfo(info, withColor = true)
values.put(TaskLists.OWNER, account.name)
values.put(TaskLists.SYNC_ENABLED, 1)
values.put(TaskLists.VISIBLE, 1)
return DmfsTaskList.Companion.create(account, provider, providerName, values)
}

override fun getAll(account: Account, provider: ContentProviderClient): List<LocalTaskList> {
TODO("Not yet implemented")
private fun valuesFromCollectionInfo(info: Collection, withColor: Boolean): ContentValues {
val values = ContentValues(3)
values.put(TaskLists._SYNC_ID, info.url.toString())
values.put(TaskLists.LIST_NAME,
if (info.displayName.isNullOrBlank()) info.url.lastSegment else info.displayName)

if (withColor && info.color != null)
values.put(TaskLists.LIST_COLOR, info.color)

if (info.privWriteContent && !info.forceReadOnly)
values.put(TaskListColumns.ACCESS_LEVEL, TaskListColumns.ACCESS_LEVEL_OWNER)
else
values.put(TaskListColumns.ACCESS_LEVEL, TaskListColumns.ACCESS_LEVEL_READ)

return values
}


override fun getAll(account: Account, provider: ContentProviderClient) =
DmfsTaskList.find(account, LocalTaskList.Factory, provider, providerName, null, null)


override fun update(provider: ContentProviderClient, localCollection: LocalTaskList, fromCollection: Collection) {
TODO("Not yet implemented")
logger.log(Level.FINE, "Updating local task list ${fromCollection.url}", fromCollection)
val accountSettings = accountSettingsFactory.create(localCollection.account)
localCollection.update(valuesFromCollectionInfo(fromCollection, withColor = accountSettings.getManageCalendarColors()))
}


override fun delete(localCollection: LocalTaskList) {
TODO("Not yet implemented")
localCollection.delete()
}

}
17 changes: 2 additions & 15 deletions app/src/main/kotlin/at/bitfire/davdroid/sync/TaskSyncer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TaskSyncer @AssistedInject constructor(
@Assisted override val authority: String,
@Assisted extras: Array<String>,
@Assisted syncResult: SyncResult,
private val localTaskListStoreFactory: LocalTaskListStore.Factory,
private val tasksAppManager: dagger.Lazy<TasksAppManager>,
private val tasksSyncManagerFactory: TasksSyncManager.Factory,
): Syncer<LocalTaskListStore, LocalTaskList>(account, extras, syncResult) {
Expand All @@ -36,14 +37,11 @@ class TaskSyncer @AssistedInject constructor(

private val providerName = TaskProvider.ProviderName.fromAuthority(authority)

override val dataStore: LocalTaskListStore
get() = TODO("Not yet implemented")
override val dataStore = localTaskListStoreFactory.create(authority)

override val serviceType: String
get() = Service.TYPE_CALDAV

/*override fun getLocalCollections(provider: ContentProviderClient): List<LocalTaskList>
= DmfsTaskList.find(account, LocalTaskList.Factory, provider, providerName, "${TaskLists.SYNC_ENABLED}!=0", null)*/

override fun prepare(provider: ContentProviderClient): Boolean {
// Don't sync if task provider is too old
Expand All @@ -70,17 +68,6 @@ class TaskSyncer @AssistedInject constructor(
override fun getDbSyncCollections(serviceId: Long): List<Collection> =
collectionRepository.getSyncTaskLists(serviceId)

/*override fun update(localCollection: LocalTaskList, remoteCollection: Collection) {
logger.log(Level.FINE, "Updating local task list ${remoteCollection.url}", remoteCollection)
localCollection.update(remoteCollection, accountSettings.getManageCalendarColors())
}*/

/*override fun create(provider: ContentProviderClient, remoteCollection: Collection): LocalTaskList {
logger.log(Level.INFO, "Adding local task list", remoteCollection)
val uri = LocalTaskList.create(account, provider, providerName, remoteCollection)
return DmfsTaskList.findByID(account, provider, providerName, LocalTaskList.Factory, ContentUris.parseId(uri))
}*/

override fun syncCollection(provider: ContentProviderClient, localCollection: LocalTaskList, remoteCollection: Collection) {
logger.info("Synchronizing task list #${localCollection.id} [${localCollection.syncId}]")

Expand Down

0 comments on commit fbdf465

Please sign in to comment.