Named presets

This commit is contained in:
Kelvin 2023-12-15 20:10:28 +01:00
parent 4930ea8183
commit 35c5045b3f
3 changed files with 39 additions and 12 deletions

View file

@ -0,0 +1,20 @@
package com.futo.platformplayer
class PresetImages {
companion object {
val images = mapOf<String, Int>(
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;
}
}
}

View file

@ -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));
}

View file

@ -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<SelectableCreatorBarViewHolder.Selectable, SelectableCreatorBarViewHolder>;
private val _creators: ArrayList<SelectableCreatorBarViewHolder.Selectable> = arrayListOf();
private val _presets: ArrayList<PresetImage> = 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<PresetImage> =
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<RecyclerView>(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<PresetImage>();
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);
}