From 021ad93b6ae42f24a20584b35983ee20b5ea616e Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 17 Jul 2017 22:51:41 +0200 Subject: [PATCH] move google sync event type handling in a separate function --- .../asynctasks/FetchGoogleEventsTask.kt | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) 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 1fc189a49..f70bef6be 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/FetchGoogleEventsTask.kt @@ -33,6 +33,7 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre private var service: com.google.api.services.calendar.Calendar private var lastError: Exception? = null private var dbHelper = activity.dbHelper + private var eventTypes = ArrayList() init { val transport = AndroidHttp.newCompatibleTransport() @@ -62,7 +63,8 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre for ((key, value) in events) { if (key == ITEMS) { dbHelper.getEventTypes { - parseEvents(value.toString(), it) + eventTypes = it + parseEvents(value.toString()) } } } @@ -77,7 +79,7 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre return parsedEvents } - private fun parseEvents(json: String, eventTypes: ArrayList): List { + private fun parseEvents(json: String): List { val importIDs = activity.dbHelper.getImportIds() val events = ArrayList() val token = object : TypeToken>() {}.type @@ -101,18 +103,6 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre var startTS: Int var endTS: Int var flags = 0 - var eventTypeId = -1 - val eventType = "google_${googleEvent.colorId}" - eventTypes.forEach { - if (it.title.toLowerCase() == eventType) - eventTypeId = it.id - } - - if (eventTypeId == -1) { - val newEventType = EventType(0, eventType, activity.config.primaryColor) - eventTypeId = dbHelper.insertEventType(newEventType) - eventTypes.add(newEventType) - } if (start.date != null) { startTS = DateTime(start.date).withHourOfDay(1).seconds() @@ -129,6 +119,7 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre repeatRule = Parser().parseRepeatInterval(recurrence.toString().trim('\"').substring(RRULE.length), startTS) } + val eventTypeId = getEventTypeId(googleEvent) 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, lastUpdated = lastUpdate) @@ -143,6 +134,23 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre return events } + private fun getEventTypeId(googleEvent: GoogleEvent): Int { + var eventTypeId = -1 + val eventType = "google_sync_${googleEvent.colorId}" + eventTypes.forEach { + if (it.title.toLowerCase() == eventType) + eventTypeId = it.id + } + + if (eventTypeId == -1) { + val newEventType = EventType(0, eventType, activity.config.primaryColor) + eventTypeId = dbHelper.insertEventType(newEventType) + eventTypes.add(newEventType) + } + + return eventTypeId + } + private fun getReminder(json: JsonObject): List { val array = json.getAsJsonArray(OVERRIDES) val token = object : TypeToken>() {}.type