fix some glitches related to notes synced to files

This commit is contained in:
tibbi 2019-03-08 22:56:35 +01:00
parent 853ee3143c
commit aa25f23c38
2 changed files with 45 additions and 22 deletions

View File

@ -479,19 +479,20 @@ class MainActivity : SimpleActivity() {
RadioGroupDialog(this, items) { RadioGroupDialog(this, items) {
val syncFile = it as Int == EXPORT_FILE_SYNC val syncFile = it as Int == EXPORT_FILE_SYNC
val storedValue = mCurrentNote.getNoteStoredValue() ?: ""
tryExportNoteValueToFile(exportPath, textToExport, true) { tryExportNoteValueToFile(exportPath, textToExport, true) {
if (syncFile) { if (syncFile) {
mCurrentNote.path = exportPath mCurrentNote.path = exportPath
if (mCurrentNote.getNoteStoredValue() == textToExport) {
mCurrentNote.value = "" mCurrentNote.value = ""
} else {
mCurrentNote.path = ""
mCurrentNote.value = storedValue
} }
NotesHelper(this).insertOrUpdateNote(mCurrentNote) NotesHelper(this).insertOrUpdateNote(mCurrentNote)
} }
} }
} }
}
private fun tryExportAllNotes() { private fun tryExportAllNotes() {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
@ -503,6 +504,12 @@ class MainActivity : SimpleActivity() {
private fun exportAllNotes() { private fun exportAllNotes() {
ExportFilesDialog(this, mNotes) { parent, extension -> ExportFilesDialog(this, mNotes) { parent, extension ->
val items = arrayListOf(
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),
RadioItem(EXPORT_FILE_NO_SYNC, getString(R.string.only_export_file_content)))
RadioGroupDialog(this, items) {
val syncFile = it as Int == EXPORT_FILE_SYNC
var failCount = 0 var failCount = 0
NotesHelper(this).getNotes { NotesHelper(this).getNotes {
mNotes = it mNotes = it
@ -512,7 +519,22 @@ class MainActivity : SimpleActivity() {
if (!filename.isAValidFilename()) { if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename))) toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else { } else {
val noteStoredValue = note.getNoteStoredValue() ?: ""
tryExportNoteValueToFile(file.absolutePath, note.value, false) { tryExportNoteValueToFile(file.absolutePath, note.value, false) {
if (syncFile) {
note.path = file.absolutePath
note.value = ""
} else {
note.path = ""
note.value = noteStoredValue
}
NotesHelper(this).insertOrUpdateNote(note)
if (mCurrentNote.id == note.id) {
mCurrentNote.value = note.value
mCurrentNote.path = note.path
}
if (!it) { if (!it) {
failCount++ failCount++
} }
@ -526,6 +548,7 @@ class MainActivity : SimpleActivity() {
} }
} }
} }
}
fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) { fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {

View File

@ -17,7 +17,7 @@ data class Note(
fun getNoteStoredValue(): String? { fun getNoteStoredValue(): String? {
return if (path.isNotEmpty()) { return if (path.isNotEmpty()) {
return try { try {
File(path).readText() File(path).readText()
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
null null