mirror of https://github.com/tasks/tasks
Store icalendar data on disk
parent
433ae41200
commit
1154ba4be4
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
|
||||
package org.tasks.makers
|
||||
|
||||
import com.natpryce.makeiteasy.Instantiator
|
||||
import com.natpryce.makeiteasy.Property
|
||||
import com.natpryce.makeiteasy.Property.newProperty
|
||||
import com.natpryce.makeiteasy.PropertyValue
|
||||
import org.tasks.data.CaldavCalendar
|
||||
import org.tasks.makers.Maker.make
|
||||
|
||||
object CaldavCalendarMaker {
|
||||
val ACCOUNT: Property<CaldavCalendar, String> = newProperty()
|
||||
val UUID: Property<CaldavCalendar, String> = newProperty()
|
||||
|
||||
private val instantiator = Instantiator<CaldavCalendar> { lookup ->
|
||||
val calendar = CaldavCalendar()
|
||||
calendar.account = lookup.valueOf(ACCOUNT, "account")
|
||||
calendar.uuid = lookup.valueOf(UUID, "uuid")
|
||||
calendar
|
||||
}
|
||||
|
||||
fun newCaldavCalendar(vararg properties: PropertyValue<in CaldavCalendar?, *>): CaldavCalendar {
|
||||
return make(instantiator, *properties)
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import android.content.Context
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
class FileStorage @Inject constructor(
|
||||
@ApplicationContext context: Context
|
||||
) {
|
||||
val root = File(context.filesDir, "vtodo")
|
||||
|
||||
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
|
||||
fun getFile(vararg segments: String?): File? =
|
||||
if (segments.none { it.isNullOrBlank() }) {
|
||||
segments.fold(root) { f, p -> File(f, p) }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
fun read(file: File?): String? = file?.takeIf { it.exists() }?.readText()
|
||||
|
||||
fun write(file: File, data: String?) {
|
||||
if (data.isNullOrBlank()) {
|
||||
file.delete()
|
||||
} else {
|
||||
file.writeText(data)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import org.tasks.data.CaldavAccount
|
||||
import org.tasks.data.CaldavCalendar
|
||||
import org.tasks.data.CaldavDao
|
||||
import org.tasks.data.CaldavTask
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class VtodoCache @Inject constructor(
|
||||
private val caldavDao: CaldavDao,
|
||||
private val fileStorage: FileStorage,
|
||||
) {
|
||||
fun move(from: CaldavCalendar, to: CaldavCalendar, task: CaldavTask) {
|
||||
val source =
|
||||
fileStorage.getFile(from.account, from.uuid, task.`object`)
|
||||
if (source?.exists() != true) {
|
||||
return
|
||||
}
|
||||
val target =
|
||||
fileStorage.getFile(to.account, to.uuid)
|
||||
?.apply { mkdirs() }
|
||||
?.let { File(it, task.`object`!!) }
|
||||
?: return
|
||||
source.copyTo(target, overwrite = true)
|
||||
source.delete()
|
||||
}
|
||||
|
||||
suspend fun getVtodo(caldavTask: CaldavTask?): String? {
|
||||
if (caldavTask == null) {
|
||||
return null
|
||||
}
|
||||
val calendar = caldavDao.getCalendar(caldavTask.calendar!!) ?: return null
|
||||
return getVtodo(calendar, caldavTask)
|
||||
}
|
||||
|
||||
fun getVtodo(calendar: CaldavCalendar?, caldavTask: CaldavTask?): String? {
|
||||
val file = fileStorage.getFile(
|
||||
calendar?.account,
|
||||
caldavTask?.calendar,
|
||||
caldavTask?.`object`
|
||||
)
|
||||
return fileStorage.read(file)
|
||||
}
|
||||
|
||||
fun putVtodo(calendar: CaldavCalendar, caldavTask: CaldavTask, vtodo: String?) {
|
||||
val directory =
|
||||
fileStorage
|
||||
.getFile(calendar.account, caldavTask.calendar)
|
||||
?.apply { mkdirs() }
|
||||
?: return
|
||||
fileStorage.write(File(directory, caldavTask.`object`!!), vtodo)
|
||||
}
|
||||
|
||||
suspend fun delete(taskIds: List<Long>) {
|
||||
val tasks = caldavDao.getTasks(taskIds).groupBy { it.calendar!! }
|
||||
tasks.forEach { (c, t) ->
|
||||
val calendar = caldavDao.getCalendar(c) ?: return@forEach
|
||||
t.forEach { delete(calendar, it) }
|
||||
}
|
||||
}
|
||||
|
||||
fun delete(calendar: CaldavCalendar, caldavTask: CaldavTask) {
|
||||
fileStorage
|
||||
.getFile(calendar.account!!, caldavTask.calendar!!, caldavTask.`object`!!)
|
||||
?.delete()
|
||||
}
|
||||
|
||||
fun delete(calendar: CaldavCalendar) =
|
||||
fileStorage.getFile(calendar.account!!, calendar.uuid!!)?.deleteRecursively()
|
||||
|
||||
fun delete(account: CaldavAccount) =
|
||||
fileStorage.getFile(account.uuid!!)?.deleteRecursively()
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package org.tasks.extensions
|
||||
|
||||
import android.database.Cursor
|
||||
|
||||
fun Cursor.getString(columnName: String): String? =
|
||||
getColumnIndex(columnName).takeIf { it >= 0 }?.let { getString(it) }
|
Loading…
Reference in New Issue