Always export latest notes

This commit is contained in:
Naveen 2023-03-15 02:38:38 +05:30
parent 3cda2eb234
commit 36c4746829
2 changed files with 58 additions and 53 deletions

View File

@ -907,24 +907,29 @@ class MainActivity : SimpleActivity() {
private fun requestUnlockNotes(callback: (List<Long>) -> Unit) {
val lockedNotes = mNotes.filter { it.isLocked() }
if (lockedNotes.isNotEmpty()) {
UnlockNotesDialog(this, lockedNotes, callback)
runOnUiThread {
UnlockNotesDialog(this, lockedNotes, callback)
}
} else {
callback(emptyList())
}
}
private fun exportNotesTo(outputStream: OutputStream?) {
requestUnlockNotes { unlockedIds ->
toast(R.string.exporting)
ensureBackgroundThread {
val notesExporter = NotesExporter(this)
notesExporter.exportNotes(outputStream, unlockedIds) {
val toastId = when (it) {
NotesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
else -> R.string.exporting_failed
}
ensureBackgroundThread {
NotesHelper(this).getNotes {
mNotes = it
requestUnlockNotes { unlockedIds ->
toast(R.string.exporting)
val notesExporter = NotesExporter(this)
notesExporter.exportNotes(mNotes, unlockedIds, outputStream) { result ->
val toastId = when (result) {
NotesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
else -> R.string.exporting_failed
}
toast(toastId)
toast(toastId)
}
}
}
}
@ -1022,50 +1027,52 @@ class MainActivity : SimpleActivity() {
}
private fun exportAllNotesBelowQ() {
requestUnlockNotes { unlockedIds ->
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))
)
ensureBackgroundThread {
NotesHelper(this).getNotes { notes ->
mNotes = notes
requestUnlockNotes { unlockedIds ->
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) { any ->
val syncFile = any as Int == EXPORT_FILE_SYNC
var failCount = 0
NotesHelper(this).getNotes { notes ->
mNotes = notes
mNotes.filter { !it.isLocked() || it.id in unlockedIds }.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
val noteStoredValue = note.getNoteStoredValue(this) ?: ""
tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully ->
if (exportedSuccessfully) {
if (syncFile) {
note.path = file.absolutePath
note.value = ""
} else {
note.path = ""
note.value = noteStoredValue
RadioGroupDialog(this, items) { any ->
val syncFile = any as Int == EXPORT_FILE_SYNC
var failCount = 0
mNotes.filter { !it.isLocked() || it.id in unlockedIds }.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
val noteStoredValue = note.getNoteStoredValue(this) ?: ""
tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully ->
if (exportedSuccessfully) {
if (syncFile) {
note.path = file.absolutePath
note.value = ""
} else {
note.path = ""
note.value = noteStoredValue
}
NotesHelper(this).insertOrUpdateNote(note)
}
NotesHelper(this).insertOrUpdateNote(note)
}
if (mCurrentNote.id == note.id) {
mCurrentNote.value = note.value
mCurrentNote.path = note.path
getPagerAdapter().updateCurrentNoteData(view_pager.currentItem, mCurrentNote.path, mCurrentNote.value)
}
if (mCurrentNote.id == note.id) {
mCurrentNote.value = note.value
mCurrentNote.path = note.path
getPagerAdapter().updateCurrentNoteData(view_pager.currentItem, mCurrentNote.path, mCurrentNote.value)
}
if (!exportedSuccessfully) {
failCount++
}
if (!exportedSuccessfully) {
failCount++
}
if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
}
}
}
}

View File

@ -5,7 +5,6 @@ import com.google.gson.Gson
import com.google.gson.stream.JsonWriter
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.models.Note
import java.io.OutputStream
@ -16,7 +15,7 @@ class NotesExporter(private val context: Context) {
private val gson = Gson()
fun exportNotes(outputStream: OutputStream?, unlockedNoteIds: List<Long>, callback: (result: ExportResult) -> Unit) {
fun exportNotes(notes: List<Note>, unlockedNoteIds: List<Long>, outputStream: OutputStream?, callback: (result: ExportResult) -> Unit) {
ensureBackgroundThread {
if (outputStream == null) {
callback.invoke(ExportResult.EXPORT_FAIL)
@ -27,7 +26,6 @@ class NotesExporter(private val context: Context) {
try {
var written = 0
writer.beginArray()
val notes = context.notesDB.getNotes() as ArrayList<Note>
for (note in notes) {
if (!note.isLocked() || note.id in unlockedNoteIds) {
val noteToSave = getNoteToExport(note)