mirror of https://github.com/tasks/tasks
Use transaction annotation
parent
8ef23c1593
commit
8a27f5df4b
@ -1,13 +0,0 @@
|
|||||||
package org.tasks.data
|
|
||||||
|
|
||||||
import androidx.room.RoomDatabase
|
|
||||||
import androidx.room.TransactionScope
|
|
||||||
import androidx.room.Transactor
|
|
||||||
import androidx.room.useWriterConnection
|
|
||||||
|
|
||||||
suspend fun <T> RoomDatabase.withTransaction(block: suspend TransactionScope<T>.() -> T): T =
|
|
||||||
useWriterConnection { transactor ->
|
|
||||||
transactor.withTransaction(Transactor.SQLiteTransactionType.IMMEDIATE) {
|
|
||||||
block()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package org.tasks.data.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Transaction
|
||||||
|
import co.touchlab.kermit.Logger
|
||||||
|
import org.tasks.data.db.Database
|
||||||
|
import org.tasks.data.entity.Task
|
||||||
|
import org.tasks.time.DateTimeUtils2.currentTimeMillis
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
abstract class CompletionDao(private val db: Database) {
|
||||||
|
@Transaction
|
||||||
|
open suspend fun complete(
|
||||||
|
tasks: List<Task>,
|
||||||
|
completionDate: Long,
|
||||||
|
afterSave: suspend (List<Task>) -> Unit,
|
||||||
|
) {
|
||||||
|
Logger.d("CompletionDao") { "complete tasks=$tasks completionDate=$completionDate" }
|
||||||
|
val modified = currentTimeMillis()
|
||||||
|
val updated = tasks
|
||||||
|
.map {
|
||||||
|
it.copy(
|
||||||
|
completionDate = completionDate,
|
||||||
|
modificationDate = modified,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
db.alarmDao().deleteSnoozed(tasks.map { it.id })
|
||||||
|
db.taskDao().updateInternal(updated)
|
||||||
|
afterSave(updated)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue