pass an OutputStream to exporter, not a file

This commit is contained in:
tibbi 2020-03-17 00:00:16 +01:00
parent 21232c339b
commit 655d3eedcc
3 changed files with 60 additions and 63 deletions

View File

@ -796,12 +796,14 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
if (events.isEmpty()) {
toast(R.string.no_entries_for_exporting)
} else {
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
else -> R.string.exporting_failed
})
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
})
}
}
}
}

View File

@ -8,9 +8,7 @@ import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.sharePathIntent
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.RadioItem
import java.io.File
@ -26,9 +24,11 @@ fun BaseSimpleActivity.shareEvents(ids: List<Long>) {
}
val events = eventsDB.getEventsWithIds(ids) as ArrayList<Event>
IcsExporter().exportEvents(this, file, events, false) {
if (it == IcsExporter.ExportResult.EXPORT_OK) {
sharePathIntent(file.absolutePath, BuildConfig.APPLICATION_ID)
getFileOutputStream(file.toFileDirItem(this), true) {
IcsExporter().exportEvents(this, it, events, false) {
if (it == IcsExporter.ExportResult.EXPORT_OK) {
sharePathIntent(file.absolutePath, BuildConfig.APPLICATION_ID)
}
}
}
}

View File

@ -7,13 +7,11 @@ import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult.*
import com.simplemobiletools.calendar.pro.models.CalDAVCalendar
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getFileOutputStream
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.writeLn
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import java.io.BufferedWriter
import java.io.File
import java.io.OutputStream
class IcsExporter {
enum class ExportResult {
@ -24,60 +22,57 @@ class IcsExporter {
private var eventsFailed = 0
private var calendars = ArrayList<CalDAVCalendar>()
fun exportEvents(activity: BaseSimpleActivity, file: File, events: ArrayList<Event>, showExportingToast: Boolean, callback: (result: ExportResult) -> Unit) {
val fileDirItem = FileDirItem(file.absolutePath, file.name)
activity.getFileOutputStream(fileDirItem, true) {
if (it == null) {
callback(EXPORT_FAIL)
return@getFileOutputStream
fun exportEvents(activity: BaseSimpleActivity, outputStream: OutputStream?, events: ArrayList<Event>, showExportingToast: Boolean, callback: (result: ExportResult) -> Unit) {
if (outputStream == null) {
callback(EXPORT_FAIL)
return
}
ensureBackgroundThread {
calendars = activity.calDAVHelper.getCalDAVCalendars("", false)
if (showExportingToast) {
activity.toast(R.string.exporting)
}
ensureBackgroundThread {
calendars = activity.calDAVHelper.getCalDAVCalendars("", false)
if (showExportingToast) {
activity.toast(R.string.exporting)
}
outputStream.bufferedWriter().use { out ->
out.writeLn(BEGIN_CALENDAR)
out.writeLn(CALENDAR_PRODID)
out.writeLn(CALENDAR_VERSION)
for (event in events) {
out.writeLn(BEGIN_EVENT)
event.title.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") }
event.description.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$DESCRIPTION$it") }
event.importId.let { if (it.isNotEmpty()) out.writeLn("$UID$it") }
event.eventType.let { out.writeLn("$CATEGORY_COLOR${activity.eventTypesDB.getEventTypeWithId(it)?.color}") }
event.eventType.let { out.writeLn("$CATEGORIES${activity.eventTypesDB.getEventTypeWithId(it)?.title}") }
event.lastUpdated.let { out.writeLn("$LAST_MODIFIED:${Formatter.getExportedTime(it)}") }
event.location.let { if (it.isNotEmpty()) out.writeLn("$LOCATION:$it") }
it.bufferedWriter().use { out ->
out.writeLn(BEGIN_CALENDAR)
out.writeLn(CALENDAR_PRODID)
out.writeLn(CALENDAR_VERSION)
for (event in events) {
out.writeLn(BEGIN_EVENT)
event.title.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$SUMMARY:$it") }
event.description.replace("\n", "\\n").let { if (it.isNotEmpty()) out.writeLn("$DESCRIPTION$it") }
event.importId.let { if (it.isNotEmpty()) out.writeLn("$UID$it") }
event.eventType.let { out.writeLn("$CATEGORY_COLOR${activity.eventTypesDB.getEventTypeWithId(it)?.color}") }
event.eventType.let { out.writeLn("$CATEGORIES${activity.eventTypesDB.getEventTypeWithId(it)?.title}") }
event.lastUpdated.let { out.writeLn("$LAST_MODIFIED:${Formatter.getExportedTime(it)}") }
event.location.let { if (it.isNotEmpty()) out.writeLn("$LOCATION:$it") }
if (event.getIsAllDay()) {
out.writeLn("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS)}")
out.writeLn("$DTEND;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.endTS + DAY)}")
} else {
event.startTS.let { out.writeLn("$DTSTART:${Formatter.getExportedTime(it * 1000L)}") }
event.endTS.let { out.writeLn("$DTEND:${Formatter.getExportedTime(it * 1000L)}") }
}
out.writeLn("$STATUS$CONFIRMED")
Parser().getRepeatCode(event).let { if (it.isNotEmpty()) out.writeLn("$RRULE$it") }
fillReminders(event, out)
fillIgnoredOccurrences(event, out)
eventsExported++
out.writeLn(END_EVENT)
if (event.getIsAllDay()) {
out.writeLn("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS)}")
out.writeLn("$DTEND;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.endTS + DAY)}")
} else {
event.startTS.let { out.writeLn("$DTSTART:${Formatter.getExportedTime(it * 1000L)}") }
event.endTS.let { out.writeLn("$DTEND:${Formatter.getExportedTime(it * 1000L)}") }
}
out.writeLn(END_CALENDAR)
}
callback(when {
eventsExported == 0 -> EXPORT_FAIL
eventsFailed > 0 -> EXPORT_PARTIAL
else -> EXPORT_OK
})
out.writeLn("$STATUS$CONFIRMED")
Parser().getRepeatCode(event).let { if (it.isNotEmpty()) out.writeLn("$RRULE$it") }
fillReminders(event, out)
fillIgnoredOccurrences(event, out)
eventsExported++
out.writeLn(END_EVENT)
}
out.writeLn(END_CALENDAR)
}
callback(when {
eventsExported == 0 -> EXPORT_FAIL
eventsFailed > 0 -> EXPORT_PARTIAL
else -> EXPORT_OK
})
}
}