properly update google events at Updating
This commit is contained in:
parent
9f3d0c2c6a
commit
7e048eff37
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)}") }
|
||||
|
||||
|
|
|
@ -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<Int> = ArrayList(),
|
||||
var offset: String = "", var isDstIncluded: Boolean = false, var parentId: Int = 0, var lastUpdated: Long = 0L,
|
||||
var source: Int = SOURCE_SIMPLE_CALENDAR) : Serializable {
|
||||
|
|
Loading…
Reference in New Issue