Skip to content

Commit

Permalink
For mozilla-mobile#26511: apply wallpapers immediately and observe up…
Browse files Browse the repository at this point in the history
…dates
  • Loading branch information
MatthewTighe committed Sep 12, 2022
1 parent 1bc33c5 commit bda73e8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 349 deletions.
67 changes: 45 additions & 22 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import org.mozilla.fenix.ext.hideToolbar
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.runIfFragmentIsAttached
import org.mozilla.fenix.ext.scaleToBottomOfView
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.gleanplumb.DefaultMessageController
import org.mozilla.fenix.gleanplumb.MessagingFeature
Expand Down Expand Up @@ -115,6 +116,7 @@ import org.mozilla.fenix.tabstray.TabsTrayAccessPoint
import org.mozilla.fenix.utils.Settings.Companion.TOP_SITES_PROVIDER_MAX_THRESHOLD
import org.mozilla.fenix.utils.ToolbarPopupWindow
import org.mozilla.fenix.utils.allowUndo
import org.mozilla.fenix.wallpapers.Wallpaper
import java.lang.ref.WeakReference
import kotlin.math.min

Expand Down Expand Up @@ -165,8 +167,7 @@ class HomeFragment : Fragment() {
private var appBarLayout: AppBarLayout? = null
private lateinit var currentMode: CurrentMode

@VisibleForTesting
internal var wallpapersObserver: WallpapersObserver? = null
private var lastAppliedWallpaperName: String = Wallpaper.defaultName

private val topSitesFeature = ViewBoundFeatureWrapper<TopSitesFeature>()
private val messagingFeature = ViewBoundFeatureWrapper<MessagingFeature>()
Expand Down Expand Up @@ -214,15 +215,8 @@ class HomeFragment : Fragment() {
val activity = activity as HomeActivity
val components = requireComponents

if (shouldEnableWallpaper()) {
wallpapersObserver = WallpapersObserver(
appStore = components.appStore,
wallpapersUseCases = components.useCases.wallpaperUseCases,
wallpaperImageView = binding.wallpaperImageView,
).also {
viewLifecycleOwner.lifecycle.addObserver(it)
}
}
val currentWallpaperName = requireContext().settings().currentWallpaperName
applyWallpaper(wallpaperName = currentWallpaperName, orientationChange = false)

currentMode = CurrentMode(
requireContext(),
Expand Down Expand Up @@ -417,16 +411,8 @@ class HomeFragment : Fragment() {

getMenuButton()?.dismissMenu()

if (shouldEnableWallpaper()) {
// Setting the wallpaper is a potentially expensive operation - can take 100ms.
// Running this on the Main thread helps to ensure that the just updated configuration
// will be used when the wallpaper is scaled to match.
// Otherwise the portrait wallpaper may remain shown on landscape,
// see https://github.com/mozilla-mobile/fenix/issues/26638
runBlockingIncrement {
wallpapersObserver?.applyCurrentWallpaper()
}
}
val currentWallpaperName = requireContext().settings().currentWallpaperName
applyWallpaper(wallpaperName = currentWallpaperName, orientationChange = true)
}

/**
Expand Down Expand Up @@ -521,6 +507,7 @@ class HomeFragment : Fragment() {

observeSearchEngineChanges()
observeSearchEngineNameChanges()
observeWallpaperUpdates()

HomeMenuBuilder(
view = view,
Expand Down Expand Up @@ -708,7 +695,6 @@ class HomeFragment : Fragment() {
_sessionControlInteractor = null
sessionControlView = null
appBarLayout = null
wallpapersObserver = null
_binding = null
bundleArgs.clear()
}
Expand Down Expand Up @@ -953,6 +939,43 @@ class HomeFragment : Fragment() {
internal fun shouldEnableWallpaper() =
(activity as? HomeActivity)?.themeManager?.currentTheme?.isPrivate?.not() ?: false

private fun applyWallpaper(wallpaperName: String, orientationChange: Boolean) {
when {
!shouldEnableWallpaper() ||
(wallpaperName == lastAppliedWallpaperName && !orientationChange) -> return
wallpaperName == Wallpaper.defaultName -> {
binding.wallpaperImageView.isVisible = false
lastAppliedWallpaperName = wallpaperName
}
else -> {
runBlockingIncrement {
// loadBitmap does file lookups based on name, so we don't need a fully
// qualified type to load the image
val wallpaper = Wallpaper.Default.copy(name = wallpaperName)
val wallpaperImage =
requireComponents.useCases.wallpaperUseCases.loadBitmap(wallpaper)
wallpaperImage?.let {
it.scaleToBottomOfView(binding.wallpaperImageView)
binding.wallpaperImageView.isVisible = true
lastAppliedWallpaperName = wallpaperName
} ?: run {
binding.wallpaperImageView.isVisible = false
lastAppliedWallpaperName = Wallpaper.defaultName
}
}
}
}
}

private fun observeWallpaperUpdates() {
consumeFrom(requireComponents.appStore) {
val currentWallpaper = it.wallpaperState.currentWallpaper
if (currentWallpaper.name != lastAppliedWallpaperName) {
applyWallpaper(wallpaperName = currentWallpaper.name, orientationChange = false)
}
}
}

companion object {
const val ALL_NORMAL_TABS = "all_normal"
const val ALL_PRIVATE_TABS = "all_private"
Expand Down
100 changes: 0 additions & 100 deletions app/src/main/java/org/mozilla/fenix/home/WallpapersObserver.kt

This file was deleted.

44 changes: 0 additions & 44 deletions app/src/test/java/org/mozilla/fenix/home/HomeFragmentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
package org.mozilla.fenix.home

import android.content.Context
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import mozilla.components.browser.menu.view.MenuButton
import mozilla.components.browser.state.state.SearchState
import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine
import mozilla.components.feature.top.sites.TopSite
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -108,37 +105,6 @@ class HomeFragmentTest {
verify(exactly = 1) { menuButton.dismissMenu() }
}

@Test
fun `GIVEN the user is in normal mode WHEN configuration changes THEN the wallpaper is reapplied`() = runTest {
homeFragment.getMenuButton = { null }
val observer: WallpapersObserver = mockk(relaxed = true)
homeFragment.wallpapersObserver = observer
val activity: HomeActivity = mockk {
every { themeManager.currentTheme.isPrivate } returns false
}
every { homeFragment.activity } returns activity

homeFragment.onConfigurationChanged(mockk(relaxed = true))

coVerify { observer.applyCurrentWallpaper() }
}

@Test
fun `GIVEN the user is in private mode WHEN configuration changes THEN the wallpaper not updated`() = runTest {
homeFragment.getMenuButton = { null }
val observer: WallpapersObserver = mockk(relaxed = true)
homeFragment.wallpapersObserver = observer
val activity: HomeActivity = mockk {
every { themeManager.currentTheme.isPrivate } returns true
}
every { homeFragment.activity } returns activity

homeFragment.onConfigurationChanged(mockk(relaxed = true))

coVerify(exactly = 0) { observer.applyCurrentWallpaper() }
}

@Test
fun `GIVEN the user is in normal mode WHEN checking if should enable wallpaper THEN return true`() {
val activity: HomeActivity = mockk {
every { themeManager.currentTheme.isPrivate } returns false
Expand All @@ -157,14 +123,4 @@ class HomeFragmentTest {

assertFalse(homeFragment.shouldEnableWallpaper())
}

@Test
fun `GIVEN the wallpaper feature is active WHEN the fragment view is destroyed THEN cleanup the wallpaper observer`() {
homeFragment.bundleArgs = mockk(relaxed = true)
homeFragment.wallpapersObserver = mockk()

homeFragment.onDestroyView()

assertNull(homeFragment.wallpapersObserver)
}
}
Loading

0 comments on commit bda73e8

Please sign in to comment.