mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-15 19:30:50 +01:00
moving a couple new functions into EventTypesHelper
This commit is contained in:
parent
9640ca26e2
commit
27179145e1
@ -73,7 +73,7 @@ class EventActivity : SimpleActivity() {
|
||||
return@Thread
|
||||
}
|
||||
|
||||
val localEventType = dbHelper.getEventType(config.lastUsedLocalEventTypeId)
|
||||
val localEventType = eventTypesDB.getEventTypeWithId(config.lastUsedLocalEventTypeId)
|
||||
runOnUiThread {
|
||||
gotEvent(savedInstanceState, localEventType, event)
|
||||
}
|
||||
@ -583,7 +583,7 @@ class EventActivity : SimpleActivity() {
|
||||
|
||||
private fun updateEventType() {
|
||||
Thread {
|
||||
val eventType = dbHelper.getEventType(mEventTypeId)
|
||||
val eventType = eventTypesDB.getEventTypeWithId(mEventTypeId)
|
||||
if (eventType != null) {
|
||||
runOnUiThread {
|
||||
event_type.text = eventType.title
|
||||
@ -648,10 +648,11 @@ class EventActivity : SimpleActivity() {
|
||||
event_caldav_calendar_email.text = currentCalendar.accountName
|
||||
|
||||
Thread {
|
||||
val calendarColor = dbHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color ?: currentCalendar.color
|
||||
val calendarColor = EventTypesHelper().getEventTypeWithCalDAVCalendarId(applicationContext, currentCalendar.id)?.color
|
||||
?: currentCalendar.color
|
||||
|
||||
runOnUiThread {
|
||||
event_caldav_calendar_color.setFillWithStroke(calendarColor, config.backgroundColor)
|
||||
|
||||
event_caldav_calendar_name.apply {
|
||||
text = currentCalendar.displayName
|
||||
setPadding(paddingLeft, paddingTop, paddingRight, resources.getDimension(R.dimen.tiny_margin).toInt())
|
||||
@ -710,7 +711,9 @@ class EventActivity : SimpleActivity() {
|
||||
val newTitle = event_title.value
|
||||
if (newTitle.isEmpty()) {
|
||||
toast(R.string.title_empty)
|
||||
event_title.requestFocus()
|
||||
runOnUiThread {
|
||||
event_title.requestFocus()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -729,7 +732,7 @@ class EventActivity : SimpleActivity() {
|
||||
val newEventType = if (!config.caldavSync || config.lastUsedCaldavCalendarId == 0 || mEventCalendarId == STORED_LOCALLY_ONLY) {
|
||||
mEventTypeId
|
||||
} else {
|
||||
dbHelper.getEventTypeWithCalDAVCalendarId(mEventCalendarId)?.id ?: config.lastUsedLocalEventTypeId
|
||||
EventTypesHelper().getEventTypeWithCalDAVCalendarId(applicationContext, mEventCalendarId)?.id ?: config.lastUsedLocalEventTypeId
|
||||
}
|
||||
|
||||
val newSource = if (!config.caldavSync || mEventCalendarId == STORED_LOCALLY_ONLY) {
|
||||
|
@ -112,7 +112,11 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||
}
|
||||
|
||||
EventTypesHelper().getEventTypes(this) {
|
||||
mShouldFilterBeVisible = it.size > 1 || config.displayEventTypes.isEmpty()
|
||||
val newShouldFilterBeVisible = it.size > 1 || config.displayEventTypes.isEmpty()
|
||||
if (newShouldFilterBeVisible != mShouldFilterBeVisible) {
|
||||
mShouldFilterBeVisible = newShouldFilterBeVisible
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
}
|
||||
|
||||
if (config.storedView == WEEKLY_VIEW) {
|
||||
@ -390,10 +394,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||
toast(R.string.importing)
|
||||
Thread {
|
||||
val holidays = getString(R.string.holidays)
|
||||
var eventTypeId = dbHelper.getEventTypeIdWithTitle(holidays)
|
||||
var eventTypeId = EventTypesHelper().getEventTypeIdWithTitle(applicationContext, holidays)
|
||||
if (eventTypeId == -1L) {
|
||||
val eventType = EventType(0, holidays, resources.getColor(R.color.default_holidays_color))
|
||||
eventTypeId = dbHelper.insertEventType(eventType)
|
||||
val eventType = EventType(null, holidays, resources.getColor(R.color.default_holidays_color))
|
||||
eventTypeId = EventTypesHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
|
||||
}
|
||||
|
||||
val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0, false)
|
||||
@ -516,20 +520,20 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||
|
||||
private fun getBirthdaysEventTypeId(): Long {
|
||||
val birthdays = getString(R.string.birthdays)
|
||||
var eventTypeId = dbHelper.getEventTypeIdWithTitle(birthdays)
|
||||
var eventTypeId = EventTypesHelper().getEventTypeIdWithTitle(applicationContext, birthdays)
|
||||
if (eventTypeId == -1L) {
|
||||
val eventType = EventType(0, birthdays, resources.getColor(R.color.default_birthdays_color))
|
||||
eventTypeId = dbHelper.insertEventType(eventType)
|
||||
val eventType = EventType(null, birthdays, resources.getColor(R.color.default_birthdays_color))
|
||||
eventTypeId = EventTypesHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
|
||||
}
|
||||
return eventTypeId
|
||||
}
|
||||
|
||||
private fun getAnniversariesEventTypeId(): Long {
|
||||
val anniversaries = getString(R.string.anniversaries)
|
||||
var eventTypeId = dbHelper.getEventTypeIdWithTitle(anniversaries)
|
||||
var eventTypeId = EventTypesHelper().getEventTypeIdWithTitle(applicationContext, anniversaries)
|
||||
if (eventTypeId == -1L) {
|
||||
val eventType = EventType(0, anniversaries, resources.getColor(R.color.default_anniversaries_color))
|
||||
eventTypeId = dbHelper.insertEventType(eventType)
|
||||
val eventType = EventType(null, anniversaries, resources.getColor(R.color.default_anniversaries_color))
|
||||
eventTypeId = EventTypesHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
|
||||
}
|
||||
return eventTypeId
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ class SettingsActivity : SimpleActivity() {
|
||||
if (eventTypes.filter { it.caldavCalendarId == 0 }.size == 1) {
|
||||
val eventType = eventTypes.first { it.caldavCalendarId == 0 }
|
||||
eventType.color = config.primaryColor
|
||||
dbHelper.updateEventType(eventType)
|
||||
EventTypesHelper().insertOrUpdateEventTypeSync(applicationContext, eventType)
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
@ -216,9 +216,9 @@ class SettingsActivity : SimpleActivity() {
|
||||
getSyncedCalDAVCalendars().forEach {
|
||||
val calendarTitle = it.getFullTitle()
|
||||
if (!existingEventTypeNames.contains(calendarTitle.toLowerCase())) {
|
||||
val eventType = EventType(0, it.displayName, it.color, it.id, it.displayName, it.accountName)
|
||||
val eventType = EventType(null, it.displayName, it.color, it.id, it.displayName, it.accountName)
|
||||
existingEventTypeNames.add(calendarTitle.toLowerCase())
|
||||
dbHelper.insertEventType(eventType)
|
||||
EventTypesHelper().insertOrUpdateEventType(this, eventType)
|
||||
}
|
||||
}
|
||||
CalDAVHandler(applicationContext).refreshCalendars(this) {}
|
||||
@ -227,7 +227,7 @@ class SettingsActivity : SimpleActivity() {
|
||||
val removedCalendarIds = oldCalendarIds.filter { !newCalendarIds.contains(it) }
|
||||
removedCalendarIds.forEach {
|
||||
CalDAVHandler(applicationContext).deleteCalDAVCalendarEvents(it.toLong())
|
||||
dbHelper.getEventTypeWithCalDAVCalendarId(it.toInt())?.apply {
|
||||
EventTypesHelper().getEventTypeWithCalDAVCalendarId(applicationContext, it.toInt())?.apply {
|
||||
dbHelper.deleteEventTypes(arrayListOf(this), true) {}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
package com.simplemobiletools.calendar.pro
|
||||
package com.simplemobiletools.calendar.pro.databases
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao
|
||||
@ -59,6 +60,7 @@ abstract class EventsDatabase : RoomDatabase() {
|
||||
val regularEvent = context.resources.getString(R.string.regular_event)
|
||||
val eventType = EventType(DBHelper.REGULAR_EVENT_TYPE_ID, regularEvent, context.config.primaryColor)
|
||||
db!!.EventTypesDao().insertOrUpdate(eventType)
|
||||
context.config.addDisplayEventType(DBHelper.REGULAR_EVENT_TYPE_ID.toString())
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ import android.widget.ImageView
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
@ -58,7 +58,7 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu
|
||||
}
|
||||
|
||||
private fun eventTypeConfirmed(title: String, dialog: AlertDialog) {
|
||||
val eventIdWithTitle = activity.dbHelper.getEventTypeIdWithTitle(title)
|
||||
val eventIdWithTitle = EventTypesHelper().getEventTypeIdWithTitle(activity, title)
|
||||
var isEventTypeTitleTaken = isNewEvent && eventIdWithTitle != -1L
|
||||
if (!isEventTypeTitleTaken) {
|
||||
isEventTypeTitleTaken = !isNewEvent && eventType!!.id != eventIdWithTitle && eventIdWithTitle != -1L
|
||||
@ -73,15 +73,12 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu
|
||||
}
|
||||
|
||||
eventType!!.title = title
|
||||
if (eventType!!.caldavCalendarId != 0)
|
||||
if (eventType!!.caldavCalendarId != 0) {
|
||||
eventType!!.caldavDisplayName = title
|
||||
|
||||
eventType!!.id = if (isNewEvent) {
|
||||
activity.dbHelper.insertEventType(eventType!!)
|
||||
} else {
|
||||
activity.dbHelper.updateEventType(eventType!!)
|
||||
}
|
||||
|
||||
eventType!!.id = EventTypesHelper().insertOrUpdateEventTypeSync(activity, eventType!!)
|
||||
|
||||
if (eventType!!.id != -1L) {
|
||||
activity.runOnUiThread {
|
||||
dialog.dismiss()
|
||||
|
@ -5,8 +5,9 @@ import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
|
||||
import com.simplemobiletools.calendar.pro.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
|
||||
import com.simplemobiletools.calendar.pro.helpers.IcsImporter
|
||||
import com.simplemobiletools.calendar.pro.helpers.IcsImporter.ImportResult.*
|
||||
import com.simplemobiletools.commons.extensions.setFillWithStroke
|
||||
@ -21,7 +22,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
|
||||
|
||||
init {
|
||||
Thread {
|
||||
if (activity.dbHelper.getEventType(config.lastUsedLocalEventTypeId) == null) {
|
||||
if (activity.eventTypesDB.getEventTypeWithId(config.lastUsedLocalEventTypeId) == null) {
|
||||
config.lastUsedLocalEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID
|
||||
}
|
||||
activity.runOnUiThread {
|
||||
@ -33,7 +34,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
|
||||
private fun initDialog() {
|
||||
val isLastCaldavCalendarOK = config.caldavSync && config.getSyncedCalendarIdsAsList().contains(config.lastUsedCaldavCalendarId.toString())
|
||||
currEventTypeId = if (isLastCaldavCalendarOK) {
|
||||
val lastUsedCalDAVCalendar = activity.dbHelper.getEventTypeWithCalDAVCalendarId(config.lastUsedCaldavCalendarId)
|
||||
val lastUsedCalDAVCalendar = EventTypesHelper().getEventTypeWithCalDAVCalendarId(activity, config.lastUsedCaldavCalendarId)
|
||||
if (lastUsedCalDAVCalendar != null) {
|
||||
currEventTypeCalDAVCalendarId = config.lastUsedCaldavCalendarId
|
||||
lastUsedCalDAVCalendar.id!!
|
||||
@ -79,7 +80,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
|
||||
|
||||
private fun updateEventType(view: ViewGroup) {
|
||||
Thread {
|
||||
val eventType = activity.dbHelper.getEventType(currEventTypeId)
|
||||
val eventType = activity.eventTypesDB.getEventTypeWithId(currEventTypeId)
|
||||
activity.runOnUiThread {
|
||||
view.import_event_type_title.text = eventType!!.getDisplayTitle()
|
||||
view.import_event_type_color.setFillWithStroke(eventType.color, activity.config.backgroundColor)
|
||||
|
@ -8,7 +8,7 @@ import android.widget.RadioGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
|
||||
import com.simplemobiletools.calendar.pro.helpers.STORED_LOCALLY_ONLY
|
||||
import com.simplemobiletools.calendar.pro.models.CalDAVCalendar
|
||||
import com.simplemobiletools.commons.extensions.setFillWithStroke
|
||||
@ -28,7 +28,7 @@ class SelectEventCalendarDialog(val activity: Activity, val calendars: List<CalD
|
||||
|
||||
Thread {
|
||||
calendars.forEach {
|
||||
val localEventType = activity.dbHelper.getEventTypeWithCalDAVCalendarId(it.id)
|
||||
val localEventType = EventTypesHelper().getEventTypeWithCalDAVCalendarId(activity, it.id)
|
||||
if (localEventType != null) {
|
||||
it.color = localEventType.color
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.app.AlarmManagerCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.simplemobiletools.calendar.pro.EventsDatabase
|
||||
import com.simplemobiletools.calendar.pro.databases.EventsDatabase
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.activities.EventActivity
|
||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||
|
@ -26,12 +26,12 @@ class CalDAVHandler(val context: Context) {
|
||||
fun refreshCalendars(activity: SimpleActivity? = null, callback: () -> Unit) {
|
||||
val calDAVCalendars = getCalDAVCalendars(activity, context.config.caldavSyncedCalendarIDs)
|
||||
for (calendar in calDAVCalendars) {
|
||||
val localEventType = context.dbHelper.getEventTypeWithCalDAVCalendarId(calendar.id) ?: continue
|
||||
val localEventType = EventTypesHelper().getEventTypeWithCalDAVCalendarId(context, calendar.id) ?: continue
|
||||
localEventType.apply {
|
||||
title = calendar.displayName
|
||||
caldavDisplayName = calendar.displayName
|
||||
caldavEmail = calendar.accountName
|
||||
context.dbHelper.updateLocalEventType(this)
|
||||
EventTypesHelper().insertOrUpdateEventTypeSync(context, this)
|
||||
}
|
||||
|
||||
CalDAVHandler(context).fetchCalDAVCalendarEvents(calendar.id, localEventType.id!!, activity)
|
||||
|
@ -8,7 +8,6 @@ import android.database.sqlite.SQLiteOpenHelper
|
||||
import android.database.sqlite.SQLiteQueryBuilder
|
||||
import android.text.TextUtils
|
||||
import androidx.collection.LongSparseArray
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||
import com.simplemobiletools.calendar.pro.extensions.*
|
||||
import com.simplemobiletools.calendar.pro.models.Event
|
||||
@ -91,7 +90,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
private fun createTypesTable(db: SQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE $TYPES_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TYPE_TITLE TEXT, $COL_TYPE_COLOR INTEGER, " +
|
||||
"$COL_TYPE_CALDAV_CALENDAR_ID INTEGER, $COL_TYPE_CALDAV_DISPLAY_NAME TEXT, $COL_TYPE_CALDAV_EMAIL TEXT)")
|
||||
addRegularEventType(db)
|
||||
}
|
||||
|
||||
private fun createExceptionsTable(db: SQLiteDatabase) {
|
||||
@ -99,14 +97,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
"$COL_OCCURRENCE_DAYCODE INTEGER)")
|
||||
}
|
||||
|
||||
private fun addRegularEventType(db: SQLiteDatabase) {
|
||||
Thread {
|
||||
val regularEvent = context.resources.getString(R.string.regular_event)
|
||||
val eventType = EventType(REGULAR_EVENT_TYPE_ID, regularEvent, context.config.primaryColor)
|
||||
addEventType(eventType, db)
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun insert(event: Event, addToCalDAV: Boolean, activity: SimpleActivity? = null, callback: (id: Long) -> Unit) {
|
||||
if (event.startTS > event.endTS) {
|
||||
callback(0)
|
||||
@ -211,45 +201,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
}
|
||||
}
|
||||
|
||||
private fun addEventType(eventType: EventType, db: SQLiteDatabase) = insertEventType(eventType, db)
|
||||
|
||||
fun insertEventType(eventType: EventType, db: SQLiteDatabase = mDb): Long {
|
||||
val values = fillEventTypeValues(eventType)
|
||||
val insertedId = db.insert(TYPES_TABLE_NAME, null, values)
|
||||
context.config.addDisplayEventType(insertedId.toString())
|
||||
return insertedId
|
||||
}
|
||||
|
||||
fun updateEventType(eventType: EventType): Long {
|
||||
if (eventType.caldavCalendarId != 0) {
|
||||
CalDAVHandler(context).updateCalDAVCalendar(eventType)
|
||||
}
|
||||
|
||||
return updateLocalEventType(eventType)
|
||||
}
|
||||
|
||||
fun updateLocalEventType(eventType: EventType): Long {
|
||||
val selectionArgs = arrayOf(eventType.id.toString())
|
||||
val values = fillEventTypeValues(eventType)
|
||||
val selection = "$COL_ID = ?"
|
||||
val updated = mDb.update(TYPES_TABLE_NAME, values, selection, selectionArgs)
|
||||
return if (updated > 0) {
|
||||
eventType.id!!
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
private fun fillEventTypeValues(eventType: EventType): ContentValues {
|
||||
return ContentValues().apply {
|
||||
put(COL_TYPE_TITLE, eventType.title)
|
||||
put(COL_TYPE_COLOR, eventType.color)
|
||||
put(COL_TYPE_CALDAV_CALENDAR_ID, eventType.caldavCalendarId)
|
||||
put(COL_TYPE_CALDAV_DISPLAY_NAME, eventType.caldavDisplayName)
|
||||
put(COL_TYPE_CALDAV_EMAIL, eventType.caldavEmail)
|
||||
}
|
||||
}
|
||||
|
||||
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (values: ContentValues) -> Unit) {
|
||||
val childEvent = getEventWithId(parentEventId)
|
||||
if (childEvent == null) {
|
||||
@ -288,59 +239,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
}
|
||||
}
|
||||
|
||||
fun getEventTypeIdWithTitle(title: String): Long {
|
||||
val cols = arrayOf(COL_ID)
|
||||
val selection = "$COL_TYPE_TITLE = ? COLLATE NOCASE"
|
||||
val selectionArgs = arrayOf(title)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(TYPES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
return cursor.getIntValue(COL_ID).toLong()
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
fun getEventTypeWithCalDAVCalendarId(calendarId: Int): EventType? {
|
||||
val cols = arrayOf(COL_ID)
|
||||
val selection = "$COL_TYPE_CALDAV_CALENDAR_ID = ?"
|
||||
val selectionArgs = arrayOf(calendarId.toString())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(TYPES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
return getEventType(cursor.getLongValue(COL_ID))
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun getEventType(id: Long): EventType? {
|
||||
val cols = arrayOf(COL_TYPE_TITLE, COL_TYPE_COLOR, COL_TYPE_CALDAV_CALENDAR_ID, COL_TYPE_CALDAV_DISPLAY_NAME, COL_TYPE_CALDAV_EMAIL)
|
||||
val selection = "$COL_ID = ?"
|
||||
val selectionArgs = arrayOf(id.toString())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(TYPES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val title = cursor.getStringValue(COL_TYPE_TITLE)
|
||||
val color = cursor.getIntValue(COL_TYPE_COLOR)
|
||||
val calendarId = cursor.getIntValue(COL_TYPE_CALDAV_CALENDAR_ID)
|
||||
val displayName = cursor.getStringValue(COL_TYPE_CALDAV_DISPLAY_NAME)
|
||||
val email = cursor.getStringValue(COL_TYPE_CALDAV_EMAIL)
|
||||
return EventType(id, title, color, calendarId, displayName, email)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun getBirthdays(): List<Event> {
|
||||
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = ?"
|
||||
val selectionArgs = arrayOf(SOURCE_CONTACT_BIRTHDAY)
|
||||
|
@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.pro.helpers
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
|
||||
@ -16,4 +17,27 @@ class EventTypesHelper {
|
||||
}
|
||||
|
||||
fun getEventTypesSync(context: Context) = context.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
|
||||
|
||||
fun insertOrUpdateEventType(activity: Activity, eventType: EventType, callback: ((newEventTypeId: Long) -> Unit)? = null) {
|
||||
Thread {
|
||||
val eventTypeId = insertOrUpdateEventTypeSync(activity, eventType)
|
||||
activity.runOnUiThread {
|
||||
callback?.invoke(eventTypeId)
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun insertOrUpdateEventTypeSync(context: Context, eventType: EventType): Long {
|
||||
if (eventType.id != null && eventType.id!! > 0 && eventType.caldavCalendarId != 0) {
|
||||
CalDAVHandler(context).updateCalDAVCalendar(eventType)
|
||||
}
|
||||
|
||||
val newId = context.eventTypesDB.insertOrUpdate(eventType)
|
||||
context.config.addDisplayEventType(newId.toString())
|
||||
return newId
|
||||
}
|
||||
|
||||
fun getEventTypeIdWithTitle(context: Context, title: String) = context.eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L
|
||||
|
||||
fun getEventTypeWithCalDAVCalendarId(context: Context, calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.pro.helpers
|
||||
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
|
||||
import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult.*
|
||||
import com.simplemobiletools.calendar.pro.models.Event
|
||||
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||
@ -41,8 +42,8 @@ class IcsExporter {
|
||||
event.title.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") }
|
||||
event.description.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$DESCRIPTION$it") }
|
||||
event.importId.let { if (it.isNotEmpty()) out.writeLn("$UID$it") }
|
||||
event.eventType.let { out.writeLn("$CATEGORY_COLOR${activity.dbHelper.getEventType(it)?.color}") }
|
||||
event.eventType.let { out.writeLn("$CATEGORIES${activity.dbHelper.getEventType(it)?.title}") }
|
||||
event.eventType.let { out.writeLn("$CATEGORY_COLOR${activity.eventTypesDB.getEventTypeWithId(it)?.color}") }
|
||||
event.eventType.let { out.writeLn("$CATEGORIES${activity.eventTypesDB.getEventTypeWithId(it)?.title}") }
|
||||
event.lastUpdated.let { out.writeLn("$LAST_MODIFIED:${Formatter.getExportedTime(it)}") }
|
||||
event.location.let { if (it.isNotEmpty()) out.writeLn("$LOCATION:$it") }
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.simplemobiletools.calendar.pro.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import com.simplemobiletools.calendar.pro.R
|
||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||
@ -20,6 +19,7 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
private var curStart = -1
|
||||
private var curEnd = -1
|
||||
private var curTitle = ""
|
||||
private var curLocation = ""
|
||||
private var curDescription = ""
|
||||
private var curImportId = ""
|
||||
private var curFlags = 0
|
||||
@ -30,7 +30,6 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
private var curRepeatRule = 0
|
||||
private var curEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID
|
||||
private var curLastModified = 0L
|
||||
private var curLocation = ""
|
||||
private var curCategoryColor = -2
|
||||
private var isNotificationDescription = false
|
||||
private var isProperReminderAction = false
|
||||
@ -108,7 +107,7 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
}
|
||||
} else if (line.startsWith(CATEGORIES) && !overrideFileEventTypes) {
|
||||
val categories = line.substring(CATEGORIES.length)
|
||||
tryAddCategories(categories, activity)
|
||||
tryAddCategories(categories)
|
||||
} else if (line.startsWith(LAST_MODIFIED)) {
|
||||
curLastModified = getTimestamp(line.substring(LAST_MODIFIED.length)) * 1000L
|
||||
} else if (line.startsWith(EXDATE)) {
|
||||
@ -217,18 +216,18 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun tryAddCategories(categories: String, context: Context) {
|
||||
private fun tryAddCategories(categories: String) {
|
||||
val eventTypeTitle = if (categories.contains(",")) {
|
||||
categories.split(",")[0]
|
||||
} else {
|
||||
categories
|
||||
}
|
||||
|
||||
val eventId = context.dbHelper.getEventTypeIdWithTitle(eventTypeTitle)
|
||||
val eventId = EventTypesHelper().getEventTypeIdWithTitle(activity, eventTypeTitle)
|
||||
curEventTypeId = if (eventId == -1L) {
|
||||
val newTypeColor = if (curCategoryColor == -2) context.resources.getColor(R.color.color_primary) else curCategoryColor
|
||||
val newTypeColor = if (curCategoryColor == -2) activity.resources.getColor(R.color.color_primary) else curCategoryColor
|
||||
val eventType = EventType(null, eventTypeTitle, newTypeColor)
|
||||
context.dbHelper.insertEventType(eventType)
|
||||
EventTypesHelper().insertOrUpdateEventTypeSync(activity, eventType)
|
||||
} else {
|
||||
eventId
|
||||
}
|
||||
@ -246,6 +245,7 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
curStart = -1
|
||||
curEnd = -1
|
||||
curTitle = ""
|
||||
curLocation = ""
|
||||
curDescription = ""
|
||||
curImportId = ""
|
||||
curFlags = 0
|
||||
@ -257,7 +257,6 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
curEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID
|
||||
curLastModified = 0L
|
||||
curCategoryColor = -2
|
||||
curLocation = ""
|
||||
isNotificationDescription = false
|
||||
isProperReminderAction = false
|
||||
curReminderTriggerMinutes = -1
|
||||
|
@ -11,6 +11,15 @@ interface EventTypesDao {
|
||||
@Query("SELECT * FROM event_types ORDER BY title ASC")
|
||||
fun getEventTypes(): List<EventType>
|
||||
|
||||
@Query("SELECT * FROM event_types WHERE id = :id")
|
||||
fun getEventTypeWithId(id: Long): EventType?
|
||||
|
||||
@Query("SELECT id FROM event_types WHERE title = :title COLLATE NOCASE")
|
||||
fun getEventTypeIdWithTitle(title: String): Long?
|
||||
|
||||
@Query("SELECT * FROM event_types WHERE caldav_calendar_id = :calendarId")
|
||||
fun getEventTypeWithCalDAVCalendarId(calendarId: Int): EventType?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertOrUpdate(eventType: EventType): Long
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user