mirror of
https://github.com/FossifyOrg/Notes.git
synced 2026-06-11 10:09:29 -04:00
Rename ChecklistItem to Task
This commit is contained in:
@@ -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<List<ChecklistItem>>() {}.type
|
||||
val items = Gson().fromJson<ArrayList<ChecklistItem>>(note.value, checklistItemType) ?: ArrayList(1)
|
||||
val taskType = object : TypeToken<List<Task>>() {}.type
|
||||
val items = Gson().fromJson<ArrayList<Task>>(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())
|
||||
}
|
||||
|
||||
@@ -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<Note>, 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<Note>, 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<Note>, 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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<List<ChecklistItem>>() {}.type
|
||||
var items = Gson().fromJson<List<ChecklistItem>>(getNoteStoredValue(context), checklistItemType) ?: listOf()
|
||||
val taskType = object : TypeToken<List<Task>>() {}.type
|
||||
var items = Gson().fromJson<List<Task>>(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 }
|
||||
|
||||
@@ -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<ChecklistItem>(
|
||||
activity = activity, recyclerView = recyclerView, diffUtil = ChecklistItemDiffUtil(), itemClick = itemClick
|
||||
) : MyRecyclerViewListAdapter<Task>(
|
||||
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<ChecklistItem>(selectedKeys.size)
|
||||
val tasks = currentList.toMutableList()
|
||||
val tasksToRemove = ArrayList<Task>(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<ChecklistItem>) {
|
||||
listener?.saveChecklist(items) {
|
||||
private fun saveTasks(tasks: List<Task>) {
|
||||
listener?.saveTasks(tasks) {
|
||||
listener.refreshItems()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ChecklistItemDiffUtil : DiffUtil.ItemCallback<ChecklistItem>() {
|
||||
private class TaskDiffCallback : DiffUtil.ItemCallback<Task>() {
|
||||
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
|
||||
@@ -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<ChecklistItem>()
|
||||
private var tasks = mutableListOf<Task>()
|
||||
|
||||
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<ChecklistItem>
|
||||
tasks = tasks.toMutableList() as ArrayList<Task>
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -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<ChecklistItem>? {
|
||||
fun String.parseChecklistItems(): ArrayList<Task>? {
|
||||
if (startsWith("[{") && endsWith("}]")) {
|
||||
try {
|
||||
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
|
||||
return Gson().fromJson<ArrayList<ChecklistItem>>(this, checklistItemType) ?: null
|
||||
val taskType = object : TypeToken<List<Task>>() {}.type
|
||||
return Gson().fromJson<ArrayList<Task>>(this, taskType) ?: null
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ChecklistItem>()
|
||||
var tasks = mutableListOf<Task>()
|
||||
|
||||
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<List<ChecklistItem>>() {}.type
|
||||
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.getNoteStoredValue(requireActivity()), checklistItemType) ?: ArrayList(1)
|
||||
val taskType = object : TypeToken<List<Task>>() {}.type
|
||||
tasks = Gson().fromJson<ArrayList<Task>>(storedNote.getNoteStoredValue(requireActivity()), taskType) ?: ArrayList(1)
|
||||
|
||||
items = items.toMutableList() as ArrayList<ChecklistItem>
|
||||
tasks = tasks.toMutableList() as ArrayList<Task>
|
||||
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<ChecklistItem>()
|
||||
var currentMaxId = tasks.maxByOrNull { item -> item.id }?.id ?: 0
|
||||
val newItems = ArrayList<Task>()
|
||||
|
||||
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<ChecklistItem>
|
||||
tasks = tasks.filter { !it.isDone }.toMutableList() as ArrayList<Task>
|
||||
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<ChecklistItem>, callback: () -> Unit) {
|
||||
items = updatedItems.toMutableList()
|
||||
override fun saveTasks(updatedTasks: List<Task>, callback: () -> Unit) {
|
||||
tasks = updatedTasks.toMutableList()
|
||||
saveNote(callback = callback)
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package org.fossify.notes.interfaces
|
||||
|
||||
import org.fossify.notes.models.ChecklistItem
|
||||
|
||||
interface ChecklistItemsListener {
|
||||
fun refreshItems()
|
||||
|
||||
fun saveChecklist(updatedItems: List<ChecklistItem>, callback: () -> Unit = {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.fossify.notes.interfaces
|
||||
|
||||
import org.fossify.notes.models.Task
|
||||
|
||||
interface TasksActionListener {
|
||||
fun refreshItems()
|
||||
|
||||
fun saveTasks(updatedTasks: List<Task>, callback: () -> Unit = {})
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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<ChecklistItem> {
|
||||
) : Comparable<Task> {
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user