Skip to content

Commit

Permalink
For mozilla-mobile#26511: load homescreen wallpaper in blocking corou…
Browse files Browse the repository at this point in the history
…tine
  • Loading branch information
MatthewTighe committed Aug 23, 2022
1 parent 627447a commit 4b66860
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
51 changes: 33 additions & 18 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -115,6 +114,7 @@ import org.mozilla.fenix.home.topsites.DefaultTopSitesView
import org.mozilla.fenix.nimbus.FxNimbus
import org.mozilla.fenix.onboarding.FenixOnboarding
import org.mozilla.fenix.perf.MarkersFragmentLifecycleCallbacks
import org.mozilla.fenix.perf.runBlockingIncrement
import org.mozilla.fenix.tabstray.TabsTrayAccessPoint
import org.mozilla.fenix.utils.Settings.Companion.TOP_SITES_PROVIDER_MAX_THRESHOLD
import org.mozilla.fenix.utils.ToolbarPopupWindow
Expand Down Expand Up @@ -390,12 +390,14 @@ class HomeFragment : Fragment() {

FxNimbus.features.homescreen.recordExposure()

displayWallpaperIfEnabled()

binding.root.doOnPreDraw {
requireComponents.appStore.dispatch(AppAction.UpdateFirstFrameDrawn(drawn = true))
}

// See https://github.com/mozilla-mobile/fenix/issues/26555 for context as to why
// this is commented out for now
observeWallpaperChanges()

// DO NOT MOVE ANYTHING BELOW THIS addMarker CALL!
requireComponents.core.engine.profiler?.addMarker(
MarkersFragmentLifecycleCallbacks.MARKER_NAME, profilerStartTime, "HomeFragment.onCreateView",
Expand All @@ -407,6 +409,7 @@ class HomeFragment : Fragment() {
super.onConfigurationChanged(newConfig)

getMenuButton()?.dismissMenu()
setWallpaperToCurrent()
}

/**
Expand Down Expand Up @@ -923,30 +926,42 @@ class HomeFragment : Fragment() {
?.isVisible = tabCount > 0
}

private fun displayWallpaperIfEnabled() {
@Suppress("UnusedPrivateMember")
private fun observeWallpaperChanges() {
if (shouldEnableWallpaper()) {
requireComponents.appStore.flow()
.ifChanged { state -> state.wallpaperState.currentWallpaper }
.onEach { state ->
// We only want to update the wallpaper when it's different from the default one
// as the default is applied already on xml by default.
when (val currentWallpaper = state.wallpaperState.currentWallpaper) {
Wallpaper.Default -> {
binding.wallpaperImageView.visibility = View.GONE
}
else -> {
val bitmap = requireComponents.useCases.wallpaperUseCases.loadBitmap(currentWallpaper)
bitmap?.let {
it.scaleToBottomOfView(binding.wallpaperImageView)
binding.wallpaperImageView.visibility = View.VISIBLE
}
}
}
showWallpaper(state.wallpaperState.currentWallpaper)
}
.launchIn(viewLifecycleOwner.lifecycleScope)
}
}

private fun setWallpaperToCurrent() {
if (shouldEnableWallpaper()) {
val wallpaper = requireComponents.appStore.state.wallpaperState.currentWallpaper
runBlockingIncrement {
showWallpaper(wallpaper)
}
}
}

private suspend fun showWallpaper(wallpaper: Wallpaper) = when (wallpaper) {
// We only want to update the wallpaper when it's different from the default one
// as the default is applied already on xml by default.
Wallpaper.Default -> {
binding.wallpaperImageView.visibility = View.GONE
}
else -> {
val bitmap = requireComponents.useCases.wallpaperUseCases.loadBitmap(wallpaper)
bitmap?.let {
it.scaleToBottomOfView(binding.wallpaperImageView)
binding.wallpaperImageView.visibility = View.VISIBLE
}
}
}

private fun shouldEnableWallpaper() =
(activity as? HomeActivity)?.themeManager?.currentTheme?.isPrivate?.not() ?: false

Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/org/mozilla/fenix/utils/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = Wallpaper.Default.name
)

/**
* A cache of the text color to use on text overlaying the current wallpaper.
*/
var currentWallpaperTextColor by longPreference(
appContext.getPreferenceKey(R.string.pref_key_current_wallpaper_text_color),
default = 0
)

/**
* A cache of the background color to use on cards overlaying the current wallpaper.
*/
var currentWallpaperCardColor by longPreference(
appContext.getPreferenceKey(R.string.pref_key_current_wallpaper_card_color),
default = 0
)

/**
* Indicates if the wallpaper onboarding dialog should be shown.
*/
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/org/mozilla/fenix/wallpapers/Wallpaper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package org.mozilla.fenix.wallpapers

import org.mozilla.fenix.utils.Settings
import java.util.Date

/**
Expand Down Expand Up @@ -84,6 +85,20 @@ data class Wallpaper(
* @param name The name of the wallpaper.
*/
fun getLocalPath(name: String, type: ImageType) = "wallpapers/$name/${type.lowercase()}.png"

fun getCurrentWallpaperFromSettings(settings: Settings): Wallpaper? {
val name = settings.currentWallpaperName
val textColor = settings.currentWallpaperTextColor
val cardColor = settings.currentWallpaperCardColor
return if (name.isNotEmpty() && textColor != 0L && cardColor != 0L) {
Wallpaper(
name = name,
textColor = textColor,
cardColor = cardColor,
collection = DefaultCollection
)
} else null
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class WallpapersUseCases(
// This should be cleaned up as improvements are made to the storage, file management,
// and download utilities.
withContext(Dispatchers.IO) {
val dispatchedCurrent = Wallpaper.getCurrentWallpaperFromSettings(settings)?.let {
// Dispatch this ASAP so the home screen can render.
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(it))
true
} ?: false
val availableWallpapers = possibleWallpapers.getAvailableWallpapers()
val currentWallpaperName = settings.currentWallpaperName
val currentWallpaper = possibleWallpapers.find { it.name == currentWallpaperName }
Expand All @@ -136,7 +141,9 @@ class WallpapersUseCases(
)
downloadAllRemoteWallpapers(availableWallpapers)
store.dispatch(AppAction.WallpaperAction.UpdateAvailableWallpapers(availableWallpapers))
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(currentWallpaper))
if (!dispatchedCurrent) {
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(currentWallpaper))
}
}
}

