use a function for getChecklistItems, not variable

This commit is contained in:
tibbi 2022-03-08 18:50:05 +01:00
parent 4f24a2d93e
commit c1e664d9bb
4 changed files with 19 additions and 18 deletions

View File

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

View File

@ -30,11 +30,10 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
lateinit var view: ViewGroup
var items = ArrayList<ChecklistItem>()
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<List<ChecklistItem>>() {}.type
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.getNoteStoredValue(activity!!), checklistItemType) ?: ArrayList(1)
items = Gson().fromJson<ArrayList<ChecklistItem>>(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<ChecklistItem>
@ -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()
}

View File

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

View File

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