Properly migrate completed tasks from older versions

This commit is contained in:
Naveen 2022-07-21 18:46:05 +05:30
parent 607c02dfa9
commit 32196669f4
5 changed files with 23 additions and 5 deletions

View File

@ -376,6 +376,14 @@ class TaskActivity : SimpleActivity() {
endTS = startTS
title = newTitle
description = task_description.value
// migrate completed task to the new completed tasks db
if (!wasRepeatable && mTask.isTaskCompleted()) {
mTask.flags = mTask.flags.removeBit(FLAG_TASK_COMPLETED)
ensureBackgroundThread {
updateTaskCompletion(copy(startTS = mOriginalStartTS), true)
}
}
flags = mTask.flags.addBitIf(task_all_day.isChecked, FLAG_ALL_DAY)
lastUpdated = System.currentTimeMillis()
eventType = mEventTypeId

View File

@ -123,7 +123,7 @@ abstract class EventsDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("CREATE TABLE IF NOT EXISTS `tasks` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `task_id` INTEGER NOT NULL, `start_ts` INTEGER NOT NULL, `flags` INTEGER NOT NULL)")
execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_tasks_id` ON `tasks` (`id`)")
execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_tasks_id_task_id` ON `tasks` (`id`, `task_id`)")
}
}
}

View File

@ -660,8 +660,9 @@ fun Context.getDatesWeekDateTime(date: DateTime): String {
fun Context.isTaskCompleted(event: Event): Boolean {
if (event.id == null) return false
val task = completedTasksDB.getTaskWithIdAndTs(event.id!!, event.startTS) ?: return false
return task.flags and FLAG_TASK_COMPLETED != 0
val originalEvent = eventsDB.getTaskWithId(event.id!!)
val task = completedTasksDB.getTaskWithIdAndTs(event.id!!, event.startTS)
return originalEvent?.isTaskCompleted() == true || task?.isTaskCompleted() == true
}
fun Context.updateTaskCompletion(event: Event, completed: Boolean) {
@ -673,4 +674,6 @@ fun Context.updateTaskCompletion(event: Event, completed: Boolean) {
event.flags = event.flags.removeBit(FLAG_TASK_COMPLETED)
completedTasksDB.deleteTaskWithIdAndTs(event.id!!, event.startTS)
}
// mark event as "incomplete" in the main events db
eventsDB.updateTaskCompletion(event.id!!, event.flags.removeBit(FLAG_TASK_COMPLETED))
}

View File

@ -112,6 +112,10 @@ interface EventsDao {
@Query("UPDATE events SET repetition_exceptions = :repetitionExceptions WHERE id = :id AND (type = $TYPE_EVENT OR type = $TYPE_TASK)")
fun updateEventRepetitionExceptions(repetitionExceptions: String, id: Long)
@Deprecated("Use Context.updateTaskCompletion() instead unless you know what you are doing.")
@Query("UPDATE events SET flags = :newFlags WHERE id = :id")
fun updateTaskCompletion(id: Long, newFlags: Int)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(event: Event): Long

View File

@ -4,11 +4,14 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import com.simplemobiletools.calendar.pro.helpers.FLAG_TASK_COMPLETED
@Entity(tableName = "tasks", indices = [(Index(value = ["id"], unique = true))])
@Entity(tableName = "tasks", indices = [(Index(value = ["id", "task_id"], unique = true))])
data class Task(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "task_id") var task_id: Long,
@ColumnInfo(name = "start_ts") var startTS: Long = 0L,
@ColumnInfo(name = "flags") var flags: Int = 0,
)
) {
fun isTaskCompleted() = flags and FLAG_TASK_COMPLETED != 0
}