Skip to content

Commit

Permalink
Feat(di): Add new di ImageModule for the Coil + use in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
MortezaNedaei committed May 21, 2022
1 parent 4d3281a commit c017325
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
37 changes: 37 additions & 0 deletions app/src/main/java/com/mooncascade/di/ImageModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mooncascade.di

import android.content.Context
import coil.ImageLoader
import com.mooncascade.R
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
object ImageModule {

/**
* provides [ImageLoader] to use in application
* @param context: [ApplicationContext]
* @param okHttpClient: [OkHttpClient] in [NetworkModule]
*/
@Provides
@Singleton
fun provideImageLoader(
okHttpClient: OkHttpClient,
@ApplicationContext context: Context,
): ImageLoader =
ImageLoader.Builder(context)
.okHttpClient { okHttpClient }
.crossfade(true)
.placeholder(R.drawable.ic_placeholder)
.error(R.drawable.ic_placeholder)
//.diskCache(DiskCache.Builder().build())
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import android.app.Application
import coil.ImageLoader
import coil.ImageLoaderFactory
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject

@HiltAndroidApp
class MainApplication : Application(), ImageLoaderFactory {

override fun onCreate() {
super.onCreate()
}
@Inject
lateinit var imageLoader: ImageLoader


override fun newImageLoader(): ImageLoader = imageLoader

override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.crossfade(true)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.navArgs
import coil.load
import coil.ImageLoader
import coil.request.CachePolicy
import coil.request.ImageRequest
import com.mooncascade.R
import com.mooncascade.common.assistedViewModel
import com.mooncascade.common.extensions.contexts.observe
Expand All @@ -15,10 +16,10 @@ import com.mooncascade.common.extensions.onStateChangedListener
import com.mooncascade.common.extensions.snack
import com.mooncascade.common.extensions.visible
import com.mooncascade.common.materialContainerTransform
import com.mooncascade.di.qualifier.MainDispatcher
import com.mooncascade.data.entity.location.LocationEntity
import com.mooncascade.data.network.WeatherApi
import com.mooncascade.databinding.FragmentPlaceDetailsBinding
import com.mooncascade.di.qualifier.MainDispatcher
import com.mooncascade.domain.model.ViewState
import com.mooncascade.presentation.base.BaseFragment
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -43,6 +44,9 @@ class PlaceDetailsFragment : BaseFragment() {
@MainDispatcher
lateinit var coroutineDispatcher: CoroutineDispatcher

@Inject
lateinit var imageLoader: ImageLoader

private val args by navArgs<PlaceDetailsFragmentArgs>()
private val place by lazy(LazyThreadSafetyMode.NONE) { args.item }

Expand Down Expand Up @@ -115,11 +119,13 @@ class PlaceDetailsFragment : BaseFragment() {
place.airpressure
).also { binding.tvAirPressure2.text = it }

binding.imgCover.load(WeatherApi.Endpoints.GET_RANDOM_IMAGE) {
crossfade(true)
placeholder(R.drawable.ic_placeholder)
error(R.drawable.ic_placeholder)
diskCachePolicy(CachePolicy.DISABLED)
with(binding.imgCover) {
val request = ImageRequest.Builder(context)
.data(WeatherApi.Endpoints.GET_RANDOM_IMAGE)
.target(this)
.memoryCachePolicy(CachePolicy.DISABLED) // We need to get a new random image currently
.build()
imageLoader.enqueue(request)
}
}

Expand Down

0 comments on commit c017325

Please sign in to comment.