handle deleting checklist items

This commit is contained in:
tibbi 2018-12-08 23:34:53 +01:00
parent ddb5654b9f
commit 1d90b76d32
4 changed files with 55 additions and 8 deletions

View File

@ -8,14 +8,14 @@ import android.view.ViewGroup
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener
import com.simplemobiletools.notes.pro.models.ChecklistItem import com.simplemobiletools.notes.pro.models.ChecklistItem
import kotlinx.android.synthetic.main.item_checklist.view.* import kotlinx.android.synthetic.main.item_checklist.view.*
import java.util.* import java.util.*
class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<ChecklistItem>, val listener: RefreshRecyclerViewListener?, class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<ChecklistItem>, val listener: ChecklistItemsListener?,
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private lateinit var crossDrawable: Drawable private lateinit var crossDrawable: Drawable
@ -28,7 +28,15 @@ class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<Checkl
override fun getActionMenuId() = R.menu.cab_delete_only override fun getActionMenuId() = R.menu.cab_delete_only
override fun actionItemPressed(id: Int) {} override fun actionItemPressed(id: Int) {
if (selectedKeys.isEmpty()) {
return
}
when (id) {
R.id.cab_delete -> deleteSelection()
}
}
override fun getSelectableItemCount() = items.size override fun getSelectableItemCount() = items.size
@ -58,6 +66,34 @@ class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<Checkl
checkDrawable = res.getColoredDrawableWithColor(R.drawable.ic_check_big, res.getColor(R.color.md_green_700)) checkDrawable = res.getColoredDrawableWithColor(R.drawable.ic_check_big, res.getColor(R.color.md_green_700))
} }
private fun deleteSelection() {
val removeItems = ArrayList<ChecklistItem>(selectedKeys.size)
val positions = ArrayList<Int>()
selectedKeys.forEach {
val key = it
val position = items.indexOfFirst { it.id == key }
if (position != -1) {
positions.add(position)
val favorite = getItemWithKey(key)
if (favorite != null) {
removeItems.add(favorite)
}
}
}
positions.sortDescending()
removeSelectedItems(positions)
items.removeAll(removeItems)
listener?.saveChecklist()
if (items.isEmpty()) {
listener?.refreshItems()
}
}
private fun getItemWithKey(key: Int): ChecklistItem? = items.firstOrNull { it.id == key }
private fun setupView(view: View, checklistItem: ChecklistItem) { private fun setupView(view: View, checklistItem: ChecklistItem) {
val isSelected = selectedKeys.contains(checklistItem.id) val isSelected = selectedKeys.contains(checklistItem.id)
view.apply { view.apply {

View File

@ -8,7 +8,6 @@ import android.view.ViewGroup
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
@ -16,11 +15,12 @@ import com.simplemobiletools.notes.pro.dialogs.NewChecklistItemDialog
import com.simplemobiletools.notes.pro.extensions.notesDB import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.helpers.NOTE_ID import com.simplemobiletools.notes.pro.helpers.NOTE_ID
import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.interfaces.ChecklistItemsListener
import com.simplemobiletools.notes.pro.models.ChecklistItem import com.simplemobiletools.notes.pro.models.ChecklistItem
import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Note
import kotlinx.android.synthetic.main.fragment_checklist.view.* import kotlinx.android.synthetic.main.fragment_checklist.view.*
class ChecklistFragment : NoteFragment(), RefreshRecyclerViewListener { class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
private var noteId = 0L private var noteId = 0L
private var note: Note? = null private var note: Note? = null
private var items = ArrayList<ChecklistItem>() private var items = ArrayList<ChecklistItem>()
@ -64,7 +64,7 @@ class ChecklistFragment : NoteFragment(), RefreshRecyclerViewListener {
val currentMaxId = items.maxBy { it.id }?.id ?: 0 val currentMaxId = items.maxBy { it.id }?.id ?: 0
val checklistItem = ChecklistItem(currentMaxId + 1, it, false) val checklistItem = ChecklistItem(currentMaxId + 1, it, false)
items.add(checklistItem) items.add(checklistItem)
saveNote(-1) saveNote()
} }
} }
} }
@ -81,7 +81,7 @@ class ChecklistFragment : NoteFragment(), RefreshRecyclerViewListener {
} }
} }
private fun saveNote(refreshIndex: Int) { private fun saveNote(refreshIndex: Int = -1) {
Thread { Thread {
if (note != null && context != null) { if (note != null && context != null) {
if (refreshIndex != -1) { if (refreshIndex != -1) {
@ -96,6 +96,10 @@ class ChecklistFragment : NoteFragment(), RefreshRecyclerViewListener {
}.start() }.start()
} }
override fun saveChecklist() {
saveNote()
}
override fun refreshItems() { override fun refreshItems() {
} }
} }

View File

@ -0,0 +1,7 @@
package com.simplemobiletools.notes.pro.interfaces
interface ChecklistItemsListener {
fun refreshItems()
fun saveChecklist()
}

View File

@ -9,7 +9,7 @@
<com.simplemobiletools.commons.views.MyRecyclerView <com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/checklist_list" android:id="@+id/checklist_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:clipToPadding="false" android:clipToPadding="false"
android:overScrollMode="never" android:overScrollMode="never"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"/> app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"/>