avoid importing the same event twice from ics file

This commit is contained in:
tibbi 2017-01-27 00:05:42 +01:00
parent f56dfce1fe
commit 737f457dbc
3 changed files with 29 additions and 3 deletions

View File

@ -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<String> {
val ids = ArrayList<String>()
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())

View File

@ -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 = ""
}
}

View File

@ -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