diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-04-08 01:26:13 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-04-08 01:26:13 +0000 |
commit | ea38cbe8e65aac22e320e394712e94acd60f4ab4 (patch) | |
tree | ac03f16b704c3dff016895fbacc9bcd3e0eb6af0 | |
parent | de55cbafd25a3cabe56096fadb6db186f91bcc6e (diff) | |
parent | 416a060030954c78c0b7882b2401122512d8361b (diff) |
Snap for 9905614 from 416a060030954c78c0b7882b2401122512d8361b to tm-qpr3-release
Change-Id: I1e3bd8e298a31b8b494c980e398efc41795d7dd8
-rw-r--r-- | src/com/android/wallpaper/picker/individual/IndividualPickerFragment2.kt | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment2.kt b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment2.kt index 198b3fde..3186eb5f 100644 --- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment2.kt +++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment2.kt @@ -43,6 +43,7 @@ import androidx.annotation.DrawableRes import androidx.cardview.widget.CardView import androidx.core.widget.ContentLoadingProgressBar import androidx.fragment.app.DialogFragment +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.android.wallpaper.R @@ -72,6 +73,8 @@ import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDele import com.bumptech.glide.Glide import com.bumptech.glide.MemoryCategory import java.util.Date +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch /** Displays the Main UI for picking an individual wallpaper image. */ class IndividualPickerFragment2 : @@ -123,6 +126,7 @@ class IndividualPickerFragment2 : private var loading: ContentLoadingProgressBar? = null private var shouldReloadWallpapers = false private lateinit var categoryProvider: CategoryProvider + private var appliedWallpaperIds: Set<String> = setOf() /** * Staged error dialog fragments that were unable to be shown when the activity didn't allow @@ -216,7 +220,7 @@ class IndividualPickerFragment2 : isWallpapersReceived = true updateLoading() val byGroup = fetchedWallpapers.groupBy { it.getGroupName(context) } - val appliedWallpaperIds = getAppliedWallpaperIds() + appliedWallpaperIds = getAppliedWallpaperIds() byGroup.forEach { (groupName, wallpapers) -> if (!TextUtils.isEmpty(groupName)) { items.add( @@ -230,7 +234,7 @@ class IndividualPickerFragment2 : val currentWallpaper = WallpaperManager.getInstance(context).wallpaperInfo items.addAll( wallpapers.map { - var isApplied = + val isApplied = if (it is LiveWallpaperInfo) { it.isApplied(currentWallpaper) } else { @@ -449,6 +453,14 @@ class IndividualPickerFragment2 : imageGrid.layoutManager = gridLayoutManager } + private suspend fun fetchWallpapersIfNeeded() { + coroutineScope { + if (isWallpapersReceived && (shouldReloadWallpapers || isAppliedWallpaperChanged())) { + fetchWallpapers(true) + } + } + } + override fun onResume() { super.onResume() val preferences = InjectorProvider.getInjector().getPreferences(requireActivity()) @@ -466,9 +478,7 @@ class IndividualPickerFragment2 : parentFragmentManager, TAG_START_ROTATION_ERROR_DIALOG ) - if (isWallpapersReceived && shouldReloadWallpapers) { - fetchWallpapers(true) - } + lifecycleScope.launch { fetchWallpapersIfNeeded() } } stagedStartRotationErrorDialogFragment = null } @@ -650,6 +660,17 @@ class IndividualPickerFragment2 : return appliedWallpaperIds } + // TODO(b/277180178): Extract the check to another class for unit testing + private fun isAppliedWallpaperChanged(): Boolean { + // Reload wallpapers if the current wallpapers have changed + getAppliedWallpaperIds().let { + if (appliedWallpaperIds.isEmpty() || appliedWallpaperIds != it) { + return true + } + } + return false + } + sealed class PickerItem(val title: CharSequence = "") { class WallpaperItem(val wallpaperInfo: WallpaperInfo, val isApplied: Boolean) : PickerItem() |