Skip to content

Commit a2bdae2

Browse files
committed
Add differCallback in NotesAdapter.kt
1 parent 912506b commit a2bdae2

10 files changed

Lines changed: 78 additions & 24 deletions

File tree

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
11
package com.example.mynotes.adapter
22

3+
import android.annotation.SuppressLint
34
import android.view.LayoutInflater
45
import android.view.View
56
import android.view.ViewGroup
67
import androidx.navigation.findNavController
8+
import androidx.recyclerview.widget.AsyncListDiffer
9+
import androidx.recyclerview.widget.DiffUtil
710
import androidx.recyclerview.widget.RecyclerView
811
import com.example.mynotes.R
912
import com.example.mynotes.model.Note
10-
import com.example.mynotes.ui.fragments.NotesFragmentDirections
13+
import com.example.mynotes.ui.fragments.notes.NotesFragmentDirections
1114
import kotlinx.android.synthetic.main.notes_lyt.view.*
1215

1316
class NotesAdapter : RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() {
1417

15-
private var notes = emptyList<Note>()
18+
inner class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
1619

17-
class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
20+
private val differCallback = object : DiffUtil.ItemCallback<Note>() {
21+
override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {
22+
return oldItem.id == newItem.id
23+
}
24+
25+
@SuppressLint("DiffUtilEquals")
26+
override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
27+
return oldItem == newItem
28+
}
1829

1930
}
2031

32+
val differ = AsyncListDiffer(this, differCallback)
33+
2134
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
2235
val view = LayoutInflater.from(parent.context).inflate(R.layout.notes_lyt, parent, false)
2336
return NoteViewHolder(view)
2437
}
2538

2639
override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
2740

28-
val currentNote = notes[position]
41+
val currentNote = differ.currentList[position]
2942

3043
holder.itemView.title_text.text = currentNote.title
3144
holder.itemView.desc_text.text = currentNote.description
@@ -38,11 +51,8 @@ class NotesAdapter : RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() {
3851

3952
}
4053

41-
override fun getItemCount(): Int = notes.size
42-
43-
fun setNotes(note: List<Note>) {
44-
this.notes = note
45-
notifyDataSetChanged()
54+
override fun getItemCount(): Int {
55+
return differ.currentList.size
4656
}
4757

4858
}

app/src/main/java/com/example/mynotes/ui/fragments/AddNoteFragment.kt renamed to app/src/main/java/com/example/mynotes/ui/fragments/addnote/AddNoteFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.mynotes.ui.fragments
1+
package com.example.mynotes.ui.fragments.addnote
22

33
import android.os.Bundle
44
import android.text.TextUtils

app/src/main/java/com/example/mynotes/ui/fragments/NotesFragment.kt renamed to app/src/main/java/com/example/mynotes/ui/fragments/notes/NotesFragment.kt

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.mynotes.ui.fragments
1+
package com.example.mynotes.ui.fragments.notes
22

33
import android.content.res.Configuration
44
import android.os.Bundle
@@ -17,12 +17,13 @@ import com.example.mynotes.databinding.FragmentNotesBinding
1717
import com.example.mynotes.viewmodel.NotesViewModel
1818
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1919
import com.google.android.material.snackbar.Snackbar
20-
import kotlinx.android.synthetic.main.notes_lyt.view.*
20+
import kotlinx.android.synthetic.main.fragment_notes.*
2121

