diff options
| author | 2024-01-10 12:50:32 -0500 | |
|---|---|---|
| committer | 2024-01-10 23:14:04 -0500 | |
| commit | ad0066a6b651ab47b7b7e916a9f716ef77fe2e04 (patch) | |
| tree | e32893c30e6b98d2a457f48ec61a45b32db03b8a | |
| parent | android: Refactor async diff adapters to use AbstractDiffAdapter (diff) | |
| download | yuzu-ad0066a6b651ab47b7b7e916a9f716ef77fe2e04.tar.gz yuzu-ad0066a6b651ab47b7b7e916a9f716ef77fe2e04.tar.xz yuzu-ad0066a6b651ab47b7b7e916a9f716ef77fe2e04.zip | |
android: Create generic list adapter for basic lists
Simplifies basic setup for lists
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractListAdapter.kt | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractListAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractListAdapter.kt new file mode 100644 index 000000000..3dfee3d0c --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractListAdapter.kt | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2024 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | package org.yuzu.yuzu_emu.adapters | ||
| 5 | |||
| 6 | import android.annotation.SuppressLint | ||
| 7 | import androidx.recyclerview.widget.RecyclerView | ||
| 8 | import org.yuzu.yuzu_emu.viewholder.AbstractViewHolder | ||
| 9 | |||
| 10 | /** | ||
| 11 | * Generic list class meant to take care of basic lists | ||
| 12 | * @param currentList The list to show initially | ||
| 13 | */ | ||
| 14 | abstract class AbstractListAdapter<Model : Any, Holder : AbstractViewHolder<Model>>( | ||
| 15 | open var currentList: List<Model> | ||
| 16 | ) : RecyclerView.Adapter<Holder>() { | ||
| 17 | override fun onBindViewHolder(holder: Holder, position: Int) = | ||
| 18 | holder.bind(currentList[position]) | ||
| 19 | |||
| 20 | override fun getItemCount(): Int = currentList.size | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Adds an item to [currentList] and notifies the underlying adapter of the change. If no parameter | ||
| 24 | * is passed in for position, [item] is added to the end of the list. Invokes [callback] last. | ||
| 25 | * @param item The item to add to the list | ||
| 26 | * @param position Index where [item] will be added | ||
| 27 | * @param callback Lambda that's called at the end of the list changes and has the added list | ||
| 28 | * position passed in as a parameter | ||
| 29 | */ | ||
| 30 | open fun addItem(item: Model, position: Int = -1, callback: ((position: Int) -> Unit)? = null) { | ||
| 31 | val newList = currentList.toMutableList() | ||
| 32 | val positionToUpdate: Int | ||
| 33 | if (position == -1) { | ||
| 34 | newList.add(item) | ||
| 35 | currentList = newList | ||
| 36 | positionToUpdate = currentList.size - 1 | ||
| 37 | } else { | ||
| 38 | newList.add(position, item) | ||
| 39 | currentList = newList | ||
| 40 | positionToUpdate = position | ||
| 41 | } | ||
| 42 | onItemAdded(positionToUpdate, callback) | ||
| 43 | } | ||
| 44 | |||
| 45 | protected fun onItemAdded(position: Int, callback: ((Int) -> Unit)? = null) { | ||
| 46 | notifyItemInserted(position) | ||
| 47 | callback?.invoke(position) | ||
| 48 | } | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Replaces the [item] at [position] in the [currentList] and notifies the underlying adapter | ||
| 52 | * of the change. Invokes [callback] last. | ||
| 53 | * @param item New list item | ||
| 54 | * @param position Index where [item] will replace the existing list item | ||
| 55 | * @param callback Lambda that's called at the end of the list changes and has the changed list | ||
| 56 | * position passed in as a parameter | ||
| 57 | */ | ||
| 58 | fun changeItem(item: Model, position: Int, callback: ((position: Int) -> Unit)? = null) { | ||
| 59 | val newList = currentList.toMutableList() | ||
| 60 | newList[position] = item | ||
| 61 | currentList = newList | ||
| 62 | onItemChanged(position, callback) | ||
| 63 | } | ||
| 64 | |||
| 65 | protected fun onItemChanged(position: Int, callback: ((Int) -> Unit)? = null) { | ||
| 66 | notifyItemChanged(position) | ||
| 67 | callback?.invoke(position) | ||
| 68 | } | ||
| 69 | |||
| 70 | /** | ||
| 71 | * Removes the list item at [position] in [currentList] and notifies the underlying adapter | ||
| 72 | * of the change. Invokes [callback] last. | ||
| 73 | * @param position Index where the list item will be removed | ||
| 74 | * @param callback Lambda that's called at the end of the list changes and has the removed list | ||
| 75 | * position passed in as a parameter | ||
| 76 | */ | ||
| 77 | fun removeItem(position: Int, callback: ((position: Int) -> Unit)? = null) { | ||
| 78 | val newList = currentList.toMutableList() | ||
| 79 | newList.removeAt(position) | ||
| 80 | currentList = newList | ||
| 81 | onItemRemoved(position, callback) | ||
| 82 | } | ||
| 83 | |||
| 84 | protected fun onItemRemoved(position: Int, callback: ((Int) -> Unit)? = null) { | ||
| 85 | notifyItemRemoved(position) | ||
| 86 | callback?.invoke(position) | ||
| 87 | } | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Replaces [currentList] with [newList] and notifies the underlying adapter of the change. | ||
| 91 | * @param newList The new list to replace [currentList] | ||
| 92 | */ | ||
| 93 | @SuppressLint("NotifyDataSetChanged") | ||
| 94 | open fun replaceList(newList: List<Model>) { | ||
| 95 | currentList = newList | ||
| 96 | notifyDataSetChanged() | ||
| 97 | } | ||
| 98 | } | ||