allow changing the filename of the file where events will be exported

This commit is contained in:
tibbi 2017-05-17 21:56:12 +02:00
parent 749f7f1fb0
commit 4efacece63
2 changed files with 24 additions and 9 deletions

View File

@ -39,7 +39,6 @@ import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
import java.io.File
import java.io.FileOutputStream
import java.util.*
@ -309,16 +308,14 @@ class MainActivity : SimpleActivity(), NavigationListener {
private fun exportEvents() {
FilePickerDialog(this, pickFile = false) {
val path = it
ExportEventsDialog(this, path) {
ExportEventsDialog(this, path) { exportPastEvents, file ->
Thread({
val events = dbHelper.getEventsToExport(it)
val events = dbHelper.getEventsToExport(exportPastEvents)
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(when (it) {

View File

@ -4,21 +4,39 @@ import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import com.simplemobiletools.calendar.R
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.dialog_export_events.view.*
import java.io.File
class ExportEventsDialog(val activity: Activity, val path: String, val callback: (exportPastEvents: Boolean) -> Unit) : AlertDialog.Builder(activity) {
class ExportEventsDialog(val activity: Activity, val path: String, val callback: (exportPastEvents: Boolean, file: File) -> Unit) : AlertDialog.Builder(activity) {
init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_export_events, null) as ViewGroup).apply {
export_events_folder.text = activity.humanizePath(path)
export_events_filename.setText("events_${System.currentTimeMillis() / 1000}")
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialog, which -> callback(view.export_events_checkbox.isChecked) })
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.export_events)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val filename = view.export_events_filename.value
if (filename.isEmpty()) {
context.toast(R.string.empty_name)
} else if (filename.isAValidFilename()) {
val file = File(path, "$filename.ics")
if (file.exists()) {
context.toast(R.string.name_taken)
return@setOnClickListener
}
callback(view.export_events_checkbox.isChecked, file)
dismiss()
} else {
context.toast(R.string.invalid_name)
}
})
}
}
}