handle generating events to export in room
This commit is contained in:
parent
69a23c0d5f
commit
6a36e4e1fd
7 changed files with 31 additions and 34 deletions
|
@ -699,11 +699,11 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||
FilePickerDialog(this, pickFile = false, showFAB = true) {
|
||||
ExportEventsDialog(this, it) { exportPastEvents, file, eventTypes ->
|
||||
Thread {
|
||||
val events = dbHelper.getEventsToExport(exportPastEvents).filter { eventTypes.contains(it.eventType.toString()) }
|
||||
val events = eventsHelper.getEventsToExport(exportPastEvents, eventTypes)
|
||||
if (events.isEmpty()) {
|
||||
toast(R.string.no_entries_for_exporting)
|
||||
} else {
|
||||
IcsExporter().exportEvents(this, file, events as ArrayList<Event>, true) {
|
||||
IcsExporter().exportEvents(this, file, events, true) {
|
||||
toast(when (it) {
|
||||
IcsExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
|
||||
IcsExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
|
||||
|
|
|
@ -34,7 +34,7 @@ class FilterEventTypeAdapter(val activity: SimpleActivity, val eventTypes: List<
|
|||
notifyItemChanged(pos)
|
||||
}
|
||||
|
||||
fun getSelectedItemsSet() = selectedKeys.asSequence().map { it.toString() }.toHashSet()
|
||||
fun getSelectedItemsList() = selectedKeys.asSequence().map { it }.toMutableList() as ArrayList<Long>
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view = activity.layoutInflater.inflate(R.layout.filter_event_type_view, parent, false)
|
||||
|
|
|
@ -12,7 +12,7 @@ import kotlinx.android.synthetic.main.dialog_export_events.view.*
|
|||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
class ExportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (exportPastEvents: Boolean, file: File, eventTypes: HashSet<String>) -> Unit) {
|
||||
class ExportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (exportPastEvents: Boolean, file: File, eventTypes: ArrayList<Long>) -> Unit) {
|
||||
|
||||
init {
|
||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_export_events, null) as ViewGroup).apply {
|
||||
|
@ -49,7 +49,7 @@ class ExportEventsDialog(val activity: SimpleActivity, val path: String, val cal
|
|||
return@setOnClickListener
|
||||
}
|
||||
|
||||
val eventTypes = (view.export_events_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsSet()
|
||||
val eventTypes = (view.export_events_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsList()
|
||||
callback(view.export_events_checkbox.isChecked, file, eventTypes)
|
||||
dismiss()
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> U
|
|||
}
|
||||
|
||||
private fun confirmEventTypes() {
|
||||
val selectedItems = (view.filter_event_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsSet()
|
||||
val selectedItems = (view.filter_event_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsList().map { it.toString() }.toHashSet()
|
||||
if (activity.config.displayEventTypes != selectedItems) {
|
||||
activity.config.displayEventTypes = selectedItems
|
||||
callback()
|
||||
|
|
|
@ -53,32 +53,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
|
||||
|
||||
fun getEventsToExport(includePast: Boolean): ArrayList<Event> {
|
||||
val currTime = getNowSeconds().toString()
|
||||
var events = ArrayList<Event>()
|
||||
|
||||
// non repeating events
|
||||
val cursor = if (includePast) {
|
||||
getEventsCursor()
|
||||
} else {
|
||||
val selection = "$COL_END_TS > ?"
|
||||
val selectionArgs = arrayOf(currTime)
|
||||
getEventsCursor(selection, selectionArgs)
|
||||
}
|
||||
events.addAll(fillEvents(cursor))
|
||||
|
||||
// repeating events
|
||||
/*if (!includePast) {
|
||||
val selection = "$COL_REPEAT_INTERVAL != 0 AND ($COL_REPEAT_LIMIT == 0 OR $COL_REPEAT_LIMIT > ?)"
|
||||
val selectionArgs = arrayOf(currTime)
|
||||
cursor = getEventsCursor(selection, selectionArgs)
|
||||
events.addAll(fillEvents(cursor))
|
||||
}*/
|
||||
|
||||
events = events.distinctBy { it.id } as ArrayList<Event>
|
||||
return events
|
||||
}
|
||||
|
||||
private fun getEventsCursor(selection: String = "", selectionArgs: Array<String>? = null): Cursor? {
|
||||
return mDb.query(MAIN_TABLE_NAME, allColumns, selection, selectionArgs, null, null, COL_START_TS)
|
||||
}
|
||||
|
|
|
@ -291,7 +291,7 @@ class EventsHelper(val context: Context) {
|
|||
}
|
||||
} else {
|
||||
if (eventId == -1L) {
|
||||
eventsDB.getRepeatableEventsFromTo(toTS).toMutableList() as ArrayList<Event>
|
||||
eventsDB.getRepeatableEventsFromToWithTypes(toTS).toMutableList() as ArrayList<Event>
|
||||
} else {
|
||||
eventsDB.getRepeatableEventFromToWithId(eventId, toTS).toMutableList() as ArrayList<Event>
|
||||
}
|
||||
|
@ -411,4 +411,18 @@ class EventsHelper(val context: Context) {
|
|||
events.addAll(getRepeatableEventsFor(ts, ts))
|
||||
return events
|
||||
}
|
||||
|
||||
fun getEventsToExport(includePast: Boolean, eventTypes: ArrayList<Long>): ArrayList<Event> {
|
||||
val currTS = getNowSeconds()
|
||||
var events = ArrayList<Event>()
|
||||
if (includePast) {
|
||||
events.addAll(eventsDB.getAllEventsWithTypes(eventTypes))
|
||||
} else {
|
||||
events.addAll(eventsDB.getOneTimeFutureEventsWithTypes(currTS, eventTypes))
|
||||
events.addAll(eventsDB.getRepeatableFutureEventsWithTypes(currTS, eventTypes))
|
||||
}
|
||||
|
||||
events = events.distinctBy { it.id } as ArrayList<Event>
|
||||
return events
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,9 @@ interface EventsDao {
|
|||
@Query("SELECT * FROM events")
|
||||
fun getAllEvents(): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE event_type IN (:eventTypeIds)")
|
||||
fun getAllEventsWithTypes(eventTypeIds: List<Long>): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE id = :id")
|
||||
fun getEventWithId(id: Long): Event?
|
||||
|
||||
|
@ -26,8 +29,11 @@ interface EventsDao {
|
|||
@Query("SELECT * FROM events WHERE start_ts <= :toTS AND end_ts >= :fromTS AND start_ts != 0 AND repeat_interval = 0 AND event_type IN (:eventTypeIds)")
|
||||
fun getOneTimeEventsFromToWithTypes(toTS: Long, fromTS: Long, eventTypeIds: List<Long>): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE end_ts > :toTS AND repeat_interval = 0 AND event_type IN (:eventTypeIds)")
|
||||
fun getOneTimeFutureEventsWithTypes(toTS: Long, eventTypeIds: List<Long>): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE start_ts <= :toTS AND repeat_interval != 0")
|
||||
fun getRepeatableEventsFromTo(toTS: Long): List<Event>
|
||||
fun getRepeatableEventsFromToWithTypes(toTS: Long): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE id = :id AND start_ts <= :toTS AND repeat_interval != 0")
|
||||
fun getRepeatableEventFromToWithId(id: Long, toTS: Long): List<Event>
|
||||
|
@ -35,6 +41,9 @@ interface EventsDao {
|
|||
@Query("SELECT * FROM events WHERE start_ts <= :toTS AND start_ts != 0 AND repeat_interval != 0 AND event_type IN (:eventTypeIds)")
|
||||
fun getRepeatableEventsFromToWithTypes(toTS: Long, eventTypeIds: List<Long>): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE repeat_interval != 0 AND (repeat_limit == 0 OR repeat_limit > :currTS) AND event_type IN (:eventTypeIds)")
|
||||
fun getRepeatableFutureEventsWithTypes(currTS: Long, eventTypeIds: List<Long>): List<Event>
|
||||
|
||||
@Query("SELECT * FROM events WHERE id IN (:ids) AND import_id != \"\"")
|
||||
fun getEventsByIdsWithImportIds(ids: List<Long>): List<Event>
|
||||
|
||||
|
|
Loading…
Reference in a new issue