Fix image loading indefinitely when getting feed colors

This commit is contained in:
Shinokuni 2024-06-20 13:06:21 +02:00
parent 07b6059921
commit 116a1b9722
3 changed files with 18 additions and 22 deletions

View File

@ -33,7 +33,7 @@ val apiModule = module {
single {
OkHttpClient.Builder()
.callTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.HOURS)
.readTimeout(1, TimeUnit.MINUTES)
.addInterceptor(get<AuthInterceptor>())
.addInterceptor(get<ErrorInterceptor>())
//.addInterceptor(NiddlerOkHttpInterceptor(get(), "niddler"))

View File

@ -87,14 +87,12 @@ class SyncWorker(
.setProgress(syncResult.newFeedIds.size, index + 1, false)
notificationManager.notify(SYNC_NOTIFICATION_ID, notificationBuilder.build())
val color = try {
FeedColors.getFeedColor(syncResult.feeds.first { it.id == feedId.toInt() }.iconUrl!!)
try {
val color = FeedColors.getFeedColor(syncResult.feeds.first { it.id == feedId.toInt() }.iconUrl!!)
database.newFeedDao().updateFeedColor(feedId.toInt(), color)
} catch (e: Exception) {
Log.e(TAG, "${e.message}")
0
Log.e(TAG, "$feedName: ${e.message}")
}
database.newFeedDao().updateFeedColor(feedId.toInt(), color)
}
}

View File

@ -1,32 +1,30 @@
package com.readrops.app.compose.util
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.graphics.BitmapFactory
import androidx.annotation.ColorInt
import androidx.palette.graphics.Palette
import coil.imageLoader
import coil.request.ImageRequest
import okhttp3.OkHttpClient
import okhttp3.Request
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
object FeedColors : KoinComponent {
suspend fun getFeedColor(feedUrl: String): Int {
val context = get<Context>() // TODO maybe call imageLoader directly ? may require some DI changes
// use OkHttp directly instead of Coil as Coil doesn't respect OkHttp timeout
val response = get<OkHttpClient>().newCall(
Request.Builder()
.url(feedUrl)
.build()
).execute()
val result = context.imageLoader
.execute(
ImageRequest.Builder(context)
.data(feedUrl)
.allowHardware(false)
.build()
).drawable as BitmapDrawable
val palette = Palette.from(result.bitmap).generate()
val bitmap = BitmapFactory.decodeStream(response.body?.byteStream()) ?: return 0
val palette = Palette.from(bitmap).generate()
val dominantSwatch = palette.dominantSwatch
return if (dominantSwatch != null && !isColorTooBright(dominantSwatch.rgb)
&& !isColorTooDark(dominantSwatch.rgb)) {
&& !isColorTooDark(dominantSwatch.rgb)
) {
dominantSwatch.rgb
} else 0
}