From c1e664d9bb62ca107e4feaa08ffbcfa665bf8405 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 8 Mar 2022 18:50:05 +0100 Subject: [PATCH] use a function for getChecklistItems, not variable --- .../notes/pro/adapters/NotesPagerAdapter.kt | 2 +- .../notes/pro/fragments/ChecklistFragment.kt | 21 ++++++++++--------- .../notes/pro/fragments/NoteFragment.kt | 2 +- .../notes/pro/fragments/TextFragment.kt | 12 +++++------ 4 files changed, 19 insertions(+), 18 deletions(-) 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 7964037f..bdf1faaf 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 @@ -65,7 +65,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity fun getNoteChecklistRawItems(position: Int) = (fragments[position] as? ChecklistFragment)?.items - fun getNoteChecklistItems(position: Int) = (fragments[position] as? ChecklistFragment)?.checklistItems + fun getNoteChecklistItems(position: Int) = (fragments[position] as? ChecklistFragment)?.getChecklistItems() fun undo(position: Int) = (fragments[position] as? TextFragment)?.undo() 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 9a0a58e3..76edfeeb 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 @@ -30,11 +30,10 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { lateinit var view: ViewGroup var items = ArrayList() - val checklistItems get(): String = Gson().toJson(items) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { view = inflater.inflate(R.layout.fragment_checklist, container, false) as ViewGroup - noteId = arguments!!.getLong(NOTE_ID, 0L) + noteId = requireArguments().getLong(NOTE_ID, 0L) return view } @@ -52,13 +51,13 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } private fun loadNoteById(noteId: Long) { - NotesHelper(activity!!).getNoteWithId(noteId) { storedNote -> + NotesHelper(requireActivity()).getNoteWithId(noteId) { storedNote -> if (storedNote != null && activity?.isDestroyed == false) { note = storedNote try { val checklistItemType = object : TypeToken>() {}.type - items = Gson().fromJson>(storedNote.getNoteStoredValue(activity!!), checklistItemType) ?: ArrayList(1) + items = Gson().fromJson>(storedNote.getNoteStoredValue(requireActivity()), 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 +77,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { private fun migrateCheckListOnFailure(note: Note) { items.clear() - note.getNoteStoredValue(activity!!)?.split("\n")?.map { it.trim() }?.filter { it.isNotBlank() }?.forEachIndexed { index, value -> + note.getNoteStoredValue(requireActivity())?.split("\n")?.map { it.trim() }?.filter { it.isNotBlank() }?.forEachIndexed { index, value -> items.add( ChecklistItem( id = index, @@ -92,14 +91,14 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } private fun setupFragment() { - if (activity == null || activity!!.isFinishing) { + if (activity == null || requireActivity().isFinishing) { return } - val adjustedPrimaryColor = activity!!.getAdjustedPrimaryColor() + val adjustedPrimaryColor = requireActivity().getAdjustedPrimaryColor() view.checklist_fab.apply { setColors( - activity!!.config.textColor, + requireActivity().config.textColor, adjustedPrimaryColor, adjustedPrimaryColor.getContrastColor() ) @@ -110,7 +109,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } } - view.fragment_placeholder.setTextColor(activity!!.config.textColor) + view.fragment_placeholder.setTextColor(requireActivity().config.textColor) view.fragment_placeholder_2.apply { setTextColor(adjustedPrimaryColor) underlineText() @@ -201,7 +200,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } } - currentNote.value = checklistItems + currentNote.value = getChecklistItems() saveNoteValue(note!!, currentNote.value) ctx.updateWidgets() } @@ -223,6 +222,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } } + fun getChecklistItems() = Gson().toJson(items) + override fun saveChecklist() { saveNote() } 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 09b4ed53..ab459810 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 @@ -37,7 +37,7 @@ abstract class NoteFragment : Fragment() { protected fun saveNoteValue(note: Note, content: String?) { if (note.path.isEmpty()) { - NotesHelper(activity!!).insertOrUpdateNote(note) { + NotesHelper(requireActivity()).insertOrUpdateNote(note) { (activity as? MainActivity)?.noteSavedSuccessfully(note.title) } } else { 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 db5cc3f3..93bf2111 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 @@ -42,7 +42,7 @@ class TextFragment : NoteFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { view = inflater.inflate(R.layout.fragment_text, container, false) as ViewGroup - noteId = arguments!!.getLong(NOTE_ID, 0L) + noteId = requireArguments().getLong(NOTE_ID, 0L) retainInstance = true val layoutToInflate = if (config!!.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable @@ -65,7 +65,7 @@ class TextFragment : NoteFragment() { override fun onResume() { super.onResume() - NotesHelper(activity!!).getNoteWithId(noteId) { + NotesHelper(requireActivity()).getNoteWithId(noteId) { if (it != null) { note = it setupFragment() @@ -143,7 +143,7 @@ class TextFragment : NoteFragment() { onGlobalLayout { if (activity?.isDestroyed == false) { requestFocus() - val inputManager = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val inputManager = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) } } @@ -202,15 +202,15 @@ class TextFragment : NoteFragment() { } val newText = getCurrentNoteViewText() - val oldText = note!!.getNoteStoredValue(context!!) + val oldText = note!!.getNoteStoredValue(requireContext()) if (newText != null && (newText != oldText || force)) { note!!.value = newText saveNoteValue(note!!, newText) - context!!.updateWidgets() + requireContext().updateWidgets() } } - fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue(context!!) + fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue(requireContext()) fun focusEditText() { view.text_note_view.requestFocus()