From 5774dea85414eeb4b59ef4f779ae754abdc01b20 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Thu, 15 Apr 2021 03:23:56 -0400 Subject: [PATCH] Adding option to show reader mode in bottom nav Instead of clicking through, it's showing a popup to pick the reading mode Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com> --- .../tachiyomi/ui/reader/ReaderActivity.kt | 50 +++++++++++++++++-- .../ui/reader/settings/ReadingModeType.kt | 30 +++++++++++ .../tachiyomi/util/lang/EnumExtensions.kt | 7 +++ .../ic_reader_continuous_vertical_24dp.xml | 15 ++++++ .../res/drawable/ic_reader_default_24dp.xml | 12 +++++ .../main/res/drawable/ic_reader_ltr_24dp.xml | 21 ++++++++ .../main/res/drawable/ic_reader_rtl_24dp.xml | 21 ++++++++ .../res/drawable/ic_reader_vertical_24dp.xml | 12 +++++ .../res/drawable/ic_reader_webtoon_24dp.xml | 12 +++++ .../main/res/layout/reader_chapters_sheet.xml | 20 +++++++- 10 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/reader/settings/ReadingModeType.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt create mode 100644 app/src/main/res/drawable/ic_reader_continuous_vertical_24dp.xml create mode 100644 app/src/main/res/drawable/ic_reader_default_24dp.xml create mode 100644 app/src/main/res/drawable/ic_reader_ltr_24dp.xml create mode 100644 app/src/main/res/drawable/ic_reader_rtl_24dp.xml create mode 100644 app/src/main/res/drawable/ic_reader_vertical_24dp.xml create mode 100644 app/src/main/res/drawable/ic_reader_webtoon_24dp.xml 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 83f5b76008..ec19677167 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 @@ -13,6 +13,7 @@ import android.graphics.drawable.AnimatedVectorDrawable import android.graphics.drawable.LayerDrawable import android.os.Build import android.os.Bundle +import android.view.Gravity import android.view.KeyEvent import android.view.Menu import android.view.MenuItem @@ -24,11 +25,14 @@ import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.SeekBar import androidx.annotation.StringRes +import androidx.appcompat.view.menu.MenuBuilder +import androidx.appcompat.widget.PopupMenu import androidx.core.content.ContextCompat import androidx.core.graphics.ColorUtils import androidx.core.view.isVisible import androidx.core.view.GestureDetectorCompat import androidx.core.view.ViewCompat +import androidx.core.view.forEach import com.afollestad.materialdialogs.MaterialDialog import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView import com.google.android.material.bottomsheet.BottomSheetBehavior @@ -51,6 +55,7 @@ import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Success import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters +import eu.kanade.tachiyomi.ui.reader.settings.ReadingModeType import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer import eu.kanade.tachiyomi.ui.reader.viewer.pager.L2RPagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.pager.PageLayout @@ -59,6 +64,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.pager.R2LPagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.pager.VerticalPagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonViewer import eu.kanade.tachiyomi.ui.webview.WebViewActivity +import eu.kanade.tachiyomi.util.lang.tintText import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.system.GLUtil import eu.kanade.tachiyomi.util.system.ThemeUtil @@ -214,6 +220,7 @@ class ReaderActivity : enum class BottomButton(val value: String, @StringRes val stringRes: Int) { ViewChapters("vc", R.string.view_chapters), WebView("wb", R.string.open_in_webview), + ReadingMode("rm", R.string.reading_mode), CropBordersPaged("cbp", R.string.crop_borders_paged), CropBordersWebtoon("cbw", R.string.crop_borders_webtoon), PageLayout("pl", R.string.page_layout), @@ -420,8 +427,9 @@ class ReaderActivity : } } - private fun updateBasicShortcuts() { + private fun updateBottomShortcuts() { with(binding.chaptersSheet) { + readingMode.isVisible = presenter?.manga?.isLongStrip() != true && BottomButton.ReadingMode.value in preferences.readerBottomButtons().get() doublePage.isVisible = viewer is PagerViewer && BottomButton.PageLayout.value in preferences.readerBottomButtons().get() cropBordersSheetButton.isVisible = @@ -542,7 +550,7 @@ class ReaderActivity : /** * Initializes the reader menu. It sets up click listeners and the initial visibility. */ - @SuppressLint("ClickableViewAccessibility") + @SuppressLint("ClickableViewAccessibility", "RestrictedApi") private fun initializeMenu() { // Set binding.toolbar setSupportActionBar(binding.toolbar) @@ -583,6 +591,38 @@ class ReaderActivity : ) preferences.cropBorders() else preferences.cropBordersWebtoon() pref.toggle() } + readingMode.setOnClickListener { + val popup = PopupMenu(this@ReaderActivity, readingMode, Gravity.END) + val enumConstants = ReadingModeType::class.java.enumConstants + val array = enumConstants?.map { it.stringRes }.orEmpty().toTypedArray() + array.forEachIndexed { index, entry -> + popup.menu.add(0, index, 0, entry) + } + if (popup.menu is MenuBuilder) { + val m = popup.menu as MenuBuilder + m.setOptionalIconsVisible(true) + } + val blendedAccent = ColorUtils.blendARGB( + this@ReaderActivity.getResourceColor(android.R.attr.colorAccent), + this@ReaderActivity.getResourceColor(android.R.attr.textColorPrimary), + 0.5f + ) + popup.menu.forEach { + it.icon = ContextCompat.getDrawable(this@ReaderActivity, R.drawable.ic_blank_24dp) + } + popup.menu.getItem(presenter.manga?.viewer ?: 0)?.let { menuItem -> + menuItem.icon = + ContextCompat.getDrawable(this@ReaderActivity, R.drawable.ic_check_24dp) + ?.mutate()?.apply { setTint(blendedAccent) } + menuItem.title = + menuItem.title?.tintText(blendedAccent) + } + popup.setOnMenuItemClickListener { menuItem -> + presenter.setMangaViewer(menuItem.itemId) + true + } + popup.show() + } } listOf(preferences.cropBorders(), preferences.cropBordersWebtoon()) @@ -870,7 +910,9 @@ class ReaderActivity : binding.pleaseWait.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in_long)) invalidateOptionsMenu() updateCropBordersShortcut() - updateBasicShortcuts() + updateBottomShortcuts() + val viewerMode = ReadingModeType.fromPreference(presenter?.manga?.viewer ?: 0) + binding.chaptersSheet.readingMode.setImageResource(viewerMode.iconRes) } override fun onPause() { @@ -1394,7 +1436,7 @@ class ReaderActivity : preferences.readerBottomButtons().asFlow() .onEach { - updateBasicShortcuts() + updateBottomShortcuts() } .launchIn(scope) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/settings/ReadingModeType.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/settings/ReadingModeType.kt new file mode 100644 index 0000000000..4e7fd1b3ea --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/settings/ReadingModeType.kt @@ -0,0 +1,30 @@ +package eu.kanade.tachiyomi.ui.reader.settings + +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_value, R.drawable.ic_reader_default_24dp), + LEFT_TO_RIGHT(1, R.string.left_to_right_viewer, R.drawable.ic_reader_ltr_24dp), + RIGHT_TO_LEFT(2, R.string.right_to_left_viewer, R.drawable.ic_reader_rtl_24dp), + VERTICAL(3, R.string.vertical_viewer, R.drawable.ic_reader_vertical_24dp), + WEBTOON(4, R.string.webtoon, R.drawable.ic_reader_webtoon_24dp), + CONTINUOUS_VERTICAL(5, R.string.continuous_vertical, R.drawable.ic_reader_continuous_vertical_24dp), + ; + + 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 new file mode 100644 index 0000000000..4c9432303f --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt @@ -0,0 +1,7 @@ +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/res/drawable/ic_reader_continuous_vertical_24dp.xml b/app/src/main/res/drawable/ic_reader_continuous_vertical_24dp.xml new file mode 100644 index 0000000000..7997bba7bb --- /dev/null +++ b/app/src/main/res/drawable/ic_reader_continuous_vertical_24dp.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_reader_default_24dp.xml b/app/src/main/res/drawable/ic_reader_default_24dp.xml new file mode 100644 index 0000000000..3ec2d7598e --- /dev/null +++ b/app/src/main/res/drawable/ic_reader_default_24dp.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/drawable/ic_reader_ltr_24dp.xml b/app/src/main/res/drawable/ic_reader_ltr_24dp.xml new file mode 100644 index 0000000000..2c44474e90 --- /dev/null +++ b/app/src/main/res/drawable/ic_reader_ltr_24dp.xml @@ -0,0 +1,21 @@ + + + + + + + diff --git a/app/src/main/res/drawable/ic_reader_rtl_24dp.xml b/app/src/main/res/drawable/ic_reader_rtl_24dp.xml new file mode 100644 index 0000000000..bce2932fcf --- /dev/null +++ b/app/src/main/res/drawable/ic_reader_rtl_24dp.xml @@ -0,0 +1,21 @@ + + + + + + + diff --git a/app/src/main/res/drawable/ic_reader_vertical_24dp.xml b/app/src/main/res/drawable/ic_reader_vertical_24dp.xml new file mode 100644 index 0000000000..def93b2c21 --- /dev/null +++ b/app/src/main/res/drawable/ic_reader_vertical_24dp.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/drawable/ic_reader_webtoon_24dp.xml b/app/src/main/res/drawable/ic_reader_webtoon_24dp.xml new file mode 100644 index 0000000000..b7683822de --- /dev/null +++ b/app/src/main/res/drawable/ic_reader_webtoon_24dp.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/layout/reader_chapters_sheet.xml b/app/src/main/res/layout/reader_chapters_sheet.xml index f55819512a..ccfeff89ee 100644 --- a/app/src/main/res/layout/reader_chapters_sheet.xml +++ b/app/src/main/res/layout/reader_chapters_sheet.xml @@ -58,10 +58,26 @@ app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/chapters_button" - app:layout_constraintEnd_toStartOf="@id/crop_borders_sheet_button" + app:layout_constraintEnd_toStartOf="@id/reading_mode" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/ic_open_in_webview_24dp" /> + +