Use popup menus for reader shortcuts instead of toggling through

This commit is contained in:
arkon 2021-04-24 19:11:23 -04:00
parent 08a6db7d6e
commit 5f9574541f
8 changed files with 78 additions and 44 deletions

View File

@ -81,15 +81,14 @@ class DownloadHolder(private val view: View, val adapter: DownloadAdapter) :
private fun showPopupMenu(view: View) { private fun showPopupMenu(view: View) {
view.popupMenu( 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_top).isVisible = bindingAdapterPosition != 0
findItem(R.id.move_to_bottom).isVisible = findItem(R.id.move_to_bottom).isVisible =
bindingAdapterPosition != adapter.itemCount - 1 bindingAdapterPosition != adapter.itemCount - 1
}, },
{ onMenuItemClick = {
adapter.downloadItemListener.onMenuItemClick(bindingAdapterPosition, this) adapter.downloadItemListener.onMenuItemClick(bindingAdapterPosition, this)
true
} }
) )
} }

View File

@ -51,16 +51,12 @@ class ChaptersSettingsSheet(
private fun showPopupMenu(view: View) { private fun showPopupMenu(view: View) {
view.popupMenu( view.popupMenu(
R.menu.default_chapter_filter, menuRes = R.menu.default_chapter_filter,
{ onMenuItemClick = {
}, when (itemId) {
{
when (this.itemId) {
R.id.set_as_default -> { R.id.set_as_default -> {
SetChapterSettingsDialog(presenter.manga).showDialog(router) SetChapterSettingsDialog(presenter.manga).showDialog(router)
true
} }
else -> true
} }
} }
) )

View File

@ -29,7 +29,6 @@ open class BaseChapterHolder(
}, },
onMenuItemClick = { onMenuItemClick = {
adapter.clickListener.deleteChapter(position) adapter.clickListener.deleteChapter(position)
true
} }
) )
} }

View File

@ -58,6 +58,7 @@ import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.defaultBar import eu.kanade.tachiyomi.util.view.defaultBar
import eu.kanade.tachiyomi.util.view.hideBar import eu.kanade.tachiyomi.util.view.hideBar
import eu.kanade.tachiyomi.util.view.isDefaultBar 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.setTooltip
import eu.kanade.tachiyomi.util.view.showBar import eu.kanade.tachiyomi.util.view.showBar
import eu.kanade.tachiyomi.widget.SimpleAnimationListener import eu.kanade.tachiyomi.widget.SimpleAnimationListener
@ -356,8 +357,12 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
setTooltip(R.string.viewer) setTooltip(R.string.viewer)
setOnClickListener { setOnClickListener {
val newReadingMode = popupMenu(
ReadingModeType.getNextReadingMode(presenter.getMangaViewer(resolveDefault = false)) items = ReadingModeType.values().map { it.prefValue to it.stringRes },
selectedItemId = presenter.getMangaViewer(resolveDefault = false),
) {
val newReadingMode = ReadingModeType.fromPreference(itemId)
presenter.setMangaViewer(newReadingMode.prefValue) presenter.setMangaViewer(newReadingMode.prefValue)
menuToggleToast?.cancel() menuToggleToast?.cancel()
@ -366,13 +371,18 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
} }
} }
} }
}
// Rotation // Rotation
with(binding.actionRotation) { with(binding.actionRotation) {
setTooltip(R.string.pref_rotation_type) setTooltip(R.string.pref_rotation_type)
setOnClickListener { 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) preferences.rotation().set(newOrientation.prefValue)
setOrientation(newOrientation.flag) setOrientation(newOrientation.flag)
@ -381,6 +391,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
menuToggleToast = toast(newOrientation.stringRes) menuToggleToast = toast(newOrientation.stringRes)
} }
} }
}
preferences.rotation().asImmediateFlow { updateRotationShortcut(it) } preferences.rotation().asImmediateFlow { updateRotationShortcut(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)

View File

@ -4,7 +4,6 @@ import android.content.pm.ActivityInfo
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import eu.kanade.tachiyomi.R 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) { 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), 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 { companion object {
fun fromPreference(preference: Int): OrientationType = fun fromPreference(preference: Int): OrientationType =
values().find { it.prefValue == preference } ?: FREE values().find { it.prefValue == preference } ?: FREE
fun getNextOrientation(preference: Int): OrientationType {
val current = fromPreference(preference)
return current.next()
}
} }
} }

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.reader.setting
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import eu.kanade.tachiyomi.R 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) { 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), 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 { companion object {
fun fromPreference(preference: Int): ReadingModeType = values().find { it.prefValue == preference } ?: DEFAULT 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 { fun isPagerType(preference: Int): Boolean {
val mode = fromPreference(preference) val mode = fromPreference(preference)
return mode == LEFT_TO_RIGHT || mode == RIGHT_TO_LEFT || mode == VERTICAL return mode == LEFT_TO_RIGHT || mode == RIGHT_TO_LEFT || mode == VERTICAL

View File

@ -1,7 +0,0 @@
package eu.kanade.tachiyomi.util.lang
inline fun <reified T : Enum<T>> T.next(): T {
val values = enumValues<T>()
val nextOrdinal = (ordinal + 1) % values.size
return values[nextOrdinal]
}

View File

@ -2,6 +2,7 @@
package eu.kanade.tachiyomi.util.view package eu.kanade.tachiyomi.util.view
import android.annotation.SuppressLint
import android.graphics.Point import android.graphics.Point
import android.view.Gravity import android.view.Gravity
import android.view.Menu import android.view.Menu
@ -9,14 +10,18 @@ import android.view.MenuItem
import android.view.View import android.view.View
import androidx.annotation.MenuRes import androidx.annotation.MenuRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.TooltipCompat import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.core.view.forEach
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup import com.google.android.material.chip.ChipGroup
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.getResourceColor
/** /**
* Returns coordinates of view. * Returns coordinates of view.
@ -63,7 +68,7 @@ inline fun View.setTooltip(@StringRes stringRes: Int) {
inline fun View.popupMenu( inline fun View.popupMenu(
@MenuRes menuRes: Int, @MenuRes menuRes: Int,
noinline initMenu: (Menu.() -> Unit)? = null, noinline initMenu: (Menu.() -> Unit)? = null,
noinline onMenuItemClick: MenuItem.() -> Boolean noinline onMenuItemClick: MenuItem.() -> Unit
): PopupMenu { ): PopupMenu {
val popup = PopupMenu(context, this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0) val popup = PopupMenu(context, this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0)
popup.menuInflater.inflate(menuRes, popup.menu) popup.menuInflater.inflate(menuRes, popup.menu)
@ -71,7 +76,50 @@ inline fun View.popupMenu(
if (initMenu != null) { if (initMenu != null) {
popup.menu.initMenu() 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<Pair<Int, Int>>,
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() popup.show()
return popup return popup