| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,63 @@ |
| 1 |
+/* |
|
| 2 |
+ * Copyright (c) 2023 Samson Achiaga |
|
| 3 |
+ * |
|
| 4 |
+ * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 |
+ * you may not use this file except in compliance with the License. |
|
| 6 |
+ * You may obtain a copy of the License at |
|
| 7 |
+ * |
|
| 8 |
+ * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 |
+ * |
|
| 10 |
+ * Unless required by applicable law or agreed to in writing, software |
|
| 11 |
+ * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 |
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 |
+ * See the License for the specific language governing permissions and |
|
| 14 |
+ * limitations under the License. |
|
| 15 |
+ */ |
|
| 16 |
+ |
|
| 17 |
+package com.certified.audionote.adapter |
|
| 18 |
+ |
|
| 19 |
+import android.view.LayoutInflater |
|
| 20 |
+import android.view.ViewGroup |
|
| 21 |
+import androidx.recyclerview.widget.DiffUtil |
|
| 22 |
+import androidx.recyclerview.widget.ListAdapter |
|
| 23 |
+import androidx.recyclerview.widget.RecyclerView |
|
| 24 |
+import com.certified.audionote.databinding.ItemNoteBinding |
|
| 25 |
+import com.certified.audionote.model.Note |
|
| 26 |
+ |
|
| 27 |
+class NoteRecyclerAdapter(private val onItemClick: (note: Note) -> Unit) : |
|
| 28 |
+ ListAdapter<Note, NoteRecyclerAdapter.ViewHolder>(DIFF_CALLBACK) {
|
|
| 29 |
+ |
|
| 30 |
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
| 31 |
+ val binding = |
|
| 32 |
+ ItemNoteBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
|
| 33 |
+ return ViewHolder(binding) |
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
| 37 |
+ val currentItem = getItem(position) |
|
| 38 |
+ holder.bind(currentItem) |
|
| 39 |
+ } |
|
| 40 |
+ |
|
| 41 |
+ inner class ViewHolder(private val binding: ItemNoteBinding) : |
|
| 42 |
+ RecyclerView.ViewHolder(binding.root) {
|
|
| 43 |
+ |
|
| 44 |
+ fun bind(note: Note) {
|
|
| 45 |
+ binding.note = note |
|
| 46 |
+ } |
|
| 47 |
+ |
|
| 48 |
+ init {
|
|
| 49 |
+ itemView.setOnClickListener {
|
|
| 50 |
+ val position = adapterPosition |
|
| 51 |
+ if (position != RecyclerView.NO_POSITION) |
|
| 52 |
+ onItemClick.invoke(getItem(position)) |
|
| 53 |
+ } |
|
| 54 |
+ } |
|
| 55 |
+ } |
|
| 56 |
+ |
|
| 57 |
+ companion object {
|
|
| 58 |
+ val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Note>() {
|
|
| 59 |
+ override fun areItemsTheSame(oldItem: Note, newItem: Note) = oldItem.id == newItem.id |
|
| 60 |
+ override fun areContentsTheSame(oldItem: Note, newItem: Note) = oldItem == newItem |
|
| 61 |
+ } |
|
| 62 |
+ } |
|
| 63 |
+} |
|
| 0 | 64 |
\ No newline at end of file |