2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-03 23:15:31 +00:00

[fenix] Closes https://github.com/mozilla-mobile/fenix/issues/26213: Add wallpaper use case to load thumbnails.

This commit is contained in:
MatthewTighe 2022-08-19 10:15:00 -07:00 committed by mergify[bot]
parent 3dbb42988f
commit a3f055a36e

View File

@ -43,6 +43,7 @@ class WallpapersUseCases(
store: AppStore, store: AppStore,
client: Client, client: Client,
strictMode: StrictModeManager, strictMode: StrictModeManager,
filesDir: File,
) { ) {
val initialize: InitializeWallpapersUseCase by lazy { val initialize: InitializeWallpapersUseCase by lazy {
if (FeatureFlags.wallpaperV2Enabled) { if (FeatureFlags.wallpaperV2Enabled) {
@ -89,6 +90,13 @@ class WallpapersUseCases(
LegacyLoadBitmapUseCase(context) LegacyLoadBitmapUseCase(context)
} }
} }
val loadThumbnail: LoadThumbnailUseCase by lazy {
if (FeatureFlags.wallpaperV2Enabled) {
DefaultLoadThumbnailUseCase(filesDir)
} else {
LegacyLoadThumbnailUseCase(context)
}
}
val selectWallpaper: SelectWallpaperUseCase by lazy { DefaultSelectWallpaperUseCase(context.settings(), store) } val selectWallpaper: SelectWallpaperUseCase by lazy { DefaultSelectWallpaperUseCase(context.settings(), store) }
/** /**
@ -380,6 +388,37 @@ class WallpapersUseCases(
} }
} }
/**
* Contract for usecase for loading thumbnail bitmaps related to a specific wallpaper.
*/
interface LoadThumbnailUseCase {
/**
* Load the bitmap for a [wallpaper] thumbnail, if available.
*
* @param wallpaper The wallpaper to load a thumbnail for.
*/
suspend operator fun invoke(wallpaper: Wallpaper): Bitmap?
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal class LegacyLoadThumbnailUseCase(private val context: Context): LoadThumbnailUseCase {
override suspend fun invoke(wallpaper: Wallpaper): Bitmap? =
LegacyLoadBitmapUseCase(context).invoke(wallpaper)
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal class DefaultLoadThumbnailUseCase(private val filesDir: File): LoadThumbnailUseCase {
override suspend fun invoke(wallpaper: Wallpaper): Bitmap? = withContext(Dispatchers.IO) {
Result.runCatching {
val path = Wallpaper.getLocalPath(wallpaper.name, Wallpaper.ImageType.Thumbnail)
withContext(Dispatchers.IO) {
val file = File(filesDir, path)
BitmapFactory.decodeStream(file.inputStream())
}
}.getOrNull()
}
}
/** /**
* Contract for usecase of selecting a new wallpaper. * Contract for usecase of selecting a new wallpaper.
*/ */