Centralize missingIcon and condition for use in DataItem

`One missing Bitmap to rule them all and one condition to find them.` Also eliminates passing that condition between methods. The data class can simply return the same instance every time it's necessary.
This commit is contained in:
Abandoned Cart 2023-03-14 17:45:58 -04:00 committed by Niccolò Betto
parent 905c0a47fa
commit e68baf9088
4 changed files with 14 additions and 12 deletions

View File

@ -15,8 +15,6 @@ import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.snackbar.Snackbar
@ -65,9 +63,7 @@ class AppDialog : BottomSheetDialogFragment() {
}
}
val missingIcon = ContextCompat.getDrawable(requireActivity(), R.drawable.default_icon)!!.toBitmap(256, 256)
binding.gameIcon.setImageBitmap(item.icon ?: missingIcon)
binding.gameIcon.setImageBitmap(item.bitmapIcon)
binding.gameTitle.text = item.title
binding.gameVersion.text = item.version ?: item.loaderResultString(requireContext())
binding.gameTitleId.text = item.titleId
@ -94,7 +90,7 @@ class AppDialog : BottomSheetDialogFragment() {
val info = ShortcutInfo.Builder(context, item.title)
info.setShortLabel(item.title)
info.setActivity(ComponentName(requireContext(), EmulationActivity::class.java))
info.setIcon(Icon.createWithAdaptiveBitmap(item.icon ?: missingIcon))
info.setIcon(Icon.createWithAdaptiveBitmap(item.bitmapIcon))
val intent = Intent(context, EmulationActivity::class.java)
intent.data = item.uri

View File

@ -61,8 +61,6 @@ class MainActivity : AppCompatActivity() {
private val layoutType get() = LayoutType.values()[appSettings.layoutType]
private val missingIcon by lazy { ContextCompat.getDrawable(this, R.drawable.default_icon)!!.toBitmap(256, 256) }
private val viewModel by viewModels<MainViewModel>()
private var formatFilter : RomFormat? = null
@ -97,7 +95,7 @@ class MainActivity : AppCompatActivity() {
if (appSettings.refreshRequired) loadRoms(false)
}
private fun AppItem.toViewItem() = AppViewItem(layoutType, this, missingIcon, ::selectStartGame, ::selectShowGameDialog)
private fun AppItem.toViewItem() = AppViewItem(layoutType, this, ::selectStartGame, ::selectShowGameDialog)
override fun onCreate(savedInstanceState : Bundle?) {
// Need to create new instance of settings, dependency injection happens

View File

@ -89,7 +89,7 @@ class GridCompatBinding(parent : ViewGroup) : LayoutBinding<AppItemGridCompactBi
private typealias InteractionFunction = (appItem : AppItem) -> Unit
class AppViewItem(var layoutType : LayoutType, private val item : AppItem, private val missingIcon : Bitmap, private val onClick : InteractionFunction, private val onLongClick : InteractionFunction) : GenericListItem<LayoutBinding<*>>() {
class AppViewItem(var layoutType : LayoutType, private val item : AppItem, private val onClick : InteractionFunction, private val onLongClick : InteractionFunction) : GenericListItem<LayoutBinding<*>>() {
override fun getViewBindingFactory() = LayoutBindingFactory(layoutType)
override fun bind(holder : GenericViewHolder<LayoutBinding<*>>, position : Int) {
@ -102,7 +102,7 @@ class AppViewItem(var layoutType : LayoutType, private val item : AppItem, priva
binding.textVersion.isSelected = true
binding.textAuthor.isSelected = true
binding.icon.setImageBitmap(item.icon ?: missingIcon)
binding.icon.setImageBitmap(item.bitmapIcon)
if (layoutType == LayoutType.List) {
binding.icon.setOnClickListener { showIconDialog(it.context, item) }
@ -123,7 +123,7 @@ class AppViewItem(var layoutType : LayoutType, private val item : AppItem, priva
builder.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val imageView = ImageView(context)
imageView.setImageBitmap(appItem.icon ?: missingIcon)
imageView.setImageBitmap(appItem.bitmapIcon)
builder.addContentView(imageView, RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
builder.show()

View File

@ -6,8 +6,12 @@
package emu.skyline.data
import android.content.Context
import android.graphics.Bitmap
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import emu.skyline.BuildConfig
import emu.skyline.R
import emu.skyline.SkylineApplication
import emu.skyline.loader.AppEntry
import emu.skyline.loader.LoaderResult
import java.io.Serializable
@ -17,6 +21,8 @@ import java.io.Serializable
*/
const val AppItemTag = BuildConfig.APPLICATION_ID + ".APP_ITEM"
private val missingIcon by lazy { ContextCompat.getDrawable(SkylineApplication.instance, R.drawable.default_icon)!!.toBitmap(256, 256) }
sealed class DataItem : Serializable
class HeaderItem(val title : String) : DataItem()
@ -30,6 +36,8 @@ data class AppItem(private val meta : AppEntry) : DataItem() {
*/
val icon get() = meta.icon
val bitmapIcon : Bitmap get() = meta.icon ?: missingIcon
/**
* The title of the application
*/