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 19, 2022
1 parent 0c6cf5f commit 80d0262
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 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,7 +390,10 @@ class HomeFragment : Fragment() {

FxNimbus.features.homescreen.recordExposure()

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

binding.root.doOnPreDraw {
requireComponents.appStore.dispatch(AppAction.UpdateFirstFrameDrawn(drawn = true))
Expand All @@ -407,6 +410,7 @@ class HomeFragment : Fragment() {
super.onConfigurationChanged(newConfig)

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

/**
Expand Down Expand Up @@ -923,30 +927,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

0 comments on commit 80d0262

Please sign in to comment.