Skip to content

Commit

Permalink
Fix Coil image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksey-saenko committed Dec 14, 2023
1 parent db9609b commit cf8f5b8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import coil.ImageLoader
import coil.ImageLoaderFactory
import com.mrsep.musicrecognizer.feature.recognition.presentation.service.NotificationService
import dagger.hilt.android.HiltAndroidApp
import okhttp3.OkHttpClient
import javax.inject.Inject

@HiltAndroidApp
class MusicRecognizerApp : Application(), ImageLoaderFactory, Configuration.Provider {

@Inject
lateinit var okHttpClient: OkHttpClient

@Inject
lateinit var workerFactory: HiltWorkerFactory

Expand All @@ -23,7 +27,9 @@ class MusicRecognizerApp : Application(), ImageLoaderFactory, Configuration.Prov

override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.crossfade(false)
.okHttpClient { okHttpClient }
.crossfade(true)
.respectCacheHeaders(false)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONObject
Expand Down Expand Up @@ -93,7 +94,8 @@ object NetworkModule {

private class UserAgentInterceptor(appContext: Context) : Interceptor {

private val userAgent = "Audile/${appContext.getAppVersion()} (Android ${Build.VERSION.RELEASE})"
private val userAgent =
"Audile/${appContext.getAppVersion()} (Android ${Build.VERSION.RELEASE})"

override fun intercept(chain: Interceptor.Chain): Response {
return chain.proceed(
Expand Down Expand Up @@ -128,7 +130,7 @@ private class HttpFileLoggingInterceptor(

init {
try {
File("${appContext.filesDir.absolutePath}/http_logger/").run { if (!exists()) mkdir() }
File(rootDir).run { if (!exists()) mkdir() }
} catch (e: Exception) {
e.printStackTrace()
}
Expand All @@ -137,18 +139,22 @@ private class HttpFileLoggingInterceptor(
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)

val responseTime = sdf.format(Date(System.currentTimeMillis()))
val responseFile = File("$rootDir/${responseTime}_response.txt")

val body = response.body!!
val json = body.string()
if (json.isNotBlank()) {
writeLogFile(responseFile, JSONObject(json).toString(4))

val newBody = if (body.contentType() == "application/json".toMediaType()) {
val json = body.string()
if (json.isNotBlank()) {
val responseTime = sdf.format(Date(System.currentTimeMillis()))
val responseFile = File("$rootDir/${responseTime}_response.txt")
writeLogFile(responseFile, JSONObject(json).toString(4))
}
json.toResponseBody(body.contentType())
} else {
body
}

return response.newBuilder()
.body(json.toResponseBody(body.contentType()))
.body(newBody)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal fun LazyListTrackItem(
)
AsyncImage(
model = track.artworkUrl,
placeholder = placeholder,
fallback = placeholder,
error = placeholder,
contentDescription = stringResource(com.mrsep.musicrecognizer.core.strings.R.string.artwork),
contentScale = ContentScale.Crop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal fun LazyGridTrackItem(
)
AsyncImage(
model = track.artworkUrl,
placeholder = placeholder,
fallback = placeholder,
error = placeholder,
contentDescription = stringResource(StringsR.string.artwork),
contentScale = ContentScale.Crop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal fun TrackSearchItem(
)
AsyncImage(
model = track.artworkUrl,
placeholder = placeholder,
fallback = placeholder,
error = placeholder,
contentDescription = stringResource(StringsR.string.artwork),
contentScale = ContentScale.Crop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal fun AlbumArtwork(
.data(url)
.size(Size.ORIGINAL)
.allowHardware(!createSeedColor)
.crossfade(50)
.build(),
error = placeholder,
fallback = placeholder,
Expand Down

0 comments on commit cf8f5b8

Please sign in to comment.