|
|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
package org.tasks.caldav
|
|
|
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
import org.tasks.data.dao.CaldavDao
|
|
|
|
|
import org.tasks.data.entity.CaldavAccount
|
|
|
|
|
import org.tasks.data.entity.CaldavCalendar
|
|
|
|
|
@ -13,20 +15,21 @@ 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.obj)
|
|
|
|
|
if (source?.exists() != true) {
|
|
|
|
|
return
|
|
|
|
|
suspend fun move(from: CaldavCalendar, to: CaldavCalendar, task: CaldavTask) =
|
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
|
val source =
|
|
|
|
|
fileStorage.getFile(from.account, from.uuid, task.obj)
|
|
|
|
|
if (source?.exists() != true) {
|
|
|
|
|
return@withContext
|
|
|
|
|
}
|
|
|
|
|
val target =
|
|
|
|
|
fileStorage.getFile(to.account, to.uuid)
|
|
|
|
|
?.apply { mkdirs() }
|
|
|
|
|
?.let { File(it, task.obj!!) }
|
|
|
|
|
?: return@withContext
|
|
|
|
|
source.copyTo(target, overwrite = true)
|
|
|
|
|
source.delete()
|
|
|
|
|
}
|
|
|
|
|
val target =
|
|
|
|
|
fileStorage.getFile(to.account, to.uuid)
|
|
|
|
|
?.apply { mkdirs() }
|
|
|
|
|
?.let { File(it, task.obj!!) }
|
|
|
|
|
?: return
|
|
|
|
|
source.copyTo(target, overwrite = true)
|
|
|
|
|
source.delete()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun getVtodo(caldavTask: CaldavTask?): String? {
|
|
|
|
|
if (caldavTask == null) {
|
|
|
|
|
@ -36,7 +39,7 @@ class VtodoCache @Inject constructor(
|
|
|
|
|
return getVtodo(calendar, caldavTask)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getVtodo(calendar: CaldavCalendar?, caldavTask: CaldavTask?): String? {
|
|
|
|
|
suspend fun getVtodo(calendar: CaldavCalendar?, caldavTask: CaldavTask?): String? {
|
|
|
|
|
val file = fileStorage.getFile(
|
|
|
|
|
calendar?.account,
|
|
|
|
|
caldavTask?.calendar,
|
|
|
|
|
@ -45,36 +48,37 @@ class VtodoCache @Inject constructor(
|
|
|
|
|
return fileStorage.read(file)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun putVtodo(calendar: CaldavCalendar, caldavTask: CaldavTask, vtodo: String?) {
|
|
|
|
|
suspend fun putVtodo(calendar: CaldavCalendar, caldavTask: CaldavTask, vtodo: String?) {
|
|
|
|
|
val `object` = caldavTask.obj?.takeIf { it.isNotBlank() } ?: return
|
|
|
|
|
val directory =
|
|
|
|
|
fileStorage
|
|
|
|
|
.getFile(calendar.account, caldavTask.calendar)
|
|
|
|
|
?.apply { mkdirs() }
|
|
|
|
|
?: return
|
|
|
|
|
fileStorage.write(File(directory, `object`), vtodo)
|
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
|
val directory =
|
|
|
|
|
fileStorage
|
|
|
|
|
.getFile(calendar.account, caldavTask.calendar)
|
|
|
|
|
?.apply { mkdirs() }
|
|
|
|
|
?: return@withContext
|
|
|
|
|
fileStorage.write(File(directory, `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) }
|
|
|
|
|
}
|
|
|
|
|
suspend fun delete(calendar: CaldavCalendar, tasks: List<CaldavTask>) {
|
|
|
|
|
tasks.forEach { delete(calendar, it) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun delete(calendar: CaldavCalendar, caldavTask: CaldavTask) {
|
|
|
|
|
suspend fun delete(calendar: CaldavCalendar, caldavTask: CaldavTask) = withContext(Dispatchers.IO) {
|
|
|
|
|
fileStorage
|
|
|
|
|
.getFile(calendar.account, caldavTask.calendar, caldavTask.obj)
|
|
|
|
|
?.delete()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun delete(calendar: CaldavCalendar) =
|
|
|
|
|
suspend fun delete(calendar: CaldavCalendar) = withContext(Dispatchers.IO) {
|
|
|
|
|
fileStorage.getFile(calendar.account, calendar.uuid)?.deleteRecursively()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun delete(account: CaldavAccount) =
|
|
|
|
|
suspend fun delete(account: CaldavAccount) = withContext(Dispatchers.IO) {
|
|
|
|
|
fileStorage.getFile(account.uuid)?.deleteRecursively()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun clear() =
|
|
|
|
|
suspend fun clear() = withContext(Dispatchers.IO) {
|
|
|
|
|
fileStorage.getFile()?.deleteRecursively()
|
|
|
|
|
}
|
|
|
|
|
}
|