From 3287ca9cf264fb0d28bdd7e43404cef595d953de Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 16 Apr 2021 22:39:19 -0400 Subject: [PATCH] Add checkmark beside selected popup menu item Based on what's in J2K. Also renamed to MaterialSpinnerView to match what's there. Co-authored-by: Jays2Kings --- .../MaterialSpinnerView.kt} | 42 ++++++++++++++++--- app/src/main/res/drawable/ic_blank_24dp.xml | 7 ++++ app/src/main/res/drawable/ic_check_24dp.xml | 9 ++++ .../res/layout/reader_general_settings.xml | 4 +- .../main/res/layout/reader_pager_settings.xml | 8 ++-- .../layout/reader_reading_mode_settings.xml | 2 +- .../res/layout/reader_webtoon_settings.xml | 6 +-- app/src/main/res/values/attrs.xml | 2 +- 8 files changed, 64 insertions(+), 16 deletions(-) rename app/src/main/java/eu/kanade/tachiyomi/{ui/reader/setting/SpinnerPreference.kt => widget/MaterialSpinnerView.kt} (72%) create mode 100644 app/src/main/res/drawable/ic_blank_24dp.xml create mode 100644 app/src/main/res/drawable/ic_check_24dp.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/SpinnerPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/MaterialSpinnerView.kt similarity index 72% rename from app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/SpinnerPreference.kt rename to app/src/main/java/eu/kanade/tachiyomi/widget/MaterialSpinnerView.kt index c0183075c7..bb6eae9e0f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/SpinnerPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/MaterialSpinnerView.kt @@ -1,5 +1,6 @@ -package eu.kanade.tachiyomi.ui.reader.setting +package eu.kanade.tachiyomi.widget +import android.annotation.SuppressLint import android.content.Context import android.util.AttributeSet import android.view.Gravity @@ -7,15 +8,21 @@ import android.view.LayoutInflater import android.view.MenuItem import android.widget.FrameLayout import androidx.annotation.ArrayRes +import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.widget.PopupMenu +import androidx.core.content.ContextCompat +import androidx.core.view.forEach +import androidx.core.view.get import com.tfcporciuncula.flow.Preference import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.databinding.SpinnerPreferenceBinding +import eu.kanade.tachiyomi.util.system.getResourceColor -class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : +class MaterialSpinnerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) { private var entries = emptyList() + private var selectedPosition = 0 private var popup: PopupMenu? = null var onItemSelectedListener: ((Int) -> Unit)? = null @@ -30,17 +37,26 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri } } + private val emptyIcon by lazy { + ContextCompat.getDrawable(context, R.drawable.ic_blank_24dp) + } + private val checkmarkIcon by lazy { + ContextCompat.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply { + setTint(context.getResourceColor(android.R.attr.textColorPrimary)) + } + } + private val binding = SpinnerPreferenceBinding.inflate(LayoutInflater.from(context), this, false) init { addView(binding.root) - val attr = context.obtainStyledAttributes(attrs, R.styleable.SpinnerPreference) + val attr = context.obtainStyledAttributes(attrs, R.styleable.MaterialSpinnerView) - val title = attr.getString(R.styleable.SpinnerPreference_title).orEmpty() + val title = attr.getString(R.styleable.MaterialSpinnerView_title).orEmpty() binding.title.text = title - val entries = (attr.getTextArray(R.styleable.SpinnerPreference_android_entries) ?: emptyArray()).map { it.toString() } + val entries = (attr.getTextArray(R.styleable.MaterialSpinnerView_android_entries) ?: emptyArray()).map { it.toString() } this.entries = entries binding.details.text = entries.firstOrNull().orEmpty() @@ -48,6 +64,14 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri } fun setSelection(selection: Int) { + popup?.menu?.get(selectedPosition)?.let { + it.icon = emptyIcon + it.title = entries[selectedPosition] + } + selectedPosition = selection + popup?.menu?.get(selectedPosition)?.let { + it.icon = checkmarkIcon + } binding.details.text = entries.getOrNull(selection).orEmpty() } @@ -118,11 +142,19 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri return pos } + @SuppressLint("RestrictedApi") fun createPopupMenu(onItemClick: (Int) -> Unit): PopupMenu { val popup = PopupMenu(context, this, Gravity.END, R.attr.actionOverflowMenuStyle, 0) entries.forEachIndexed { index, entry -> popup.menu.add(0, index, 0, entry) } + (popup.menu as? MenuBuilder)?.setOptionalIconsVisible(true) + popup.menu.forEach { + it.icon = emptyIcon + } + popup.menu.getItem(selectedPosition)?.let { + it.icon = checkmarkIcon + } popup.setOnMenuItemClickListener { menuItem -> val pos = menuClicked(menuItem) onItemClick(pos) diff --git a/app/src/main/res/drawable/ic_blank_24dp.xml b/app/src/main/res/drawable/ic_blank_24dp.xml new file mode 100644 index 0000000000..940db069fd --- /dev/null +++ b/app/src/main/res/drawable/ic_blank_24dp.xml @@ -0,0 +1,7 @@ + + + + diff --git a/app/src/main/res/drawable/ic_check_24dp.xml b/app/src/main/res/drawable/ic_check_24dp.xml new file mode 100644 index 0000000000..b268959e37 --- /dev/null +++ b/app/src/main/res/drawable/ic_check_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/reader_general_settings.xml b/app/src/main/res/layout/reader_general_settings.xml index a5690708c6..6c3f25ee79 100644 --- a/app/src/main/res/layout/reader_general_settings.xml +++ b/app/src/main/res/layout/reader_general_settings.xml @@ -10,14 +10,14 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - - - - - - - - - +