mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-03-12 09:20:12 +01:00
handle the SAF dialog at the IcsExporter
This commit is contained in:
parent
5dff7a7140
commit
4264156d3f
@ -279,7 +279,6 @@ 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)
|
||||
@ -290,17 +289,17 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
} else {
|
||||
val filename = "events_${System.currentTimeMillis() / 1000}.ics"
|
||||
val file = File(path, filename)
|
||||
val result = IcsExporter().exportEvents(this, file, events)
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Int>) {
|
||||
fun SimpleActivity.shareEvents(ids: List<Int>) {
|
||||
val file = getTempFile()
|
||||
if (file == null) {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
@ -16,8 +17,8 @@ fun Activity.shareEvents(ids: List<Int>) {
|
||||
}
|
||||
|
||||
val events = dbHelper.getEventsWithIds(ids)
|
||||
val result = IcsExporter().exportEvents(this, file, events)
|
||||
if (result == IcsExporter.ExportResult.EXPORT_OK) {
|
||||
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 {
|
||||
@ -35,6 +36,7 @@ fun Activity.shareEvents(ids: List<Int>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Activity.getTempFile(): File? {
|
||||
val folder = File(cacheDir, "events")
|
||||
|
@ -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,14 +17,9 @@ class IcsExporter {
|
||||
var eventsExported = 0
|
||||
var eventsFailed = 0
|
||||
|
||||
fun exportEvents(context: Context, file: File, events: ArrayList<Event>): ExportResult {
|
||||
try {
|
||||
file.createNewFile()
|
||||
} catch (e: Exception) {
|
||||
return EXPORT_FAIL
|
||||
}
|
||||
|
||||
file.bufferedWriter().use { out ->
|
||||
fun exportEvents(activity: SimpleActivity, file: File, events: ArrayList<Event>, callback: (result: ExportResult) -> Unit) {
|
||||
activity.getFileOutputStream(file) {
|
||||
it.bufferedWriter().use { out ->
|
||||
out.writeLn(BEGIN_CALENDAR)
|
||||
for (event in events) {
|
||||
out.writeLn(BEGIN_EVENT)
|
||||
@ -31,7 +27,7 @@ class IcsExporter {
|
||||
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}") }
|
||||
event.eventType.let { out.writeLn("$CATEGORIES${activity.dbHelper.getEventType(it)?.title}") }
|
||||
|
||||
if (event.isAllDay) {
|
||||
out.writeLn("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS)}")
|
||||
@ -52,12 +48,13 @@ class IcsExporter {
|
||||
out.writeLn(END_CALENDAR)
|
||||
}
|
||||
|
||||
return if (eventsExported == 0) {
|
||||
callback(if (eventsExported == 0) {
|
||||
EXPORT_FAIL
|
||||
} else if (eventsFailed > 0) {
|
||||
EXPORT_PARTIAL
|
||||
} else {
|
||||
EXPORT_OK
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user