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.Screen
25 import com.android.wallpaper.model.WallpaperInfo
26 import com.android.wallpaper.picker.customization.domain.interactor.WallpaperInteractor
27 import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination
28 import com.android.wallpaper.picker.customization.shared.model.WallpaperModel
29 import com.android.wallpaper.util.PreviewUtils
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.filterNotNull
32 import kotlinx.coroutines.flow.first
33 import kotlinx.coroutines.flow.map
34 
35 /** Models the UI state for a preview of the home screen or lock screen. */
36 open class ScreenPreviewViewModel(
37     val previewUtils: PreviewUtils,
38     private val initialExtrasProvider: () -> Bundle? = { null },
39     private val wallpaperInfoProvider: suspend (forceReload: Boolean) -> WallpaperInfo?,
<lambda>null40     private val onWallpaperColorChanged: (WallpaperColors?) -> Unit = {},
41     private val wallpaperInteractor: WallpaperInteractor,
42     val screen: Screen,
43 ) {
44 
45     val previewContentDescription: Int =
46         when (screen) {
47             Screen.HOME_SCREEN -> R.string.home_wallpaper_preview_card_content_description
48             Screen.LOCK_SCREEN -> R.string.lock_wallpaper_preview_card_content_description
49         }
50 
51     /** Returns whether wallpaper picker should handle reload */
shouldReloadWallpapernull52     fun shouldReloadWallpaper(): Flow<Boolean> {
53         // Setting the lock screen to the same wp as the home screen doesn't trigger a UI update,
54         // so fix that here for now
55         // TODO(b/281730113) Remove this once better solution is ready.
56         return wallpaperUpdateEvents().map { thisWallpaper ->
57             val otherWallpaper = wallpaperUpdateEvents(otherScreen()).first()
58             true
59         }
60     }
61 
wallpaperThumbnailnull62     fun wallpaperThumbnail(): Flow<Bitmap?> {
63         return wallpaperUpdateEvents().filterNotNull().map { wallpaper ->
64             wallpaperInteractor.loadThumbnail(
65                 wallpaper.wallpaperId,
66                 wallpaper.lastUpdated,
67                 getWallpaperDestination()
68             )
69         }
70     }
71 
otherScreennull72     private fun otherScreen(): Screen {
73         return if (screen == Screen.LOCK_SCREEN) Screen.HOME_SCREEN else Screen.LOCK_SCREEN
74     }
75 
76     /** Returns a flow that is updated whenever the wallpaper has been updated */
wallpaperUpdateEventsnull77     private fun wallpaperUpdateEvents(s: Screen = screen): Flow<WallpaperModel?> {
78         return wallpaperInteractor.wallpaperUpdateEvents(s)
79     }
80 
workspaceUpdateEventsnull81     open fun workspaceUpdateEvents(): Flow<Boolean>? = null
82 
83     fun getInitialExtras(): Bundle? {
84         return initialExtrasProvider.invoke()
85     }
86 
87     /**
88      * Returns the current wallpaper's WallpaperInfo
89      *
90      * @param forceReload if true, any cached values will be ignored and current wallpaper info will
91      *   be reloaded
92      */
getWallpaperInfonull93     suspend fun getWallpaperInfo(forceReload: Boolean = false): WallpaperInfo? {
94         return wallpaperInfoProvider.invoke(forceReload)
95     }
96 
onWallpaperColorsChangednull97     fun onWallpaperColorsChanged(colors: WallpaperColors?) {
98         onWallpaperColorChanged.invoke(colors)
99     }
100 
101     open val isLoading: Flow<Boolean> =
102         wallpaperInteractor.isSelectingWallpaper(
103             destination = getWallpaperDestination(),
104         )
105 
getWallpaperDestinationnull106     private fun getWallpaperDestination() =
107         if (screen == Screen.LOCK_SCREEN) {
108             WallpaperDestination.LOCK
109         } else {
110             WallpaperDestination.HOME
111         }
112 }
113