From f753bdd61323c0c870fa47bba573703f2ddc0099 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 3 Apr 2017 20:21:43 +0200 Subject: [PATCH] create the selector selecting events for exporting --- .../calendar/activities/MainActivity.kt | 3 ++- .../calendar/helpers/DBHelper.kt | 18 +++++++++++++++++- .../calendar/helpers/IcsExporter.kt | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 50d601e0f..d79fe9151 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -263,7 +263,8 @@ class MainActivity : SimpleActivity(), NavigationListener { val path = it ExportEventsDialog(this, path) { Thread({ - val result = IcsExporter().exportEvents(this, path) + val events = dbHelper.getEventsToExport(it) + val result = IcsExporter().exportEvents(this, path, events) runOnUiThread { toast(when (result) { IcsExporter.ExportResult.EXPORT_OK -> R.string.events_exported_successfully diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 9c045914e..086e47c06 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -435,7 +435,23 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return fillEvents(cursor) } - private fun getEventsCursor(selection: String, selectionArgs: Array?): Cursor? { + fun getEventsToExport(includePast: Boolean): ArrayList { + val events = ArrayList() + + val cursor = if (includePast) { + getEventsCursor() + } else { + val endTime = (System.currentTimeMillis() / 1000).toString() + val selection = "$COL_END_TS > ?" + val selectionArgs = arrayOf(endTime) + getEventsCursor(selection, selectionArgs) + } + + events.addAll(fillEvents(cursor)) + return events + } + + private fun getEventsCursor(selection: String = "", selectionArgs: Array? = null): Cursor? { val builder = SQLiteQueryBuilder() builder.tables = "$MAIN_TABLE_NAME LEFT OUTER JOIN $META_TABLE_NAME ON $COL_EVENT_ID = $MAIN_TABLE_NAME.$COL_ID" val projection = allColumns diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt index fb9bd2e82..7e4a0f6a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.helpers import android.content.Context import com.simplemobiletools.calendar.extensions.writeLn import com.simplemobiletools.calendar.helpers.IcsExporter.ExportResult.* +import com.simplemobiletools.calendar.models.Event import java.io.File class IcsExporter { @@ -13,7 +14,7 @@ class IcsExporter { var eventsExported = 0 var eventsFailed = 0 - fun exportEvents(context: Context, path: String): ExportResult { + fun exportEvents(context: Context, path: String, events: ArrayList): ExportResult { File(path, "events_${System.currentTimeMillis() / 1000}.ics").bufferedWriter().use { out -> out.writeLn(BEGIN_CALENDAR) out.writeLn(END_CALENDAR)