From 78b3ff97d27e78506197e5064c9824958cc5d37f Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 11 Jan 2020 17:06:11 -0800 Subject: [PATCH] Autmotic Reader theme now based on page or by theme --- .../ui/reader/loader/HttpPageLoader.kt | 6 ++++-- .../tachiyomi/ui/reader/model/ReaderPage.kt | 3 ++- .../ui/reader/viewer/pager/PagerPageHolder.kt | 16 ++++++++++++---- .../ui/setting/SettingsReaderController.kt | 5 +++-- .../java/eu/kanade/tachiyomi/util/ImageUtil.kt | 18 +++++++++--------- app/src/main/res/values/arrays.xml | 3 ++- app/src/main/res/values/strings.xml | 5 ++++- 7 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index fe1a063c2d..be72081306 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -231,10 +231,12 @@ class HttpPageLoader( } } .doOnNext { - if (preferences.readerTheme().get() == 2) { + val readerTheme = preferences.readerTheme().getOrDefault() + if (readerTheme >= 2) { val stream = chapterCache.getImageFile(imageUrl).inputStream() val image = BitmapFactory.decodeStream(stream) - page.bg = ImageUtil.autoSetBackground(image) + page.bg = ImageUtil.autoSetBackground(image, readerTheme == 2) + page.bgAlwaysWhite = readerTheme == 2 stream.close() } page.stream = { chapterCache.getImageFile(imageUrl).inputStream() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderPage.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderPage.kt index 4b9b242ebc..48e3540425 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderPage.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderPage.kt @@ -9,7 +9,8 @@ class ReaderPage( url: String = "", imageUrl: String? = null, var stream: (() -> InputStream)? = null, - var bg: Drawable? = null + var bg: Drawable? = null, + var bgAlwaysWhite: Boolean? = null ) : Page(index, url, imageUrl, null) { lateinit var chapter: ReaderChapter diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt index 160cfbb85c..3fab78156e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt @@ -3,7 +3,9 @@ package eu.kanade.tachiyomi.ui.reader.viewer.pager import android.annotation.SuppressLint import android.content.Intent import android.graphics.BitmapFactory +import android.graphics.Color import android.graphics.PointF +import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.net.Uri import android.view.GestureDetector @@ -29,6 +31,8 @@ import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView import com.github.chrisbanes.photoview.PhotoView import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.glide.GlideApp +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressBar @@ -45,6 +49,7 @@ import rx.Observable import rx.Subscription import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers +import uy.kohesive.injekt.injectLazy import java.io.InputStream import java.util.concurrent.TimeUnit @@ -248,9 +253,10 @@ class PagerPageHolder( .observeOn(AndroidSchedulers.mainThread()) .doOnNext { isAnimated -> if (!isAnimated) { - if (viewer.config.readerTheme == 2) { + if (viewer.config.readerTheme >= 2) { val imageView = initSubsamplingImageView() - if (page.bg != null) { + if (page.bg != null && + page.bgAlwaysWhite == (viewer.config.readerTheme == 2)) { imageView.setImage(ImageSource.inputStream(openStream!!)) imageView.background = page.bg } @@ -264,6 +270,7 @@ class PagerPageHolder( launchUI { imageView.background = setBG(bytesArray) page.bg = imageView.background + page.bgAlwaysWhite = viewer.config.readerTheme == 2 } } } @@ -273,7 +280,7 @@ class PagerPageHolder( } else { val imageView = initImageView() imageView.setImage(openStream!!) - if (viewer.config.readerTheme == 2 && page.bg != null) + if (viewer.config.readerTheme >= 2 && page.bg != null) imageView.background = page.bg } } @@ -285,9 +292,10 @@ class PagerPageHolder( private suspend fun setBG(bytesArray: ByteArray): Drawable { return withContext(Default) { + val preferences by injectLazy() ImageUtil.autoSetBackground(BitmapFactory.decodeByteArray( bytesArray, 0, bytesArray.size - )) + ), preferences.readerTheme().getOrDefault() == 2) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt index 71d89c6ffd..93c2a1e674 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt @@ -50,8 +50,9 @@ class SettingsReaderController : SettingsController() { intListPreference { key = Keys.readerTheme titleRes = R.string.pref_reader_theme - entriesRes = arrayOf(R.string.white_background, R.string.black_background, R.string.zoom_start_automatic) - entryValues = arrayOf("0", "1", "2") + entriesRes = arrayOf(R.string.white_background, R.string.black_background, R.string + .reader_theme_smart, R.string.reader_theme_smart_theme) + entryValues = arrayOf("0", "1", "2", "3") defaultValue = "0" summary = "%s" } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt index 6281f31a1e..fe3b391d73 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt @@ -53,9 +53,10 @@ object ImageUtil { return null } - fun autoSetBackground(image: Bitmap): Drawable { + fun autoSetBackground(image: Bitmap, useWhiteAlways: Boolean): Drawable { + val backgroundColor = if (useWhiteAlways) Color.WHITE else android.R.attr.colorBackground if (image.width < 50 || image.height < 50) - return ColorDrawable(android.R.attr.colorBackground) + return ColorDrawable(backgroundColor) val top = 5 val bot = image.height - 5 val left = (image.width * 0.0275).toInt() @@ -94,7 +95,7 @@ object ImageUtil { topRightIsDark -> image.getPixel(right, top) botLeftIsDark -> image.getPixel(left, bot) botRightIsDark -> image.getPixel(right, bot) - else -> android.R.attr.colorBackground + else -> backgroundColor } var overallWhitePixels = 0 @@ -179,25 +180,24 @@ object ImageUtil { darkBG = false if (topIsBlackStreak && bottomIsBlackStreak) darkBG = true - val whiteColor = android.R.attr.colorBackground if (darkBG) { return if (isWhite(image.getPixel(left, bot)) && isWhite(image.getPixel(right, bot))) GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, - intArrayOf(blackPixel, blackPixel, whiteColor, whiteColor)) + intArrayOf(blackPixel, blackPixel, backgroundColor, backgroundColor)) else if (isWhite(image.getPixel(left, top)) && isWhite(image.getPixel(right, top))) GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, - intArrayOf(whiteColor, whiteColor, blackPixel, blackPixel)) + intArrayOf(backgroundColor, backgroundColor, blackPixel, blackPixel)) else ColorDrawable(blackPixel) } if (topIsBlackStreak || (topLeftIsDark && topRightIsDark && isDark(image.getPixel(left - offsetX, top)) && isDark(image.getPixel(right + offsetX, top)) && (topMidIsDark || overallBlackPixels > 9))) return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, - intArrayOf(blackPixel, blackPixel, whiteColor, whiteColor)) + intArrayOf(blackPixel, blackPixel, backgroundColor, backgroundColor)) else if (bottomIsBlackStreak || (botLeftIsDark && botRightIsDark && isDark(image.getPixel(left - offsetX, bot)) && isDark(image.getPixel(right + offsetX, bot)) && (isDark(image.getPixel(midX, bot)) || overallBlackPixels > 9))) return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, - intArrayOf(whiteColor, whiteColor, blackPixel, blackPixel)) - return ColorDrawable(whiteColor) + intArrayOf(backgroundColor, backgroundColor, blackPixel, blackPixel)) + return ColorDrawable(backgroundColor) } fun Boolean.toInt() = if (this) 1 else 0 diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 632566da09..7db14ceb15 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -25,7 +25,8 @@ @string/white_background @string/black_background - @string/auto_background + @string/smart_background + @string/smart_theme_background diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 069da382b5..93950dc2b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -241,7 +241,8 @@ Background color White Black - Automatic + Smart (by page) + Smart (by theme) Default viewer Default Left to right @@ -274,6 +275,8 @@ G B A + Smart (based on page) + Smart (based on page and theme)