[fenix] For https://github.com/mozilla-mobile/fenix/issues/22851 toggle wallpapers when tap on the Firefox logo
parent
de1e52a234
commit
5143063813
@ -0,0 +1,16 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.wallpapers
|
||||
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
/**
|
||||
* A enum that represents the available wallpapers and their states.
|
||||
*/
|
||||
enum class Wallpaper(val drawable: Int) {
|
||||
FIRST(R.drawable.wallpaper_1),
|
||||
SECOND(R.drawable.wallpaper_2),
|
||||
NONE(R.attr.homeBackground);
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.wallpapers
|
||||
|
||||
import android.view.View
|
||||
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
/**
|
||||
* Provides access to available wallpapers and manages their states.
|
||||
*/
|
||||
class WallpaperManager(private val settings: Settings) {
|
||||
|
||||
var currentWallpaper: Wallpaper = getCurrentWallpaperFromSettings()
|
||||
set(value) {
|
||||
settings.currentWallpaper = value.name
|
||||
field = value
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the [newWallpaper] into the [wallpaperContainer] and update the [currentWallpaper].
|
||||
*/
|
||||
fun updateWallpaper(wallpaperContainer: View, newWallpaper: Wallpaper) {
|
||||
if (newWallpaper == Wallpaper.NONE) {
|
||||
val context = wallpaperContainer.context
|
||||
wallpaperContainer.setBackgroundColor(context.getColorFromAttr(newWallpaper.drawable))
|
||||
} else {
|
||||
wallpaperContainer.setBackgroundResource(newWallpaper.drawable)
|
||||
}
|
||||
currentWallpaper = newWallpaper
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next available [Wallpaper], the [currentWallpaper] is the last one then
|
||||
* the first available [Wallpaper] will be returned.
|
||||
*/
|
||||
fun switchToNextWallpaper(): Wallpaper {
|
||||
val values = Wallpaper.values()
|
||||
val index = values.indexOf(currentWallpaper) + 1
|
||||
|
||||
return if (index >= values.size) {
|
||||
values.first()
|
||||
} else {
|
||||
values[index]
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCurrentWallpaperFromSettings(): Wallpaper {
|
||||
val currentWallpaper = settings.currentWallpaper
|
||||
return if (currentWallpaper.isEmpty()) {
|
||||
Wallpaper.NONE
|
||||
} else {
|
||||
Wallpaper.valueOf(currentWallpaper)
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 167 KiB |
Binary file not shown.
After Width: | Height: | Size: 164 KiB |
Loading…
Reference in New Issue