For #27289: standardize coroutine usage in WallpaperFileManager

scope variable inlined as in only one place it is used, class injected dispatcher is used in all withcontext to use differnt dispatchers in different scenarios, clean function suspend keyword removed, as there no requirement to know its result, unnecessary return removed
This commit is contained in:
suman 2022-10-10 23:12:30 +05:30 committed by mergify[bot]
parent 3207e6213e
commit fab8300a78
1 changed files with 6 additions and 7 deletions

View File

@ -21,16 +21,15 @@ import java.io.File
*/
class WallpaperFileManager(
private val storageRootDirectory: File,
coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
) {
private val scope = CoroutineScope(coroutineDispatcher)
private val wallpapersDirectory = File(storageRootDirectory, "wallpapers")
/**
* Lookup all the files for a wallpaper name. This lookup will fail if there are not
* files for each of a portrait and landscape orientation as well as a thumbnail.
*/
suspend fun lookupExpiredWallpaper(name: String): Wallpaper? = withContext(Dispatchers.IO) {
suspend fun lookupExpiredWallpaper(name: String): Wallpaper? = withContext(coroutineDispatcher) {
if (allAssetsExist(name)) {
Wallpaper(
name = name,
@ -54,8 +53,8 @@ class WallpaperFileManager(
/**
* Remove all wallpapers that are not the [currentWallpaper] or in [availableWallpapers].
*/
suspend fun clean(currentWallpaper: Wallpaper, availableWallpapers: List<Wallpaper>) = withContext(Dispatchers.IO) {
scope.launch {
fun clean(currentWallpaper: Wallpaper, availableWallpapers: List<Wallpaper>) {
CoroutineScope(coroutineDispatcher).launch {
val wallpapersToKeep = (listOf(currentWallpaper) + availableWallpapers).map { it.name }
wallpapersDirectory.listFiles()?.forEach { file ->
if (file.isDirectory && !wallpapersToKeep.contains(file.name)) {
@ -68,7 +67,7 @@ class WallpaperFileManager(
/**
* Checks whether all the assets for a wallpaper exist on the file system.
*/
suspend fun wallpaperImagesExist(wallpaper: Wallpaper): Boolean = withContext(Dispatchers.IO) {
return@withContext allAssetsExist(wallpaper.name)
suspend fun wallpaperImagesExist(wallpaper: Wallpaper): Boolean = withContext(coroutineDispatcher) {
allAssetsExist(wallpaper.name)
}
}