Add type column to distinguish between predefined event types

This commit is contained in:
Naveen 2022-08-07 23:44:42 +05:30
parent da2f800c36
commit 08136948cb
6 changed files with 55 additions and 19 deletions

View File

@ -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.helpers.IcsImporter.ImportResult
import com.simplemobiletools.calendar.pro.jobs.CalDAVUpdateListener import com.simplemobiletools.calendar.pro.jobs.CalDAVUpdateListener
import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
import com.simplemobiletools.calendar.pro.models.ListEvent import com.simplemobiletools.calendar.pro.models.ListEvent
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog
@ -593,10 +592,9 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
toast(R.string.importing) toast(R.string.importing)
ensureBackgroundThread { ensureBackgroundThread {
val holidays = getString(R.string.holidays) val holidays = getString(R.string.holidays)
var eventTypeId = eventsHelper.getEventTypeIdWithTitle(holidays) var eventTypeId = eventsHelper.getEventTypeIdWithClass(HOLIDAY_EVENT)
if (eventTypeId == -1L) { if (eventTypeId == -1L) {
val eventType = EventType(null, holidays, resources.getColor(R.color.default_holidays_color)) eventTypeId = eventsHelper.createPredefinedEventType(holidays, R.color.default_holidays_color, HOLIDAY_EVENT, true)
eventTypeId = eventsHelper.insertOrUpdateEventTypeSync(eventType)
} }
val result = IcsImporter(this).importEvents(selectedHoliday as String, eventTypeId, 0, false, reminders) val result = IcsImporter(this).importEvents(selectedHoliday as String, eventTypeId, 0, false, reminders)
handleParseResult(result) handleParseResult(result)

View File

@ -22,7 +22,7 @@ import com.simplemobiletools.calendar.pro.models.Widget
import com.simplemobiletools.commons.extensions.getProperPrimaryColor import com.simplemobiletools.commons.extensions.getProperPrimaryColor
import java.util.concurrent.Executors 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) @TypeConverters(Converters::class)
abstract class EventsDatabase : RoomDatabase() { abstract class EventsDatabase : RoomDatabase() {
@ -54,6 +54,7 @@ abstract class EventsDatabase : RoomDatabase() {
.addMigrations(MIGRATION_4_5) .addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6) .addMigrations(MIGRATION_5_6)
.addMigrations(MIGRATION_6_7) .addMigrations(MIGRATION_6_7)
.addMigrations(MIGRATION_7_8)
.build() .build()
db!!.openHelper.setWriteAheadLoggingEnabled(true) 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")
}
}
}
} }
} }

View File

@ -41,6 +41,7 @@ const val REMINDER_DEFAULT_VALUE = "${REMINDER_OFF},${REMINDER_OFF},${REMINDER_O
const val OTHER_EVENT = 0 const val OTHER_EVENT = 0
const val BIRTHDAY_EVENT = 1 const val BIRTHDAY_EVENT = 1
const val ANNIVERSARY_EVENT = 2 const val ANNIVERSARY_EVENT = 2
const val HOLIDAY_EVENT = 3
const val ITEM_EVENT = 0 const val ITEM_EVENT = 0
const val ITEM_SECTION_DAY = 1 const val ITEM_SECTION_DAY = 1

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.pro.helpers
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.widget.Toast import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.collection.LongSparseArray import androidx.collection.LongSparseArray
import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.extensions.*
@ -76,8 +77,12 @@ class EventsHelper(val context: Context) {
fun getEventTypeIdWithTitle(title: String) = eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L 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 getLocalEventTypeIdWithTitle(title: String) = eventTypesDB.getLocalEventTypeIdWithTitle(title) ?: -1L
private fun getLocalEventTypeIdWithClass(classId: Int) = eventTypesDB.getLocalEventTypeIdWithClass(classId) ?: -1L
fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId) fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean) { fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
@ -338,22 +343,36 @@ class EventsHelper(val context: Context) {
callback(events) 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 { fun getLocalBirthdaysEventTypeId(createIfNotExists: Boolean = true): Long {
val birthdays = context.getString(R.string.birthdays) var eventTypeId = getLocalEventTypeIdWithClass(BIRTHDAY_EVENT)
var eventTypeId = getLocalEventTypeIdWithTitle(birthdays)
if (eventTypeId == -1L && createIfNotExists) { if (eventTypeId == -1L && createIfNotExists) {
val eventType = EventType(null, birthdays, context.resources.getColor(R.color.default_birthdays_color)) val birthdays = context.getString(R.string.birthdays)
eventTypeId = insertOrUpdateEventTypeSync(eventType) eventTypeId = createPredefinedEventType(birthdays, R.color.default_birthdays_color, BIRTHDAY_EVENT)
} }
return eventTypeId return eventTypeId
} }
fun getAnniversariesEventTypeId(createIfNotExists: Boolean = true): Long { fun getAnniversariesEventTypeId(createIfNotExists: Boolean = true): Long {
val anniversaries = context.getString(R.string.anniversaries) var eventTypeId = getLocalEventTypeIdWithClass(ANNIVERSARY_EVENT)
var eventTypeId = getLocalEventTypeIdWithTitle(anniversaries)
if (eventTypeId == -1L && createIfNotExists) { if (eventTypeId == -1L && createIfNotExists) {
val eventType = EventType(null, anniversaries, context.resources.getColor(R.color.default_anniversaries_color)) val anniversaries = context.getString(R.string.anniversaries)
eventTypeId = insertOrUpdateEventTypeSync(eventType) eventTypeId = createPredefinedEventType(anniversaries, R.color.default_anniversaries_color, ANNIVERSARY_EVENT)
} }
return eventTypeId return eventTypeId
} }

View File

@ -17,6 +17,12 @@ interface EventTypesDao {
@Query("SELECT id FROM event_types WHERE title = :title AND caldav_calendar_id = 0 COLLATE NOCASE") @Query("SELECT id FROM event_types WHERE title = :title AND caldav_calendar_id = 0 COLLATE NOCASE")
fun getLocalEventTypeIdWithTitle(title: String): Long? 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") @Query("SELECT * FROM event_types WHERE caldav_calendar_id = :calendarId")
fun getEventTypeWithCalDAVCalendarId(calendarId: Int): EventType? fun getEventTypeWithCalDAVCalendarId(calendarId: Int): EventType?

View File

@ -4,15 +4,18 @@ import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Index import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.simplemobiletools.calendar.pro.helpers.OTHER_EVENT
@Entity(tableName = "event_types", indices = [(Index(value = ["id"], unique = true))]) @Entity(tableName = "event_types", indices = [(Index(value = ["id"], unique = true))])
data class EventType( data class EventType(
@PrimaryKey(autoGenerate = true) var id: Long?, @PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "title") var title: String, @ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "color") var color: Int, @ColumnInfo(name = "color") var color: Int,
@ColumnInfo(name = "caldav_calendar_id") var caldavCalendarId: Int = 0, @ColumnInfo(name = "caldav_calendar_id") var caldavCalendarId: Int = 0,
@ColumnInfo(name = "caldav_display_name") var caldavDisplayName: String = "", @ColumnInfo(name = "caldav_display_name") var caldavDisplayName: String = "",
@ColumnInfo(name = "caldav_email") var caldavEmail: 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 getDisplayTitle() = if (caldavCalendarId == 0) title else "$caldavDisplayName ($caldavEmail)"
fun isSyncedEventType() = caldavCalendarId != 0 fun isSyncedEventType() = caldavCalendarId != 0