android: Refactor async diff adapters to use AbstractDiffAdapter

This commit is contained in:
t895 2024-01-08 00:17:01 -05:00
parent 51ad2d10de
commit 78c323c4eb
3 changed files with 93 additions and 173 deletions

View File

@ -5,48 +5,28 @@ package org.yuzu.yuzu_emu.adapters
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.yuzu.yuzu_emu.databinding.ListItemAddonBinding import org.yuzu.yuzu_emu.databinding.ListItemAddonBinding
import org.yuzu.yuzu_emu.model.Addon import org.yuzu.yuzu_emu.model.Addon
import org.yuzu.yuzu_emu.viewholder.AbstractViewHolder
class AddonAdapter : ListAdapter<Addon, AddonAdapter.AddonViewHolder>( class AddonAdapter : AbstractDiffAdapter<Addon, AddonAdapter.AddonViewHolder>() {
AsyncDifferConfig.Builder(DiffCallback()).build()
) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddonViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddonViewHolder {
ListItemAddonBinding.inflate(LayoutInflater.from(parent.context), parent, false) ListItemAddonBinding.inflate(LayoutInflater.from(parent.context), parent, false)
.also { return AddonViewHolder(it) } .also { return AddonViewHolder(it) }
} }
override fun getItemCount(): Int = currentList.size
override fun onBindViewHolder(holder: AddonViewHolder, position: Int) =
holder.bind(currentList[position])
inner class AddonViewHolder(val binding: ListItemAddonBinding) : inner class AddonViewHolder(val binding: ListItemAddonBinding) :
RecyclerView.ViewHolder(binding.root) { AbstractViewHolder<Addon>(binding) {
fun bind(addon: Addon) { override fun bind(model: Addon) {
binding.root.setOnClickListener { binding.root.setOnClickListener {
binding.addonSwitch.isChecked = !binding.addonSwitch.isChecked binding.addonSwitch.isChecked = !binding.addonSwitch.isChecked
} }
binding.title.text = addon.title binding.title.text = model.title
binding.version.text = addon.version binding.version.text = model.version
binding.addonSwitch.setOnCheckedChangeListener { _, checked -> binding.addonSwitch.setOnCheckedChangeListener { _, checked ->
addon.enabled = checked model.enabled = checked
} }
binding.addonSwitch.isChecked = addon.enabled binding.addonSwitch.isChecked = model.enabled
}
}
private class DiffCallback : DiffUtil.ItemCallback<Addon>() {
override fun areItemsTheSame(oldItem: Addon, newItem: Addon): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Addon, newItem: Addon): Boolean {
return oldItem == newItem
} }
} }
} }

View File

