diff --git a/app/src/main/java/com/futo/platformplayer/PresetImages.kt b/app/src/main/java/com/futo/platformplayer/PresetImages.kt new file mode 100644 index 00000000..cbdf2834 --- /dev/null +++ b/app/src/main/java/com/futo/platformplayer/PresetImages.kt @@ -0,0 +1,20 @@ +package com.futo.platformplayer + +class PresetImages { + companion object { + val images = mapOf( + Pair("xp_book", R.drawable.xp_book), + Pair("xp_forest", R.drawable.xp_forest), + Pair("xp_code", R.drawable.xp_code), + Pair("xp_controller", R.drawable.xp_controller), + Pair("xp_laptop", R.drawable.xp_laptop) + ); + + fun getPresetResIdByName(name: String): Int { + return images[name] ?: -1; + } + fun getPresetNameByResId(id: Int): String? { + return images.entries.find { it.value == id }?.key; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/futo/platformplayer/models/ImageVariable.kt b/app/src/main/java/com/futo/platformplayer/models/ImageVariable.kt index 84715c5e..1de1f917 100644 --- a/app/src/main/java/com/futo/platformplayer/models/ImageVariable.kt +++ b/app/src/main/java/com/futo/platformplayer/models/ImageVariable.kt @@ -1,9 +1,11 @@ package com.futo.platformplayer.models +import android.annotation.SuppressLint import android.graphics.Bitmap import android.graphics.BitmapFactory import android.widget.ImageView import com.bumptech.glide.Glide +import com.futo.platformplayer.PresetImages import com.futo.platformplayer.R import kotlinx.serialization.Contextual import kotlinx.serialization.Transient @@ -15,8 +17,10 @@ data class ImageVariable( val resId: Int? = null, @Transient @Contextual - private val bitmap: Bitmap? = null) { + private val bitmap: Bitmap? = null, + val presetName: String? = null) { + @SuppressLint("DiscouragedApi") fun setImageView(imageView: ImageView, fallbackResId: Int = -1) { if(bitmap != null) { Glide.with(imageView) @@ -31,6 +35,9 @@ data class ImageVariable( .load(url) .placeholder(R.drawable.placeholder_channel_thumbnail) .into(imageView); + } else if(!presetName.isNullOrEmpty()) { + val resId = PresetImages.getPresetResIdByName(presetName); + imageView.setImageResource(resId); } else if (fallbackResId != -1) { Glide.with(imageView) .load(fallbackResId) @@ -52,6 +59,9 @@ data class ImageVariable( fun fromBitmap(bitmap: Bitmap): ImageVariable { return ImageVariable(null, null, bitmap); } + fun fromPresetName(str: String): ImageVariable { + return ImageVariable(null, null, null, str); + } fun fromFile(file: File): ImageVariable { return ImageVariable.fromBitmap(BitmapFactory.decodeFile(file.absolutePath)); } diff --git a/app/src/main/java/com/futo/platformplayer/views/overlays/ImageVariableOverlay.kt b/app/src/main/java/com/futo/platformplayer/views/overlays/ImageVariableOverlay.kt index ca577654..0971d577 100644 --- a/app/src/main/java/com/futo/platformplayer/views/overlays/ImageVariableOverlay.kt +++ b/app/src/main/java/com/futo/platformplayer/views/overlays/ImageVariableOverlay.kt @@ -19,6 +19,7 @@ import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide +import com.futo.platformplayer.PresetImages import com.futo.platformplayer.R import com.futo.platformplayer.UIDialogs import com.futo.platformplayer.activities.IWithResultLauncher @@ -53,13 +54,8 @@ class ImageVariableOverlay: ConstraintLayout { private val _recyclerCreators: AnyAdapterView; private val _creators: ArrayList = arrayListOf(); - private val _presets: ArrayList = arrayListOf( - PresetImage(R.drawable.xp_book, false), - PresetImage(R.drawable.xp_forest, false), - PresetImage(R.drawable.xp_laptop, false), - PresetImage(R.drawable.xp_controller, false), - PresetImage(R.drawable.xp_code, false), - ); + private val _presets: ArrayList = + ArrayList(PresetImages.images.map { PresetImage(it.value, it.key, false) }); private var _selected: ImageVariable? = null; private var _selectedFile: String? = null; @@ -89,7 +85,7 @@ class ImageVariableOverlay: ConstraintLayout { _buttonSelect = findViewById(R.id.button_select); _recyclerPresets = findViewById(R.id.recycler_presets).asAny(_presets, RecyclerView.HORIZONTAL) { it.onClick.subscribe { - _selected = ImageVariable(null, it.id); + _selected = ImageVariable.fromPresetName(it.name); updateSelected(); }; }; @@ -153,8 +149,9 @@ class ImageVariableOverlay: ConstraintLayout { fun updateSelected() { val id = _selected?.resId; + val name = _selected?.presetName; val url = _selected?.url; - _presets.forEach { p -> p.active = p.id == id }; + _presets.forEach { p -> p.active = p.name == name }; _recyclerPresets.notifyContentChanged(); _creators.forEach { p -> p.active = p.channel.thumbnail == url }; _recyclerCreators.notifyContentChanged(); @@ -199,7 +196,7 @@ class ImageVariableOverlay: ConstraintLayout { private val view = _view as LinearLayout; private val imageView = ShapeableImageView(viewGroup.context); - private var value: PresetImage = PresetImage(0, false); + private var value: PresetImage = PresetImage(0, "", false); val onClick = Event1(); init { @@ -233,5 +230,5 @@ class ImageVariableOverlay: ConstraintLayout { } } - data class PresetImage(var id: Int, var active: Boolean); + data class PresetImage(var id: Int, var name: String, var active: Boolean); } \ No newline at end of file