moving some event type fetching functions into Room

This commit is contained in:
tibbi 2018-11-13 23:11:34 +01:00
parent b1a263d54f
commit 9640ca26e2
12 changed files with 80 additions and 100 deletions

View file

@ -111,7 +111,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
updateViewPager()
}
dbHelper.getEventTypes {
EventTypesHelper().getEventTypes(this) {
mShouldFilterBeVisible = it.size > 1 || config.displayEventTypes.isEmpty()
}

View file

@ -7,6 +7,7 @@ import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.adapters.ManageEventTypesAdapter
import com.simplemobiletools.calendar.pro.dialogs.EditEventTypeDialog
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
import com.simplemobiletools.calendar.pro.interfaces.DeleteEventTypesListener
import com.simplemobiletools.calendar.pro.models.EventType
import com.simplemobiletools.commons.extensions.toast
@ -30,13 +31,11 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener {
}
private fun getEventTypes() {
dbHelper.getEventTypes {
runOnUiThread {
val adapter = ManageEventTypesAdapter(this, it, this, manage_event_types_list) {
showEventTypeDialog(it as EventType)
}
manage_event_types_list.adapter = adapter
EventTypesHelper().getEventTypes(this) {
val adapter = ManageEventTypesAdapter(this, it, this, manage_event_types_list) {
showEventTypeDialog(it as EventType)
}
manage_event_types_list.adapter = adapter
}
}

View file

@ -92,13 +92,14 @@ class SettingsActivity : SimpleActivity() {
private fun checkPrimaryColor() {
if (config.primaryColor != mStoredPrimaryColor) {
dbHelper.getEventTypes {
if (it.filter { it.caldavCalendarId == 0 }.size == 1) {
val eventType = it.first { it.caldavCalendarId == 0 }
Thread {
val eventTypes = EventTypesHelper().getEventTypesSync(this)
if (eventTypes.filter { it.caldavCalendarId == 0 }.size == 1) {
val eventType = eventTypes.first { it.caldavCalendarId == 0 }
eventType.color = config.primaryColor
dbHelper.updateEventType(eventType)
}
}
}.start()
}
}
@ -211,7 +212,7 @@ class SettingsActivity : SimpleActivity() {
Thread {
if (newCalendarIds.isNotEmpty()) {
val existingEventTypeNames = dbHelper.getEventTypesSync().map { it.getDisplayTitle().toLowerCase() } as ArrayList<String>
val existingEventTypeNames = EventTypesHelper().getEventTypesSync(applicationContext).map { it.getDisplayTitle().toLowerCase() } as ArrayList<String>
getSyncedCalDAVCalendars().forEach {
val calendarTitle = it.getFullTitle()
if (!existingEventTypeNames.contains(calendarTitle.toLowerCase())) {

View file

@ -6,7 +6,7 @@ import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.adapters.FilterEventTypeAdapter
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.dialog_export_events.view.*
import java.io.File
@ -19,18 +19,16 @@ class ExportEventsDialog(val activity: SimpleActivity, val path: String, val cal
export_events_folder.text = activity.humanizePath(path)
export_events_filename.setText("${activity.getString(R.string.events)}_${activity.getCurrentFormattedDateTime()}")
activity.dbHelper.getEventTypes {
EventTypesHelper().getEventTypes(activity) {
val eventTypes = HashSet<String>()
it.mapTo(eventTypes) { it.id.toString() }
activity.runOnUiThread {
export_events_types_list.adapter = FilterEventTypeAdapter(activity, it, eventTypes)
if (it.size > 1) {
export_events_pick_types.beVisible()
export_events_types_list.adapter = FilterEventTypeAdapter(activity, it, eventTypes)
if (it.size > 1) {
export_events_pick_types.beVisible()
val margin = activity.resources.getDimension(R.dimen.normal_margin).toInt()
(export_events_checkbox.layoutParams as LinearLayout.LayoutParams).leftMargin = margin
}
val margin = activity.resources.getDimension(R.dimen.normal_margin).toInt()
(export_events_checkbox.layoutParams as LinearLayout.LayoutParams).leftMargin = margin
}
}
}

View file

@ -5,7 +5,7 @@ import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.adapters.FilterEventTypeAdapter
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_filter_event_types.view.*
@ -14,18 +14,16 @@ class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> U
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_event_types, null)
init {
activity.dbHelper.getEventTypes {
EventTypesHelper().getEventTypes(activity) {
val displayEventTypes = activity.config.displayEventTypes
activity.runOnUiThread {
view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it, displayEventTypes)
view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it, displayEventTypes)
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmEventTypes() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.filter_events_by_type)
}
}
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmEventTypes() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.filter_events_by_type)
}
}
}

View file

@ -26,7 +26,7 @@ class SelectEventCalendarDialog(val activity: Activity, val calendars: List<CalD
val view = activity.layoutInflater.inflate(R.layout.dialog_select_radio_group, null) as ViewGroup
radioGroup = view.dialog_radio_group
activity.dbHelper.getEventTypes {
Thread {
calendars.forEach {
val localEventType = activity.dbHelper.getEventTypeWithCalDAVCalendarId(it.id)
if (localEventType != null) {
@ -42,7 +42,7 @@ class SelectEventCalendarDialog(val activity: Activity, val calendars: List<CalD
wasInit = true
activity.updateTextColors(view.dialog_radio_holder)
}
}
}.start()
dialog = AlertDialog.Builder(activity)
.create().apply {

View file

@ -7,7 +7,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.models.EventType
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.setFillWithStroke
@ -31,7 +31,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Long, val
val view = activity.layoutInflater.inflate(R.layout.dialog_select_radio_group, null) as ViewGroup
radioGroup = view.dialog_radio_group
activity.dbHelper.getEventTypes {
EventTypesHelper().getEventTypes(activity) {
eventTypes = it
activity.runOnUiThread {
eventTypes.filter { showCalDAVCalendars || it.caldavCalendarId == 0 }.forEach {

View file

@ -17,7 +17,6 @@ import androidx.fragment.app.Fragment
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.EventActivity
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.extensions.seconds
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.helpers.Formatter
@ -58,16 +57,16 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private var allDayRows = ArrayList<HashSet<Int>>()
private var eventTypeColors = LongSparseArray<Int>()
lateinit var inflater: LayoutInflater
lateinit var mView: View
lateinit var mScrollView: MyScrollView
lateinit var mCalendar: WeeklyCalendarImpl
lateinit var mRes: Resources
lateinit var mConfig: Config
private lateinit var inflater: LayoutInflater
private lateinit var mView: View
private lateinit var mScrollView: MyScrollView
private lateinit var mCalendar: WeeklyCalendarImpl
private lateinit var mRes: Resources
private lateinit var mConfig: Config
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
context!!.dbHelper.getEventTypes {
EventTypesHelper().getEventTypes(activity!!) {
it.map { eventTypeColors.put(it.id!!, it.color) }
}

View file

@ -838,7 +838,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private fun fillEvents(cursor: Cursor?): List<Event> {
val eventTypeColors = LongSparseArray<Int>()
getEventTypesSync().forEach {
context.eventTypesDB.getEventTypes().forEach {
eventTypeColors.put(it.id!!, it.color)
}
@ -880,38 +880,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return events
}
fun getEventTypes(callback: (types: ArrayList<EventType>) -> Unit) {
Thread {
callback(getEventTypesSync())
}.start()
}
fun getEventTypesSync(): ArrayList<EventType> {
val eventTypes = ArrayList<EventType>()
val cols = arrayOf(COL_ID, COL_TYPE_TITLE, COL_TYPE_COLOR, COL_TYPE_CALDAV_CALENDAR_ID, COL_TYPE_CALDAV_DISPLAY_NAME, COL_TYPE_CALDAV_EMAIL)
var cursor: Cursor? = null
try {
cursor = mDb.query(TYPES_TABLE_NAME, cols, null, null, null, null, "$COL_TYPE_TITLE ASC")
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getLongValue(COL_ID)
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)
val eventType = EventType(id, title, color, calendarId, displayName, email)
eventTypes.add(eventType)
} while (cursor.moveToNext())
}
} catch (ignored: Exception) {
} finally {
cursor?.close()
}
return eventTypes
}
fun doEventTypesContainEvents(types: ArrayList<EventType>, callback: (contain: Boolean) -> Unit) {
Thread {
val args = TextUtils.join(", ", types.map { it.id })

View file

@ -0,0 +1,19 @@
package com.simplemobiletools.calendar.pro.helpers
import android.app.Activity
import android.content.Context
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
import com.simplemobiletools.calendar.pro.models.EventType
class EventTypesHelper {
fun getEventTypes(activity: Activity, callback: (notes: ArrayList<EventType>) -> Unit) {
Thread {
val eventTypes = activity.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
activity.runOnUiThread {
callback(eventTypes)
}
}.start()
}
fun getEventTypesSync(context: Context) = context.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
}

View file

@ -42,7 +42,7 @@ class IcsImporter(val activity: SimpleActivity) {
fun importEvents(path: String, defaultEventTypeId: Long, calDAVCalendarId: Int, overrideFileEventTypes: Boolean): ImportResult {
try {
val eventTypes = activity.dbHelper.getEventTypesSync()
val eventTypes = EventTypesHelper().getEventTypesSync(activity)
val existingEvents = activity.dbHelper.getEventsWithImportIds()
val eventsToInsert = ArrayList<Event>()
var prevLine = ""

View file

@ -82,33 +82,31 @@ class MonthlyCalendarImpl(val callback: MonthlyCalendar, val context: Context) {
// it works more often than not, dont touch
private fun markDaysWithEvents(days: ArrayList<DayMonthly>) {
context.dbHelper.getEventTypes {
val dayEvents = HashMap<String, ArrayList<Event>>()
mEvents.forEach {
val startDateTime = Formatter.getDateTimeFromTS(it.startTS)
val endDateTime = Formatter.getDateTimeFromTS(it.endTS)
val endCode = Formatter.getDayCodeFromDateTime(endDateTime)
val dayEvents = HashMap<String, ArrayList<Event>>()
mEvents.forEach {
val startDateTime = Formatter.getDateTimeFromTS(it.startTS)
val endDateTime = Formatter.getDateTimeFromTS(it.endTS)
val endCode = Formatter.getDayCodeFromDateTime(endDateTime)
var currDay = startDateTime
var dayCode = Formatter.getDayCodeFromDateTime(currDay)
var currDayEvents = dayEvents[dayCode] ?: ArrayList()
var currDay = startDateTime
var dayCode = Formatter.getDayCodeFromDateTime(currDay)
var currDayEvents = dayEvents[dayCode] ?: ArrayList()
currDayEvents.add(it)
dayEvents[dayCode] = currDayEvents
while (Formatter.getDayCodeFromDateTime(currDay) != endCode) {
currDay = currDay.plusDays(1)
dayCode = Formatter.getDayCodeFromDateTime(currDay)
currDayEvents = dayEvents[dayCode] ?: ArrayList()
currDayEvents.add(it)
dayEvents[dayCode] = currDayEvents
while (Formatter.getDayCodeFromDateTime(currDay) != endCode) {
currDay = currDay.plusDays(1)
dayCode = Formatter.getDayCodeFromDateTime(currDay)
currDayEvents = dayEvents[dayCode] ?: ArrayList()
currDayEvents.add(it)
dayEvents[dayCode] = currDayEvents
}
}
days.filter { dayEvents.keys.contains(it.code) }.forEach {
it.dayEvents = dayEvents[it.code]!!
}
callback.updateMonthlyCalendar(context, monthName, days, true, mTargetDate)
}
days.filter { dayEvents.keys.contains(it.code) }.forEach {
it.dayEvents = dayEvents[it.code]!!
}
callback.updateMonthlyCalendar(context, monthName, days, true, mTargetDate)
}
private fun isToday(targetDate: DateTime, curDayInMonth: Int): Boolean {