From 1e58b05ead62e68373775afb309fcad8b28df957 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 13 Mar 2021 16:47:16 -0500 Subject: [PATCH] Add reading mode toggle --- .../tachiyomi/ui/reader/ReaderActivity.kt | 50 ++++++++++--------- .../ui/reader/setting/ReaderSettingsSheet.kt | 2 +- .../ui/reader/setting/ReadingModeType.kt | 25 ++++++++++ app/src/main/res/layout/reader_activity.xml | 27 +++++----- 4 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt 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 d7651535f6..acaeafee01 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 @@ -47,6 +47,7 @@ import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.setting.OrientationType import eu.kanade.tachiyomi.ui.reader.setting.ReaderColorFilterSheet import eu.kanade.tachiyomi.ui.reader.setting.ReaderSettingsSheet +import eu.kanade.tachiyomi.ui.reader.setting.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.R2LPagerViewer @@ -81,6 +82,16 @@ import kotlin.math.abs @RequiresPresenter(ReaderPresenter::class) class ReaderActivity : BaseRxActivity() { + companion object { + fun newIntent(context: Context, manga: Manga, chapter: Chapter): Intent { + return Intent(context, ReaderActivity::class.java).apply { + putExtra("manga", manga.id) + putExtra("chapter", chapter.id) + addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + } + } + } + private val preferences: PreferencesHelper by injectLazy() /** @@ -113,24 +124,7 @@ class ReaderActivity : BaseRxActivity() @Suppress("DEPRECATION") private var progressDialog: ProgressDialog? = null - private var rotationToast: Toast? = null - - companion object { - @Suppress("unused") - const val LEFT_TO_RIGHT = 1 - const val RIGHT_TO_LEFT = 2 - const val VERTICAL = 3 - const val WEBTOON = 4 - const val VERTICAL_PLUS = 5 - - fun newIntent(context: Context, manga: Manga, chapter: Chapter): Intent { - return Intent(context, ReaderActivity::class.java).apply { - putExtra("manga", manga.id) - putExtra("chapter", chapter.id) - addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - } - } - } + private var menuToggleToast: Toast? = null /** * Called when the activity is created. Initializes the presenter and configuration. @@ -348,14 +342,22 @@ class ReaderActivity : BaseRxActivity() } } + binding.actionReaderMode.setOnClickListener { + val newReadingMode = ReadingModeType.getNextReadingMode(presenter.manga?.viewer ?: 0) + presenter.setMangaViewer(newReadingMode.prefValue) + + menuToggleToast?.cancel() + menuToggleToast = toast(newReadingMode.stringRes) + } + binding.actionRotation.setOnClickListener { val newOrientation = OrientationType.getNextOrientation(preferences.rotation().get(), resources) preferences.rotation().set(newOrientation.prefValue) setOrientation(newOrientation.flag) - rotationToast?.cancel() - rotationToast = toast(newOrientation.stringRes) + menuToggleToast?.cancel() + menuToggleToast = toast(newOrientation.stringRes) } preferences.rotation().asImmediateFlow { updateRotationShortcut(it) } .onEach { @@ -463,10 +465,10 @@ class ReaderActivity : BaseRxActivity() fun setManga(manga: Manga) { val prevViewer = viewer val newViewer = when (presenter.getMangaViewer()) { - RIGHT_TO_LEFT -> R2LPagerViewer(this) - VERTICAL -> VerticalPagerViewer(this) - WEBTOON -> WebtoonViewer(this) - VERTICAL_PLUS -> WebtoonViewer(this, isContinuous = false) + ReadingModeType.RIGHT_TO_LEFT.prefValue -> R2LPagerViewer(this) + ReadingModeType.VERTICAL.prefValue -> VerticalPagerViewer(this) + ReadingModeType.WEBTOON.prefValue -> WebtoonViewer(this) + ReadingModeType.CONTINUOUS_VERTICAL.prefValue -> WebtoonViewer(this, isContinuous = false) else -> L2RPagerViewer(this) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt index d53e8f604e..f03471d3cf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt @@ -57,7 +57,7 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BaseBottomShee activity.presenter.setMangaViewer(position) val mangaViewer = activity.presenter.getMangaViewer() - if (mangaViewer == ReaderActivity.WEBTOON || mangaViewer == ReaderActivity.VERTICAL_PLUS) { + if (mangaViewer == ReadingModeType.WEBTOON.prefValue || mangaViewer == ReadingModeType.CONTINUOUS_VERTICAL.prefValue) { initWebtoonPreferences() } else { initPagerPreferences() 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 new file mode 100644 index 0000000000..945aff462f --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt @@ -0,0 +1,25 @@ +package eu.kanade.tachiyomi.ui.reader.setting + +import androidx.annotation.StringRes +import eu.kanade.tachiyomi.R +import kotlin.math.max + +enum class ReadingModeType(val prefValue: Int, @StringRes val stringRes: Int) { + DEFAULT(0, R.string.default_viewer), + LEFT_TO_RIGHT(1, R.string.left_to_right_viewer), + RIGHT_TO_LEFT(2, R.string.right_to_left_viewer), + VERTICAL(3, R.string.vertical_viewer), + WEBTOON(4, R.string.webtoon_viewer), + CONTINUOUS_VERTICAL(5, R.string.vertical_plus_viewer), + ; + + companion object { + fun fromPreference(preference: Int): ReadingModeType = values().find { it.prefValue == preference } ?: DEFAULT + + fun getNextReadingMode(preference: Int): ReadingModeType { + // There's only 6 options (0 to 5) + val newReadingMode = max(0, (preference + 1) % 6) + return fromPreference(newReadingMode) + } + } +} diff --git a/app/src/main/res/layout/reader_activity.xml b/app/src/main/res/layout/reader_activity.xml index 4a4139af10..6421d020d4 100644 --- a/app/src/main/res/layout/reader_activity.xml +++ b/app/src/main/res/layout/reader_activity.xml @@ -145,20 +145,19 @@ android:layout_gravity="bottom" android:background="?attr/colorPrimary"> - - - - - - - - - - - - + -