properly update google events at Updating

This commit is contained in:
tibbi 2017-07-25 23:07:51 +02:00
parent 9f3d0c2c6a
commit 7e048eff37
4 changed files with 45 additions and 31 deletions

View File

@ -437,6 +437,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
} }
val wasRepeatable = mEvent.repeatInterval > 0 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 reminders = sortedSetOf(mReminder1Minutes, mReminder2Minutes, mReminder3Minutes).filter { it != REMINDER_OFF }
val newDescription = event_description.value val newDescription = event_description.value
@ -449,6 +450,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
reminder2Minutes = reminders.elementAtOrElse(1) { REMINDER_OFF } reminder2Minutes = reminders.elementAtOrElse(1) { REMINDER_OFF }
reminder3Minutes = reminders.elementAtOrElse(2) { REMINDER_OFF } reminder3Minutes = reminders.elementAtOrElse(2) { REMINDER_OFF }
repeatInterval = mRepeatInterval repeatInterval = mRepeatInterval
importId = newImportId
flags = if (event_all_day.isChecked) (mEvent.flags or FLAG_ALL_DAY) else (mEvent.flags.removeFlag(FLAG_ALL_DAY)) 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 repeatLimit = if (repeatInterval == 0) 0 else mRepeatLimit
repeatRule = mRepeatRule repeatRule = mRepeatRule

View File

@ -27,36 +27,14 @@ class GoogleSyncHandler {
private fun createRemoteGoogleEvent(activity: SimpleActivity, event: Event) { private fun createRemoteGoogleEvent(activity: SimpleActivity, event: Event) {
try { try {
com.google.api.services.calendar.model.Event().apply { val googleEvent = mergeMyEventToGoogleEvent(com.google.api.services.calendar.model.Event(), event)
summary = event.title try {
description = event.description activity.getGoogleSyncService().events().insert(PRIMARY, googleEvent).execute()
} catch (e: GoogleJsonResponseException) {
if (event.getIsAllDay()) { val msg = String.format(activity.getString(R.string.google_sync_error_insert), e.getGoogleMessageError())
start = EventDateTime().setDate(com.google.api.client.util.DateTime(true, event.startTS * 1000L, null)) activity.toast(msg, Toast.LENGTH_LONG)
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)
}
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
@ -71,12 +49,46 @@ class GoogleSyncHandler {
return com.google.api.services.calendar.model.Event.Reminders().setOverrides(reminders) 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) { fun updateGoogleEvent(activity: SimpleActivity, event: Event) {
if (activity.isGoogleSyncActive()) { if (activity.isGoogleSyncActive()) {
if (activity.isOnline()) { if (activity.isOnline()) {
Thread({ Thread({
try { try {
val googleEvent = activity.getGoogleSyncService().events().get(PRIMARY, event.importId).execute() 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) { } catch (e: GoogleJsonResponseException) {
val msg = String.format(activity.getString(R.string.google_sync_error_update), e.getGoogleMessageError()) val msg = String.format(activity.getString(R.string.google_sync_error_update), e.getGoogleMessageError())
activity.toast(msg, Toast.LENGTH_LONG) activity.toast(msg, Toast.LENGTH_LONG)

View File

@ -25,7 +25,7 @@ class IcsExporter {
out.writeLn(BEGIN_EVENT) out.writeLn(BEGIN_EVENT)
event.title.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") } 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.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.eventType.let { out.writeLn("$CATEGORIES${activity.dbHelper.getEventType(it)?.title}") }
event.lastUpdated.let { out.writeLn("$LAST_MODIFIED:${Formatter.getExportedTime(it)}") } event.lastUpdated.let { out.writeLn("$LAST_MODIFIED:${Formatter.getExportedTime(it)}") }

View File

@ -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 = "", 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 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<Int> = ArrayList(), var eventType: Int = DBHelper.REGULAR_EVENT_TYPE_ID, var ignoreEventOccurrences: ArrayList<Int> = ArrayList(),
var offset: String = "", var isDstIncluded: Boolean = false, var parentId: Int = 0, var lastUpdated: Long = 0L, var offset: String = "", var isDstIncluded: Boolean = false, var parentId: Int = 0, var lastUpdated: Long = 0L,
var source: Int = SOURCE_SIMPLE_CALENDAR) : Serializable { var source: Int = SOURCE_SIMPLE_CALENDAR) : Serializable {