Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 6cfbbc2

Browse files
committed
Refactor task list management into DmfsTaskListStore
1 parent 240f756 commit 6cfbbc2

File tree

2 files changed

+84
-69
lines changed

2 files changed

+84
-69
lines changed

lib/src/main/kotlin/at/bitfire/ical4android/DmfsTaskList.kt

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import java.util.LinkedList
2323
import java.util.logging.Level
2424
import java.util.logging.Logger
2525

26-
2726
/**
2827
* Represents a locally stored task list, containing [DmfsTask]s (tasks).
2928
* Communicates with tasks.org-compatible content providers (currently tasks.org and OpenTasks) to store the tasks.
@@ -36,80 +35,15 @@ abstract class DmfsTaskList<out T : DmfsTask>(
3635
val id: Long
3736
) {
3837

39-
companion object {
40-
41-
private val logger
42-
get() = Logger.getLogger(DmfsTaskList::class.java.name)
43-
44-
fun create(account: Account, provider: ContentProviderClient, providerName: TaskProvider.ProviderName, info: ContentValues): Uri {
45-
info.put(TaskContract.ACCOUNT_NAME, account.name)
46-
info.put(TaskContract.ACCOUNT_TYPE, account.type)
47-
48-
val url = TaskLists.getContentUri(providerName.authority).asSyncAdapter(account)
49-
logger.log(Level.FINE, "Creating ${providerName.authority} task list", info)
50-
return provider.insert(url, info)
51-
?: throw CalendarStorageException("Couldn't create task list (empty result from provider)")
52-
}
53-
54-
fun <T : DmfsTaskList<DmfsTask>> findByID(
55-
account: Account,
56-
provider: ContentProviderClient,
57-
providerName: TaskProvider.ProviderName,
58-
factory: DmfsTaskListFactory<T>,
59-
id: Long
60-
): T {
61-
provider.query(
62-
ContentUris.withAppendedId(TaskLists.getContentUri(providerName.authority), id).asSyncAdapter(account),
63-
null,
64-
null,
65-
null,
66-
null
67-
)?.use { cursor ->
68-
if (cursor.moveToNext()) {
69-
val taskList = factory.newInstance(account, provider, providerName, id)
70-
taskList.populate(cursor.toValues())
71-
return taskList
72-
}
73-
}
74-
throw FileNotFoundException()
75-
}
76-
77-
fun <T : DmfsTaskList<DmfsTask>> find(
78-
account: Account,
79-
factory: DmfsTaskListFactory<T>,
80-
provider: ContentProviderClient,
81-
providerName: TaskProvider.ProviderName,
82-
where: String?,
83-
whereArgs: Array<String>?
84-
): List<T> {
85-
val taskLists = LinkedList<T>()
86-
provider.query(
87-
TaskLists.getContentUri(providerName.authority).asSyncAdapter(account),
88-
null,
89-
where,
90-
whereArgs,
91-
null
92-
)?.use { cursor ->
93-
while (cursor.moveToNext()) {
94-
val values = cursor.toValues()
95-
val taskList =
96-
factory.newInstance(account, provider, providerName, values.getAsLong(TaskLists._ID))
97-
taskList.populate(values)
98-
taskLists += taskList
99-
}
100-
}
101-
return taskLists
102-
}
103-
104-
}
38+
private val logger
39+
get() = Logger.getLogger(DmfsTaskList::class.java.name)
10540

10641
var syncId: String? = null
10742
var name: String? = null
10843
var color: Int? = null
10944
var isSynced = false
11045
var isVisible = false
11146

112-
11347
/**
11448
* Sets the task list properties ([syncId], [name] etc.) from the passed argument,
11549
* which is usually directly taken from the tasks provider.
@@ -120,7 +54,7 @@ abstract class DmfsTaskList<out T : DmfsTask>(
12054
* @param values values from tasks provider
12155
*/
12256
@CallSuper
123-
protected open fun populate(values: ContentValues) {
57+
internal open fun populate(values: ContentValues) {
12458
syncId = values.getAsString(TaskLists._SYNC_ID)
12559
name = values.getAsString(TaskLists.LIST_NAME)
12660
color = values.getAsInteger(TaskLists.LIST_COLOR)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of ical4android which is released under GPLv3.
3+
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package at.bitfire.ical4android
8+
9+
import android.accounts.Account
10+
import android.content.ContentProviderClient
11+
import android.content.ContentUris
12+
import android.content.ContentValues
13+
import android.net.Uri
14+
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
15+
import at.bitfire.ical4android.util.MiscUtils.toValues
16+
import org.dmfs.tasks.contract.TaskContract
17+
import org.dmfs.tasks.contract.TaskContract.TaskLists
18+
import java.io.FileNotFoundException
19+
import java.util.LinkedList
20+
import java.util.logging.Level
21+
import java.util.logging.Logger
22+
23+
class DmfsTaskListStore<TaskList: DmfsTaskList<Task>, Task: DmfsTask>(
24+
private val account: Account,
25+
private val providerName: TaskProvider.ProviderName,
26+
private val provider: ContentProviderClient,
27+
private val taskListFactory: DmfsTaskListFactory<TaskList>
28+
) {
29+
30+
private val logger
31+
get() = Logger.getLogger(javaClass.name)
32+
33+
fun create(info: ContentValues): Uri {
34+
info.put(TaskContract.ACCOUNT_NAME, account.name)
35+
info.put(TaskContract.ACCOUNT_TYPE, account.type)
36+
37+
logger.log(Level.FINE, "Creating ${providerName.authority} task list", info)
38+
val uri = TaskLists.getContentUri(providerName.authority).asSyncAdapter(account)
39+
return provider.insert(uri, info)
40+
?: throw CalendarStorageException("Couldn't create task list (empty result from provider)")
41+
}
42+
43+
fun getById(id: Long): TaskList {
44+
provider.query(
45+
/* url = */ ContentUris.withAppendedId(TaskLists.getContentUri(providerName.authority), id).asSyncAdapter(account),
46+
/* projection = */ null,
47+
/* selection = */ null,
48+
/* selectionArgs = */ null,
49+
/* sortOrder = */ null
50+
)?.use { cursor ->
51+
if (cursor.moveToNext()) {
52+
val taskList = taskListFactory.newInstance(account, provider, providerName, id)
53+
taskList.populate(cursor.toValues())
54+
return taskList
55+
}
56+
}
57+
58+
// no task list found
59+
throw FileNotFoundException()
60+
}
61+
62+
fun find(where: String?, whereArgs: Array<String>?): List<TaskList> {
63+
val taskLists = LinkedList<TaskList>()
64+
provider.query(
65+
/* url = */ TaskLists.getContentUri(providerName.authority).asSyncAdapter(account),
66+
/* projection = */ null,
67+
/* selection = */ where,
68+
/* selectionArgs = */ whereArgs,
69+
/* sortOrder = */ null
70+
)?.use { cursor ->
71+
while (cursor.moveToNext()) {
72+
val values = cursor.toValues()
73+
val taskList = taskListFactory.newInstance(account, provider, providerName, values.getAsLong(TaskLists._ID))
74+
taskList.populate(values)
75+
taskLists += taskList
76+
}
77+
}
78+
return taskLists
79+
}
80+
81+
}

0 commit comments

Comments
 (0)