From 737f457dbcc3a8ccd1f667f878f69d83bd387c7a Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 27 Jan 2017 00:05:42 +0100 Subject: [PATCH] avoid importing the same event twice from ics file --- .../calendar/helpers/DBHelper.kt | 20 +++++++++++++++++++ .../calendar/helpers/IcsParser.kt | 10 ++++++++-- .../calendar/models/Event.kt | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 7972856bc..3a02d560c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -118,6 +118,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V put(COL_TITLE, event.title) put(COL_DESCRIPTION, event.description) put(COL_REMINDER_MINUTES, event.reminderMinutes) + put(COL_IMPORT_ID, event.importId) } } @@ -152,6 +153,25 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V mEventsListener?.eventsDeleted(ids.size) } + fun getImportIds(): ArrayList { + val ids = ArrayList() + val columns = arrayOf(COL_IMPORT_ID) + val selection = "$COL_IMPORT_ID IS NOT NULL" + var cursor: Cursor? = null + try { + cursor = mDb.query(MAIN_TABLE_NAME, columns, selection, null, null, null, null) + if (cursor != null && cursor.moveToFirst()) { + do { + val id = cursor.getStringValue(COL_IMPORT_ID) + ids.add(id) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + return ids + } + fun getEvent(id: Int): Event? { val selection = "$MAIN_TABLE_NAME.$COL_ID = ?" val selectionArgs = arrayOf(id.toString()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt index 9818adf4a..d53716aba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsParser.kt @@ -21,11 +21,13 @@ class IcsParser { private val DTEND = "DTEND:" private val SUMMARY = "SUMMARY:" private val DESCRIPTION = "DESCRIPTION:" + private val UID = "UID:" var curStart = -1 var curEnd = -1 var curTitle = "" var curDescription = "" + var curImportId = "" var eventsImported = 0 var eventsFailed = 0 @@ -33,6 +35,7 @@ class IcsParser { fun parseIcs(context: Context, reminderMinutes: Int, path: String): ImportResult { try { val dbHelper = DBHelper(context) + val importIDs = dbHelper.getImportIds() File(path).inputStream().bufferedReader().use { while (true) { @@ -47,11 +50,13 @@ class IcsParser { curTitle = line.substring(SUMMARY.length) } else if (line.startsWith(DESCRIPTION)) { curDescription = line.substring(DESCRIPTION.length) + } else if (line.startsWith(UID)) { + curImportId = line.substring(UID.length) } else if (line == END || line == BEGIN_ALARM) { - if (curTitle.isEmpty() || curStart == -1 || curEnd == -1) + if (curTitle.isEmpty() || curStart == -1 || curEnd == -1 || importIDs.contains(curImportId)) continue - val event = Event(0, curStart, curEnd, curTitle, curDescription, reminderMinutes) + val event = Event(0, curStart, curEnd, curTitle, curDescription, reminderMinutes, importId = curImportId) dbHelper.insert(event) { context.scheduleNotification(event) } @@ -89,5 +94,6 @@ class IcsParser { curEnd = -1 curTitle = "" curDescription = "" + curImportId = "" } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt index 782f57fa7..1f9187501 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt @@ -6,7 +6,7 @@ import org.joda.time.DateTime import java.io.Serializable data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var title: String = "", var description: String = "", - var reminderMinutes: Int = 0, var repeatInterval: Int = 0) : Serializable { + var reminderMinutes: Int = 0, var repeatInterval: Int = 0, var importId: String = "") : Serializable { companion object { private val serialVersionUID = -32456795132344616L