try parsing one category from ics files at importing

This commit is contained in:
tibbi
2017-03-05 11:34:20 +01:00
parent 848c084a68
commit c8af7f25be
5 changed files with 33 additions and 11 deletions

View File

@ -27,7 +27,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private var mReminder3Minutes = 0 private var mReminder3Minutes = 0
private var mRepeatInterval = 0 private var mRepeatInterval = 0
private var mRepeatLimit = 0 private var mRepeatLimit = 0
private var mEventTypeId = DBHelper.REGULAR_EVENT_ID private var mEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID
private var mDialogTheme = 0 private var mDialogTheme = 0
lateinit var mEventStartDateTime: DateTime lateinit var mEventStartDateTime: DateTime

View File

@ -46,7 +46,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteItemsListener {
} }
override fun deleteItems(ids: ArrayList<Int>) { override fun deleteItems(ids: ArrayList<Int>) {
if (ids.contains(DBHelper.REGULAR_EVENT_ID)) { if (ids.contains(DBHelper.REGULAR_EVENT_TYPE_ID)) {
toast(R.string.cannot_delete_default_type) toast(R.string.cannot_delete_default_type)
} }

View File

@ -51,7 +51,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
companion object { companion object {
private val DB_VERSION = 7 private val DB_VERSION = 7
val DB_NAME = "events.db" val DB_NAME = "events.db"
val REGULAR_EVENT_ID = 1 val REGULAR_EVENT_TYPE_ID = 1
private var mEventsListener: EventUpdateListener? = null private var mEventsListener: EventUpdateListener? = null
@ -64,7 +64,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_START_TS INTEGER, $COL_END_TS INTEGER, $COL_TITLE TEXT, " + db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_START_TS INTEGER, $COL_END_TS INTEGER, $COL_TITLE TEXT, " +
"$COL_DESCRIPTION TEXT, $COL_REMINDER_MINUTES INTEGER, $COL_REMINDER_MINUTES_2 INTEGER, $COL_REMINDER_MINUTES_3 INTEGER, " + "$COL_DESCRIPTION TEXT, $COL_REMINDER_MINUTES INTEGER, $COL_REMINDER_MINUTES_2 INTEGER, $COL_REMINDER_MINUTES_3 INTEGER, " +
"$COL_IMPORT_ID TEXT, $COL_FLAGS INTEGER, $COL_EVENT_TYPE INTEGER NOT NULL DEFAULT $REGULAR_EVENT_ID)") "$COL_IMPORT_ID TEXT, $COL_FLAGS INTEGER, $COL_EVENT_TYPE INTEGER NOT NULL DEFAULT $REGULAR_EVENT_TYPE_ID)")
createMetaTable(db) createMetaTable(db)
createTypesTable(db) createTypesTable(db)
@ -95,7 +95,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
if (oldVersion < 7) { if (oldVersion < 7) {
createTypesTable(db) createTypesTable(db)
db.execSQL("ALTER TABLE $MAIN_TABLE_NAME ADD COLUMN $COL_EVENT_TYPE INTEGER NOT NULL DEFAULT $REGULAR_EVENT_ID") db.execSQL("ALTER TABLE $MAIN_TABLE_NAME ADD COLUMN $COL_EVENT_TYPE INTEGER NOT NULL DEFAULT $REGULAR_EVENT_TYPE_ID")
} }
} }
@ -111,7 +111,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private fun addRegularEventType(db: SQLiteDatabase) { private fun addRegularEventType(db: SQLiteDatabase) {
val regularEvent = context.resources.getString(R.string.regular_event) val regularEvent = context.resources.getString(R.string.regular_event)
val eventType = EventType(REGULAR_EVENT_ID, regularEvent, context.config.primaryColor) val eventType = EventType(REGULAR_EVENT_TYPE_ID, regularEvent, context.config.primaryColor)
addEventType(eventType, db) addEventType(eventType, db)
} }
@ -261,8 +261,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun deleteEventTypes(ids: ArrayList<Int>, callback: (deletedCnt: Int) -> Unit) { fun deleteEventTypes(ids: ArrayList<Int>, callback: (deletedCnt: Int) -> Unit) {
var deleteIds = ids var deleteIds = ids
if (ids.contains(DBHelper.REGULAR_EVENT_ID)) if (ids.contains(DBHelper.REGULAR_EVENT_TYPE_ID))
deleteIds = ids.filter { it != DBHelper.REGULAR_EVENT_ID } as ArrayList<Int> deleteIds = ids.filter { it != DBHelper.REGULAR_EVENT_TYPE_ID } as ArrayList<Int>
val deletedSet = HashSet<String>() val deletedSet = HashSet<String>()
deleteIds.map { deletedSet.add(it.toString()) } deleteIds.map { deletedSet.add(it.toString()) }
@ -281,7 +281,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private fun resetEventsWithType(eventTypeId: Int) { private fun resetEventsWithType(eventTypeId: Int) {
val values = ContentValues() val values = ContentValues()
values.put(COL_EVENT_TYPE, REGULAR_EVENT_ID) values.put(COL_EVENT_TYPE, REGULAR_EVENT_TYPE_ID)
val selection = "$COL_EVENT_TYPE = ?" val selection = "$COL_EVENT_TYPE = ?"
val selectionArgs = arrayOf(eventTypeId.toString()) val selectionArgs = arrayOf(eventTypeId.toString())

View File

@ -1,9 +1,11 @@
package com.simplemobiletools.calendar.helpers package com.simplemobiletools.calendar.helpers
import android.content.Context import android.content.Context
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.* import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.*
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.models.EventType
import org.joda.time.DateTimeZone import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat import org.joda.time.format.DateTimeFormat
import java.io.File import java.io.File
@ -24,6 +26,7 @@ class IcsParser {
private val ACTION = "ACTION:" private val ACTION = "ACTION:"
private val TRIGGER = "TRIGGER:" private val TRIGGER = "TRIGGER:"
private val RRULE = "RRULE:" private val RRULE = "RRULE:"
private val CATEGORIES = "CATEGORIES:"
private val DISPLAY = "DISPLAY" private val DISPLAY = "DISPLAY"
private val FREQ = "FREQ" private val FREQ = "FREQ"
@ -45,6 +48,7 @@ class IcsParser {
var curReminderMinutes = -1 var curReminderMinutes = -1
var curRepeatInterval = 0 var curRepeatInterval = 0
var curRepeatLimit = 0 var curRepeatLimit = 0
var curEventType = DBHelper.REGULAR_EVENT_TYPE_ID
var curShouldHaveNotification = false var curShouldHaveNotification = false
var isNotificationDescription = false var isNotificationDescription = false
@ -94,13 +98,16 @@ class IcsParser {
if (curReminderMinutes == -1 && curShouldHaveNotification) { if (curReminderMinutes == -1 && curShouldHaveNotification) {
curReminderMinutes = decodeTime(line.substring(TRIGGER.length)) / 60 curReminderMinutes = decodeTime(line.substring(TRIGGER.length)) / 60
} }
} else if (line.startsWith(CATEGORIES)) {
val categories = line.substring(CATEGORIES.length)
tryAddCategories(categories, context)
} else if (line == END) { } else if (line == END) {
if (curTitle.isEmpty() || curStart == -1 || curEnd == -1 || importIDs.contains(curImportId)) if (curTitle.isEmpty() || curStart == -1 || curEnd == -1 || importIDs.contains(curImportId))
continue continue
importIDs.add(curImportId) importIDs.add(curImportId)
val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes, -1, -1, curRepeatInterval, val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes, -1, -1, curRepeatInterval,
curImportId, curFlags, curRepeatLimit) curImportId, curFlags, curRepeatLimit, curEventType)
dbHelper.insert(event) { } dbHelper.insert(event) { }
eventsImported++ eventsImported++
resetValues() resetValues()
@ -146,6 +153,20 @@ class IcsParser {
} }
} }
private fun tryAddCategories(categories: String, context: Context) {
if (!categories.contains(",")) {
val eventTitle = categories
val dbHelper = DBHelper.newInstance(context)
val eventId = dbHelper.getEventTypeIdWithTitle(eventTitle)
if (eventId == -1) {
val eventType = EventType(0, eventTitle, context.resources.getColor(R.color.color_primary))
curEventType = dbHelper.insertEventType(eventType)
} else {
curEventType = eventId
}
}
}
// P0DT1H0M0S // P0DT1H0M0S
private fun decodeTime(duration: String): Int { private fun decodeTime(duration: String): Int {
val weeks = getDurationValue(duration, "W") val weeks = getDurationValue(duration, "W")
@ -216,6 +237,7 @@ class IcsParser {
curReminderMinutes = -1 curReminderMinutes = -1
curRepeatInterval = 0 curRepeatInterval = 0
curRepeatLimit = 0 curRepeatLimit = 0
curEventType = DBHelper.REGULAR_EVENT_TYPE_ID
curShouldHaveNotification = false curShouldHaveNotification = false
isNotificationDescription = false isNotificationDescription = false
} }

View File

@ -7,7 +7,7 @@ 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 = "", data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var title: String = "", var description: String = "",
var reminder1Minutes: Int = -1, var reminder2Minutes: Int = -1, var reminder3Minutes: Int = -1, var repeatInterval: Int = 0, var reminder1Minutes: Int = -1, var reminder2Minutes: Int = -1, var reminder3Minutes: Int = -1, var repeatInterval: Int = 0,
var importId: String? = "", var flags: Int = 0, var repeatLimit: Int = 0, var eventType: Int = DBHelper.REGULAR_EVENT_ID) : Serializable { var importId: String? = "", var flags: Int = 0, var repeatLimit: Int = 0, var eventType: Int = DBHelper.REGULAR_EVENT_TYPE_ID) : Serializable {
companion object { companion object {
private val serialVersionUID = -32456795132344616L private val serialVersionUID = -32456795132344616L