moving 2 more functions into Room

This commit is contained in:
tibbi 2018-11-14 20:37:01 +01:00
parent 467585e229
commit ea80900eea
4 changed files with 11 additions and 32 deletions

View File

@ -301,8 +301,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
if (uri.path.startsWith("/events")) { if (uri.path.startsWith("/events")) {
// intents like content://com.android.calendar/events/1756 // intents like content://com.android.calendar/events/1756
val eventId = uri.lastPathSegment val eventId = uri.lastPathSegment
val id = dbHelper.getEventIdWithLastImportId(eventId) val id = eventsDB.getEventIdWithLastImportId("%-$eventId")
if (id != 0L) { if (id != null) {
Intent(this, EventActivity::class.java).apply { Intent(this, EventActivity::class.java).apply {
putExtra(EVENT_ID, id) putExtra(EVENT_ID, id)
startActivity(this) startActivity(this)

View File

@ -9,10 +9,7 @@ import android.provider.CalendarContract
import android.provider.CalendarContract.Reminders import android.provider.CalendarContract.Reminders
import android.util.SparseIntArray import android.util.SparseIntArray
import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.extensions.refreshCalDAVCalendars
import com.simplemobiletools.calendar.pro.extensions.scheduleCalDAVSync
import com.simplemobiletools.calendar.pro.models.CalDAVCalendar import com.simplemobiletools.calendar.pro.models.CalDAVCalendar
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.EventType
@ -232,8 +229,8 @@ class CalDAVHandler(val context: Context) {
// if the event is an exception from another events repeat rule, find the original parent event // if the event is an exception from another events repeat rule, find the original parent event
if (originalInstanceTime != 0L) { if (originalInstanceTime != 0L) {
val parentImportId = "$source-$originalId" val parentImportId = "$source-$originalId"
val parentEventId = context.dbHelper.getEventIdWithImportId(parentImportId) val parentEventId = context.eventsDB.getEventIdWithImportId(parentImportId)
if (parentEventId != 0L) { if (parentEventId != null) {
event.parentId = parentEventId event.parentId = parentEventId
context.dbHelper.addEventRepeatException(parentEventId, (originalInstanceTime / 1000).toInt(), false, event.importId) context.dbHelper.addEventRepeatException(parentEventId, (originalInstanceTime / 1000).toInt(), false, event.importId)
} }

View File

@ -199,30 +199,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs)
} }
fun getEventIdWithImportId(id: String): Long {
val selection = "$MAIN_TABLE_NAME.$COL_IMPORT_ID = ?"
val selectionArgs = arrayOf(id)
val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor)
return if (events.isNotEmpty()) {
events.minBy { it.id!! }?.id ?: 0L
} else {
0L
}
}
fun getEventIdWithLastImportId(id: String): Long {
val selection = "$MAIN_TABLE_NAME.$COL_IMPORT_ID LIKE ?"
val selectionArgs = arrayOf("%-$id")
val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor)
return if (events.isNotEmpty()) {
events.minBy { it.id!! }?.id ?: 0L
} else {
0L
}
}
fun getEventsWithSearchQuery(text: String, callback: (searchedText: String, events: List<Event>) -> Unit) { fun getEventsWithSearchQuery(text: String, callback: (searchedText: String, events: List<Event>) -> Unit) {
Thread { Thread {
val searchQuery = "%$text%" val searchQuery = "%$text%"

View File

@ -13,6 +13,12 @@ interface EventsDao {
@Query("SELECT * FROM events WHERE id = :id") @Query("SELECT * FROM events WHERE id = :id")
fun getEventWithId(id: Long): Event? fun getEventWithId(id: Long): Event?
@Query("SELECT id FROM events WHERE import_id = :importId")
fun getEventIdWithImportId(importId: String): Long?
@Query("SELECT id FROM events WHERE import_id LIKE :importId")
fun getEventIdWithLastImportId(importId: String): Long?
@Query("SELECT * FROM events WHERE id IN (:ids)") @Query("SELECT * FROM events WHERE id IN (:ids)")
fun getEventsWithIds(ids: List<Long>): List<Event> fun getEventsWithIds(ids: List<Long>): List<Event>