-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fixes: #26052 remove WallpaperManager::updateWallpaper #26249
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,30 +58,6 @@ class WallpaperManager( | |
fileManager.clean(currentWallpaper, wallpapers.filterIsInstance<Wallpaper.Remote>()) | ||
} | ||
|
||
/** | ||
* Apply the [newWallpaper] into the [wallpaperContainer] and update the [currentWallpaper]. | ||
*/ | ||
fun updateWallpaper(wallpaperContainer: ImageView, newWallpaper: Wallpaper) { | ||
val context = wallpaperContainer.context | ||
if (newWallpaper == defaultWallpaper) { | ||
wallpaperContainer.visibility = View.GONE | ||
logger.info("Wallpaper update to default background") | ||
} else { | ||
val bitmap = loadSavedWallpaper(context, newWallpaper) | ||
if (bitmap == null) { | ||
val message = "Could not load wallpaper bitmap. Resetting to default." | ||
logger.error(message) | ||
currentWallpaper = defaultWallpaper | ||
wallpaperContainer.visibility = View.GONE | ||
return | ||
} else { | ||
wallpaperContainer.visibility = View.VISIBLE | ||
scaleBitmapToBottom(bitmap, wallpaperContainer) | ||
} | ||
} | ||
currentWallpaper = newWallpaper | ||
} | ||
|
||
/** | ||
* Download all known remote wallpapers. | ||
*/ | ||
|
@@ -104,7 +80,7 @@ class WallpaperManager( | |
} else { | ||
values[index] | ||
}.also { | ||
appStore.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(it)) | ||
currentWallpaper = it | ||
} | ||
} | ||
|
||
|
@@ -139,10 +115,10 @@ class WallpaperManager( | |
/** | ||
* Load a wallpaper that is saved locally. | ||
*/ | ||
fun loadSavedWallpaper(context: Context, wallpaper: Wallpaper): Bitmap? = | ||
when (wallpaper) { | ||
is Wallpaper.Local -> loadWallpaperFromDrawables(context, wallpaper) | ||
is Wallpaper.Remote -> loadWallpaperFromDisk(context, wallpaper) | ||
fun Wallpaper.load(context: Context): Bitmap? = | ||
when (this) { | ||
is Wallpaper.Local -> loadWallpaperFromDrawables(context, this) | ||
is Wallpaper.Remote -> loadWallpaperFromDisk(context, this) | ||
else -> null | ||
} | ||
|
||
|
@@ -163,7 +139,14 @@ class WallpaperManager( | |
} | ||
}.getOrNull() | ||
|
||
private fun scaleBitmapToBottom(bitmap: Bitmap, view: ImageView) { | ||
/** | ||
* This will scale the received [Bitmap] to the size of the [view]. It retains the bitmap's | ||
* original aspect ratio, but will shrink or enlarge it to fit the viewport. If bitmap does not | ||
* correctly fit the aspect ratio of the view, it will be shifted to prioritize the bottom-left | ||
* of the bitmap. | ||
*/ | ||
fun Bitmap.scaleBitmapToBottomOfView(view: ImageView) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should this extension instead live in a extension function file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one would make more sense to move into an extension function file, IMO, because it isn't necessarily a strictly wallpaper-related function. I just always worry about polluting namespaces without an obvious need. I'd defer to your opinion if you think we should move it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel super strongly either way, so if you feel this will be used elsewhere, I'd lean that way. Otherwise, it can just live here for now. |
||
val bitmap = this | ||
view.setImageBitmap(bitmap) | ||
view.scaleType = ImageView.ScaleType.MATRIX | ||
val matrix = Matrix() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Should this extension instead live in a extension function file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one technically could, but structuring it this way was an intentional plan for refactoring the
WallpaperManager
as aWallpaperUseCase
. It forces a client to have a reference to theUseCase
to be able to load wallpapers. That way, we can treat the UseCase as an abstraction barrier. Right now, all the loading is done using implicitContext
-dependent stuff, but I imagine the callstack would look more like this soon:Not sure that notation really makes sense lol. But basically we could inject the
context
(or aWallpaperLoader
or whatever else we wanted to use to actually do disk reading) into the object hierarchy and hide it from call sites ofwallpaper.load()
.Sidenote: the pattern here might be more clear if we were on Kotlin 1.6.20 and could use context receivers.