Expand Down Expand Up @@ -217,6 +224,9 @@ class WallpapersUseCases(
private val currentLocale: String,
) : InitializeWallpapersUseCase {
override suspend fun invoke() {
Wallpaper.getCurrentWallpaperFromSettings(settings)?.let {
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(it))
}
val currentWallpaperName = withContext(Dispatchers.IO) { settings.currentWallpaperName }
val possibleWallpapers = metadataFetcher.downloadWallpaperList().filter {
!it.isExpired() && it.isAvailableInLocale()
Expand All @@ -225,7 +235,8 @@ class WallpapersUseCases(
?: fileManager.lookupExpiredWallpaper(currentWallpaperName)
?: Wallpaper.Default

// Dispatching this early will make it accessible to the home screen ASAP
// Dispatching this early will make it accessible to the home screen ASAP. If it has been
// dispatched above, we may still need to update other metadata about it.
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(currentWallpaper))

fileManager.clean(
Expand Down Expand Up @@ -383,6 +394,8 @@ class WallpapersUseCases(
*/
override fun invoke(wallpaper: Wallpaper) {
settings.currentWallpaperName = wallpaper.name
settings.currentWallpaperTextColor = wallpaper.textColor ?: 0
settings.currentWallpaperCardColor = wallpaper.cardColor ?: 0
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper))
Wallpapers.wallpaperSelected.record(
Wallpapers.WallpaperSelectedExtra(
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@
<!-- Wallpaper Settings -->
<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" translatable="false">pref_key_current_wallpaper_card_color</string>
<string name="pref_key_wallpapers_onboarding" translatable="false">pref_key_wallpapers_onboarding</string>

<string name="pref_key_encryption_key_generated" translatable="false">pref_key_encryption_key_generated</string>
Expand Down

0 comments on commit 4b66860

Please sign in to comment.