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) { private fun requestUnlockNotes(callback: (List<Long>) -> Unit) {
val lockedNotes = mNotes.filter { it.isLocked() } val lockedNotes = mNotes.filter { it.isLocked() }
if (lockedNotes.isNotEmpty()) { if (lockedNotes.isNotEmpty()) {
UnlockNotesDialog(this, lockedNotes, callback) runOnUiThread {
UnlockNotesDialog(this, lockedNotes, callback)
}
} else { } else {
callback(emptyList()) callback(emptyList())
} }
} }
private fun exportNotesTo(outputStream: OutputStream?) { private fun exportNotesTo(outputStream: OutputStream?) {
requestUnlockNotes { unlockedIds -> ensureBackgroundThread {
toast(R.string.exporting) NotesHelper(this).getNotes {
ensureBackgroundThread { mNotes = it
val notesExporter = NotesExporter(this) requestUnlockNotes { unlockedIds ->
notesExporter.exportNotes(outputStream, unlockedIds) { toast(R.string.exporting)
val toastId = when (it) { val notesExporter = NotesExporter(this)
NotesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful notesExporter.exportNotes(mNotes, unlockedIds, outputStream) { result ->
else -> R.string.exporting_failed 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() { private fun exportAllNotesBelowQ() {
requestUnlockNotes { unlockedIds -> ensureBackgroundThread {
ExportFilesDialog(this, mNotes) { parent, extension -> NotesHelper(this).getNotes { notes ->
val items = arrayListOf( mNotes = notes
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)), requestUnlockNotes { unlockedIds ->
RadioItem(EXPORT_FILE_NO_SYNC, getString(R.string.only_export_file_content)) 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 -> RadioGroupDialog(this, items) { any ->
val syncFile = any as Int == EXPORT_FILE_SYNC val syncFile = any as Int == EXPORT_FILE_SYNC
var failCount = 0 var failCount = 0
NotesHelper(this).getNotes { notes -> mNotes.filter { !it.isLocked() || it.id in unlockedIds }.forEachIndexed { index, note ->
mNotes = notes val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
mNotes.filter { !it.isLocked() || it.id in unlockedIds }.forEachIndexed { index, note -> val file = File(parent, filename)
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension" if (!filename.isAValidFilename()) {
val file = File(parent, filename) toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
if (!filename.isAValidFilename()) { } else {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename))) val noteStoredValue = note.getNoteStoredValue(this) ?: ""
} else { tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully ->
val noteStoredValue = note.getNoteStoredValue(this) ?: "" if (exportedSuccessfully) {
tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully -> if (syncFile) {
if (exportedSuccessfully) { note.path = file.absolutePath
if (syncFile) { note.value = ""
note.path = file.absolutePath } else {
note.value = "" note.path = ""
} else { note.value = noteStoredValue
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) { if (!exportedSuccessfully) {
mCurrentNote.value = note.value failCount++
mCurrentNote.path = note.path }
getPagerAdapter().updateCurrentNoteData(view_pager.currentItem, mCurrentNote.path, mCurrentNote.value)
}
if (!exportedSuccessfully) { if (index == mNotes.size - 1) {
failCount++ 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.google.gson.stream.JsonWriter
import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.commons.helpers.PROTECTION_NONE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Note
import java.io.OutputStream import java.io.OutputStream
@ -16,7 +15,7 @@ class NotesExporter(private val context: Context) {
private val gson = Gson() 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 { ensureBackgroundThread {
if (outputStream == null) { if (outputStream == null) {
callback.invoke(ExportResult.EXPORT_FAIL) callback.invoke(ExportResult.EXPORT_FAIL)
@ -27,7 +26,6 @@ class NotesExporter(private val context: Context) {
try { try {
var written = 0 var written = 0
writer.beginArray() writer.beginArray()
val notes = context.notesDB.getNotes() as ArrayList<Note>
for (note in notes) { for (note in notes) {
if (!note.isLocked() || note.id in unlockedNoteIds) { if (!note.isLocked() || note.id in unlockedNoteIds) {
val noteToSave = getNoteToExport(note) val noteToSave = getNoteToExport(note)