@ -8,19 +8,14 @@ import android.text.TextUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.yuzu.yuzu_emu.databinding.CardFolderBinding import org.yuzu.yuzu_emu.databinding.CardFolderBinding
import org.yuzu.yuzu_emu.fragments.GameFolderPropertiesDialogFragment import org.yuzu.yuzu_emu.fragments.GameFolderPropertiesDialogFragment
import org.yuzu.yuzu_emu.model.GameDir import org.yuzu.yuzu_emu.model.GameDir
import org.yuzu.yuzu_emu.model.GamesViewModel import org.yuzu.yuzu_emu.model.GamesViewModel
import org.yuzu.yuzu_emu.viewholder.AbstractViewHolder
class FolderAdapter(val activity: FragmentActivity, val gamesViewModel: GamesViewModel) : class FolderAdapter(val activity: FragmentActivity, val gamesViewModel: GamesViewModel) :
ListAdapter<GameDir, FolderAdapter.FolderViewHolder>( AbstractDiffAdapter<GameDir, FolderAdapter.FolderViewHolder>() {
AsyncDifferConfig.Builder(DiffCallback()).build()
) {
override fun onCreateViewHolder( override fun onCreateViewHolder(
parent: ViewGroup, parent: ViewGroup,
viewType: Int viewType: Int
@ -29,18 +24,11 @@ class FolderAdapter(val activity: FragmentActivity, val gamesViewModel: GamesVie
.also { return FolderViewHolder(it) } .also { return FolderViewHolder(it) }
} }
override fun onBindViewHolder(holder: FolderAdapter.FolderViewHolder, position: Int) =
holder.bind(currentList[position])
inner class FolderViewHolder(val binding: CardFolderBinding) : inner class FolderViewHolder(val binding: CardFolderBinding) :
RecyclerView.ViewHolder(binding.root) { AbstractViewHolder<GameDir>(binding) {
private lateinit var gameDir: GameDir override fun bind(model: GameDir) {
fun bind(gameDir: GameDir) {
this.gameDir = gameDir
binding.apply { binding.apply {
path.text = Uri.parse(gameDir.uriString).path path.text = Uri.parse(model.uriString).path
path.postDelayed( path.postDelayed(
{ {
path.isSelected = true path.isSelected = true
@ -50,7 +38,7 @@ class FolderAdapter(val activity: FragmentActivity, val gamesViewModel: GamesVie
) )
buttonEdit.setOnClickListener { buttonEdit.setOnClickListener {
GameFolderPropertiesDialogFragment.newInstance(this@FolderViewHolder.gameDir) GameFolderPropertiesDialogFragment.newInstance(model)
.show( .show(
activity.supportFragmentManager, activity.supportFragmentManager,
GameFolderPropertiesDialogFragment.TAG GameFolderPropertiesDialogFragment.TAG
@ -58,19 +46,9 @@ class FolderAdapter(val activity: FragmentActivity, val gamesViewModel: GamesVie
} }
buttonDelete.setOnClickListener { buttonDelete.setOnClickListener {
gamesViewModel.removeFolder(this@FolderViewHolder.gameDir) gamesViewModel.removeFolder(model)
} }
} }
} }
} }
private class DiffCallback : DiffUtil.ItemCallback<GameDir>() {
override fun areItemsTheSame(oldItem: GameDir, newItem: GameDir): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: GameDir, newItem: GameDir): Boolean {
return oldItem == newItem
}
}
} }

View File

@ -9,7 +9,6 @@ import android.graphics.drawable.LayerDrawable
import android.net.Uri import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.Toast import android.widget.Toast
@ -25,10 +24,6 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -36,42 +31,43 @@ import org.yuzu.yuzu_emu.HomeNavigationDirections
import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.activities.EmulationActivity import org.yuzu.yuzu_emu.activities.EmulationActivity
import org.yuzu.yuzu_emu.adapters.GameAdapter.GameViewHolder
import org.yuzu.yuzu_emu.databinding.CardGameBinding import org.yuzu.yuzu_emu.databinding.CardGameBinding
import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.Game
import org.yuzu.yuzu_emu.model.GamesViewModel import org.yuzu.yuzu_emu.model.GamesViewModel
import org.yuzu.yuzu_emu.utils.GameIconUtils import org.yuzu.yuzu_emu.utils.GameIconUtils
import org.yuzu.yuzu_emu.viewholder.AbstractViewHolder
class GameAdapter(private val activity: AppCompatActivity) : class GameAdapter(private val activity: AppCompatActivity) :
ListAdapter<Game, GameViewHolder>(AsyncDifferConfig.Builder(DiffCallback()).build()), AbstractDiffAdapter<Game, GameAdapter.GameViewHolder>() {
View.OnClickListener,
View.OnLongClickListener {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameViewHolder {
// Create a new view. CardGameBinding.inflate(LayoutInflater.from(parent.context), parent, false)
val binding = CardGameBinding.inflate(LayoutInflater.from(parent.context), parent, false) .also { return GameViewHolder(it) }
binding.cardGame.setOnClickListener(this)
binding.cardGame.setOnLongClickListener(this)
// Use that view to create a ViewHolder.
return GameViewHolder(binding)
} }
override fun onBindViewHolder(holder: GameViewHolder, position: Int) = inner class GameViewHolder(val binding: CardGameBinding) :
holder.bind(currentList[position]) AbstractViewHolder<Game>(binding) {
override fun bind(model: Game) {
binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP
GameIconUtils.loadGameIcon(model, binding.imageGameScreen)
override fun getItemCount(): Int = currentList.size binding.textGameTitle.text = model.title.replace("[\\t\\n\\r]+".toRegex(), " ")
/** binding.textGameTitle.postDelayed(
* Launches the game that was clicked on. {
* binding.textGameTitle.ellipsize = TextUtils.TruncateAt.MARQUEE
* @param view The card representing the game the user wants to play. binding.textGameTitle.isSelected = true
*/ },
override fun onClick(view: View) { 3000
val holder = view.tag as GameViewHolder )
binding.cardGame.setOnClickListener { onClick(model) }
binding.cardGame.setOnLongClickListener { onLongClick(model) }
}
fun onClick(game: Game) {
val gameExists = DocumentFile.fromSingleUri( val gameExists = DocumentFile.fromSingleUri(
YuzuApplication.appContext, YuzuApplication.appContext,
Uri.parse(holder.game.path) Uri.parse(game.path)
)?.exists() == true )?.exists() == true
if (!gameExists) { if (!gameExists) {
Toast.makeText( Toast.makeText(
@ -84,17 +80,19 @@ class GameAdapter(private val activity: AppCompatActivity) :
return return
} }
val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) val preferences =
PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
preferences.edit() preferences.edit()
.putLong( .putLong(
holder.game.keyLastPlayedTime, game.keyLastPlayedTime,
System.currentTimeMillis() System.currentTimeMillis()
) )
.apply() .apply()
val openIntent = Intent(YuzuApplication.appContext, EmulationActivity::class.java).apply { val openIntent =
Intent(YuzuApplication.appContext, EmulationActivity::class.java).apply {
action = Intent.ACTION_VIEW action = Intent.ACTION_VIEW
data = Uri.parse(holder.game.path) data = Uri.parse(game.path)
} }
activity.lifecycleScope.launch { activity.lifecycleScope.launch {
@ -106,15 +104,15 @@ class GameAdapter(private val activity: AppCompatActivity) :
) as LayerDrawable ) as LayerDrawable
layerDrawable.setDrawableByLayerId( layerDrawable.setDrawableByLayerId(
R.id.shortcut_foreground, R.id.shortcut_foreground,
GameIconUtils.getGameIcon(activity, holder.game) GameIconUtils.getGameIcon(activity, game)
.toDrawable(YuzuApplication.appContext.resources) .toDrawable(YuzuApplication.appContext.resources)
) )
val inset = YuzuApplication.appContext.resources val inset = YuzuApplication.appContext.resources
.getDimensionPixelSize(R.dimen.icon_inset) .getDimensionPixelSize(R.dimen.icon_inset)
layerDrawable.setLayerInset(1, inset, inset, inset, inset) layerDrawable.setLayerInset(1, inset, inset, inset, inset)
val shortcut = val shortcut =
ShortcutInfoCompat.Builder(YuzuApplication.appContext, holder.game.path) ShortcutInfoCompat.Builder(YuzuApplication.appContext, game.path)
.setShortLabel(holder.game.title) .setShortLabel(game.title)
.setIcon( .setIcon(
IconCompat.createWithAdaptiveBitmap( IconCompat.createWithAdaptiveBitmap(
layerDrawable.toBitmap(config = Bitmap.Config.ARGB_8888) layerDrawable.toBitmap(config = Bitmap.Config.ARGB_8888)
@ -126,50 +124,14 @@ class GameAdapter(private val activity: AppCompatActivity) :
} }
} }
val action = HomeNavigationDirections.actionGlobalEmulationActivity(holder.game, true) val action = HomeNavigationDirections.actionGlobalEmulationActivity(game, true)
view.findNavController().navigate(action) binding.root.findNavController().navigate(action)
} }
override fun onLongClick(view: View): Boolean { fun onLongClick(game: Game): Boolean {
val holder = view.tag as GameViewHolder val action = HomeNavigationDirections.actionGlobalPerGamePropertiesFragment(game)
val action = HomeNavigationDirections.actionGlobalPerGamePropertiesFragment(holder.game) binding.root.findNavController().navigate(action)
view.findNavController().navigate(action)
return true return true
} }
inner class GameViewHolder(val binding: CardGameBinding) :
RecyclerView.ViewHolder(binding.root) {
lateinit var game: Game
init {
binding.cardGame.tag = this
}
fun bind(game: Game) {
this.game = game
binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP
GameIconUtils.loadGameIcon(game, binding.imageGameScreen)
binding.textGameTitle.text = game.title.replace("[\\t\\n\\r]+".toRegex(), " ")
binding.textGameTitle.postDelayed(
{
binding.textGameTitle.ellipsize = TextUtils.TruncateAt.MARQUEE
binding.textGameTitle.isSelected = true
},
3000
)
}
}
private class DiffCallback : DiffUtil.ItemCallback<Game>() {
override fun areItemsTheSame(oldItem: Game, newItem: Game): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Game, newItem: Game): Boolean {
return oldItem == newItem
}
} }
} }