From cdc7f64637ffef9a853f3eee31ad9ade03ff39f2 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Tue, 13 Apr 2021 23:43:41 -0400 Subject: [PATCH] Adding button to crop borders (only for webtoons) in reader bar And because I can't cherry pick things, I spite myself by using a different icon for non cropped mode AND its having an animation to and from crop mode. Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com> --- .../data/preference/PreferencesHelper.kt | 2 +- .../tachiyomi/ui/reader/ReaderActivity.kt | 52 +++++++++++++++++-- app/src/main/res/drawable/crop_to_free.xml | 45 ++++++++++++++++ app/src/main/res/drawable/free_to_crop.xml | 43 +++++++++++++++ .../main/res/layout/reader_chapters_sheet.xml | 25 +++++++-- app/src/main/res/values/strings.xml | 1 + 6 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/drawable/crop_to_free.xml create mode 100644 app/src/main/res/drawable/free_to_crop.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 4e5140fd12..d44fd0da95 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -41,7 +41,7 @@ fun com.tfcporciuncula.flow.Preference.asImmediateFlow(scope: CoroutineSc .launchIn(scope) } -fun Preference.invert(): Boolean = getOrDefault().let { set(!it); !it } +fun com.tfcporciuncula.flow.Preference.toggle() = set(!get()) private class DateFormatConverter : Preference.Adapter { override fun get(key: String, preferences: SharedPreferences): DateFormat { 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 bc25481a2a..fc07b11ff5 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 @@ -9,6 +9,7 @@ import android.content.res.ColorStateList import android.content.res.Configuration import android.graphics.Bitmap import android.graphics.Color +import android.graphics.drawable.AnimatedVectorDrawable import android.graphics.drawable.LayerDrawable import android.os.Build import android.os.Bundle @@ -36,6 +37,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.toggle import eu.kanade.tachiyomi.databinding.ReaderActivityBinding import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.base.MaterialMenuSheet @@ -73,6 +75,7 @@ import eu.kanade.tachiyomi.util.system.openInBrowser import eu.kanade.tachiyomi.util.system.setThemeAndNight import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.collapse +import eu.kanade.tachiyomi.util.view.compatToolTipText import eu.kanade.tachiyomi.util.view.doOnApplyWindowInsets import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.hide @@ -349,11 +352,35 @@ class ReaderActivity : else R.drawable.ic_book_open_variant_24dp ) ) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - binding.chaptersSheet.doublePage.tooltipText = + binding.chaptersSheet.doublePage.compatToolTipText = + getString( + if (isDoublePage) R.string.switch_to_single + else R.string.switch_to_double + ) + } + + private fun updateCropBordersShortcut() { + val isPagerType = viewer is PagerViewer || (viewer as? WebtoonViewer)?.hasMargins == true + val enabled = if (isPagerType) { + preferences.cropBorders().get() + } else { + preferences.cropBordersWebtoon().get() + } + + with(binding.chaptersSheet.cropBordersSheetButton) { + setImageResource( + if (enabled) { + R.drawable.free_to_crop + } else { + R.drawable.crop_to_free + } + ) + val animDrawable = drawable as AnimatedVectorDrawable + animDrawable.start() + compatToolTipText = getString( - if (isDoublePage) R.string.switch_to_single - else R.string.switch_to_double + if (enabled) R.string.remove_crop + else R.string.crop_borders ) } } @@ -500,6 +527,20 @@ class ReaderActivity : preferences.pageLayout().set(1 - preferences.pageLayout().get()) } } + binding.chaptersSheet.cropBordersSheetButton.setOnClickListener { + (viewer as? WebtoonViewer)?.let { + val pref = if (it.hasMargins) preferences.cropBorders() else preferences.cropBordersWebtoon() + pref.toggle() + } + } + + listOf(preferences.cropBorders(), preferences.cropBordersWebtoon()) + .forEach { pref -> + pref.asFlow() + .onEach { updateCropBordersShortcut() } + .launchIn(scope) + } + binding.chaptersSheet.shiftPageButton.setOnClickListener { shiftDoublePages() } @@ -708,7 +749,6 @@ class ReaderActivity : val prevViewer = viewer val noDefault = manga.viewer == -1 val mangaViewer = presenter.getMangaViewer() - invalidateOptionsMenu() val newViewer = when (mangaViewer) { RIGHT_TO_LEFT -> R2LPagerViewer(this) VERTICAL -> VerticalPagerViewer(this) @@ -745,6 +785,7 @@ class ReaderActivity : } viewer = newViewer binding.chaptersSheet.doublePage.isVisible = viewer is PagerViewer + binding.chaptersSheet.cropBordersSheetButton.isVisible = viewer !is PagerViewer binding.viewerContainer.addView(newViewer.getView()) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -780,6 +821,7 @@ class ReaderActivity : binding.pleaseWait.visible() binding.pleaseWait.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in_long)) invalidateOptionsMenu() + updateCropBordersShortcut() } override fun onPause() { diff --git a/app/src/main/res/drawable/crop_to_free.xml b/app/src/main/res/drawable/crop_to_free.xml new file mode 100644 index 0000000000..31ee38205e --- /dev/null +++ b/app/src/main/res/drawable/crop_to_free.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/free_to_crop.xml b/app/src/main/res/drawable/free_to_crop.xml new file mode 100644 index 0000000000..3718b61a9f --- /dev/null +++ b/app/src/main/res/drawable/free_to_crop.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/reader_chapters_sheet.xml b/app/src/main/res/layout/reader_chapters_sheet.xml index fba588351d..f55819512a 100644 --- a/app/src/main/res/layout/reader_chapters_sheet.xml +++ b/app/src/main/res/layout/reader_chapters_sheet.xml @@ -58,10 +58,28 @@ app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/chapters_button" - app:layout_constraintEnd_toStartOf="@id/double_page" + app:layout_constraintEnd_toStartOf="@id/crop_borders_sheet_button" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/ic_open_in_webview_24dp" /> + + @@ -93,7 +111,8 @@ app:layout_constraintStart_toEndOf="@id/double_page" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toStartOf="@id/display_options" - app:srcCompat="@drawable/ic_page_next_outline_24dp" /> + app:srcCompat="@drawable/ic_page_next_outline_24dp" + tools:visibility="visible" /> Invert double pages Crop borders + Remove crop Use custom brightness Use custom color filter Color filter blend mode