2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/27330 - Cache selected wallpaper card colors

This commit is contained in:
Noah Bond 2022-10-11 09:51:18 -07:00 committed by mergify[bot]
parent 1a4af485fd
commit 3733fcb703
7 changed files with 69 additions and 19 deletions

View File

@ -200,6 +200,24 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = 0,
)
/**
* A cache of the background color to use on cards overlaying the current wallpaper when the user's
* theme is set to Light.
*/
var currentWallpaperCardColorLight by longPreference(
appContext.getPreferenceKey(R.string.pref_key_current_wallpaper_card_color_light),
default = 0,
)
/**
* A cache of the background color to use on cards overlaying the current wallpaper when the user's
* theme is set to Dark.
*/
var currentWallpaperCardColorDark by longPreference(
appContext.getPreferenceKey(R.string.pref_key_current_wallpaper_card_color_dark),
default = 0,
)
/**
* Indicates if the current legacy wallpaper should be migrated.
*/

View File

@ -117,15 +117,18 @@ data class Wallpaper(
*
* @param settings The local cache.
*/
@Suppress("ComplexCondition")
fun getCurrentWallpaperFromSettings(settings: Settings): Wallpaper? {
val name = settings.currentWallpaperName
val textColor = settings.currentWallpaperTextColor
return if (name.isNotEmpty() && textColor != 0L) {
val cardColorLight = settings.currentWallpaperCardColorLight
val cardColorDark = settings.currentWallpaperCardColorDark
return if (name.isNotEmpty() && textColor != 0L && cardColorLight != 0L && cardColorDark != 0L) {
Wallpaper(
name = name,
textColor = textColor,
cardColorLight = null,
cardColorDark = null,
cardColorLight = cardColorLight,
cardColorDark = cardColorDark,
collection = DefaultCollection,
thumbnailFileState = ImageFileState.Downloaded,
assetsFileState = ImageFileState.Downloaded,

View File

@ -9,6 +9,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.getLocalPath
import java.io.File
@ -28,15 +29,18 @@ class WallpaperFileManager(
/**
* 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.
*
* @param settings The local cache.
*/
suspend fun lookupExpiredWallpaper(name: String): Wallpaper? = withContext(coroutineDispatcher) {
suspend fun lookupExpiredWallpaper(settings: Settings): Wallpaper? = withContext(coroutineDispatcher) {
val name = settings.currentWallpaperName
if (allAssetsExist(name)) {
Wallpaper(
name = name,
collection = Wallpaper.DefaultCollection,
textColor = null,
cardColorLight = null,
cardColorDark = null,
textColor = settings.currentWallpaperTextColor,
cardColorLight = settings.currentWallpaperCardColorLight,
cardColorDark = settings.currentWallpaperCardColorDark,
thumbnailFileState = Wallpaper.ImageFileState.Downloaded,
assetsFileState = Wallpaper.ImageFileState.Downloaded,
)

View File

@ -261,7 +261,7 @@ class WallpapersUseCases(
!it.isExpired() && it.isAvailableInLocale()
}
val currentWallpaper = possibleWallpapers.find { it.name == currentWallpaperName }
?: fileManager.lookupExpiredWallpaper(currentWallpaperName)
?: fileManager.lookupExpiredWallpaper(settings)
?: Wallpaper.Default
// Dispatching this early will make it accessible to the home screen ASAP. If it has been
@ -459,6 +459,8 @@ class WallpapersUseCases(
override suspend fun invoke(wallpaper: Wallpaper): Wallpaper.ImageFileState {
settings.currentWallpaperName = wallpaper.name
settings.currentWallpaperTextColor = wallpaper.textColor ?: 0
settings.currentWallpaperCardColorLight = wallpaper.cardColorLight ?: 0
settings.currentWallpaperCardColorDark = wallpaper.cardColorDark ?: 0
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper))
return Wallpaper.ImageFileState.Downloaded
}
@ -496,6 +498,8 @@ class WallpapersUseCases(
internal fun selectWallpaper(wallpaper: Wallpaper) {
settings.currentWallpaperName = wallpaper.name
settings.currentWallpaperTextColor = wallpaper.textColor ?: 0L
settings.currentWallpaperCardColorLight = wallpaper.cardColorLight ?: 0L
settings.currentWallpaperCardColorDark = wallpaper.cardColorDark ?: 0L
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper))
}

View File

@ -205,6 +205,8 @@
<string name="pref_key_wallpapers" translatable="false">pref_key_wallpapers</string>
<string name="pref_key_current_wallpaper" translatable="false">pref_key_current_wallpaper</string>
<string name="pref_key_current_wallpaper_text_color" translatable="false">pref_key_current_wallpaper_text_color</string>
<string name="pref_key_current_wallpaper_card_color_light" translatable="false">pref_key_current_wallpaper_card_color_light</string>
<string name="pref_key_current_wallpaper_card_color_dark" translatable="false">pref_key_current_wallpaper_card_color_dark</string>
<string name="pref_key_wallpapers_onboarding" translatable="false">pref_key_wallpapers_onboarding</string>
<string name="pref_key_should_migrate_wallpaper" translatable="false">pref_key_should_migrate_wallpaper</string>

View File

@ -1,5 +1,7 @@
package org.mozilla.fenix.wallpapers
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
@ -9,6 +11,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.mozilla.fenix.utils.Settings
import java.io.File
class WallpaperFileManagerTest {
@ -21,6 +24,8 @@ class WallpaperFileManagerTest {
private lateinit var fileManager: WallpaperFileManager
private lateinit var settings: Settings
@Before
fun setup() {
wallpapersFolder = File(tempFolder.root, "wallpapers")
@ -28,14 +33,19 @@ class WallpaperFileManagerTest {
storageRootDirectory = tempFolder.root,
coroutineDispatcher = dispatcher,
)
settings = mockk {
every { currentWallpaperName } returns wallpaperName
every { currentWallpaperTextColor } returns 0L
every { currentWallpaperCardColorLight } returns 0L
every { currentWallpaperCardColorDark } returns 0L
}
}
@Test
fun `GIVEN wallpaper directory exists WHEN looked up THEN wallpaper created with correct name`() = runTest {
val wallpaperName = "name"
createAllFiles(wallpaperName)
val result = fileManager.lookupExpiredWallpaper(wallpaperName)
val result = fileManager.lookupExpiredWallpaper(settings)
val expected = generateWallpaper(name = wallpaperName)
assertEquals(expected, result)
@ -43,7 +53,6 @@ class WallpaperFileManagerTest {
@Test
fun `GIVEN portrait file missing in directories WHEN expired wallpaper looked up THEN null returned`() = runTest {
val wallpaperName = "name"
File(wallpapersFolder, "$wallpaperName/landscape.png").apply {
mkdirs()
createNewFile()
@ -53,14 +62,13 @@ class WallpaperFileManagerTest {
createNewFile()
}
val result = fileManager.lookupExpiredWallpaper(wallpaperName)
val result = fileManager.lookupExpiredWallpaper(settings)
assertEquals(null, result)
}
@Test
fun `GIVEN landscape file missing in directories WHEN expired wallpaper looked up THEN null returned`() = runTest {
val wallpaperName = "name"
File(wallpapersFolder, "$wallpaperName/portrait.png").apply {
mkdirs()
createNewFile()
@ -70,14 +78,13 @@ class WallpaperFileManagerTest {
createNewFile()
}
val result = fileManager.lookupExpiredWallpaper(wallpaperName)
val result = fileManager.lookupExpiredWallpaper(settings)
assertEquals(null, result)
}
@Test
fun `GIVEN thumbnail file missing in directories WHEN expired wallpaper looked up THEN null returned`() = runTest {
val wallpaperName = "name"
File(wallpapersFolder, "$wallpaperName/portrait.png").apply {
mkdirs()
createNewFile()
@ -87,7 +94,7 @@ class WallpaperFileManagerTest {
createNewFile()
}
val result = fileManager.lookupExpiredWallpaper(wallpaperName)
val result = fileManager.lookupExpiredWallpaper(settings)
assertEquals(null, result)
}
@ -153,11 +160,15 @@ class WallpaperFileManagerTest {
private fun generateWallpaper(name: String) = Wallpaper(
name = name,
textColor = null,
cardColorLight = null,
cardColorDark = null,
textColor = 0L,
cardColorLight = 0L,
cardColorDark = 0L,
thumbnailFileState = Wallpaper.ImageFileState.Downloaded,
assetsFileState = Wallpaper.ImageFileState.Downloaded,
collection = Wallpaper.DefaultCollection,
)
private companion object {
const val wallpaperName = "name"
}
}

View File

@ -41,6 +41,10 @@ class WallpapersUseCasesTest {
private val mockSettings = mockk<Settings> {
every { currentWallpaperTextColor } returns 0L
every { currentWallpaperTextColor = any() } just Runs
every { currentWallpaperCardColorLight } returns 0L
every { currentWallpaperCardColorLight = any() } just Runs
every { currentWallpaperCardColorDark } returns 0L
every { currentWallpaperCardColorDark = any() } just Runs
every { shouldMigrateLegacyWallpaper } returns false
every { shouldMigrateLegacyWallpaper = any() } just Runs
}
@ -609,6 +613,8 @@ class WallpapersUseCasesTest {
val wallpaper: Wallpaper = mockk {
every { name } returns "Test"
every { textColor } returns null
every { cardColorLight } returns null
every { cardColorDark } returns null
}
wallpaperFileState.selectWallpaper(wallpaper)
@ -631,6 +637,8 @@ class WallpapersUseCasesTest {
val wallpaper: Wallpaper = mockk {
every { name } returns "Test"
every { textColor } returns 321L
every { cardColorLight } returns 321L
every { cardColorDark } returns 321L
}
wallpaperFileState.selectWallpaper(wallpaper)