From d89c723af76503d5498647fabe7af332646b0255 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 17 Jul 2017 22:22:24 +0200 Subject: [PATCH] check last updated timestamp at reimporting google events --- .../calendar/activities/EventActivity.kt | 2 ++ .../calendar/asynctasks/FetchGoogleEventsTask.kt | 8 ++++++-- .../simplemobiletools/calendar/helpers/DBHelper.kt | 13 ++++++++++++- .../calendar/models/GoogleEvent.kt | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt index 01a2407bf..f8dd5d889 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -456,6 +456,8 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { eventType = mEventTypeId offset = getCurrentOffset() isDstIncluded = TimeZone.getDefault().inDaylightTime(Date()) + lastUpdated = System.currentTimeMillis() + source = "Simple Calendar" } if (mEvent.id == 0) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt index d6087deca..1fc189a49 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt @@ -86,9 +86,13 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre if (googleEvent.status != CONFIRMED) continue + val lastUpdate = DateTime(googleEvent.updated).millis val importId = googleEvent.iCalUID if (importIDs.contains(importId)) { - continue + val oldEvent = dbHelper.getEventWithImportId(importId) + if (oldEvent != null && oldEvent.lastUpdated >= lastUpdate) { + continue + } } val reminders = getReminder(googleEvent.reminders) @@ -127,7 +131,7 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre val event = Event(0, startTS, endTS, googleEvent.summary, googleEvent.description, reminders.getOrElse(0, { -1 }), reminders.getOrElse(1, { -1 }), reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval, importId, flags, repeatRule.repeatLimit, repeatRule.repeatRule, - eventTypeId) + eventTypeId, lastUpdated = lastUpdate) if (event.isAllDay && endTS > startTS) { event.endTS -= DAY diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 150337cab..a6e02803b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -226,7 +226,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont put(COL_PARENT_EVENT_ID, event.parentId) put(COL_OFFSET, event.offset) put(COL_IS_DST_INCLUDED, if (event.isDstIncluded) 1 else 0) - put(COL_LAST_UPDATED, System.currentTimeMillis()) + put(COL_LAST_UPDATED, event.lastUpdated) put(COL_SOURCE, event.source) } } @@ -432,6 +432,17 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return ids.filter { it.trim().isNotEmpty() } as ArrayList } + fun getEventWithImportId(importId: String): Event? { + val selection = "$MAIN_TABLE_NAME.$COL_IMPORT_ID = ?" + val selectionArgs = arrayOf(importId.toString()) + val cursor = getEventsCursor(selection, selectionArgs) + val events = fillEvents(cursor) + return if (!events.isEmpty()) + events[0] + else + null + } + fun getEventWithId(id: Int): Event? { val selection = "$MAIN_TABLE_NAME.$COL_ID = ?" val selectionArgs = arrayOf(id.toString()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleEvent.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleEvent.kt index e0a78bc98..2d6259433 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleEvent.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/GoogleEvent.kt @@ -4,4 +4,4 @@ import com.google.gson.JsonArray import com.google.gson.JsonObject data class GoogleEvent(val summary: String, val description: String, val status: String, val start: GoogleEventDateTime, val end: GoogleEventDateTime, - val reminders: JsonObject, val recurrence: JsonArray?, val iCalUID: String, val colorId: Int) + val reminders: JsonObject, val recurrence: JsonArray?, val iCalUID: String, val colorId: Int, val updated: String)