From 3733fcb70317a704b0732fd7d5dba40c8740ba26 Mon Sep 17 00:00:00 2001 From: Noah Bond Date: Tue, 11 Oct 2022 09:51:18 -0700 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/27330 - Cache selected wallpaper card colors --- .../java/org/mozilla/fenix/utils/Settings.kt | 18 ++++++++++ .../org/mozilla/fenix/wallpapers/Wallpaper.kt | 9 +++-- .../fenix/wallpapers/WallpaperFileManager.kt | 12 ++++--- .../fenix/wallpapers/WallpapersUseCases.kt | 6 +++- app/src/main/res/values/preference_keys.xml | 2 ++ .../wallpapers/WallpaperFileManagerTest.kt | 33 ++++++++++++------- .../wallpapers/WallpapersUseCasesTest.kt | 8 +++++ 7 files changed, 69 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt index ca97ad3655..8a7d3f77cb 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt @@ -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. */ diff --git a/app/src/main/java/org/mozilla/fenix/wallpapers/Wallpaper.kt b/app/src/main/java/org/mozilla/fenix/wallpapers/Wallpaper.kt index 897478d018..efcbed05c2 100644 --- a/app/src/main/java/org/mozilla/fenix/wallpapers/Wallpaper.kt +++ b/app/src/main/java/org/mozilla/fenix/wallpapers/Wallpaper.kt @@ -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, diff --git a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperFileManager.kt b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperFileManager.kt index e8d87c8f94..d7421dc069 100644 --- a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperFileManager.kt +++ b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperFileManager.kt @@ -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, ) diff --git a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt index 6f96672ef8..72ea35829f 100644 --- a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt +++ b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt @@ -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)) } diff --git a/app/src/main/res/values/preference_keys.xml b/app/src/main/res/values/preference_keys.xml index 6f18756194..6809cdda60 100644 --- a/app/src/main/res/values/preference_keys.xml +++ b/app/src/main/res/values/preference_keys.xml @@ -205,6 +205,8 @@ pref_key_wallpapers pref_key_current_wallpaper pref_key_current_wallpaper_text_color + pref_key_current_wallpaper_card_color_light + pref_key_current_wallpaper_card_color_dark pref_key_wallpapers_onboarding pref_key_should_migrate_wallpaper diff --git a/app/src/test/java/org/mozilla/fenix/wallpapers/WallpaperFileManagerTest.kt b/app/src/test/java/org/mozilla/fenix/wallpapers/WallpaperFileManagerTest.kt index a0013ba0ad..2f31e91b4a 100644 --- a/app/src/test/java/org/mozilla/fenix/wallpapers/WallpaperFileManagerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/wallpapers/WallpaperFileManagerTest.kt @@ -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" + } } diff --git a/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt b/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt index 5e96665ffc..9a6993bcd5 100644 --- a/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt +++ b/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt @@ -41,6 +41,10 @@ class WallpapersUseCasesTest { private val mockSettings = mockk { 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)