From a9a2c2229276c52528b54ddfea1f3b8f23ec2bb7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 17 Mar 2020 17:00:42 +0100 Subject: [PATCH] tweaking event exporting a bit --- .../calendar/pro/activities/MainActivity.kt | 33 ++++++++++--------- .../pro/dialogs/ExportEventsDialog.kt | 13 +++++--- .../calendar/pro/helpers/EventsHelper.kt | 4 +-- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 6c0c9d3ee..177e36ffe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -48,6 +48,7 @@ import kotlinx.android.synthetic.main.activity_main.* import org.joda.time.DateTime import org.joda.time.DateTimeZone import java.io.FileOutputStream +import java.io.OutputStream import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList @@ -784,27 +785,29 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { ExportEventsDialog(this, config.lastExportPath, false) { file, eventTypes -> - ensureBackgroundThread { - val events = eventsHelper.getEventsToExport(config.exportPastEvents, eventTypes) - if (events.isEmpty()) { - toast(R.string.no_entries_for_exporting) - } else { - getFileOutputStream(file.toFileDirItem(this), true) { - IcsExporter().exportEvents(this, it, events, true) { - toast(when (it) { - IcsExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful - IcsExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed - else -> R.string.exporting_failed - }) - } - } - } + getFileOutputStream(file.toFileDirItem(this), true) { + exportEventsTo(eventTypes, it) } } } } } + private fun exportEventsTo(eventTypes: ArrayList, outputStream: OutputStream?) { + val events = eventsHelper.getEventsToExport(eventTypes) + if (events.isEmpty()) { + toast(R.string.no_entries_for_exporting) + } else { + IcsExporter().exportEvents(this, outputStream, events, true) { + toast(when (it) { + IcsExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful + IcsExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed + else -> R.string.exporting_failed + }) + } + } + } + private fun launchSettings() { startActivity(Intent(applicationContext, SettingsActivity::class.java)) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt index 495c0a735..c71559030 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt @@ -10,6 +10,7 @@ import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.eventsHelper import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import kotlinx.android.synthetic.main.dialog_export_events.view.* import java.io.File import java.util.* @@ -68,12 +69,14 @@ class ExportEventsDialog(val activity: SimpleActivity, val path: String, val hid return@setOnClickListener } - config.lastExportPath = file.absolutePath.getParentPath() - config.exportPastEvents = view.export_events_checkbox.isChecked + ensureBackgroundThread { + config.lastExportPath = file.absolutePath.getParentPath() + config.exportPastEvents = view.export_events_checkbox.isChecked - val eventTypes = (view.export_events_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsList() - callback(file, eventTypes) - dismiss() + val eventTypes = (view.export_events_types_list.adapter as FilterEventTypeAdapter).getSelectedItemsList() + callback(file, eventTypes) + dismiss() + } } else -> activity.toast(R.string.invalid_name) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index e09774346..87b21ef2b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -401,10 +401,10 @@ class EventsHelper(val context: Context) { return events } - fun getEventsToExport(includePast: Boolean, eventTypes: ArrayList): ArrayList { + fun getEventsToExport(eventTypes: ArrayList): ArrayList { val currTS = getNowSeconds() var events = ArrayList() - if (includePast) { + if (config.exportPastEvents) { events.addAll(eventsDB.getAllEventsWithTypes(eventTypes)) } else { events.addAll(eventsDB.getOneTimeFutureEventsWithTypes(currTS, eventTypes))