From f2ef7d79e237ab786e2a53353a756f20757eb9af Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 15 Jan 2023 19:47:56 +0530 Subject: [PATCH] Add import IDs to existing tasks on start --- .../calendar/pro/activities/EventActivity.kt | 2 +- .../calendar/pro/activities/MainActivity.kt | 4 ++++ .../calendar/pro/activities/TaskActivity.kt | 2 +- .../calendar/pro/extensions/Context.kt | 21 +++++++++++++++++++ .../calendar/pro/helpers/Constants.kt | 5 +++++ .../calendar/pro/interfaces/EventsDao.kt | 6 ++++++ 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 869cf0e34..06fd98faa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -1115,7 +1115,7 @@ class EventActivity : SimpleActivity() { val newImportId = if (mEvent.id != null) { mEvent.importId } else { - UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis().toString() + generateImportId() } val newEventType = if (!config.caldavSync || config.lastUsedCaldavCalendarId == 0 || mEventCalendarId == STORED_LOCALLY_ONLY) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 7277c01b7..20dd00e37 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -159,6 +159,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { ConfirmationDialog(this, "", R.string.upgraded_to_pro_calendar, R.string.ok, 0, false) {} config.wasUpgradedFromFreeShown = true } + + addImportIdsToTasks { + refreshViewPager() + } } override fun onResume() { 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 ac37dc165..ed5a88269 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 @@ -354,7 +354,7 @@ class TaskActivity : SimpleActivity() { val newImportId = if (mTask.id != null) { mTask.importId } else { - UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis().toString() + generateImportId() } val reminders = getReminders() 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 c0e15a072..9135c0211 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 @@ -708,3 +708,24 @@ inline fun Context.queryCursorInlined( } } } + +fun Context.addImportIdsToTasks(callback: () -> Unit) { + ensureBackgroundThread { + var count = 0 + + eventsDB.getAllTasks() + .forEach { task -> + if (task.importId.isEmpty()) { + eventsDB.updateTaskImportId( + importId = generateImportId(), + id = task.id!! + ) + count += 1 + } + } + + if (count > 0) { + callback() + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index a81f5b7ed..4bb1a5019 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.pro.helpers import com.simplemobiletools.calendar.pro.activities.EventActivity import com.simplemobiletools.calendar.pro.activities.TaskActivity import com.simplemobiletools.commons.helpers.MONTH_SECONDS +import java.util.* const val STORED_LOCALLY_ONLY = 0 const val ROW_COUNT = 6 @@ -258,3 +259,7 @@ fun getActivityToOpen(isTask: Boolean) = if (isTask) { } else { EventActivity::class.java } + +fun generateImportId(): String { + return UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis().toString() +} 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 f88ede950..63ec3c9be 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 @@ -12,6 +12,9 @@ interface EventsDao { @Query("SELECT * FROM events") fun getAllEvents(): List + @Query("SELECT * FROM events WHERE type = $TYPE_TASK") + fun getAllTasks(): List + @Query("SELECT * FROM events WHERE event_type IN (:eventTypeIds) AND type = $TYPE_EVENT") fun getAllEventsWithTypes(eventTypeIds: List): List @@ -122,6 +125,9 @@ interface EventsDao { @Query("UPDATE events SET flags = :newFlags WHERE id = :id") fun updateTaskCompletion(id: Long, newFlags: Int) + @Query("UPDATE events SET import_id = :importId WHERE id = :id AND type = $TYPE_TASK") + fun updateTaskImportId(importId: String, id: Long) + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertOrUpdate(event: Event): Long