Skip to content

Commit e5e1101

Browse files
swipe to delete added
1 parent b3d2ce2 commit e5e1101

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

app/src/main/java/com/decimalab/todokotlin/fragments/list/ListFragment.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import android.widget.Toast
77
import androidx.fragment.app.Fragment
88
import androidx.fragment.app.viewModels
99
import androidx.lifecycle.Observer
10+
import androidx.recyclerview.widget.ItemTouchHelper
1011
import androidx.recyclerview.widget.LinearLayoutManager
12+
import androidx.recyclerview.widget.RecyclerView
1113
import com.decimalab.todokotlin.R
14+
import com.decimalab.todokotlin.data.models.ToDoData
1215
import com.decimalab.todokotlin.data.viewmodel.ToDoViewModel
1316
import com.decimalab.todokotlin.databinding.FragmentListBinding
1417
import com.decimalab.todokotlin.fragments.BaseViewModel
18+
import com.decimalab.todokotlin.fragments.list.adapter.ListAdapter
19+
import com.google.android.material.snackbar.Snackbar
1520

1621
class ListFragment : Fragment() {
1722

@@ -53,6 +58,41 @@ class ListFragment : Fragment() {
5358
val recyclerView = binding.recyclerView
5459
recyclerView.adapter = adapter
5560
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
61+
62+
swipeToDelete(recyclerView)
63+
}
64+
65+
private fun swipeToDelete(recyclerView: RecyclerView) {
66+
val swipeToDeleteCallback = object : SwipeToDelete() {
67+
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
68+
val deletedItem = adapter.dataList[viewHolder.adapterPosition]
69+
// Delete Item
70+
mToDoViewModel.deleteItem(deletedItem)
71+
72+
Toast.makeText(
73+
requireContext(),
74+
"Deleted",
75+
Toast.LENGTH_SHORT
76+
).show()
77+
78+
adapter.notifyItemRemoved(viewHolder.adapterPosition)
79+
// Restore Deleted Item
80+
restoreDeletedData(viewHolder.itemView, deletedItem)
81+
}
82+
}
83+
val itemTouchHelper = ItemTouchHelper(swipeToDeleteCallback)
84+
itemTouchHelper.attachToRecyclerView(recyclerView)
85+
}
86+
87+
private fun restoreDeletedData(view: View, deletedItem: ToDoData) {
88+
val snackBar = Snackbar.make(
89+
view, "Deleted '${deletedItem.title}'",
90+
Snackbar.LENGTH_LONG
91+
)
92+
snackBar.setAction("Undo") {
93+
mToDoViewModel.insertData(deletedItem)
94+
}
95+
snackBar.show()
5696
}
5797

5898
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.decimalab.todokotlin.fragments.list
2+
3+
import androidx.recyclerview.widget.ItemTouchHelper
4+
import androidx.recyclerview.widget.RecyclerView
5+
6+
/**
7+
* Created by Shakil Ahmed Shaj on 31,August,2020.
8+
* shakilahmedshaj@gmail.com
9+
*/
10+
abstract class SwipeToDelete : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
11+
override fun onMove(
12+
recyclerView: RecyclerView,
13+
viewHolder: RecyclerView.ViewHolder,
14+
target: RecyclerView.ViewHolder
15+
): Boolean {
16+
return false
17+
}
18+
}

app/src/main/java/com/decimalab/todokotlin/fragments/list/ListAdapter.kt renamed to app/src/main/java/com/decimalab/todokotlin/fragments/list/adapter/ListAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.decimalab.todokotlin.fragments.list
1+
package com.decimalab.todokotlin.fragments.list.adapter
22

33
import android.view.LayoutInflater
44
import android.view.ViewGroup
@@ -13,7 +13,7 @@ import com.decimalab.todokotlin.databinding.RowLayoutBinding
1313
class ListAdapter : RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
1414

1515

16-
private var dataList = emptyList<ToDoData>()
16+
var dataList = emptyList<ToDoData>()
1717

1818

1919
class MyViewHolder(private val binding: RowLayoutBinding) :

0 commit comments

Comments
 (0)