@@ -7,44 +7,46 @@ import androidx.recyclerview.widget.RecyclerView
77import com.codexo.notes.data.Note
88import com.codexo.notes.databinding.ItemNotesBinding
99
10- class NotesAdapter : RecyclerView .Adapter <NotesAdapter .TodoViewHolder >() {
10+ class NotesAdapter (private val listener : OnItemClickListener ) :
11+ RecyclerView .Adapter <NotesAdapter .NoteViewHolder >() {
1112
12- var todoList = emptyList<Note >()
13+ var NoteList = emptyList<Note >()
1314
14- class TodoViewHolder (private val binding : ItemNotesBinding ) :
15- RecyclerView .ViewHolder (binding.root) {
16- fun bind (note : Note ) {
17- binding.note = note
18- binding.executePendingBindings()
19- }
20-
21- companion object {
22- fun from (parent : ViewGroup ): TodoViewHolder {
23- val layoutInflater = LayoutInflater .from(parent.context)
24- val binding = ItemNotesBinding .inflate(layoutInflater, parent, false )
25- return TodoViewHolder (binding)
26- }
27- }
15+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): NoteViewHolder {
16+ val binding = ItemNotesBinding .inflate(LayoutInflater .from(parent.context), parent, false )
17+ return NoteViewHolder (binding)
2818 }
2919
30- override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): TodoViewHolder {
31- return TodoViewHolder .from(parent)
32- }
33-
34- override fun onBindViewHolder (holder : TodoViewHolder , position : Int ) {
35- val currentItem = todoList[position]
20+ override fun onBindViewHolder (holder : NoteViewHolder , position : Int ) {
21+ val currentItem = NoteList [position]
3622 holder.bind(currentItem)
3723 }
3824
3925 override fun getItemCount (): Int {
40- return todoList .size
26+ return NoteList .size
4127 }
4228
4329 fun setData (notesData : List <Note >) {
44- val notesDiffUtil = DiffUtil (todoList , notesData)
30+ val notesDiffUtil = DiffUtil (NoteList , notesData)
4531 val notesDiffResult = DiffUtil .calculateDiff(notesDiffUtil)
4632
47- this .todoList = notesData
33+ this .NoteList = notesData
4834 notesDiffResult.dispatchUpdatesTo(this )
4935 }
36+
37+ inner class NoteViewHolder (private val binding : ItemNotesBinding ) :
38+ RecyclerView .ViewHolder (binding.root) {
39+ fun bind (note : Note ) {
40+ binding.note = note
41+ binding.ivFavorite.setOnClickListener {
42+ listener.onFavoriteClicked(! note.favorite, note.id)
43+ }
44+
45+ binding.executePendingBindings()
46+ }
47+ }
48+
49+ interface OnItemClickListener {
50+ fun onFavoriteClicked (markedFavorite : Boolean , id : Long )
51+ }
5052}
0 commit comments