diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt index 4765563bf..81ccfa0cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/TaskActivity.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt index 6199a71f7..65c9e1307 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt @@ -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`)") } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 5679bba2b..42722770b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -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)) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt index a22ad4e0c..791ec4667 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Task.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Task.kt index ff2cb1630..3e8ab5eaa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Task.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Task.kt @@ -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 +}