|
| 1 | +@file:Suppress("TooGenericExceptionCaught") |
| 2 | + |
| 3 | +package io.snabble.sdk.assetservice |
| 4 | + |
| 5 | +import android.content.Context |
| 6 | +import android.content.res.Configuration |
| 7 | +import android.graphics.Bitmap |
| 8 | +import android.graphics.BitmapFactory |
| 9 | +import android.graphics.Canvas |
| 10 | +import android.util.DisplayMetrics |
| 11 | +import com.caverock.androidsvg.SVG |
| 12 | +import io.snabble.sdk.Project |
| 13 | +import io.snabble.sdk.assetservice.assets.data.AssetsRepositoryImpl |
| 14 | +import io.snabble.sdk.assetservice.assets.data.source.LocalAssetDataSourceImpl |
| 15 | +import io.snabble.sdk.assetservice.assets.data.source.RemoteAssetsSourceImpl |
| 16 | +import io.snabble.sdk.assetservice.assets.domain.AssetsRepository |
| 17 | +import io.snabble.sdk.assetservice.image.data.ImageRepositoryImpl |
| 18 | +import io.snabble.sdk.assetservice.image.data.local.image.LocalDiskDataSourceImpl |
| 19 | +import io.snabble.sdk.assetservice.image.data.local.image.LocalMemorySourceImpl |
| 20 | +import io.snabble.sdk.assetservice.image.domain.ImageRepository |
| 21 | +import io.snabble.sdk.assetservice.image.domain.model.Type |
| 22 | +import io.snabble.sdk.assetservice.image.domain.model.UiMode |
| 23 | +import io.snabble.sdk.utils.Logger |
| 24 | +import java.io.InputStream |
| 25 | +import kotlin.math.roundToInt |
| 26 | + |
| 27 | +interface AssetService { |
| 28 | + |
| 29 | + suspend fun updateAllAssets() |
| 30 | + |
| 31 | + suspend fun loadAsset(name: String, type: Type, uiMode: UiMode): Bitmap? |
| 32 | +} |
| 33 | + |
| 34 | +internal class AssetServiceImpl( |
| 35 | + private val displayMetrics: DisplayMetrics, |
| 36 | + private val assetRepository: AssetsRepository, |
| 37 | + private val imageRepository: ImageRepository, |
| 38 | +) : AssetService { |
| 39 | + |
| 40 | + /** |
| 41 | + * Updates all assets and safes them locally |
| 42 | + */ |
| 43 | + override suspend fun updateAllAssets() { |
| 44 | + assetRepository.updateAllAssets() |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Loads an asset and returns it converted as [Bitmap]. |
| 49 | + * Bitmap type can be any of these [Type]. |
| 50 | + * To define the [UiMode] use the helper function [Context.getUiMode] or set it directly if needed. |
| 51 | + */ |
| 52 | + override suspend fun loadAsset(name: String, type: Type, uiMode: UiMode): Bitmap? { |
| 53 | + val bitmap = when (val bitmap = imageRepository.getBitmap(key = name)) { |
| 54 | + null -> createBitmap(name, type, uiMode) |
| 55 | + else -> bitmap |
| 56 | + } |
| 57 | + |
| 58 | + if (bitmap == null) { |
| 59 | + val newBitmap = updateAssetsAndRetry(name, type, uiMode) |
| 60 | + return newBitmap?.also { |
| 61 | + imageRepository.putBitmap(name, it) |
| 62 | + } |
| 63 | + } else { |
| 64 | + //Save converted bitmap |
| 65 | + imageRepository.putBitmap(name, bitmap) |
| 66 | + } |
| 67 | + |
| 68 | + return bitmap |
| 69 | + } |
| 70 | + |
| 71 | + private fun createSVGBitmap(data: InputStream): Bitmap? { |
| 72 | + val svg = SVG.getFromInputStream(data) |
| 73 | + return try { |
| 74 | + |
| 75 | + val width = svg.getDocumentWidth() * displayMetrics.density |
| 76 | + val height = svg.getDocumentHeight() * displayMetrics.density |
| 77 | + |
| 78 | + // Set the SVG's view box to the desired size |
| 79 | + svg.setDocumentWidth(width) |
| 80 | + svg.setDocumentHeight(height) |
| 81 | + |
| 82 | + // Create bitmap and canvas |
| 83 | + val bitmap = androidx.core.graphics.createBitmap(width.roundToInt(), height.roundToInt()) |
| 84 | + val canvas = Canvas(bitmap) |
| 85 | + |
| 86 | + // Render SVG to canvas |
| 87 | + svg.renderToCanvas(canvas) |
| 88 | + |
| 89 | + bitmap |
| 90 | + } catch (e: Exception) { |
| 91 | + Logger.e("Error converting SVG to bitmap", e) |
| 92 | + null |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private suspend fun createBitmap(name: String, type: Type, uiMode: UiMode): Bitmap? { |
| 97 | + val cachedAsset = |
| 98 | + assetRepository.loadAsset(name = name, type = type, uiMode = uiMode) ?: return null |
| 99 | + return when (type) { |
| 100 | + Type.SVG -> createSVGBitmap(cachedAsset.data) |
| 101 | + Type.JPG, |
| 102 | + Type.WEBP -> BitmapFactory.decodeStream(cachedAsset.data) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private suspend fun updateAssetsAndRetry(name: String, type: Type, uiMode: UiMode): Bitmap? { |
| 107 | + assetRepository.updateAllAssets() |
| 108 | + return createBitmap(name, type, uiMode) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fun assetServiceFactory( |
| 113 | + project: Project, |
| 114 | + context: Context |
| 115 | +): AssetService { |
| 116 | + val localDiskDataSource = LocalDiskDataSourceImpl(storageDirectory = project.internalStorageDirectory) |
| 117 | + val localMemoryDataSource = LocalMemorySourceImpl() |
| 118 | + val imageRepository = ImageRepositoryImpl( |
| 119 | + localMemoryDataSource = localMemoryDataSource, |
| 120 | + localDiskDataSource = localDiskDataSource |
| 121 | + ) |
| 122 | + |
| 123 | + val localAssetDataSource = LocalAssetDataSourceImpl(project) |
| 124 | + val remoteAssetsSource = RemoteAssetsSourceImpl(project) |
| 125 | + val assetRepository = AssetsRepositoryImpl( |
| 126 | + remoteAssetsSource = remoteAssetsSource, |
| 127 | + localAssetDataSource = localAssetDataSource |
| 128 | + ) |
| 129 | + |
| 130 | + return AssetServiceImpl( |
| 131 | + assetRepository = assetRepository, |
| 132 | + imageRepository = imageRepository, |
| 133 | + displayMetrics = context.resources.displayMetrics |
| 134 | + ) |
| 135 | +} |
| 136 | + |
| 137 | +fun Context.getUiMode() = if (isDarkMode()) UiMode.NIGHT else UiMode.DAY |
| 138 | + |
| 139 | +// Method 2: Extension function for cleaner usage |
| 140 | +private fun Context.isDarkMode(): Boolean { |
| 141 | + return when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { |
| 142 | + Configuration.UI_MODE_NIGHT_YES -> true |
| 143 | + Configuration.UI_MODE_NIGHT_NO -> false |
| 144 | + else -> false |
| 145 | + } |
| 146 | +} |
0 commit comments