Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Closes #26213: Add wallpaper use case to load thumbnails.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewTighe committed Aug 22, 2022
1 parent 0cbf69a commit c17a084
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class WallpapersUseCases(
store: AppStore,
client: Client,
strictMode: StrictModeManager,
filesDir: File,
) {
val initialize: InitializeWallpapersUseCase by lazy {
if (FeatureFlags.wallpaperV2Enabled) {
Expand Down Expand Up @@ -89,6 +90,13 @@ class WallpapersUseCases(
LegacyLoadBitmapUseCase(context)
}
}
val loadThumbnail: LoadThumbnailUseCase by lazy {
if (FeatureFlags.wallpaperV2Enabled) {
DefaultLoadThumbnailUseCase(filesDir)
} else {
LegacyLoadThumbnailUseCase(context)
}
}
val selectWallpaper: SelectWallpaperUseCase by lazy { DefaultSelectWallpaperUseCase(context.settings(), store) }

/**
Expand Down Expand Up @@ -369,6 +377,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.
*/
Expand Down

0 comments on commit c17a084

Please sign in to comment.