move reminder fetching into a separate function

This commit is contained in:
tibbi 2017-07-17 22:58:05 +02:00
parent 021ad93b6a
commit 69293dd399
1 changed files with 12 additions and 8 deletions

View File

@ -97,7 +97,6 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre
} }
} }
val reminders = getReminder(googleEvent.reminders)
val start = googleEvent.start val start = googleEvent.start
val end = googleEvent.end val end = googleEvent.end
var startTS: Int var startTS: Int
@ -113,12 +112,8 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre
endTS = DateTime(end.dateTime).seconds() endTS = DateTime(end.dateTime).seconds()
} }
val recurrence = googleEvent.recurrence?.firstOrNull() val reminders = getReminders(googleEvent.reminders)
var repeatRule = RepeatRule(0, 0, 0) val repeatRule = getRepeatRule(googleEvent, startTS)
if (recurrence != null) {
repeatRule = Parser().parseRepeatInterval(recurrence.toString().trim('\"').substring(RRULE.length), startTS)
}
val eventTypeId = getEventTypeId(googleEvent) val eventTypeId = getEventTypeId(googleEvent)
val event = Event(0, startTS, endTS, googleEvent.summary, googleEvent.description, reminders.getOrElse(0, { -1 }), reminders.getOrElse(1, { -1 }), 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, reminders.getOrElse(2, { -1 }), repeatRule.repeatInterval, importId, flags, repeatRule.repeatLimit, repeatRule.repeatRule,
@ -151,7 +146,16 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre
return eventTypeId return eventTypeId
} }
private fun getReminder(json: JsonObject): List<Int> { private fun getRepeatRule(googleEvent: GoogleEvent, startTS: Int): RepeatRule {
val recurrence = googleEvent.recurrence?.firstOrNull()
return if (recurrence != null) {
Parser().parseRepeatInterval(recurrence.toString().trim('\"').substring(RRULE.length), startTS)
} else {
RepeatRule(0, 0, 0)
}
}
private fun getReminders(json: JsonObject): List<Int> {
val array = json.getAsJsonArray(OVERRIDES) val array = json.getAsJsonArray(OVERRIDES)
val token = object : TypeToken<List<GoogleEventReminder>>() {}.type val token = object : TypeToken<List<GoogleEventReminder>>() {}.type
val reminders = Gson().fromJson<ArrayList<GoogleEventReminder>>(array, token) ?: ArrayList<GoogleEventReminder>(2) val reminders = Gson().fromJson<ArrayList<GoogleEventReminder>>(array, token) ?: ArrayList<GoogleEventReminder>(2)