From 5f9574541fae585d28777ed61cb37313189d6a26 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 24 Apr 2021 19:11:23 -0400 Subject: [PATCH] Use popup menus for reader shortcuts instead of toggling through --- .../tachiyomi/ui/download/DownloadHolder.kt | 7 ++- .../ui/manga/chapter/ChaptersSettingsSheet.kt | 10 ++-- .../manga/chapter/base/BaseChapterHolder.kt | 1 - .../tachiyomi/ui/reader/ReaderActivity.kt | 33 ++++++++---- .../ui/reader/setting/OrientationType.kt | 6 --- .../ui/reader/setting/ReadingModeType.kt | 6 --- .../tachiyomi/util/lang/EnumExtensions.kt | 7 --- .../tachiyomi/util/view/ViewExtensions.kt | 52 ++++++++++++++++++- 8 files changed, 78 insertions(+), 44 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt index aa3e7b14b..f7d76643a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt @@ -81,15 +81,14 @@ class DownloadHolder(private val view: View, val adapter: DownloadAdapter) : private fun showPopupMenu(view: View) { view.popupMenu( - R.menu.download_single, - { + menuRes = R.menu.download_single, + initMenu = { findItem(R.id.move_to_top).isVisible = bindingAdapterPosition != 0 findItem(R.id.move_to_bottom).isVisible = bindingAdapterPosition != adapter.itemCount - 1 }, - { + onMenuItemClick = { adapter.downloadItemListener.onMenuItemClick(bindingAdapterPosition, this) - true } ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt index 48ba1784a..4a23cad6d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt @@ -51,16 +51,12 @@ class ChaptersSettingsSheet( private fun showPopupMenu(view: View) { view.popupMenu( - R.menu.default_chapter_filter, - { - }, - { - when (this.itemId) { + menuRes = R.menu.default_chapter_filter, + onMenuItemClick = { + when (itemId) { R.id.set_as_default -> { SetChapterSettingsDialog(presenter.manga).showDialog(router) - true } - else -> true } } ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt index 60daf5405..4506ab59d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt @@ -29,7 +29,6 @@ open class BaseChapterHolder( }, onMenuItemClick = { adapter.clickListener.deleteChapter(position) - true } ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 327da24c5..71ce36ef0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -58,6 +58,7 @@ import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.defaultBar import eu.kanade.tachiyomi.util.view.hideBar import eu.kanade.tachiyomi.util.view.isDefaultBar +import eu.kanade.tachiyomi.util.view.popupMenu import eu.kanade.tachiyomi.util.view.setTooltip import eu.kanade.tachiyomi.util.view.showBar import eu.kanade.tachiyomi.widget.SimpleAnimationListener @@ -356,13 +357,18 @@ class ReaderActivity : BaseRxActivity() setTooltip(R.string.viewer) setOnClickListener { - val newReadingMode = - ReadingModeType.getNextReadingMode(presenter.getMangaViewer(resolveDefault = false)) - presenter.setMangaViewer(newReadingMode.prefValue) + popupMenu( + items = ReadingModeType.values().map { it.prefValue to it.stringRes }, + selectedItemId = presenter.getMangaViewer(resolveDefault = false), + ) { + val newReadingMode = ReadingModeType.fromPreference(itemId) - menuToggleToast?.cancel() - if (!preferences.showReadingMode()) { - menuToggleToast = toast(newReadingMode.stringRes) + presenter.setMangaViewer(newReadingMode.prefValue) + + menuToggleToast?.cancel() + if (!preferences.showReadingMode()) { + menuToggleToast = toast(newReadingMode.stringRes) + } } } } @@ -372,13 +378,18 @@ class ReaderActivity : BaseRxActivity() setTooltip(R.string.pref_rotation_type) setOnClickListener { - val newOrientation = OrientationType.getNextOrientation(preferences.rotation().get()) + popupMenu( + items = OrientationType.values().map { it.prefValue to it.stringRes }, + selectedItemId = preferences.rotation().get(), + ) { + val newOrientation = OrientationType.fromPreference(itemId) - preferences.rotation().set(newOrientation.prefValue) - setOrientation(newOrientation.flag) + preferences.rotation().set(newOrientation.prefValue) + setOrientation(newOrientation.flag) - menuToggleToast?.cancel() - menuToggleToast = toast(newOrientation.stringRes) + menuToggleToast?.cancel() + menuToggleToast = toast(newOrientation.stringRes) + } } } preferences.rotation().asImmediateFlow { updateRotationShortcut(it) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt index 5f358a670..007b5e27a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt @@ -4,7 +4,6 @@ import android.content.pm.ActivityInfo import androidx.annotation.DrawableRes import androidx.annotation.StringRes import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.util.lang.next enum class OrientationType(val prefValue: Int, val flag: Int, @StringRes val stringRes: Int, @DrawableRes val iconRes: Int) { FREE(1, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED, R.string.rotation_free, R.drawable.ic_screen_rotation_24dp), @@ -17,10 +16,5 @@ enum class OrientationType(val prefValue: Int, val flag: Int, @StringRes val str companion object { fun fromPreference(preference: Int): OrientationType = values().find { it.prefValue == preference } ?: FREE - - fun getNextOrientation(preference: Int): OrientationType { - val current = fromPreference(preference) - return current.next() - } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt index 4f0c1a3f8..936546645 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.reader.setting import androidx.annotation.DrawableRes import androidx.annotation.StringRes import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.util.lang.next enum class ReadingModeType(val prefValue: Int, @StringRes val stringRes: Int, @DrawableRes val iconRes: Int) { DEFAULT(0, R.string.default_viewer, R.drawable.ic_reader_default_24dp), @@ -17,11 +16,6 @@ enum class ReadingModeType(val prefValue: Int, @StringRes val stringRes: Int, @D companion object { fun fromPreference(preference: Int): ReadingModeType = values().find { it.prefValue == preference } ?: DEFAULT - fun getNextReadingMode(preference: Int): ReadingModeType { - val current = fromPreference(preference) - return current.next() - } - fun isPagerType(preference: Int): Boolean { val mode = fromPreference(preference) return mode == LEFT_TO_RIGHT || mode == RIGHT_TO_LEFT || mode == VERTICAL diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt deleted file mode 100644 index 4c9432303..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt +++ /dev/null @@ -1,7 +0,0 @@ -package eu.kanade.tachiyomi.util.lang - -inline fun > T.next(): T { - val values = enumValues() - val nextOrdinal = (ordinal + 1) % values.size - return values[nextOrdinal] -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt index c50f111bf..3133f0e95 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.util.view +import android.annotation.SuppressLint import android.graphics.Point import android.view.Gravity import android.view.Menu @@ -9,14 +10,18 @@ import android.view.MenuItem import android.view.View import androidx.annotation.MenuRes import androidx.annotation.StringRes +import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.TooltipCompat +import androidx.core.content.ContextCompat +import androidx.core.view.forEach import androidx.recyclerview.widget.RecyclerView import com.google.android.material.chip.Chip import com.google.android.material.chip.ChipGroup import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton import com.google.android.material.snackbar.Snackbar import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.util.system.getResourceColor /** * Returns coordinates of view. @@ -63,7 +68,7 @@ inline fun View.setTooltip(@StringRes stringRes: Int) { inline fun View.popupMenu( @MenuRes menuRes: Int, noinline initMenu: (Menu.() -> Unit)? = null, - noinline onMenuItemClick: MenuItem.() -> Boolean + noinline onMenuItemClick: MenuItem.() -> Unit ): PopupMenu { val popup = PopupMenu(context, this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0) popup.menuInflater.inflate(menuRes, popup.menu) @@ -71,7 +76,50 @@ inline fun View.popupMenu( if (initMenu != null) { popup.menu.initMenu() } - popup.setOnMenuItemClickListener { it.onMenuItemClick() } + popup.setOnMenuItemClickListener { + it.onMenuItemClick() + true + } + + popup.show() + return popup +} + +/** + * Shows a popup menu on top of this view. + * + * @param items menu item names to inflate the menu with. List of itemId to stringRes pairs. + * @param selectedItemId optionally show a checkmark beside an item with this itemId. + * @param onMenuItemClick function to execute when a menu item is clicked. + */ +@SuppressLint("RestrictedApi") +inline fun View.popupMenu( + items: List>, + selectedItemId: Int? = null, + noinline onMenuItemClick: MenuItem.() -> Unit +): PopupMenu { + val popup = PopupMenu(context, this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0) + items.forEach { (id, stringRes) -> + popup.menu.add(0, id, 0, stringRes) + } + + if (selectedItemId != null) { + (popup.menu as? MenuBuilder)?.setOptionalIconsVisible(true) + val emptyIcon = ContextCompat.getDrawable(context, R.drawable.ic_blank_24dp) + popup.menu.forEach { item -> + item.icon = when (item.itemId) { + selectedItemId -> ContextCompat.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply { + setTint(context.getResourceColor(android.R.attr.textColorPrimary)) + } + else -> emptyIcon + } + } + } + + popup.setOnMenuItemClickListener { + it.onMenuItemClick() + true + } popup.show() return popup