1 /*
<lambda>null2  * Copyright (C) 2023 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.domain.interactor
19 
20 import android.graphics.Bitmap
21 import com.android.wallpaper.model.Screen
22 import com.android.wallpaper.module.logging.UserEventLogger.SetWallpaperEntryPoint
23 import com.android.wallpaper.picker.customization.data.repository.WallpaperRepository
24 import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination
25 import com.android.wallpaper.picker.customization.shared.model.WallpaperModel
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.StateFlow
28 import kotlinx.coroutines.flow.distinctUntilChanged
29 import kotlinx.coroutines.flow.map
30 
31 /** Handles business logic for wallpaper-related use-cases. */
32 class WallpaperInteractor(
33     private val repository: WallpaperRepository,
34     /** Returns whether wallpaper picker should handle reload */
35     val shouldHandleReload: () -> Boolean = { true },
36 ) {
37     val areRecentsAvailable: Boolean = repository.areRecentsAvailable
38     val maxOptions = repository.maxOptions
39 
40     /** Returns a flow that is updated whenever the wallpaper has been updated */
wallpaperUpdateEventsnull41     fun wallpaperUpdateEvents(screen: Screen): Flow<WallpaperModel?> {
42         return when (screen) {
43             Screen.LOCK_SCREEN ->
44                 previews(WallpaperDestination.LOCK, 1).map { recentWallpapers ->
45                     if (recentWallpapers.isEmpty()) null else recentWallpapers[0]
46                 }
47             Screen.HOME_SCREEN ->
48                 previews(WallpaperDestination.HOME, 1).map { recentWallpapers ->
49                     if (recentWallpapers.isEmpty()) null else recentWallpapers[0]
50                 }
51         }
52     }
53 
54     /** Returns the ID of the currently-selected wallpaper. */
selectedWallpaperIdnull55     fun selectedWallpaperId(
56         destination: WallpaperDestination,
57     ): StateFlow<String> {
58         return repository.selectedWallpaperId(destination = destination)
59     }
60 
61     /**
62      * Returns the ID of the wallpaper that is in the process of becoming the selected wallpaper or
63      * `null` if no such transaction is currently taking place.
64      */
selectingWallpaperIdnull65     fun selectingWallpaperId(
66         destination: WallpaperDestination,
67     ): Flow<String?> {
68         return repository.selectingWallpaperId.map { it[destination] }
69     }
70 
71     /** This is true when a wallpaper is selected but not yet set to the System. */
isSelectingWallpapernull72     fun isSelectingWallpaper(
73         destination: WallpaperDestination,
74     ): Flow<Boolean> {
75         return selectingWallpaperId(destination).distinctUntilChanged().map { it != null }
76     }
77 
78     /**
79      * Lists the [maxResults] most recent wallpapers.
80      *
81      * The first one is the most recent (current) wallpaper.
82      */
previewsnull83     fun previews(
84         destination: WallpaperDestination,
85         maxResults: Int,
86     ): Flow<List<WallpaperModel>> {
87         return repository
88             .recentWallpapers(
89                 destination = destination,
90                 limit = maxResults,
91             )
92             .map { previews ->
93                 if (previews.size > maxResults) {
94                     previews.subList(0, maxResults)
95                 } else {
96                     previews
97                 }
98             }
99     }
100 
101     /** Sets the wallpaper to the one with the given ID. */
setRecentWallpapernull102     suspend fun setRecentWallpaper(
103         @SetWallpaperEntryPoint setWallpaperEntryPoint: Int,
104         destination: WallpaperDestination,
105         wallpaperId: String,
106     ) {
107         repository.setRecentWallpaper(
108             setWallpaperEntryPoint = setWallpaperEntryPoint,
109             destination = destination,
110             wallpaperId = wallpaperId,
111         )
112     }
113 
114     /** Returns a thumbnail for the wallpaper with the given ID and destination. */
loadThumbnailnull115     suspend fun loadThumbnail(
116         wallpaperId: String,
117         lastUpdatedTimestamp: Long,
118         destination: WallpaperDestination
119     ): Bitmap? {
120         return repository.loadThumbnail(
121             wallpaperId = wallpaperId,
122             lastUpdatedTimestamp = lastUpdatedTimestamp,
123             destination = destination
124         )
125     }
126 }
127