From 5bfd3c04fb89a0e9bc2483af04e55401d65f9e0a Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Sat, 20 Jul 2024 00:48:24 +0530 Subject: [PATCH] Rename `ChecklistItem` to `Task` --- .../activities/WidgetConfigureActivity.kt | 22 ++-- .../notes/adapters/NotesPagerAdapter.kt | 12 +-- .../fossify/notes/adapters/OpenNoteAdapter.kt | 10 +- .../{ChecklistAdapter.kt => TasksAdapter.kt} | 100 +++++++++--------- .../fossify/notes/adapters/WidgetAdapter.kt | 16 +-- ...ecklistItemDialog.kt => EditTaskDialog.kt} | 2 +- .../org/fossify/notes/extensions/String.kt | 8 +- ...{ChecklistFragment.kt => TasksFragment.kt} | 80 +++++++------- .../interfaces/ChecklistItemsListener.kt | 9 -- .../notes/interfaces/TasksActionListener.kt | 9 ++ .../kotlin/org/fossify/notes/models/Note.kt | 2 +- .../models/{ChecklistItem.kt => Task.kt} | 6 +- 12 files changed, 138 insertions(+), 138 deletions(-) rename app/src/main/kotlin/org/fossify/notes/adapters/{ChecklistAdapter.kt => TasksAdapter.kt} (71%) rename app/src/main/kotlin/org/fossify/notes/dialogs/{RenameChecklistItemDialog.kt => EditTaskDialog.kt} (91%) rename app/src/main/kotlin/org/fossify/notes/fragments/{ChecklistFragment.kt => TasksFragment.kt} (72%) delete mode 100644 app/src/main/kotlin/org/fossify/notes/interfaces/ChecklistItemsListener.kt create mode 100644 app/src/main/kotlin/org/fossify/notes/interfaces/TasksActionListener.kt rename app/src/main/kotlin/org/fossify/notes/models/{ChecklistItem.kt => Task.kt} (86%) diff --git a/app/src/main/kotlin/org/fossify/notes/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/org/fossify/notes/activities/WidgetConfigureActivity.kt index 287d07a5..30619b5f 100644 --- a/app/src/main/kotlin/org/fossify/notes/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/org/fossify/notes/activities/WidgetConfigureActivity.kt @@ -20,15 +20,15 @@ import org.fossify.commons.helpers.PROTECTION_NONE import org.fossify.commons.helpers.ensureBackgroundThread import org.fossify.commons.models.RadioItem import org.fossify.notes.R -import org.fossify.notes.adapters.ChecklistAdapter +import org.fossify.notes.adapters.TasksAdapter import org.fossify.notes.databinding.WidgetConfigBinding import org.fossify.notes.extensions.config import org.fossify.notes.extensions.getPercentageFontSize import org.fossify.notes.extensions.widgetsDB import org.fossify.notes.helpers.* -import org.fossify.notes.models.ChecklistItem import org.fossify.notes.models.Note import org.fossify.notes.models.NoteType +import org.fossify.notes.models.Task import org.fossify.notes.models.Widget class WidgetConfigureActivity : SimpleActivity() { @@ -159,19 +159,19 @@ class WidgetConfigureActivity : SimpleActivity() { binding.notesPickerValue.text = note.title binding.textNoteViewTitle.text = note.title if (note.type == NoteType.TYPE_CHECKLIST) { - val checklistItemType = object : TypeToken>() {}.type - val items = Gson().fromJson>(note.value, checklistItemType) ?: ArrayList(1) + val taskType = object : TypeToken>() {}.type + val items = Gson().fromJson>(note.value, taskType) ?: ArrayList(1) items.apply { if (isEmpty()) { - add(ChecklistItem(0, System.currentTimeMillis(), "Milk", true)) - add(ChecklistItem(1, System.currentTimeMillis(), "Butter", true)) - add(ChecklistItem(2, System.currentTimeMillis(), "Salt", false)) - add(ChecklistItem(3, System.currentTimeMillis(), "Water", false)) - add(ChecklistItem(4, System.currentTimeMillis(), "Meat", true)) + add(Task(0, System.currentTimeMillis(), "Milk", true)) + add(Task(1, System.currentTimeMillis(), "Butter", true)) + add(Task(2, System.currentTimeMillis(), "Salt", false)) + add(Task(3, System.currentTimeMillis(), "Water", false)) + add(Task(4, System.currentTimeMillis(), "Meat", true)) } } - ChecklistAdapter(this, null, binding.checklistNoteView).apply { + TasksAdapter(this, null, binding.checklistNoteView).apply { updateTextColor(mTextColor) binding.checklistNoteView.adapter = this submitList(items.toList()) @@ -244,7 +244,7 @@ class WidgetConfigureActivity : SimpleActivity() { private fun updateTextColor() { binding.textNoteView.setTextColor(mTextColor) binding.textNoteViewTitle.setTextColor(mTextColor) - (binding.checklistNoteView.adapter as? ChecklistAdapter)?.updateTextColor(mTextColor) + (binding.checklistNoteView.adapter as? TasksAdapter)?.updateTextColor(mTextColor) binding.configTextColor.setFillWithStroke(mTextColor, mTextColor) binding.configSave.setTextColor(getProperPrimaryColor().getContrastColor()) } diff --git a/app/src/main/kotlin/org/fossify/notes/adapters/NotesPagerAdapter.kt b/app/src/main/kotlin/org/fossify/notes/adapters/NotesPagerAdapter.kt index bf2d6f38..3e1e3845 100644 --- a/app/src/main/kotlin/org/fossify/notes/adapters/NotesPagerAdapter.kt +++ b/app/src/main/kotlin/org/fossify/notes/adapters/NotesPagerAdapter.kt @@ -6,8 +6,8 @@ import android.view.ViewGroup import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter import org.fossify.commons.extensions.showErrorToast -import org.fossify.notes.fragments.ChecklistFragment import org.fossify.notes.fragments.NoteFragment +import org.fossify.notes.fragments.TasksFragment import org.fossify.notes.fragments.TextFragment import org.fossify.notes.helpers.NOTE_ID import org.fossify.notes.models.Note @@ -30,7 +30,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity return fragments[position]!! } - val fragment = if (note.type == NoteType.TYPE_TEXT) TextFragment() else ChecklistFragment() + val fragment = if (note.type == NoteType.TYPE_TEXT) TextFragment() else TasksFragment() fragment.arguments = bundle fragments[position] = fragment return fragment @@ -63,9 +63,9 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity fun saveAllFragmentTexts() = fragments.values.forEach { (it as? TextFragment)?.saveText(false) } - fun getNoteChecklistRawItems(position: Int) = (fragments[position] as? ChecklistFragment)?.items + fun getNoteChecklistRawItems(position: Int) = (fragments[position] as? TasksFragment)?.tasks - fun getNoteChecklistItems(position: Int) = (fragments[position] as? ChecklistFragment)?.getChecklistItems() + fun getNoteChecklistItems(position: Int) = (fragments[position] as? TasksFragment)?.getTasks() fun undo(position: Int) = (fragments[position] as? TextFragment)?.undo() @@ -91,10 +91,10 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity } fun removeDoneCheckListItems(position: Int) { - (fragments[position] as? ChecklistFragment)?.removeDoneItems() + (fragments[position] as? TasksFragment)?.removeDoneItems() } fun refreshChecklist(position: Int) { - (fragments[position] as? ChecklistFragment)?.refreshItems() + (fragments[position] as? TasksFragment)?.refreshItems() } } diff --git a/app/src/main/kotlin/org/fossify/notes/adapters/OpenNoteAdapter.kt b/app/src/main/kotlin/org/fossify/notes/adapters/OpenNoteAdapter.kt index 69729e9a..ab333220 100644 --- a/app/src/main/kotlin/org/fossify/notes/adapters/OpenNoteAdapter.kt +++ b/app/src/main/kotlin/org/fossify/notes/adapters/OpenNoteAdapter.kt @@ -19,9 +19,9 @@ import org.fossify.commons.views.MyRecyclerView import org.fossify.notes.R import org.fossify.notes.databinding.OpenNoteItemBinding import org.fossify.notes.extensions.config -import org.fossify.notes.models.ChecklistItem import org.fossify.notes.models.Note import org.fossify.notes.models.NoteType +import org.fossify.notes.models.Task class OpenNoteAdapter( activity: BaseSimpleActivity, @@ -122,12 +122,12 @@ class OpenNoteAdapter( return when (type) { NoteType.TYPE_TEXT -> getNoteStoredValue(context) NoteType.TYPE_CHECKLIST -> { - val checklistItemType = object : TypeToken>() {}.type - var items = Gson().fromJson>(getNoteStoredValue(context), checklistItemType) ?: listOf() + val taskType = object : TypeToken>() {}.type + var items = Gson().fromJson>(getNoteStoredValue(context), taskType) ?: listOf() items = items.let { val sorting = context.config.sorting - ChecklistItem.sorting = sorting - if (ChecklistItem.sorting and SORT_BY_CUSTOM == 0) { + Task.sorting = sorting + if (Task.sorting and SORT_BY_CUSTOM == 0) { it.sorted().let { if (context.config.moveDoneChecklistItems) { it.sortedBy { it.isDone } diff --git a/app/src/main/kotlin/org/fossify/notes/adapters/ChecklistAdapter.kt b/app/src/main/kotlin/org/fossify/notes/adapters/TasksAdapter.kt similarity index 71% rename from app/src/main/kotlin/org/fossify/notes/adapters/ChecklistAdapter.kt rename to app/src/main/kotlin/org/fossify/notes/adapters/TasksAdapter.kt index 992dee73..8e7cd48d 100644 --- a/app/src/main/kotlin/org/fossify/notes/adapters/ChecklistAdapter.kt +++ b/app/src/main/kotlin/org/fossify/notes/adapters/TasksAdapter.kt @@ -23,21 +23,21 @@ import org.fossify.commons.interfaces.StartReorderDragListener import org.fossify.commons.views.MyRecyclerView import org.fossify.notes.R import org.fossify.notes.databinding.ItemChecklistBinding -import org.fossify.notes.dialogs.RenameChecklistItemDialog +import org.fossify.notes.dialogs.EditTaskDialog import org.fossify.notes.extensions.config import org.fossify.notes.extensions.getPercentageFontSize import org.fossify.notes.helpers.DONE_CHECKLIST_ITEM_ALPHA -import org.fossify.notes.interfaces.ChecklistItemsListener -import org.fossify.notes.models.ChecklistItem +import org.fossify.notes.interfaces.TasksActionListener +import org.fossify.notes.models.Task import java.util.Collections -class ChecklistAdapter( +class TasksAdapter( activity: BaseSimpleActivity, - val listener: ChecklistItemsListener?, + val listener: TasksActionListener?, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit = {}, -) : MyRecyclerViewListAdapter( - activity = activity, recyclerView = recyclerView, diffUtil = ChecklistItemDiffUtil(), itemClick = itemClick +) : MyRecyclerViewListAdapter( + activity = activity, recyclerView = recyclerView, diffUtil = TaskDiffCallback(), itemClick = itemClick ), ItemTouchHelperContract { private var touchHelper: ItemTouchHelper? = null @@ -111,72 +111,72 @@ class ChecklistAdapter( } private fun renameChecklistItem() { - val item = getSelectedItems().first() - RenameChecklistItemDialog(activity, item.title) { title -> - val items = currentList.toMutableList() - items[getSelectedItemPositions().first()] = item.copy(title = title) - saveChecklist(items) + val task = getSelectedItems().first() + EditTaskDialog(activity, task.title) { title -> + val tasks = currentList.toMutableList() + tasks[getSelectedItemPositions().first()] = task.copy(title = title) + saveTasks(tasks) finishActMode() } } private fun deleteSelection() { - val items = currentList.toMutableList() - val itemsToRemove = ArrayList(selectedKeys.size) + val tasks = currentList.toMutableList() + val tasksToRemove = ArrayList(selectedKeys.size) selectedKeys.forEach { key -> - val position = items.indexOfFirst { it.id == key } + val position = tasks.indexOfFirst { it.id == key } if (position != -1) { val favorite = getItemWithKey(key) if (favorite != null) { - itemsToRemove.add(favorite) + tasksToRemove.add(favorite) } } } - items.removeAll(itemsToRemove.toSet()) - saveChecklist(items) + tasks.removeAll(tasksToRemove.toSet()) + saveTasks(tasks) } private fun moveSelectedItemsToTop() { activity.config.sorting = SORT_BY_CUSTOM - val items = currentList.toMutableList() - selectedKeys.reversed().forEach { checklistId -> - val position = items.indexOfFirst { it.id == checklistId } - val tempItem = items[position] - items.removeAt(position) - items.add(0, tempItem) + val tasks = currentList.toMutableList() + selectedKeys.reversed().forEach { id -> + val position = tasks.indexOfFirst { it.id == id } + val tempItem = tasks[position] + tasks.removeAt(position) + tasks.add(0, tempItem) } - saveChecklist(items) + saveTasks(tasks) } private fun moveSelectedItemsToBottom() { activity.config.sorting = SORT_BY_CUSTOM - val items = currentList.toMutableList() - selectedKeys.forEach { checklistId -> - val position = items.indexOfFirst { it.id == checklistId } - val tempItem = items[position] - items.removeAt(position) - items.add(items.size, tempItem) + val tasks = currentList.toMutableList() + selectedKeys.forEach { id -> + val position = tasks.indexOfFirst { it.id == id } + val tempItem = tasks[position] + tasks.removeAt(position) + tasks.add(tasks.size, tempItem) } - saveChecklist(items) + saveTasks(tasks) } - private fun getItemWithKey(key: Int): ChecklistItem? = currentList.firstOrNull { it.id == key } + private fun getItemWithKey(key: Int): Task? = currentList.firstOrNull { it.id == key } private fun getSelectedItems() = currentList.filter { selectedKeys.contains(it.id) }.toMutableList() - private fun setupView(view: View, checklistItem: ChecklistItem, holder: ViewHolder) { - val isSelected = selectedKeys.contains(checklistItem.id) + private fun setupView(view: View, task: Task, holder: ViewHolder) { + val isSelected = selectedKeys.contains(task.id) ItemChecklistBinding.bind(view).apply { checklistTitle.apply { - text = checklistItem.title + text = task.title setTextColor(textColor) setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getPercentageFontSize()) gravity = context.config.getTextGravity() - if (checklistItem.isDone) { + if (task.isDone) { paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG alpha = DONE_CHECKLIST_ITEM_ALPHA } else { @@ -185,7 +185,7 @@ class ChecklistAdapter( } } - checklistCheckbox.isChecked = checklistItem.isDone + checklistCheckbox.isChecked = task.isDone checklistHolder.isSelected = isSelected checklistDragHandle.beVisibleIf(selectedKeys.isNotEmpty()) @@ -201,42 +201,42 @@ class ChecklistAdapter( override fun onRowMoved(fromPosition: Int, toPosition: Int) { activity.config.sorting = SORT_BY_CUSTOM - val items = currentList.toMutableList() + val tasks = currentList.toMutableList() if (fromPosition < toPosition) { for (i in fromPosition until toPosition) { - Collections.swap(items, i, i + 1) + Collections.swap(tasks, i, i + 1) } } else { for (i in fromPosition downTo toPosition + 1) { - Collections.swap(items, i, i - 1) + Collections.swap(tasks, i, i - 1) } } - saveChecklist(items) + saveTasks(tasks) } override fun onRowSelected(myViewHolder: MyRecyclerViewAdapter.ViewHolder?) {} override fun onRowClear(myViewHolder: MyRecyclerViewAdapter.ViewHolder?) { - saveChecklist(currentList.toList()) + saveTasks(currentList.toList()) } - private fun saveChecklist(items: List) { - listener?.saveChecklist(items) { + private fun saveTasks(tasks: List) { + listener?.saveTasks(tasks) { listener.refreshItems() } } } -private class ChecklistItemDiffUtil : DiffUtil.ItemCallback() { +private class TaskDiffCallback : DiffUtil.ItemCallback() { override fun areItemsTheSame( - oldItem: ChecklistItem, - newItem: ChecklistItem + oldItem: Task, + newItem: Task ) = oldItem.id == newItem.id override fun areContentsTheSame( - oldItem: ChecklistItem, - newItem: ChecklistItem + oldItem: Task, + newItem: Task ) = oldItem.id == newItem.id && oldItem.isDone == newItem.isDone && oldItem.title == newItem.title diff --git a/app/src/main/kotlin/org/fossify/notes/adapters/WidgetAdapter.kt b/app/src/main/kotlin/org/fossify/notes/adapters/WidgetAdapter.kt index f1ab47db..d0510ac5 100644 --- a/app/src/main/kotlin/org/fossify/notes/adapters/WidgetAdapter.kt +++ b/app/src/main/kotlin/org/fossify/notes/adapters/WidgetAdapter.kt @@ -18,9 +18,9 @@ import org.fossify.notes.extensions.config import org.fossify.notes.extensions.getPercentageFontSize import org.fossify.notes.extensions.notesDB import org.fossify.notes.helpers.* -import org.fossify.notes.models.ChecklistItem import org.fossify.notes.models.Note import org.fossify.notes.models.NoteType +import org.fossify.notes.models.Task class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory { private val textIds = arrayOf( @@ -33,7 +33,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi ) private var widgetTextColor = DEFAULT_WIDGET_TEXT_COLOR private var note: Note? = null - private var checklistItems = mutableListOf() + private var tasks = mutableListOf() override fun getViewAt(position: Int): RemoteViews { val noteId = intent.getLongExtra(NOTE_ID, 0L) @@ -46,7 +46,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi val textSize = context.getPercentageFontSize() / context.resources.displayMetrics.density if (note!!.type == NoteType.TYPE_CHECKLIST) { remoteView = RemoteViews(context.packageName, R.layout.item_checklist_widget).apply { - val checklistItem = checklistItems.getOrNull(position) ?: return@apply + val checklistItem = tasks.getOrNull(position) ?: return@apply val widgetNewTextColor = if (checklistItem.isDone) widgetTextColor.adjustAlpha(DONE_CHECKLIST_ITEM_ALPHA) else widgetTextColor val paintFlags = if (checklistItem.isDone) Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG else Paint.ANTI_ALIAS_FLAG @@ -125,15 +125,15 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi val noteId = intent.getLongExtra(NOTE_ID, 0L) note = context.notesDB.getNoteWithId(noteId) if (note?.type == NoteType.TYPE_CHECKLIST) { - checklistItems = note!!.getNoteStoredValue(context)?.ifEmpty { "[]" }?.let { Json.decodeFromString(it) } ?: mutableListOf() + tasks = note!!.getNoteStoredValue(context)?.ifEmpty { "[]" }?.let { Json.decodeFromString(it) } ?: mutableListOf() // checklist title can be null only because of the glitch in upgrade to 6.6.0, remove this check in the future - checklistItems = checklistItems.toMutableList() as ArrayList + tasks = tasks.toMutableList() as ArrayList val sorting = context.config.sorting if (sorting and SORT_BY_CUSTOM == 0) { - checklistItems.sort() + tasks.sort() if (context.config.moveDoneChecklistItems) { - checklistItems.sortBy { it.isDone } + tasks.sortBy { it.isDone } } } } @@ -143,7 +143,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi override fun getCount(): Int { return if (note?.type == NoteType.TYPE_CHECKLIST) { - checklistItems.size + tasks.size } else { 1 } diff --git a/app/src/main/kotlin/org/fossify/notes/dialogs/RenameChecklistItemDialog.kt b/app/src/main/kotlin/org/fossify/notes/dialogs/EditTaskDialog.kt similarity index 91% rename from app/src/main/kotlin/org/fossify/notes/dialogs/RenameChecklistItemDialog.kt rename to app/src/main/kotlin/org/fossify/notes/dialogs/EditTaskDialog.kt index bf9a318b..27b7962d 100644 --- a/app/src/main/kotlin/org/fossify/notes/dialogs/RenameChecklistItemDialog.kt +++ b/app/src/main/kotlin/org/fossify/notes/dialogs/EditTaskDialog.kt @@ -5,7 +5,7 @@ import android.content.DialogInterface.BUTTON_POSITIVE import org.fossify.commons.extensions.* import org.fossify.notes.databinding.DialogRenameChecklistItemBinding -class RenameChecklistItemDialog(val activity: Activity, val oldTitle: String, callback: (newTitle: String) -> Unit) { +class EditTaskDialog(val activity: Activity, val oldTitle: String, callback: (newTitle: String) -> Unit) { init { val binding = DialogRenameChecklistItemBinding.inflate(activity.layoutInflater).apply { checklistItemTitle.setText(oldTitle) diff --git a/app/src/main/kotlin/org/fossify/notes/extensions/String.kt b/app/src/main/kotlin/org/fossify/notes/extensions/String.kt index fb763260..4b00002a 100644 --- a/app/src/main/kotlin/org/fossify/notes/extensions/String.kt +++ b/app/src/main/kotlin/org/fossify/notes/extensions/String.kt @@ -2,13 +2,13 @@ package org.fossify.notes.extensions import com.google.gson.Gson import com.google.gson.reflect.TypeToken -import org.fossify.notes.models.ChecklistItem +import org.fossify.notes.models.Task -fun String.parseChecklistItems(): ArrayList? { +fun String.parseChecklistItems(): ArrayList? { if (startsWith("[{") && endsWith("}]")) { try { - val checklistItemType = object : TypeToken>() {}.type - return Gson().fromJson>(this, checklistItemType) ?: null + val taskType = object : TypeToken>() {}.type + return Gson().fromJson>(this, taskType) ?: null } catch (e: Exception) { } } diff --git a/app/src/main/kotlin/org/fossify/notes/fragments/ChecklistFragment.kt b/app/src/main/kotlin/org/fossify/notes/fragments/TasksFragment.kt similarity index 72% rename from app/src/main/kotlin/org/fossify/notes/fragments/ChecklistFragment.kt rename to app/src/main/kotlin/org/fossify/notes/fragments/TasksFragment.kt index 9206e62e..1ae6b9f5 100644 --- a/app/src/main/kotlin/org/fossify/notes/fragments/ChecklistFragment.kt +++ b/app/src/main/kotlin/org/fossify/notes/fragments/TasksFragment.kt @@ -12,25 +12,25 @@ import org.fossify.commons.extensions.* import org.fossify.commons.helpers.SORT_BY_CUSTOM import org.fossify.commons.helpers.ensureBackgroundThread import org.fossify.notes.activities.SimpleActivity -import org.fossify.notes.adapters.ChecklistAdapter +import org.fossify.notes.adapters.TasksAdapter import org.fossify.notes.databinding.FragmentChecklistBinding import org.fossify.notes.dialogs.NewChecklistItemDialog import org.fossify.notes.extensions.config import org.fossify.notes.extensions.updateWidgets import org.fossify.notes.helpers.NOTE_ID import org.fossify.notes.helpers.NotesHelper -import org.fossify.notes.interfaces.ChecklistItemsListener -import org.fossify.notes.models.ChecklistItem +import org.fossify.notes.interfaces.TasksActionListener import org.fossify.notes.models.Note +import org.fossify.notes.models.Task import java.io.File -class ChecklistFragment : NoteFragment(), ChecklistItemsListener { +class TasksFragment : NoteFragment(), TasksActionListener { private var noteId = 0L private lateinit var binding: FragmentChecklistBinding - var items = mutableListOf() + var tasks = mutableListOf() override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { binding = FragmentChecklistBinding.inflate(inflater, container, false) @@ -50,7 +50,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { if (menuVisible) { activity?.hideKeyboard() } else if (::binding.isInitialized) { - (binding.checklistList.adapter as? ChecklistAdapter)?.finishActMode() + (binding.checklistList.adapter as? TasksAdapter)?.finishActMode() } } @@ -60,13 +60,13 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { note = storedNote try { - val checklistItemType = object : TypeToken>() {}.type - items = Gson().fromJson>(storedNote.getNoteStoredValue(requireActivity()), checklistItemType) ?: ArrayList(1) + val taskType = object : TypeToken>() {}.type + tasks = Gson().fromJson>(storedNote.getNoteStoredValue(requireActivity()), taskType) ?: ArrayList(1) - items = items.toMutableList() as ArrayList + tasks = tasks.toMutableList() as ArrayList val sorting = config?.sorting ?: 0 if (sorting and SORT_BY_CUSTOM == 0 && config?.moveDoneChecklistItems == true) { - items.sortBy { it.isDone } + tasks.sortBy { it.isDone } } setupFragment() @@ -78,11 +78,11 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } private fun migrateCheckListOnFailure(note: Note) { - items.clear() + tasks.clear() note.getNoteStoredValue(requireActivity())?.split("\n")?.map { it.trim() }?.filter { it.isNotBlank() }?.forEachIndexed { index, value -> - items.add( - ChecklistItem( + tasks.add( + Task( id = index, title = value, isDone = false @@ -90,7 +90,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { ) } - saveChecklist(items) + saveTasks(tasks) } private fun setupFragment() { @@ -114,7 +114,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { setOnClickListener { showNewItemDialog() - (binding.checklistList.adapter as? ChecklistAdapter)?.finishActMode() + (binding.checklistList.adapter as? TasksAdapter)?.finishActMode() } } @@ -142,20 +142,20 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { private fun showNewItemDialog() { NewChecklistItemDialog(activity as SimpleActivity) { titles -> - var currentMaxId = items.maxByOrNull { item -> item.id }?.id ?: 0 - val newItems = ArrayList() + var currentMaxId = tasks.maxByOrNull { item -> item.id }?.id ?: 0 + val newItems = ArrayList() titles.forEach { title -> title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row -> - newItems.add(ChecklistItem(currentMaxId + 1, System.currentTimeMillis(), row, false)) + newItems.add(Task(currentMaxId + 1, System.currentTimeMillis(), row, false)) currentMaxId++ } } if (config?.addNewChecklistItemsTop == true) { - items.addAll(0, newItems) + tasks.addAll(0, newItems) } else { - items.addAll(newItems) + tasks.addAll(newItems) } saveNote() @@ -165,33 +165,33 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { private fun setupAdapter() { updateUIVisibility() - ChecklistItem.sorting = requireContext().config.sorting - if (ChecklistItem.sorting and SORT_BY_CUSTOM == 0) { - items.sort() + Task.sorting = requireContext().config.sorting + if (Task.sorting and SORT_BY_CUSTOM == 0) { + tasks.sort() if (context?.config?.moveDoneChecklistItems == true) { - items.sortBy { it.isDone } + tasks.sortBy { it.isDone } } } - var checklistAdapter = binding.checklistList.adapter as? ChecklistAdapter - if (checklistAdapter == null) { - checklistAdapter = ChecklistAdapter( + var tasksAdapter = binding.checklistList.adapter as? TasksAdapter + if (tasksAdapter == null) { + tasksAdapter = TasksAdapter( activity = activity as SimpleActivity, listener = this, recyclerView = binding.checklistList, itemClick = ::toggleCompletion ) - binding.checklistList.adapter = checklistAdapter + binding.checklistList.adapter = tasksAdapter } - checklistAdapter.submitList(items.toList()) + tasksAdapter.submitList(tasks.toList()) } private fun toggleCompletion(any: Any) { - val item = any as ChecklistItem - val index = items.indexOf(item) + val item = any as Task + val index = tasks.indexOf(item) if (index != -1) { - items[index] = item.copy(isDone = !item.isDone) + tasks[index] = item.copy(isDone = !item.isDone) saveNote { loadNoteById(noteId) } @@ -212,7 +212,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } if (note != null) { - note!!.value = getChecklistItems() + note!!.value = getTasks() ensureBackgroundThread { saveNoteValue(note!!, note!!.value) @@ -223,23 +223,23 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } fun removeDoneItems() { - items = items.filter { !it.isDone }.toMutableList() as ArrayList + tasks = tasks.filter { !it.isDone }.toMutableList() as ArrayList saveNote() setupAdapter() } private fun updateUIVisibility() { binding.apply { - fragmentPlaceholder.beVisibleIf(items.isEmpty()) - fragmentPlaceholder2.beVisibleIf(items.isEmpty()) - checklistList.beVisibleIf(items.isNotEmpty()) + fragmentPlaceholder.beVisibleIf(tasks.isEmpty()) + fragmentPlaceholder2.beVisibleIf(tasks.isEmpty()) + checklistList.beVisibleIf(tasks.isNotEmpty()) } } - fun getChecklistItems() = Gson().toJson(items) + fun getTasks() = Gson().toJson(tasks) - override fun saveChecklist(updatedItems: List, callback: () -> Unit) { - items = updatedItems.toMutableList() + override fun saveTasks(updatedTasks: List, callback: () -> Unit) { + tasks = updatedTasks.toMutableList() saveNote(callback = callback) } diff --git a/app/src/main/kotlin/org/fossify/notes/interfaces/ChecklistItemsListener.kt b/app/src/main/kotlin/org/fossify/notes/interfaces/ChecklistItemsListener.kt deleted file mode 100644 index 10780be6..00000000 --- a/app/src/main/kotlin/org/fossify/notes/interfaces/ChecklistItemsListener.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.fossify.notes.interfaces - -import org.fossify.notes.models.ChecklistItem - -interface ChecklistItemsListener { - fun refreshItems() - - fun saveChecklist(updatedItems: List, callback: () -> Unit = {}) -} diff --git a/app/src/main/kotlin/org/fossify/notes/interfaces/TasksActionListener.kt b/app/src/main/kotlin/org/fossify/notes/interfaces/TasksActionListener.kt new file mode 100644 index 00000000..c092c639 --- /dev/null +++ b/app/src/main/kotlin/org/fossify/notes/interfaces/TasksActionListener.kt @@ -0,0 +1,9 @@ +package org.fossify.notes.interfaces + +import org.fossify.notes.models.Task + +interface TasksActionListener { + fun refreshItems() + + fun saveTasks(updatedTasks: List, callback: () -> Unit = {}) +} diff --git a/app/src/main/kotlin/org/fossify/notes/models/Note.kt b/app/src/main/kotlin/org/fossify/notes/models/Note.kt index da9b7a0e..0aecc47b 100644 --- a/app/src/main/kotlin/org/fossify/notes/models/Note.kt +++ b/app/src/main/kotlin/org/fossify/notes/models/Note.kt @@ -12,7 +12,7 @@ import java.io.File /** * Represents a note. * - * @property value The content of the note. Could be plain text or [ChecklistItem] + * @property value The content of the note. Could be plain text or [Task] * @property type The type of the note. Should be one of the [NoteType] enum entries. */ @Serializable diff --git a/app/src/main/kotlin/org/fossify/notes/models/ChecklistItem.kt b/app/src/main/kotlin/org/fossify/notes/models/Task.kt similarity index 86% rename from app/src/main/kotlin/org/fossify/notes/models/ChecklistItem.kt rename to app/src/main/kotlin/org/fossify/notes/models/Task.kt index c55b4648..b15b3046 100644 --- a/app/src/main/kotlin/org/fossify/notes/models/ChecklistItem.kt +++ b/app/src/main/kotlin/org/fossify/notes/models/Task.kt @@ -6,18 +6,18 @@ import org.fossify.commons.helpers.SORT_DESCENDING import org.fossify.notes.helpers.CollatorBasedComparator @Serializable -data class ChecklistItem( +data class Task( val id: Int, val dateCreated: Long = 0L, val title: String, val isDone: Boolean -) : Comparable { +) : Comparable { companion object { var sorting = 0 } - override fun compareTo(other: ChecklistItem): Int { + override fun compareTo(other: Task): Int { var result = when { sorting and SORT_BY_TITLE != 0 -> CollatorBasedComparator().compare(title, other.title) else -> dateCreated.compareTo(other.dateCreated)