From e965c6ec37a07c7d5a6f01e0dc8730e94d7b2744 Mon Sep 17 00:00:00 2001 From: Agnieszka C <85929121+Aga-C@users.noreply.github.com> Date: Thu, 24 Feb 2022 19:53:28 +0100 Subject: [PATCH] Added synchronization to checklist export and import --- .../notes/pro/activities/MainActivity.kt | 16 ++++++------ .../notes/pro/adapters/NotesPagerAdapter.kt | 2 +- .../notes/pro/fragments/ChecklistFragment.kt | 20 ++++++++++++--- .../notes/pro/fragments/NoteFragment.kt | 23 +++++++++++++++++ .../notes/pro/fragments/TextFragment.kt | 25 +------------------ 5 files changed, 50 insertions(+), 36 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index 9e61b798..7dc141af 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -643,7 +643,11 @@ class MainActivity : SimpleActivity() { if (checklistItems != null) { val title = it.absolutePath.getFilenameFromPath().substringBeforeLast('.') val note = Note(null, title, fileText, NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "") - displayNewNoteDialog(note.value, title = title, setChecklistAsDefault = true) + runOnUiThread { + OpenFileDialog(this, it.path) { + displayNewNoteDialog(note.value, title = it.title, it.path, setChecklistAsDefault = true) + } + } } else { runOnUiThread { OpenFileDialog(this, it.path) { @@ -740,11 +744,9 @@ class MainActivity : SimpleActivity() { } } - if (checklistItems != null) { - val note = Note(null, noteTitle, content, NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "") - displayNewNoteDialog(note.value, title = noteTitle, setChecklistAsDefault = true) - } else if (!canSyncNoteWithFile) { - val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") + val noteType = if (checklistItems != null) NoteType.TYPE_CHECKLIST.value else NoteType.TYPE_TEXT.value + if (!canSyncNoteWithFile) { + val note = Note(null, noteTitle, content, noteType, "", PROTECTION_NONE, "") displayNewNoteDialog(note.value, title = noteTitle, "") } else { val items = arrayListOf( @@ -755,7 +757,7 @@ class MainActivity : SimpleActivity() { RadioGroupDialog(this, items) { val syncFile = it as Int == IMPORT_FILE_SYNC val path = if (syncFile) uri.toString() else "" - val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") + val note = Note(null, noteTitle, content, noteType, "", PROTECTION_NONE, "") displayNewNoteDialog(note.value, title = noteTitle, path) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt index 6f4e78bf..7964037f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt @@ -39,7 +39,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity override fun getPageTitle(position: Int) = notes[position].title fun updateCurrentNoteData(position: Int, path: String, value: String) { - (fragments[position] as? TextFragment)?.apply { + (fragments[position])?.apply { updateNotePath(path) updateNoteValue(value) } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt index 236723ba..9a0a58e3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt @@ -14,7 +14,6 @@ import com.simplemobiletools.notes.pro.activities.SimpleActivity import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter import com.simplemobiletools.notes.pro.dialogs.NewChecklistItemDialog import com.simplemobiletools.notes.pro.extensions.config -import com.simplemobiletools.notes.pro.extensions.notesDB import com.simplemobiletools.notes.pro.extensions.updateWidgets import com.simplemobiletools.notes.pro.helpers.NOTE_ID import com.simplemobiletools.notes.pro.helpers.NotesHelper @@ -22,6 +21,7 @@ import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener import com.simplemobiletools.notes.pro.models.ChecklistItem import com.simplemobiletools.notes.pro.models.Note import kotlinx.android.synthetic.main.fragment_checklist.view.* +import java.io.File class ChecklistFragment : NoteFragment(), ChecklistItemsListener { @@ -58,7 +58,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { try { val checklistItemType = object : TypeToken>() {}.type - items = Gson().fromJson>(storedNote.value, checklistItemType) ?: ArrayList(1) + items = Gson().fromJson>(storedNote.getNoteStoredValue(activity!!), checklistItemType) ?: ArrayList(1) // checklist title can be null only because of the glitch in upgrade to 6.6.0, remove this check in the future items = items.filter { it.title != null }.toMutableList() as ArrayList @@ -78,7 +78,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { private fun migrateCheckListOnFailure(note: Note) { items.clear() - note.value.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value -> + note.getNoteStoredValue(activity!!)?.split("\n")?.map { it.trim() }?.filter { it.isNotBlank() }?.forEachIndexed { index, value -> items.add( ChecklistItem( id = index, @@ -180,6 +180,18 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } private fun saveNote(refreshIndex: Int = -1) { + if (note == null) { + return + } + + if (note!!.path.isNotEmpty() && !note!!.path.startsWith("content://") && !File(note!!.path).exists()) { + return + } + + if (context == null || activity == null) { + return + } + ensureBackgroundThread { context?.let { ctx -> note?.let { currentNote -> @@ -190,7 +202,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } currentNote.value = checklistItems - ctx.notesDB.insertOrUpdate(currentNote) + saveNoteValue(note!!, currentNote.value) ctx.updateWidgets() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt index a266935c..0ed438ec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt @@ -8,8 +8,10 @@ import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.performSecurityCheck import com.simplemobiletools.commons.helpers.PROTECTION_NONE +import com.simplemobiletools.notes.pro.activities.MainActivity import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.getPercentageFontSize +import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.models.Note import kotlinx.android.synthetic.main.fragment_checklist.view.* @@ -33,6 +35,19 @@ abstract class NoteFragment : Fragment() { } } + protected fun saveNoteValue(note: Note, content: String?) { + if (note.path.isEmpty()) { + NotesHelper(activity!!).insertOrUpdateNote(note) { + (activity as? MainActivity)?.noteSavedSuccessfully(note.title) + } + } else { + if (content != null) { + val displaySuccess = activity?.config?.displaySuccess ?: false + (activity as? MainActivity)?.tryExportNoteValueToFile(note.path, content, displaySuccess) + } + } + } + fun handleUnlocking(callback: (() -> Unit)? = null) { if (callback != null && (note!!.protectionType == PROTECTION_NONE || shouldShowLockedContent)) { callback() @@ -50,5 +65,13 @@ abstract class NoteFragment : Fragment() { ) } + fun updateNoteValue(value: String) { + note?.value = value + } + + fun updateNotePath(path: String) { + note?.path = path + } + abstract fun checkLockState() } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt index a53b293d..db5cc3f3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt @@ -23,7 +23,6 @@ import com.simplemobiletools.notes.pro.extensions.updateWidgets import com.simplemobiletools.notes.pro.helpers.MyMovementMethod import com.simplemobiletools.notes.pro.helpers.NOTE_ID import com.simplemobiletools.notes.pro.helpers.NotesHelper -import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.TextHistory import com.simplemobiletools.notes.pro.models.TextHistoryItem import kotlinx.android.synthetic.main.fragment_text.view.* @@ -187,14 +186,6 @@ class TextFragment : NoteFragment() { } } - fun updateNoteValue(value: String) { - note?.value = value - } - - fun updateNotePath(path: String) { - note?.path = path - } - fun getNotesView() = view.text_note_view fun saveText(force: Boolean) { @@ -214,7 +205,7 @@ class TextFragment : NoteFragment() { val oldText = note!!.getNoteStoredValue(context!!) if (newText != null && (newText != oldText || force)) { note!!.value = newText - saveNoteValue(note!!) + saveNoteValue(note!!, newText) context!!.updateWidgets() } } @@ -225,20 +216,6 @@ class TextFragment : NoteFragment() { view.text_note_view.requestFocus() } - private fun saveNoteValue(note: Note) { - if (note.path.isEmpty()) { - NotesHelper(activity!!).insertOrUpdateNote(note) { - (activity as? MainActivity)?.noteSavedSuccessfully(note.title) - } - } else { - val currentText = getCurrentNoteViewText() - if (currentText != null) { - val displaySuccess = activity?.config?.displaySuccess ?: false - (activity as? MainActivity)?.tryExportNoteValueToFile(note.path, currentText, displaySuccess) - } - } - } - fun getCurrentNoteViewText() = view.text_note_view?.text?.toString() private fun setWordCounter(text: String) {