2222
class NotesFragment : Fragment() {
2323

2424
private lateinit var binding: FragmentNotesBinding
2525
private lateinit var viewModel: NotesViewModel
26+
private lateinit var noteadapter: NotesAdapter
2627

2728
override fun onCreateView(
2829
inflater: LayoutInflater, container: ViewGroup?,
@@ -34,12 +35,13 @@ class NotesFragment : Fragment() {
3435

3536
setHasOptionsMenu(true)
3637

37-
val adapter = NotesAdapter()
38+
swipeToDeleteNote()
39+
noteadapter = NotesAdapter()
3840

3941
binding.apply {
4042

4143
recyclerView.layoutManager = LinearLayoutManager(requireContext())
42-
recyclerView.adapter = adapter
44+
recyclerView.adapter = noteadapter
4345
recyclerView.setHasFixedSize(true)
4446

4547
floatingActionButton.setOnClickListener {
@@ -52,13 +54,54 @@ class NotesFragment : Fragment() {
5254
binding.imageLlyt.visibility = View.VISIBLE
5355
} else {
5456
binding.recyclerView.visibility = View.VISIBLE
55-
adapter.setNotes(it)
57+
noteadapter.differ.submitList(it)
5658
}
5759
})
5860

5961
return binding.root
6062
}
6163

64+
private fun swipeToDeleteNote() {
65+
66+
val itemTouchHelperCallback = object : ItemTouchHelper.SimpleCallback(
67+
ItemTouchHelper.UP or ItemTouchHelper.DOWN,
68+
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
69+
) {
70+
override fun onMove(
71+
recyclerView: RecyclerView,
72+
viewHolder: RecyclerView.ViewHolder,
73+
target: RecyclerView.ViewHolder,
74+
): Boolean {
75+
return true
76+
}
77+
78+
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
79+
// get current item
80+
val position = viewHolder.adapterPosition
81+
val note = noteadapter.differ.currentList[position]
82+
viewModel.deleteNote(note)
83+
84+
Snackbar.make(
85+
binding.root,
86+
getString(R.string.note_deleted),
87+
Snackbar.LENGTH_LONG
88+
)
89+
.apply {
90+
setAction("Undo") {
91+
viewModel.insertNote(note)
92+
image_llyt.visibility = View.GONE
93+
}
94+
show()
95+
}
96+
}
97+
}
98+
99+
// attach swipe to recyclerview
100+
ItemTouchHelper(itemTouchHelperCallback).apply {
101+
attachToRecyclerView(binding.recyclerView)
102+
}
103+
}
104+
62105
// navigate to addnotes fragment
63106
private fun goToAddNoteFragment() {
64107
findNavController().navigate(NotesFragmentDirections.actionNotesFragmentToAddNoteFragment())

app/src/main/java/com/example/mynotes/ui/fragments/UpdateFragment.kt renamed to app/src/main/java/com/example/mynotes/ui/fragments/updatenote/UpdateFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.mynotes.ui.fragments
1+
package com.example.mynotes.ui.fragments.updatenote
22

33
import android.os.Bundle
44
import android.text.TextUtils

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<merge xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
@@ -18,4 +18,4 @@
1818
app:layout_constraintTop_toTopOf="parent"
1919
app:navGraph="@navigation/my_nav" />
2020

21-
</merge>
21+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/fragment_add_note.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<androidx.constraintlayout.widget.ConstraintLayout
77
android:layout_width="match_parent"
88
android:layout_height="match_parent"
9-
tools:context=".ui.fragments.AddNoteFragment">
9+
tools:context=".ui.fragments.addnote.AddNoteFragment">
1010

1111
<com.google.android.material.textfield.TextInputEditText
1212
android:id="@+id/title_txt"

app/src/main/res/layout/fragment_notes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
android:configChanges="uiMode"
88
android:layout_width="match_parent"
99
android:layout_height="match_parent"
10-
tools:context=".ui.fragments.NotesFragment">
10+
tools:context=".ui.fragments.notes.NotesFragment">
1111

1212
<androidx.recyclerview.widget.RecyclerView
1313
android:id="@+id/recyclerView"

app/src/main/res/layout/fragment_update.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:tools="http://schemas.android.com/tools"
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
8-
tools:context=".ui.fragments.UpdateFragment">
8+
tools:context=".ui.fragments.updatenote.UpdateFragment">
99

1010
<com.google.android.material.textfield.TextInputEditText
1111
android:id="@+id/update_title_txt"

app/src/main/res/navigation/my_nav.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<fragment
99
android:id="@+id/notesFragment"
10-
android:name="com.example.mynotes.ui.fragments.NotesFragment"
10+
android:name="com.example.mynotes.ui.fragments.notes.NotesFragment"
1111
android:label="Notes"
1212
tools:layout="@layout/fragment_notes" >
1313
<action
@@ -19,7 +19,7 @@
1919
</fragment>
2020
<fragment
2121
android:id="@+id/updateFragment"
22-
android:name="com.example.mynotes.ui.fragments.UpdateFragment"
22+
android:name="com.example.mynotes.ui.fragments.updatenote.UpdateFragment"
2323
android:label="Update note"
2424
tools:layout="@layout/fragment_update" >
2525
<action
@@ -31,7 +31,7 @@
3131
</fragment>
3232
<fragment
3333
android:id="@+id/addNoteFragment"
34-
android:name="com.example.mynotes.ui.fragments.AddNoteFragment"
34+
android:name="com.example.mynotes.ui.fragments.addnote.AddNoteFragment"
3535
android:label="Add note"
3636
tools:layout="@layout/fragment_add_note" >
3737
<action

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<!-- TODO: Remove or change this placeholder text -->
44
<string name="delete">Delete</string>
55
<string name="mode">Mode</string>
6+
<string name="note_deleted">Note deleted</string>
67
</resources>

0 commit comments

Comments
 (0)