From 08136948cb44bef70a46b08403c6351d1ca35c74 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 7 Aug 2022 23:44:42 +0530 Subject: [PATCH] Add type column to distinguish between predefined event types --- .../calendar/pro/activities/MainActivity.kt | 6 ++-- .../calendar/pro/databases/EventsDatabase.kt | 11 +++++- .../calendar/pro/helpers/Constants.kt | 1 + .../calendar/pro/helpers/EventsHelper.kt | 35 ++++++++++++++----- .../calendar/pro/interfaces/EventTypesDao.kt | 6 ++++ .../calendar/pro/models/EventType.kt | 15 ++++---- 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 5f9f3c897..0e61aec58 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -38,7 +38,6 @@ import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult import com.simplemobiletools.calendar.pro.helpers.IcsImporter.ImportResult import com.simplemobiletools.calendar.pro.jobs.CalDAVUpdateListener import com.simplemobiletools.calendar.pro.models.Event -import com.simplemobiletools.calendar.pro.models.EventType import com.simplemobiletools.calendar.pro.models.ListEvent import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog @@ -593,10 +592,9 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { toast(R.string.importing) ensureBackgroundThread { val holidays = getString(R.string.holidays) - var eventTypeId = eventsHelper.getEventTypeIdWithTitle(holidays) + var eventTypeId = eventsHelper.getEventTypeIdWithClass(HOLIDAY_EVENT) if (eventTypeId == -1L) { - val eventType = EventType(null, holidays, resources.getColor(R.color.default_holidays_color)) - eventTypeId = eventsHelper.insertOrUpdateEventTypeSync(eventType) + eventTypeId = eventsHelper.createPredefinedEventType(holidays, R.color.default_holidays_color, HOLIDAY_EVENT, true) } val result = IcsImporter(this).importEvents(selectedHoliday as String, eventTypeId, 0, false, reminders) handleParseResult(result) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt index 65c9e1307..ddc6dd17c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/databases/EventsDatabase.kt @@ -22,7 +22,7 @@ import com.simplemobiletools.calendar.pro.models.Widget import com.simplemobiletools.commons.extensions.getProperPrimaryColor import java.util.concurrent.Executors -@Database(entities = [Event::class, EventType::class, Widget::class, Task::class], version = 7) +@Database(entities = [Event::class, EventType::class, Widget::class, Task::class], version = 8) @TypeConverters(Converters::class) abstract class EventsDatabase : RoomDatabase() { @@ -54,6 +54,7 @@ abstract class EventsDatabase : RoomDatabase() { .addMigrations(MIGRATION_4_5) .addMigrations(MIGRATION_5_6) .addMigrations(MIGRATION_6_7) + .addMigrations(MIGRATION_7_8) .build() db!!.openHelper.setWriteAheadLoggingEnabled(true) } @@ -127,5 +128,13 @@ abstract class EventsDatabase : RoomDatabase() { } } } + + private val MIGRATION_7_8 = object : Migration(7, 8) { + override fun migrate(database: SupportSQLiteDatabase) { + database.apply { + execSQL("ALTER TABLE event_types ADD COLUMN type INTEGER NOT NULL DEFAULT 0") + } + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index 43519c25d..a2ddc1b9b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -41,6 +41,7 @@ const val REMINDER_DEFAULT_VALUE = "${REMINDER_OFF},${REMINDER_OFF},${REMINDER_O const val OTHER_EVENT = 0 const val BIRTHDAY_EVENT = 1 const val ANNIVERSARY_EVENT = 2 +const val HOLIDAY_EVENT = 3 const val ITEM_EVENT = 0 const val ITEM_SECTION_DAY = 1 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index ae55f19db..b98fe0cff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.pro.helpers import android.app.Activity import android.content.Context import android.widget.Toast +import androidx.annotation.ColorRes import androidx.collection.LongSparseArray import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.extensions.* @@ -76,8 +77,12 @@ class EventsHelper(val context: Context) { fun getEventTypeIdWithTitle(title: String) = eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L + fun getEventTypeIdWithClass(classId: Int) = eventTypesDB.getEventTypeIdWithClass(classId) ?: -1L + private fun getLocalEventTypeIdWithTitle(title: String) = eventTypesDB.getLocalEventTypeIdWithTitle(title) ?: -1L + private fun getLocalEventTypeIdWithClass(classId: Int) = eventTypesDB.getLocalEventTypeIdWithClass(classId) ?: -1L + fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId) fun deleteEventTypes(eventTypes: ArrayList, deleteEvents: Boolean) { @@ -338,22 +343,36 @@ class EventsHelper(val context: Context) { callback(events) } + fun createPredefinedEventType(title: String, @ColorRes colorResId: Int, type: Int, caldav: Boolean = false): Long { + val eventType = EventType(id = null, title = title, color = context.resources.getColor(colorResId), type = type) + + // check if the event type already exists but without the type (e.g. BIRTHDAY_EVENT) so as to avoid duplication + val originalEventTypeId = if (caldav) { + getEventTypeIdWithTitle(title) + } else { + getLocalEventTypeIdWithTitle(title) + } + if (originalEventTypeId != -1L) { + eventType.id = originalEventTypeId + } + + return insertOrUpdateEventTypeSync(eventType) + } + fun getLocalBirthdaysEventTypeId(createIfNotExists: Boolean = true): Long { - val birthdays = context.getString(R.string.birthdays) - var eventTypeId = getLocalEventTypeIdWithTitle(birthdays) + var eventTypeId = getLocalEventTypeIdWithClass(BIRTHDAY_EVENT) if (eventTypeId == -1L && createIfNotExists) { - val eventType = EventType(null, birthdays, context.resources.getColor(R.color.default_birthdays_color)) - eventTypeId = insertOrUpdateEventTypeSync(eventType) + val birthdays = context.getString(R.string.birthdays) + eventTypeId = createPredefinedEventType(birthdays, R.color.default_birthdays_color, BIRTHDAY_EVENT) } return eventTypeId } fun getAnniversariesEventTypeId(createIfNotExists: Boolean = true): Long { - val anniversaries = context.getString(R.string.anniversaries) - var eventTypeId = getLocalEventTypeIdWithTitle(anniversaries) + var eventTypeId = getLocalEventTypeIdWithClass(ANNIVERSARY_EVENT) if (eventTypeId == -1L && createIfNotExists) { - val eventType = EventType(null, anniversaries, context.resources.getColor(R.color.default_anniversaries_color)) - eventTypeId = insertOrUpdateEventTypeSync(eventType) + val anniversaries = context.getString(R.string.anniversaries) + eventTypeId = createPredefinedEventType(anniversaries, R.color.default_anniversaries_color, ANNIVERSARY_EVENT) } return eventTypeId } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt index afef1d7c6..1f8e2e185 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt @@ -17,6 +17,12 @@ interface EventTypesDao { @Query("SELECT id FROM event_types WHERE title = :title AND caldav_calendar_id = 0 COLLATE NOCASE") fun getLocalEventTypeIdWithTitle(title: String): Long? + @Query("SELECT id FROM event_types WHERE type = :classId") + fun getEventTypeIdWithClass(classId: Int): Long? + + @Query("SELECT id FROM event_types WHERE type = :classId AND caldav_calendar_id = 0") + fun getLocalEventTypeIdWithClass(classId: Int): Long? + @Query("SELECT * FROM event_types WHERE caldav_calendar_id = :calendarId") fun getEventTypeWithCalDAVCalendarId(calendarId: Int): EventType? diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventType.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventType.kt index 696e81436..12aff09bd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventType.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventType.kt @@ -4,15 +4,18 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey +import com.simplemobiletools.calendar.pro.helpers.OTHER_EVENT @Entity(tableName = "event_types", indices = [(Index(value = ["id"], unique = true))]) data class EventType( - @PrimaryKey(autoGenerate = true) var id: Long?, - @ColumnInfo(name = "title") var title: String, - @ColumnInfo(name = "color") var color: Int, - @ColumnInfo(name = "caldav_calendar_id") var caldavCalendarId: Int = 0, - @ColumnInfo(name = "caldav_display_name") var caldavDisplayName: String = "", - @ColumnInfo(name = "caldav_email") var caldavEmail: String = "") { + @PrimaryKey(autoGenerate = true) var id: Long?, + @ColumnInfo(name = "title") var title: String, + @ColumnInfo(name = "color") var color: Int, + @ColumnInfo(name = "caldav_calendar_id") var caldavCalendarId: Int = 0, + @ColumnInfo(name = "caldav_display_name") var caldavDisplayName: String = "", + @ColumnInfo(name = "caldav_email") var caldavEmail: String = "", + @ColumnInfo(name = "type") var type: Int = OTHER_EVENT +) { fun getDisplayTitle() = if (caldavCalendarId == 0) title else "$caldavDisplayName ($caldavEmail)" fun isSyncedEventType() = caldavCalendarId != 0