Updated on 2026-08-14
This commit is contained in:
parent
509d1ffd3c
commit
df39526354
11 changed files with 241 additions and 20 deletions
|
|
@ -3,12 +3,14 @@ package com.tangem.tap.common.images
|
|||
import android.content.Context
|
||||
import android.util.Log
|
||||
import coil.ImageLoader
|
||||
import coil.memory.MemoryCache
|
||||
import coil.util.Logger
|
||||
import com.tangem.datasource.api.common.createNetworkLoggingInterceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import timber.log.Timber
|
||||
|
||||
private const val COIL_LOG_TAG = "COIL"
|
||||
private const val COIL_MEMORY_CACHE_SIZE = 0.25
|
||||
|
||||
fun createCoilImageLoader(context: Context, logEnabled: Boolean = false): ImageLoader {
|
||||
return ImageLoader.Builder(context)
|
||||
|
|
@ -22,6 +24,11 @@ fun createCoilImageLoader(context: Context, logEnabled: Boolean = false): ImageL
|
|||
.build()
|
||||
}
|
||||
}
|
||||
.memoryCache {
|
||||
MemoryCache.Builder(context)
|
||||
.maxSizePercent(COIL_MEMORY_CACHE_SIZE)
|
||||
.build()
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package com.tangem.tap.common.images
|
||||
|
||||
import android.content.Context
|
||||
import coil.imageLoader
|
||||
import coil.request.ImageRequest
|
||||
import com.tangem.core.ui.coil.ImagePreloader
|
||||
|
||||
internal class DefaultImagePreloader(
|
||||
private val appContext: Context,
|
||||
) : ImagePreloader {
|
||||
override fun preload(url: String) {
|
||||
appContext.imageLoader.enqueue(
|
||||
ImageRequest.Builder(appContext)
|
||||
.data(url)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.tangem.tap.di.core
|
||||
|
||||
import android.content.Context
|
||||
import com.tangem.core.ui.coil.ImagePreloader
|
||||
import com.tangem.tap.common.images.DefaultImagePreloader
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
internal object ImageLoaderModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideImageLoader(@ApplicationContext context: Context): ImagePreloader {
|
||||
return DefaultImagePreloader(context)
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import com.tangem.core.analytics.models.event.TechAnalyticsEvent
|
|||
import com.tangem.core.decompose.di.GlobalUiMessageSender
|
||||
import com.tangem.core.decompose.ui.UiMessageSender
|
||||
import com.tangem.core.ui.R
|
||||
import com.tangem.core.ui.coil.ImagePreloader
|
||||
import com.tangem.core.ui.extensions.resourceReference
|
||||
import com.tangem.core.ui.message.BottomSheetMessage
|
||||
import com.tangem.core.ui.message.EventMessageAction
|
||||
|
|
@ -20,6 +21,8 @@ import com.tangem.domain.balancehiding.BalanceHidingSettings
|
|||
import com.tangem.domain.balancehiding.GetBalanceHidingSettingsUseCase
|
||||
import com.tangem.domain.balancehiding.ListenToFlipsUseCase
|
||||
import com.tangem.domain.balancehiding.UpdateBalanceHidingSettingsUseCase
|
||||
import com.tangem.domain.promo.GetStoryContentUseCase
|
||||
import com.tangem.domain.promo.models.StoryContentIds
|
||||
import com.tangem.domain.settings.DeleteDeprecatedLogsUseCase
|
||||
import com.tangem.domain.settings.IncrementAppLaunchCounterUseCase
|
||||
import com.tangem.domain.settings.usercountry.FetchUserCountryUseCase
|
||||
|
|
@ -50,6 +53,8 @@ internal class MainViewModel @Inject constructor(
|
|||
@GlobalUiMessageSender private val messageSender: UiMessageSender,
|
||||
private val keyboardValidator: KeyboardValidator,
|
||||
private val analyticsEventHandler: AnalyticsEventHandler,
|
||||
private val getStoryContentUseCase: GetStoryContentUseCase,
|
||||
private val imagePreloader: ImagePreloader,
|
||||
getBalanceHidingSettingsUseCase: GetBalanceHidingSettingsUseCase,
|
||||
) : ViewModel() {
|
||||
|
||||
|
|
@ -80,6 +85,8 @@ internal class MainViewModel @Inject constructor(
|
|||
deleteDeprecatedLogsUseCase()
|
||||
|
||||
sendKeyboardIdentifierEvent()
|
||||
|
||||
preloadImages()
|
||||
}
|
||||
|
||||
/** Loading the resources needed to run the application */
|
||||
|
|
@ -278,4 +285,20 @@ internal class MainViewModel @Inject constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Preload stories to display on startup before navigation to targeted screen
|
||||
private fun preloadImages() {
|
||||
try {
|
||||
viewModelScope.launch {
|
||||
val swapStories = getStoryContentUseCase(StoryContentIds.STORY_FIRST_TIME_SWAP.id).getOrNull()
|
||||
?: return@launch
|
||||
val storiesImages = swapStories.getImageUrls()
|
||||
|
||||
// try to preload images for stories
|
||||
storiesImages.forEach(imagePreloader::preload)
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
Timber.e(ex.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue