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