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 223e9811e3..55b32e5aa5 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 @@ -1,11 +1,17 @@ package eu.kanade.tachiyomi.ui.reader.loader +import android.graphics.BitmapFactory import eu.kanade.tachiyomi.data.cache.ChapterCache +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.source.online.HttpSource import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderPage +import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerConfig +import eu.kanade.tachiyomi.util.ImageUtil import eu.kanade.tachiyomi.util.plusAssign +import kotlinx.coroutines.experimental.async import rx.Completable import rx.Observable import rx.schedulers.Schedulers @@ -15,6 +21,7 @@ import rx.subscriptions.CompositeSubscription import timber.log.Timber import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy import java.util.concurrent.PriorityBlockingQueue import java.util.concurrent.atomic.AtomicInteger @@ -37,6 +44,11 @@ class HttpPageLoader( */ private val subscriptions = CompositeSubscription() + /** + * Preferences helper. + */ + private val preferences by injectLazy() + init { subscriptions += Observable.defer { Observable.just(queue.take().page) } .filter { it.status == Page.QUEUE } @@ -206,6 +218,10 @@ class HttpPageLoader( } .doOnNext { page.stream = { chapterCache.getImageFile(imageUrl).inputStream() } + if (preferences.readerTheme().get() == 2) { + val image = BitmapFactory.decodeStream(chapterCache.getImageFile(imageUrl).inputStream()) + page.bg = ImageUtil.autoSetBackground(image) + } page.status = Page.READY } .doOnError { page.status = Page.ERROR } 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 34b415253d..4b9b242ebc 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 @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.ui.reader.model +import android.graphics.drawable.Drawable import eu.kanade.tachiyomi.source.model.Page import java.io.InputStream @@ -7,7 +8,8 @@ class ReaderPage( index: Int, url: String = "", imageUrl: String? = null, - var stream: (() -> InputStream)? = null + var stream: (() -> InputStream)? = null, + var bg: Drawable? = 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 ad7ba428cf..892126fb31 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 @@ -244,16 +244,23 @@ class PagerPageHolder( .doOnNext { isAnimated -> if (!isAnimated) { if (viewer.config.readerTheme == 2) { - val bytesArray = openStream!!.readBytes() - val imageView = initSubsamplingImageView() - val bytesStream = bytesArray.inputStream() - imageView.setImage(ImageSource.inputStream(bytesStream)) - bytesStream.close() + if (page.bg != null) { + imageView.setImage(ImageSource.inputStream(openStream!!)) + imageView.background = page.bg + } + // if the user switches to automatic when pages are already cached, the bg needs to be loaded + else { + val bytesArray = openStream!!.readBytes() + val bytesStream = bytesArray.inputStream() + imageView.setImage(ImageSource.inputStream(bytesStream)) + bytesStream.close() - launchUI { - val image = async { BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.size) } - imageView.background = ImageUtil.autoSetBackground(image.await()) + launchUI { + val image = async { BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.size) } + imageView.background = ImageUtil.autoSetBackground(image.await()) + page.bg = imageView.background + } } } else { 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 da910d2af0..c6912747f3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt @@ -98,7 +98,7 @@ object ImageUtil { var overallWhitePixels = 0 var overallBlackPixels = 0 - outer@ for (x in intArrayOf(left, left - offsetX, right, right + offsetX)) { + outer@ for (x in intArrayOf(left, right, left - offsetX, right + offsetX)) { var whitePixelsStreak = 0 var whitePixels = 0 var blackPixelsStreak = 0