diff options
author | t895 <clombardo169@gmail.com> | 2024-01-08 06:16:28 +0100 |
---|---|---|
committer | t895 <clombardo169@gmail.com> | 2024-01-11 05:14:04 +0100 |
commit | 51ad2d10de80393371a0e1b92e107c90231d0f72 (patch) | |
tree | 87d9a6d9ec52e323b79f4889de09c8f4a463693d /src/android | |
parent | Merge pull request #12639 from liamwhite/format-oops (diff) | |
download | yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.tar yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.tar.gz yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.tar.bz2 yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.tar.lz yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.tar.xz yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.tar.zst yuzu-51ad2d10de80393371a0e1b92e107c90231d0f72.zip |
Diffstat (limited to 'src/android')
-rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractDiffAdapter.kt | 33 | ||||
-rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/viewholder/AbstractViewHolder.kt | 18 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractDiffAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractDiffAdapter.kt new file mode 100644 index 000000000..f006f9e3d --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AbstractDiffAdapter.kt @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.adapters + +import android.annotation.SuppressLint +import androidx.recyclerview.widget.AsyncDifferConfig +import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.ListAdapter +import org.yuzu.yuzu_emu.viewholder.AbstractViewHolder +import androidx.recyclerview.widget.RecyclerView + +/** + * Generic adapter that implements an [AsyncDifferConfig] and covers some of the basic boilerplate + * code used in every [RecyclerView]. + * Type assigned to [Model] must inherit from [Object] in order to be compared properly. + */ +abstract class AbstractDiffAdapter<Model : Any, Holder : AbstractViewHolder<Model>> : + ListAdapter<Model, Holder>(AsyncDifferConfig.Builder(DiffCallback<Model>()).build()) { + override fun onBindViewHolder(holder: Holder, position: Int) = + holder.bind(currentList[position]) + + private class DiffCallback<Model> : DiffUtil.ItemCallback<Model>() { + override fun areItemsTheSame(oldItem: Model & Any, newItem: Model & Any): Boolean { + return oldItem === newItem + } + + @SuppressLint("DiffUtilEquals") + override fun areContentsTheSame(oldItem: Model & Any, newItem: Model & Any): Boolean { + return oldItem == newItem + } + } +} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/viewholder/AbstractViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/viewholder/AbstractViewHolder.kt new file mode 100644 index 000000000..7101ad434 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/viewholder/AbstractViewHolder.kt @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.viewholder + +import androidx.recyclerview.widget.RecyclerView +import androidx.viewbinding.ViewBinding +import org.yuzu.yuzu_emu.adapters.AbstractDiffAdapter +import org.yuzu.yuzu_emu.adapters.AbstractListAdapter + +/** + * [RecyclerView.ViewHolder] meant to work together with a [AbstractDiffAdapter] or a + * [AbstractListAdapter] so we can run [bind] on each list item without needing a manual hookup. + */ +abstract class AbstractViewHolder<Model>(binding: ViewBinding) : + RecyclerView.ViewHolder(binding.root) { + abstract fun bind(model: Model) +} |