Added synchronization to checklist export and import

This commit is contained in:
Agnieszka C 2022-02-24 19:53:28 +01:00
parent 3ddc03f442
commit e965c6ec37
5 changed files with 50 additions and 36 deletions

View File

@ -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)
}
}

View File

@ -39,7 +39,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, 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)
}

View File

@ -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<List<ChecklistItem>>() {}.type
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.value, checklistItemType) ?: ArrayList(1)
items = Gson().fromJson<ArrayList<ChecklistItem>>(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<ChecklistItem>
@ -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()
}
}

View File

@ -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()
}

View File

@ -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) {