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