handle the SAF dialog when opening a note from SD card with sync

This commit is contained in:
tibbi 2017-03-18 23:19:05 +01:00
parent a25f826c5a
commit 255a5ade89

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.notes.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.ViewGroup
@ -9,18 +8,21 @@ import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.R.id.open_file_update_file
import com.simplemobiletools.notes.activities.SimpleActivity
import com.simplemobiletools.notes.helpers.TYPE_NOTE
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.dialog_open_file.view.*
import java.io.File
class OpenFileDialog(val activity: Activity, val path: String, val callback: (note: Note) -> Unit) : AlertDialog.Builder(activity) {
class OpenFileDialog(val activity: SimpleActivity, val path: String, val callback: (note: Note) -> Unit) : AlertDialog.Builder(activity) {
private var dialog: AlertDialog
init {
val view = (LayoutInflater.from(activity).inflate(R.layout.dialog_open_file, null) as ViewGroup).apply {
open_file_filename.text = activity.humanizePath(path)
}
AlertDialog.Builder(activity)
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
@ -30,11 +32,21 @@ class OpenFileDialog(val activity: Activity, val path: String, val callback: (no
val storePath = if (updateFileOnEdit) path else ""
val storeContent = if (updateFileOnEdit) "" else File(path).readText()
val filename = path.getFilenameFromPath()
val note = Note(0, filename, storeContent, TYPE_NOTE, storePath)
callback.invoke(note)
dismiss()
if (updateFileOnEdit) {
activity.handleSAFDialog(File(path)) {
saveNote(storeContent, storePath)
}
} else {
saveNote(storeContent, storePath)
}
})
}
}
private fun saveNote(storeContent: String, storePath: String) {
val filename = path.getFilenameFromPath()
val note = Note(0, filename, storeContent, TYPE_NOTE, storePath)
callback.invoke(note)
dialog.dismiss()
}
}