mirror of
https://github.com/yuzu-emu/yuzu.git
synced 2024-11-26 21:05:37 +01:00
android: Refactor async diff adapters to use AbstractDiffAdapter
This commit is contained in:
parent
51ad2d10de
commit
78c323c4eb
@ -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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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,122 +31,26 @@ 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) =
|
|
||||||
holder.bind(currentList[position])
|
|
||||||
|
|
||||||
override fun getItemCount(): Int = currentList.size
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Launches the game that was clicked on.
|
|
||||||
*
|
|
||||||
* @param view The card representing the game the user wants to play.
|
|
||||||
*/
|
|
||||||
override fun onClick(view: View) {
|
|
||||||
val holder = view.tag as GameViewHolder
|
|
||||||
|
|
||||||
val gameExists = DocumentFile.fromSingleUri(
|
|
||||||
YuzuApplication.appContext,
|
|
||||||
Uri.parse(holder.game.path)
|
|
||||||
)?.exists() == true
|
|
||||||
if (!gameExists) {
|
|
||||||
Toast.makeText(
|
|
||||||
YuzuApplication.appContext,
|
|
||||||
R.string.loader_error_file_not_found,
|
|
||||||
Toast.LENGTH_LONG
|
|
||||||
).show()
|
|
||||||
|
|
||||||
ViewModelProvider(activity)[GamesViewModel::class.java].reloadGames(true)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val preferences = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
|
|
||||||
preferences.edit()
|
|
||||||
.putLong(
|
|
||||||
holder.game.keyLastPlayedTime,
|
|
||||||
System.currentTimeMillis()
|
|
||||||
)
|
|
||||||
.apply()
|
|
||||||
|
|
||||||
val openIntent = Intent(YuzuApplication.appContext, EmulationActivity::class.java).apply {
|
|
||||||
action = Intent.ACTION_VIEW
|
|
||||||
data = Uri.parse(holder.game.path)
|
|
||||||
}
|
|
||||||
|
|
||||||
activity.lifecycleScope.launch {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
val layerDrawable = ResourcesCompat.getDrawable(
|
|
||||||
YuzuApplication.appContext.resources,
|
|
||||||
R.drawable.shortcut,
|
|
||||||
null
|
|
||||||
) as LayerDrawable
|
|
||||||
layerDrawable.setDrawableByLayerId(
|
|
||||||
R.id.shortcut_foreground,
|
|
||||||
GameIconUtils.getGameIcon(activity, holder.game)
|
|
||||||
.toDrawable(YuzuApplication.appContext.resources)
|
|
||||||
)
|
|
||||||
val inset = YuzuApplication.appContext.resources
|
|
||||||
.getDimensionPixelSize(R.dimen.icon_inset)
|
|
||||||
layerDrawable.setLayerInset(1, inset, inset, inset, inset)
|
|
||||||
val shortcut =
|
|
||||||
ShortcutInfoCompat.Builder(YuzuApplication.appContext, holder.game.path)
|
|
||||||
.setShortLabel(holder.game.title)
|
|
||||||
.setIcon(
|
|
||||||
IconCompat.createWithAdaptiveBitmap(
|
|
||||||
layerDrawable.toBitmap(config = Bitmap.Config.ARGB_8888)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.setIntent(openIntent)
|
|
||||||
.build()
|
|
||||||
ShortcutManagerCompat.pushDynamicShortcut(YuzuApplication.appContext, shortcut)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val action = HomeNavigationDirections.actionGlobalEmulationActivity(holder.game, true)
|
|
||||||
view.findNavController().navigate(action)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onLongClick(view: View): Boolean {
|
|
||||||
val holder = view.tag as GameViewHolder
|
|
||||||
val action = HomeNavigationDirections.actionGlobalPerGamePropertiesFragment(holder.game)
|
|
||||||
view.findNavController().navigate(action)
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class GameViewHolder(val binding: CardGameBinding) :
|
inner class GameViewHolder(val binding: CardGameBinding) :
|
||||||
RecyclerView.ViewHolder(binding.root) {
|
AbstractViewHolder<Game>(binding) {
|
||||||
lateinit var game: Game
|
override fun bind(model: Game) {
|
||||||
|
|
||||||
init {
|
|
||||||
binding.cardGame.tag = this
|
|
||||||
}
|
|
||||||
|
|
||||||
fun bind(game: Game) {
|
|
||||||
this.game = game
|
|
||||||
|
|
||||||
binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP
|
binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP
|
||||||
GameIconUtils.loadGameIcon(game, binding.imageGameScreen)
|
GameIconUtils.loadGameIcon(model, binding.imageGameScreen)
|
||||||
|
|
||||||
binding.textGameTitle.text = game.title.replace("[\\t\\n\\r]+".toRegex(), " ")
|
binding.textGameTitle.text = model.title.replace("[\\t\\n\\r]+".toRegex(), " ")
|
||||||
|
|
||||||
binding.textGameTitle.postDelayed(
|
binding.textGameTitle.postDelayed(
|
||||||
{
|
{
|
||||||
@ -160,16 +59,79 @@ class GameAdapter(private val activity: AppCompatActivity) :
|
|||||||
},
|
},
|
||||||
3000
|
3000
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class DiffCallback : DiffUtil.ItemCallback<Game>() {
|
binding.cardGame.setOnClickListener { onClick(model) }
|
||||||
override fun areItemsTheSame(oldItem: Game, newItem: Game): Boolean {
|
binding.cardGame.setOnLongClickListener { onLongClick(model) }
|
||||||
return oldItem == newItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: Game, newItem: Game): Boolean {
|
fun onClick(game: Game) {
|
||||||
return oldItem == newItem
|
val gameExists = DocumentFile.fromSingleUri(
|
||||||
|
YuzuApplication.appContext,
|
||||||
|
Uri.parse(game.path)
|
||||||
|
)?.exists() == true
|
||||||
|
if (!gameExists) {
|
||||||
|
Toast.makeText(
|
||||||
|
YuzuApplication.appContext,
|
||||||
|
R.string.loader_error_file_not_found,
|
||||||
|
Toast.LENGTH_LONG
|
||||||
|
).show()
|
||||||
|
|
||||||
|
ViewModelProvider(activity)[GamesViewModel::class.java].reloadGames(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val preferences =
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
|
||||||
|
preferences.edit()
|
||||||
|
.putLong(
|
||||||
|
game.keyLastPlayedTime,
|
||||||
|
System.currentTimeMillis()
|
||||||
|
)
|
||||||
|
.apply()
|
||||||
|
|
||||||
|
val openIntent =
|
||||||
|
Intent(YuzuApplication.appContext, EmulationActivity::class.java).apply {
|
||||||
|
action = Intent.ACTION_VIEW
|
||||||
|
data = Uri.parse(game.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
activity.lifecycleScope.launch {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
val layerDrawable = ResourcesCompat.getDrawable(
|
||||||
|
YuzuApplication.appContext.resources,
|
||||||
|
R.drawable.shortcut,
|
||||||
|
null
|
||||||
|
) as LayerDrawable
|
||||||
|
layerDrawable.setDrawableByLayerId(
|
||||||
|
R.id.shortcut_foreground,
|
||||||
|
GameIconUtils.getGameIcon(activity, game)
|
||||||
|
.toDrawable(YuzuApplication.appContext.resources)
|
||||||
|
)
|
||||||
|
val inset = YuzuApplication.appContext.resources
|
||||||
|
.getDimensionPixelSize(R.dimen.icon_inset)
|
||||||
|
layerDrawable.setLayerInset(1, inset, inset, inset, inset)
|
||||||
|
val shortcut =
|
||||||
|
ShortcutInfoCompat.Builder(YuzuApplication.appContext, game.path)
|
||||||
|
.setShortLabel(game.title)
|
||||||
|
.setIcon(
|
||||||
|
IconCompat.createWithAdaptiveBitmap(
|
||||||
|
layerDrawable.toBitmap(config = Bitmap.Config.ARGB_8888)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.setIntent(openIntent)
|
||||||
|
.build()
|
||||||
|
ShortcutManagerCompat.pushDynamicShortcut(YuzuApplication.appContext, shortcut)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val action = HomeNavigationDirections.actionGlobalEmulationActivity(game, true)
|
||||||
|
binding.root.findNavController().navigate(action)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onLongClick(game: Game): Boolean {
|
||||||
|
val action = HomeNavigationDirections.actionGlobalPerGamePropertiesFragment(game)
|
||||||
|
binding.root.findNavController().navigate(action)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user