• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 package com.android.wallpaper.picker.customization.ui.viewmodel
19 
20 import android.app.WallpaperColors
21 import android.graphics.Bitmap
22 import android.os.Bundle
23 import com.android.wallpaper.R
24 import com.android.wallpaper.model.WallpaperInfo
25 import com.android.wallpaper.module.CustomizationSections
26 import com.android.wallpaper.module.CustomizationSections.Screen
27 import com.android.wallpaper.picker.customization.domain.interactor.WallpaperInteractor
28 import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination
29 import com.android.wallpaper.picker.customization.shared.model.WallpaperModel
30 import com.android.wallpaper.util.PreviewUtils
31 import kotlinx.coroutines.flow.Flow
32 import kotlinx.coroutines.flow.filterNotNull
33 import kotlinx.coroutines.flow.first
34 import kotlinx.coroutines.flow.map
35 
36 /** Models the UI state for a preview of the home screen or lock screen. */
37 open class ScreenPreviewViewModel(
38     val previewUtils: PreviewUtils,
39     private val initialExtrasProvider: () -> Bundle? = { null },
40     private val wallpaperInfoProvider: suspend (forceReload: Boolean) -> WallpaperInfo?,
<lambda>null41     private val onWallpaperColorChanged: (WallpaperColors?) -> Unit = {},
42     private val wallpaperInteractor: WallpaperInteractor,
43     val screen: Screen,
44     val onPreviewClicked: (() -> Unit)? = null,
45 ) {
46 
47     val previewContentDescription: Int =
48         when (screen) {
49             Screen.HOME_SCREEN -> R.string.home_wallpaper_preview_card_content_description
50             Screen.LOCK_SCREEN -> R.string.lock_wallpaper_preview_card_content_description
51         }
52 
53     /** Returns whether wallpaper picker should handle reload */
shouldReloadWallpapernull54     fun shouldReloadWallpaper(): Flow<Boolean> {
55         // Setting the lock screen to the same wp as the home screen doesn't trigger a UI update,
56         // so fix that here for now
57         // TODO(b/281730113) Remove this once better solution is ready.
58         return wallpaperUpdateEvents().map { thisWallpaper ->
59             val otherWallpaper = wallpaperUpdateEvents(otherScreen()).first()
60             wallpaperInteractor.shouldHandleReload() ||
61                 thisWallpaper?.wallpaperId == otherWallpaper?.wallpaperId
62         }
63     }
64 
wallpaperThumbnailnull65     fun wallpaperThumbnail(): Flow<Bitmap?> {
66         return wallpaperUpdateEvents().filterNotNull().map { wallpaper ->
67             wallpaperInteractor.loadThumbnail(wallpaper.wallpaperId, wallpaper.lastUpdated)
68         }
69     }
70 
otherScreennull71     private fun otherScreen(): Screen {
72         return if (screen == Screen.LOCK_SCREEN) Screen.HOME_SCREEN else Screen.LOCK_SCREEN
73     }
74 
75     /** Returns a flow that is updated whenever the wallpaper has been updated */
wallpaperUpdateEventsnull76     private fun wallpaperUpdateEvents(
77         s: CustomizationSections.Screen = screen
78     ): Flow<WallpaperModel?> {
79         return wallpaperInteractor.wallpaperUpdateEvents(s)
80     }
81 
workspaceUpdateEventsnull82     open fun workspaceUpdateEvents(): Flow<Boolean>? = null
83 
84     fun getInitialExtras(): Bundle? {
85         return initialExtrasProvider.invoke()
86     }
87 
88     /**
89      * Returns the current wallpaper's WallpaperInfo
90      *
91      * @param forceReload if true, any cached values will be ignored and current wallpaper info will
92      *   be reloaded
93      */
getWallpaperInfonull94     suspend fun getWallpaperInfo(forceReload: Boolean = false): WallpaperInfo? {
95         return wallpaperInfoProvider.invoke(forceReload)
96     }
97 
onWallpaperColorsChangednull98     fun onWallpaperColorsChanged(colors: WallpaperColors?) {
99         onWallpaperColorChanged.invoke(colors)
100     }
101 
102     open val isLoading: Flow<Boolean> =
103         wallpaperInteractor.isSelectingWallpaper(
104             destination =
105                 if (screen == Screen.LOCK_SCREEN) {
106                     WallpaperDestination.LOCK
107                 } else {
108                     WallpaperDestination.HOME
109                 },
110         )
111 }
112