You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.9 KiB
Kotlin
59 lines
1.9 KiB
Kotlin
3 years ago
|
/* 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)
|
||
|
}
|
||
|
}
|
||
|
}
|