Skip to content

Commit

Permalink
update coil and fix inconsistent color generation on manga info
Browse files Browse the repository at this point in the history
  • Loading branch information
nonproto committed Jan 15, 2021
1 parent 6b545e8 commit 5393ba3
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.data.cache
import android.content.Context
import android.text.format.Formatter
import coil.Coil
import coil.memory.MemoryCache
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga
Expand Down Expand Up @@ -165,7 +166,7 @@ class CoverCache(val context: Context) {
fun setCustomCoverToCache(manga: Manga, inputStream: InputStream) {
getCustomCoverFile(manga).outputStream().use {
inputStream.copyTo(it)
Coil.imageLoader(context).invalidate(manga.key())
Coil.imageLoader(context).memoryCache.remove(MemoryCache.Key(manga.key()))
}
}

Expand All @@ -179,7 +180,7 @@ class CoverCache(val context: Context) {
val result = getCustomCoverFile(manga).let {
it.exists() && it.delete()
}
Coil.imageLoader(context).invalidate(manga.key())
Coil.imageLoader(context).memoryCache.remove(MemoryCache.Key(manga.key()))
return result
}

Expand All @@ -204,7 +205,8 @@ class CoverCache(val context: Context) {
fun deleteFromCache(name: String?) {
if (name.isNullOrEmpty()) return
val file = getCoverFile(MangaImpl().apply { thumbnail_url = name })
Coil.imageLoader(context).invalidate(file.name)
Coil.imageLoader(context).memoryCache.remove(MemoryCache.Key(file.name))

if (file.exists()) file.delete()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.kanade.tachiyomi.data.image.coil

import coil.bitmappool.BitmapPool
import coil.bitmap.BitmapPool
import coil.decode.DataSource
import coil.decode.Options
import coil.fetch.FetchResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.view.View
import android.widget.ImageView
import coil.Coil
import coil.request.CachePolicy
import coil.request.LoadRequest
import coil.request.ImageRequest
import coil.target.ImageViewTarget
import com.mikepenz.iconics.typeface.library.materialdesigndx.MaterialDesignDx
import eu.kanade.tachiyomi.util.system.iconicsDrawableLarge
Expand All @@ -25,9 +25,9 @@ class CoverViewTarget(
view.scaleType = ImageView.ScaleType.CENTER
view.setImageDrawable(view.context.iconicsDrawableLarge(MaterialDesignDx.Icon.gmf_broken_image, color = android.R.attr.textColorSecondary))
} else {
val request = LoadRequest.Builder(view.context).data(errorUrl).memoryCachePolicy(CachePolicy.ENABLED)
val request = ImageRequest.Builder(view.context).data(errorUrl).memoryCachePolicy(CachePolicy.ENABLED)
.target(CoverViewTarget(view, progress)).build()
Coil.imageLoader(view.context).execute(request)
Coil.imageLoader(view.context).enqueue(request)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.graphics.drawable.Drawable
import android.widget.ImageView
import coil.Coil
import coil.ImageLoader
import coil.request.LoadRequest
import coil.request.LoadRequestBuilder
import coil.request.RequestDisposable
import coil.memory.MemoryCache
import coil.request.Disposable
import coil.request.ImageRequest
import coil.target.ImageViewTarget
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.models.Manga
Expand All @@ -30,7 +30,8 @@ class LibraryMangaImageTarget(
BitmapFactory.decodeFile(file.path, options)
if (options.outWidth == -1 || options.outHeight == -1) {
file.delete()
Coil.imageLoader(view.context).invalidate(manga.key())

Coil.imageLoader(view.context).memoryCache.remove(MemoryCache.Key(manga.key()))
}
}
}
Expand All @@ -40,12 +41,12 @@ class LibraryMangaImageTarget(
inline fun ImageView.loadLibraryManga(
manga: Manga,
imageLoader: ImageLoader = Coil.imageLoader(context),
builder: LoadRequestBuilder.() -> Unit = {}
): RequestDisposable {
val request = LoadRequest.Builder(context)
builder: ImageRequest.Builder.() -> Unit = {}
): Disposable {
val request = ImageRequest.Builder(context)
.data(manga)
.target(LibraryMangaImageTarget(this, manga))
.apply(builder)
.build()
return imageLoader.execute(request)
return imageLoader.enqueue(request)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.kanade.tachiyomi.data.image.coil

import android.webkit.MimeTypeMap
import coil.bitmappool.BitmapPool
import coil.bitmap.BitmapPool
import coil.decode.DataSource
import coil.decode.Options
import coil.fetch.FetchResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import coil.Coil
import coil.request.CachePolicy
import coil.request.GetRequest
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Chapter
Expand Down Expand Up @@ -136,22 +136,24 @@ class LibraryUpdateNotifier(private val context: Context) {
context.notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_neko_notification)
try {
val request = GetRequest.Builder(context).data(manga)
val request = ImageRequest.Builder(context).data(manga)
.networkCachePolicy(CachePolicy.DISABLED)
.transformations(CircleCropTransformation()).size(width = ICON_SIZE, height = ICON_SIZE)
.build()
Coil.imageLoader(context)
.execute(request).drawable?.let { drawable ->
setLargeIcon((drawable as BitmapDrawable).bitmap)
}
setLargeIcon((drawable as BitmapDrawable).bitmap)
}
} catch (e: Exception) {
}
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
setContentTitle(manga.title)
color = ContextCompat.getColor(context, R.color.colorAccent)
val chaptersNames = if (chapterNames.size > MAX_CHAPTERS) {
"${chapterNames.take(MAX_CHAPTERS - 1)
.joinToString(", ")}, " + context.resources.getQuantityString(
"${
chapterNames.take(MAX_CHAPTERS - 1)
.joinToString(", ")
}, " + context.resources.getQuantityString(
R.plurals.notification_and_n_more,
(chapterNames.size - (MAX_CHAPTERS - 1)),
(chapterNames.size - (MAX_CHAPTERS - 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.os.PowerManager
import android.widget.Toast
import coil.Coil
import coil.request.CachePolicy
import coil.request.LoadRequest
import coil.request.ImageRequest
import com.elvishew.xlog.XLog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.cache.CoverCache
Expand Down Expand Up @@ -397,9 +397,9 @@ class LibraryUpdateService(
coverCache.deleteFromCache(thumbnailUrl)
// load new covers in background
val request =
LoadRequest.Builder(this@LibraryUpdateService).data(manga)
ImageRequest.Builder(this@LibraryUpdateService).data(manga)
.memoryCachePolicy(CachePolicy.DISABLED).build()
Coil.imageLoader(this@LibraryUpdateService).execute(request)
Coil.imageLoader(this@LibraryUpdateService).enqueue(request)
}
db.insertManga(manga).executeAsBlocking()
// add mdlist tracker if manga in library has it missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Activity
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import coil.api.clear
import coil.clear
import coil.size.Precision
import coil.size.Scale
import eu.kanade.tachiyomi.data.database.models.Manga
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.ui.library

import android.view.View
import android.view.ViewGroup
import coil.api.clear
import coil.clear
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.image.coil.loadLibraryManga
import eu.kanade.tachiyomi.util.system.dpToPx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.ui.manga

import android.graphics.Bitmap
import android.view.View
import androidx.recyclerview.widget.ItemTouchHelper
import eu.davidea.flexibleadapter.items.IFlexible
Expand Down Expand Up @@ -132,5 +133,6 @@ class MangaDetailsAdapter(
fun updateScroll()
fun setFavButtonPopup(popupView: View)
fun showExternalSheet()
fun generatePalette(input: Bitmap)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ import androidx.transition.ChangeImageTransform
import androidx.transition.TransitionManager
import androidx.transition.TransitionSet
import coil.Coil
import coil.bitmappool.BitmapPool
import coil.request.LoadRequest
import coil.size.Size
import coil.transform.Transformation
import coil.request.ImageRequest
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.checkbox.checkBoxPrompt
import com.afollestad.materialdialogs.checkbox.isCheckPromptChecked
Expand Down Expand Up @@ -111,6 +108,9 @@ import kotlinx.android.synthetic.main.main_activity.*
import kotlinx.android.synthetic.main.manga_details_controller.*
import kotlinx.android.synthetic.main.manga_grid_item.*
import kotlinx.android.synthetic.main.manga_header_item.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
Expand Down Expand Up @@ -155,7 +155,6 @@ class MangaDetailsController :

var colorAnimator: ValueAnimator? = null
val presenter: MangaDetailsPresenter
var coverColor: Int? = null
var toolbarIsColored = false
private var snack: Snackbar? = null
val fromCatalogue = args.getBoolean(FROM_CATALOGUE_EXTRA, false)
Expand Down Expand Up @@ -188,11 +187,9 @@ class MangaDetailsController :
//region UI Methods
override fun onViewCreated(view: View) {
super.onViewCreated(view)
coverColor = null
fullCoverActive = false

setRecycler(view)
setPaletteColor()
adapter?.fastScroller = fast_scroller
fast_scroller.addOnScrollStateChangeListener {
activity?.appbar?.y = 0f
Expand Down Expand Up @@ -285,7 +282,7 @@ class MangaDetailsController :
return
}
val color =
coverColor ?: activity!!.getResourceColor(R.attr.colorPrimaryVariant)
presenter.coverColor ?: activity!!.getResourceColor(R.attr.colorPrimaryVariant)
val colorFrom =
if (colorAnimator?.isRunning == true) activity?.window?.statusBarColor
?: color
Expand All @@ -312,37 +309,6 @@ class MangaDetailsController :
}
}

/** Get the color of the manga cover*/
fun setPaletteColor() {
val view = view ?: return

val request = LoadRequest.Builder(view.context).data(presenter.manga).allowHardware(false)
.transformations(object : Transformation {
override fun key() = "BackDropColorTransformer"

override suspend fun transform(pool: BitmapPool, input: Bitmap, size: Size): Bitmap {
val p = Palette.from(input).generate()
if (recycler != null) {
val colorBack = view.context.getResourceColor(
android.R.attr.colorBackground
)
// this makes the color more consistent regardless of theme
val backDropColor =
ColorUtils.blendARGB(p.getVibrantColor(colorBack), colorBack, .35f)
coverColor = backDropColor
getHeader()?.setBackDrop(backDropColor)
if (toolbarIsColored) {
val translucentColor = ColorUtils.setAlphaComponent(backDropColor, 175)
(activity as MainActivity).toolbar.setBackgroundColor(translucentColor)
activity?.window?.statusBarColor = translucentColor
}
}
return input
}
}).target(manga_cover_full).build()
Coil.imageLoader(view.context).execute(request)
}

/** Set toolbar theme for themes that are inverted (ie. light blue theme) */
private fun setActionBar(forThis: Boolean) {
val activity = activity ?: return
Expand Down Expand Up @@ -378,7 +344,7 @@ class MangaDetailsController :

private fun setStatusBarAndToolbar() {
activity?.window?.statusBarColor = if (toolbarIsColored) {
val translucentColor = ColorUtils.setAlphaComponent(coverColor ?: Color.TRANSPARENT, 175)
val translucentColor = ColorUtils.setAlphaComponent(presenter.coverColor ?: Color.TRANSPARENT, 175)
(activity as MainActivity).toolbar.setBackgroundColor(translucentColor)
translucentColor
} else Color.TRANSPARENT
Expand Down Expand Up @@ -407,8 +373,6 @@ class MangaDetailsController :
presenter.refreshTracking()
refreshTracker = null
}
// fetch cover again in case the user set a new cover while reading
setPaletteColor()
val isCurrentController = router?.backstack?.lastOrNull()?.controller() ==
this
if (isCurrentController) {
Expand Down Expand Up @@ -815,15 +779,15 @@ class MangaDetailsController :

override fun prepareToShareManga() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val request = LoadRequest.Builder(activity!!).data(presenter.manga).target(
val request = ImageRequest.Builder(activity!!).data(presenter.manga).target(
onError = {
shareManga()
},
onSuccess = {
presenter.shareManga((it as BitmapDrawable).bitmap)
}
).build()
Coil.imageLoader(activity!!).execute(request)
Coil.imageLoader(activity!!).enqueue(request)
} else {
shareManga()
}
Expand Down Expand Up @@ -931,7 +895,7 @@ class MangaDetailsController :
}

//region Interface methods
override fun coverColor(): Int? = coverColor
override fun coverColor(): Int? = presenter.coverColor
override fun topCoverHeight(): Int = headerHeight

override fun startDownloadNow(position: Int) {
Expand Down Expand Up @@ -1187,6 +1151,30 @@ class MangaDetailsController :
externalBottomSheet?.show()
}

override fun generatePalette(input: Bitmap) {
val view = view ?: return
presenter.scope.launch(Dispatchers.Main) {
val p = async(Dispatchers.IO) { Palette.from(input).generate() }
if (recycler != null) {
val colorBack = view.context.getResourceColor(
android.R.attr.colorBackground
)

// this makes the color more consistent regardless of theme
val backDropColor =
ColorUtils.blendARGB(p.await().getVibrantColor(colorBack), colorBack, .35f)
presenter.coverColor = backDropColor
getHeader()?.setBackDrop(backDropColor)
setStatusBarAndToolbar()
if (toolbarIsColored) {
val translucentColor = ColorUtils.setAlphaComponent(backDropColor, 175)
(activity as MainActivity).toolbar.setBackgroundColor(translucentColor)
activity?.window?.statusBarColor = translucentColor
}
}
}
}

//endregion

//region Tracking methods
Expand Down Expand Up @@ -1454,6 +1442,7 @@ class MangaDetailsController :

fun clearCoverCache() {
backdrop.setImageDrawable(null)
true_backdrop.setBackgroundColor(Color.TRANSPARENT)
manga_cover_full.setImageDrawable(null)
manga_cover.setImageDrawable(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class MangaDetailsPresenter(
private val chapterFilter: ChapterFilter = Injekt.get()
) : DownloadQueue.DownloadListener, LibraryServiceListener {

private var scope = CoroutineScope(Job() + Dispatchers.Default)
var scope = CoroutineScope(Job() + Dispatchers.Default)

var coverColor: Int? = null

private val customMangaManager: CustomMangaManager by injectLazy()

Expand Down Expand Up @@ -449,9 +451,6 @@ class MangaDetailsPresenter(
controller.clearCoverCache()
}
}
withContext(Dispatchers.Main) {
controller.setPaletteColor()
}
db.insertManga(manga).executeOnIO()
}
fetchExternalLinks()
Expand Down
Loading

0 comments on commit 5393ba3

Please sign in to comment.