From 52c6864a70484fdd061913921d00ea631b35d2e7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 2 May 2018 20:48:53 +0200 Subject: [PATCH] allow overriding the CATEGORIES field at ics events --- .../calendar/activities/MainActivity.kt | 2 +- .../calendar/dialogs/ImportEventsDialog.kt | 3 ++- .../calendar/helpers/IcsImporter.kt | 18 ++++++++++-------- .../calendar/models/EventType.kt | 2 ++ .../main/res/layout/dialog_import_events.xml | 13 ++++++++++++- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 1bf614dcd..fa6ff0292 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -380,7 +380,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { eventTypeId = dbHelper.insertEventType(eventType) } - val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0) + val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0, false) handleParseResult(result) if (result != IcsImporter.ImportResult.IMPORT_FAIL) { runOnUiThread { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt index 7a938165f..b74a93420 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt @@ -60,7 +60,8 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { activity.toast(R.string.importing) Thread { - val result = IcsImporter(activity).importEvents(path, currEventTypeId, currEventTypeCalDAVCalendarId) + val overrideFileEventTypes = view.import_events_checkbox.isChecked + val result = IcsImporter(activity).importEvents(path, currEventTypeId, currEventTypeCalDAVCalendarId, overrideFileEventTypes) handleParseResult(result) dismiss() }.start() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt index eff91f071..4dd240e7c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsImporter.kt @@ -28,7 +28,7 @@ class IcsImporter(val activity: SimpleActivity) { private var curRepeatInterval = 0 private var curRepeatLimit = 0 private var curRepeatRule = 0 - private var curEventType = DBHelper.REGULAR_EVENT_TYPE_ID + private var curEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID private var curLastModified = 0L private var curLocation = "" private var curCategoryColor = -2 @@ -40,8 +40,9 @@ class IcsImporter(val activity: SimpleActivity) { private var eventsImported = 0 private var eventsFailed = 0 - fun importEvents(path: String, defaultEventType: Int, calDAVCalendarId: Int): ImportResult { + fun importEvents(path: String, defaultEventTypeId: Int, calDAVCalendarId: Int, overrideFileEventTypes: Boolean): ImportResult { try { + val eventTypes = activity.dbHelper.getEventTypesSync() val existingEvents = activity.dbHelper.getEventsWithImportIds() val eventsToInsert = ArrayList() var prevLine = "" @@ -74,7 +75,7 @@ class IcsImporter(val activity: SimpleActivity) { if (line == BEGIN_EVENT) { resetValues() - curEventType = defaultEventType + curEventTypeId = defaultEventTypeId } else if (line.startsWith(DTSTART)) { curStart = getTimestamp(line.substring(DTSTART.length)) } else if (line.startsWith(DTEND)) { @@ -105,7 +106,7 @@ class IcsImporter(val activity: SimpleActivity) { if (color.trimStart('-').areDigitsOnly()) { curCategoryColor = Integer.parseInt(color) } - } else if (line.startsWith(CATEGORIES)) { + } else if (line.startsWith(CATEGORIES) && !overrideFileEventTypes) { val categories = line.substring(CATEGORIES.length) tryAddCategories(categories, activity) } else if (line.startsWith(LAST_MODIFIED)) { @@ -137,10 +138,11 @@ class IcsImporter(val activity: SimpleActivity) { continue } - val source = if (calDAVCalendarId == 0) SOURCE_IMPORTED_ICS else "$CALDAV-$calDAVCalendarId" + val eventType = eventTypes.firstOrNull { it.id == curEventTypeId } + val source = if (calDAVCalendarId == 0 || eventType?.isSyncedEventType() == false) SOURCE_IMPORTED_ICS else "$CALDAV-$calDAVCalendarId" val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }), curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval, - curImportId, curFlags, curRepeatLimit, curRepeatRule, curEventType, lastUpdated = curLastModified, + curImportId, curFlags, curRepeatLimit, curRepeatRule, curEventTypeId, lastUpdated = curLastModified, source = source, location = curLocation) if (event.getIsAllDay() && curEnd > curStart) { @@ -209,7 +211,7 @@ class IcsImporter(val activity: SimpleActivity) { } val eventId = context.dbHelper.getEventTypeIdWithTitle(eventTypeTitle) - curEventType = if (eventId == -1) { + curEventTypeId = if (eventId == -1) { val newTypeColor = if (curCategoryColor == -2) context.resources.getColor(R.color.color_primary) else curCategoryColor val eventType = EventType(0, eventTypeTitle, newTypeColor) context.dbHelper.insertEventType(eventType) @@ -238,7 +240,7 @@ class IcsImporter(val activity: SimpleActivity) { curRepeatInterval = 0 curRepeatLimit = 0 curRepeatRule = 0 - curEventType = DBHelper.REGULAR_EVENT_TYPE_ID + curEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID curLastModified = 0L curCategoryColor = -2 curLocation = "" diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/EventType.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/EventType.kt index c677bfa7f..77d063a2b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/EventType.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/EventType.kt @@ -2,4 +2,6 @@ package com.simplemobiletools.calendar.models data class EventType(var id: Int = 0, var title: String, var color: Int, var caldavCalendarId: Int = 0, var caldavDisplayName: String = "", var caldavEmail: String = "") { fun getDisplayTitle() = if (caldavCalendarId == 0) title else "$caldavDisplayName ($caldavEmail)" + + fun isSyncedEventType() = caldavCalendarId != 0 } diff --git a/app/src/main/res/layout/dialog_import_events.xml b/app/src/main/res/layout/dialog_import_events.xml index fad9689b3..acdcc46ed 100644 --- a/app/src/main/res/layout/dialog_import_events.xml +++ b/app/src/main/res/layout/dialog_import_events.xml @@ -5,7 +5,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - android:paddingLeft="@dimen/activity_margin" android:paddingRight="@dimen/activity_margin" android:paddingTop="@dimen/activity_margin"> @@ -13,6 +12,7 @@ android:id="@+id/import_events_event_type_label" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginLeft="@dimen/activity_margin" android:text="@string/default_event_type" android:textSize="@dimen/smaller_text_size"/> @@ -20,6 +20,7 @@ android:id="@+id/import_event_type_holder" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginLeft="@dimen/activity_margin" android:background="?attr/selectableItemBackground" android:padding="@dimen/small_margin"> @@ -42,4 +43,14 @@ android:clickable="false"/> + + +