From 7e048eff376d9ddaaec7fcd67bfd40c618e3f536 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 25 Jul 2017 23:07:51 +0200 Subject: [PATCH] properly update google events at Updating --- .../calendar/activities/EventActivity.kt | 2 + .../calendar/helpers/GoogleSyncHandler.kt | 70 +++++++++++-------- .../calendar/helpers/IcsExporter.kt | 2 +- .../calendar/models/Event.kt | 2 +- 4 files changed, 45 insertions(+), 31 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 ab51a1f76..beb44936f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -437,6 +437,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { } val wasRepeatable = mEvent.repeatInterval > 0 + val newImportId = if (mEvent.id != 0) mEvent.importId else UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis().toString() val reminders = sortedSetOf(mReminder1Minutes, mReminder2Minutes, mReminder3Minutes).filter { it != REMINDER_OFF } val newDescription = event_description.value @@ -449,6 +450,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { reminder2Minutes = reminders.elementAtOrElse(1) { REMINDER_OFF } reminder3Minutes = reminders.elementAtOrElse(2) { REMINDER_OFF } repeatInterval = mRepeatInterval + importId = newImportId flags = if (event_all_day.isChecked) (mEvent.flags or FLAG_ALL_DAY) else (mEvent.flags.removeFlag(FLAG_ALL_DAY)) repeatLimit = if (repeatInterval == 0) 0 else mRepeatLimit repeatRule = mRepeatRule diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt index 3d97f603c..bc56a02dc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/GoogleSyncHandler.kt @@ -27,36 +27,14 @@ class GoogleSyncHandler { private fun createRemoteGoogleEvent(activity: SimpleActivity, event: Event) { try { - com.google.api.services.calendar.model.Event().apply { - summary = event.title - description = event.description - - if (event.getIsAllDay()) { - start = EventDateTime().setDate(com.google.api.client.util.DateTime(true, event.startTS * 1000L, null)) - end = EventDateTime().setDate(com.google.api.client.util.DateTime(true, (event.endTS + DAY) * 1000L, null)) - } else { - start = EventDateTime().setDateTime(com.google.api.client.util.DateTime(event.startTS * 1000L)).setTimeZone(TimeZone.getDefault().id) - end = EventDateTime().setDateTime(com.google.api.client.util.DateTime(event.endTS * 1000L)).setTimeZone(TimeZone.getDefault().id) - } - - status = CONFIRMED.toLowerCase() - Parser().getShortRepeatInterval(event).let { - if (it.isNotEmpty()) { - recurrence = listOf(it) - } - } - - if (event.getReminders().isNotEmpty()) { - reminders = getEventReminders(event).setUseDefault(false) - } - - try { - activity.getGoogleSyncService().events().insert(PRIMARY, this).execute() - } catch (e: GoogleJsonResponseException) { - val msg = String.format(activity.getString(R.string.google_sync_error_insert), e.getGoogleMessageError()) - activity.toast(msg, Toast.LENGTH_LONG) - } + val googleEvent = mergeMyEventToGoogleEvent(com.google.api.services.calendar.model.Event(), event) + try { + activity.getGoogleSyncService().events().insert(PRIMARY, googleEvent).execute() + } catch (e: GoogleJsonResponseException) { + val msg = String.format(activity.getString(R.string.google_sync_error_insert), e.getGoogleMessageError()) + activity.toast(msg, Toast.LENGTH_LONG) } + } catch (ignored: Exception) { } @@ -71,12 +49,46 @@ class GoogleSyncHandler { return com.google.api.services.calendar.model.Event.Reminders().setOverrides(reminders) } + private fun mergeMyEventToGoogleEvent(googleEvent: com.google.api.services.calendar.model.Event, event: Event): com.google.api.services.calendar.model.Event { + googleEvent.apply { + summary = event.title + description = event.description + + if (event.getIsAllDay()) { + start = EventDateTime().setDate(com.google.api.client.util.DateTime(true, event.startTS * 1000L, null)) + end = EventDateTime().setDate(com.google.api.client.util.DateTime(true, (event.endTS + DAY) * 1000L, null)) + } else { + start = EventDateTime().setDateTime(com.google.api.client.util.DateTime(event.startTS * 1000L)).setTimeZone(TimeZone.getDefault().id) + end = EventDateTime().setDateTime(com.google.api.client.util.DateTime(event.endTS * 1000L)).setTimeZone(TimeZone.getDefault().id) + } + + id = event.importId + status = CONFIRMED.toLowerCase() + Parser().getShortRepeatInterval(event).let { + if (it.isNotEmpty()) { + recurrence = listOf(it) + } else { + recurrence = null + } + } + + if (event.getReminders().isNotEmpty()) { + reminders = getEventReminders(event).setUseDefault(false) + } else { + reminders = com.google.api.services.calendar.model.Event.Reminders().setUseDefault(false) + } + } + return googleEvent + } + fun updateGoogleEvent(activity: SimpleActivity, event: Event) { if (activity.isGoogleSyncActive()) { if (activity.isOnline()) { Thread({ try { val googleEvent = activity.getGoogleSyncService().events().get(PRIMARY, event.importId).execute() + val newGoogleEvent = mergeMyEventToGoogleEvent(googleEvent, event) + activity.getGoogleSyncService().events().update(PRIMARY, newGoogleEvent.id, newGoogleEvent).execute() } catch (e: GoogleJsonResponseException) { val msg = String.format(activity.getString(R.string.google_sync_error_update), e.getGoogleMessageError()) activity.toast(msg, Toast.LENGTH_LONG) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt index eddecbb5b..ada4ce253 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt @@ -25,7 +25,7 @@ class IcsExporter { out.writeLn(BEGIN_EVENT) event.title.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") } event.description.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$DESCRIPTION$it") } - event.importId?.let { if (it.isNotEmpty()) out.writeLn("$UID$it") } + event.importId.let { if (it.isNotEmpty()) out.writeLn("$UID$it") } event.eventType.let { out.writeLn("$CATEGORIES${activity.dbHelper.getEventType(it)?.title}") } event.lastUpdated.let { out.writeLn("$LAST_MODIFIED:${Formatter.getExportedTime(it)}") } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt index 712d822d8..d17fbf57e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt @@ -9,7 +9,7 @@ import java.util.* data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var title: String = "", var description: String = "", var reminder1Minutes: Int = -1, var reminder2Minutes: Int = -1, var reminder3Minutes: Int = -1, var repeatInterval: Int = 0, - var importId: String? = "", var flags: Int = 0, var repeatLimit: Int = 0, var repeatRule: Int = 0, + var importId: String = "", var flags: Int = 0, var repeatLimit: Int = 0, var repeatRule: Int = 0, var eventType: Int = DBHelper.REGULAR_EVENT_TYPE_ID, var ignoreEventOccurrences: ArrayList = ArrayList(), var offset: String = "", var isDstIncluded: Boolean = false, var parentId: Int = 0, var lastUpdated: Long = 0L, var source: Int = SOURCE_SIMPLE_CALENDAR) : Serializable {