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 9f6ffa107..8f5b0735d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -279,28 +279,27 @@ class MainActivity : SimpleActivity(), NavigationListener { private fun exportEvents() { FilePickerDialog(this, pickFile = false) { val path = it - handleSAFDialog(File(path)) { - ExportEventsDialog(this, path) { - Thread({ - val events = dbHelper.getEventsToExport(it) - if (events.isEmpty()) { + ExportEventsDialog(this, path) { + Thread({ + val events = dbHelper.getEventsToExport(it) + if (events.isEmpty()) { + runOnUiThread { + toast(R.string.no_events_for_exporting) + } + } else { + val filename = "events_${System.currentTimeMillis() / 1000}.ics" + val file = File(path, filename) + IcsExporter().exportEvents(this, file, events) { runOnUiThread { - toast(R.string.no_events_for_exporting) - } - } else { - val filename = "events_${System.currentTimeMillis() / 1000}.ics" - val file = File(path, filename) - val result = IcsExporter().exportEvents(this, file, events) - runOnUiThread { - toast(when (result) { + toast(when (it) { IcsExporter.ExportResult.EXPORT_OK -> R.string.events_exported_successfully IcsExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_events_failed else -> R.string.exporting_events_failed }) } } - }).start() - } + } + }).start() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt index fcde8c374..370f7af43 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Activity.kt @@ -4,11 +4,12 @@ import android.app.Activity import android.content.Intent import android.support.v4.content.FileProvider import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.helpers.IcsExporter import com.simplemobiletools.commons.extensions.toast import java.io.File -fun Activity.shareEvents(ids: List) { +fun SimpleActivity.shareEvents(ids: List) { val file = getTempFile() if (file == null) { toast(R.string.unknown_error_occurred) @@ -16,21 +17,22 @@ fun Activity.shareEvents(ids: List) { } val events = dbHelper.getEventsWithIds(ids) - val result = IcsExporter().exportEvents(this, file, events) - if (result == IcsExporter.ExportResult.EXPORT_OK) { - val uri = FileProvider.getUriForFile(this, "com.simplemobiletools.calendar.fileprovider", file) - val shareTitle = resources.getString(R.string.share_via) - Intent().apply { - action = Intent.ACTION_SEND - addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - setDataAndType(uri, contentResolver.getType(uri)) - putExtra(Intent.EXTRA_STREAM, uri) - type = "text/calendar" + IcsExporter().exportEvents(this, file, events) { + if (it == IcsExporter.ExportResult.EXPORT_OK) { + val uri = FileProvider.getUriForFile(this, "com.simplemobiletools.calendar.fileprovider", file) + val shareTitle = resources.getString(R.string.share_via) + Intent().apply { + action = Intent.ACTION_SEND + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + setDataAndType(uri, contentResolver.getType(uri)) + putExtra(Intent.EXTRA_STREAM, uri) + type = "text/calendar" - if (resolveActivity(packageManager) != null) { - startActivity(Intent.createChooser(this, shareTitle)) - } else { - toast(R.string.no_app_for_ics) + if (resolveActivity(packageManager) != null) { + startActivity(Intent.createChooser(this, shareTitle)) + } else { + toast(R.string.no_app_for_ics) + } } } } 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 af53f772b..d84d2a60d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/IcsExporter.kt @@ -1,10 +1,11 @@ package com.simplemobiletools.calendar.helpers -import android.content.Context +import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.writeLn import com.simplemobiletools.calendar.helpers.IcsExporter.ExportResult.* import com.simplemobiletools.calendar.models.Event +import com.simplemobiletools.commons.extensions.getFileOutputStream import java.io.BufferedWriter import java.io.File @@ -16,48 +17,44 @@ class IcsExporter { var eventsExported = 0 var eventsFailed = 0 - fun exportEvents(context: Context, file: File, events: ArrayList): ExportResult { - try { - file.createNewFile() - } catch (e: Exception) { - return EXPORT_FAIL - } + fun exportEvents(activity: SimpleActivity, file: File, events: ArrayList, callback: (result: ExportResult) -> Unit) { + activity.getFileOutputStream(file) { + it.bufferedWriter().use { out -> + out.writeLn(BEGIN_CALENDAR) + for (event in events) { + out.writeLn(BEGIN_EVENT) - file.bufferedWriter().use { out -> - out.writeLn(BEGIN_CALENDAR) - for (event in events) { - out.writeLn(BEGIN_EVENT) + event.title.let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") } + event.description.let { if (it.isNotEmpty()) out.writeLn("$DESCRIPTION$it") } + event.importId?.let { if (it.isNotEmpty()) out.writeLn("$UID$it") } + event.eventType.let { out.writeLn("$CATEGORIES${activity.dbHelper.getEventType(it)?.title}") } - event.title.let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") } - event.description.let { if (it.isNotEmpty()) out.writeLn("$DESCRIPTION$it") } - event.importId?.let { if (it.isNotEmpty()) out.writeLn("$UID$it") } - event.eventType.let { out.writeLn("$CATEGORIES${context.dbHelper.getEventType(it)?.title}") } + if (event.isAllDay) { + out.writeLn("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS)}") + } else { + event.startTS.let { out.writeLn("$DTSTART:${Formatter.getExportedTime(it)}") } + event.endTS.let { out.writeLn("$DTEND:${Formatter.getExportedTime(it)}") } + } - if (event.isAllDay) { - out.writeLn("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS)}") - } else { - event.startTS.let { out.writeLn("$DTSTART:${Formatter.getExportedTime(it)}") } - event.endTS.let { out.writeLn("$DTEND:${Formatter.getExportedTime(it)}") } + out.writeLn("$STATUS$CONFIRMED") + + fillRepeatInterval(event, out) + fillReminders(event, out) + fillIgnoredOccurrences(event, out) + + eventsExported++ + out.writeLn(END_EVENT) } - - out.writeLn("$STATUS$CONFIRMED") - - fillRepeatInterval(event, out) - fillReminders(event, out) - fillIgnoredOccurrences(event, out) - - eventsExported++ - out.writeLn(END_EVENT) + out.writeLn(END_CALENDAR) } - out.writeLn(END_CALENDAR) - } - return if (eventsExported == 0) { - EXPORT_FAIL - } else if (eventsFailed > 0) { - EXPORT_PARTIAL - } else { - EXPORT_OK + callback(if (eventsExported == 0) { + EXPORT_FAIL + } else if (eventsFailed > 0) { + EXPORT_PARTIAL + } else { + EXPORT_OK + }) } }