1 /*
<lambda>null2  * Copyright (C) 2024 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 package com.android.wallpaper.picker.preview.data.repository
18 
19 import com.android.wallpaper.picker.data.WallpaperModel.LiveWallpaperModel
20 import com.android.wallpaper.picker.preview.data.util.LiveWallpaperDownloader
21 import com.android.wallpaper.picker.preview.shared.model.DownloadStatus.DOWNLOADED
22 import com.android.wallpaper.picker.preview.shared.model.DownloadStatus.DOWNLOADING
23 import com.android.wallpaper.picker.preview.shared.model.DownloadStatus.DOWNLOAD_NOT_AVAILABLE
24 import com.android.wallpaper.picker.preview.shared.model.DownloadStatus.READY_TO_DOWNLOAD
25 import com.android.wallpaper.picker.preview.shared.model.DownloadableWallpaperModel
26 import dagger.hilt.android.scopes.ActivityRetainedScoped
27 import javax.inject.Inject
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.MutableStateFlow
30 import kotlinx.coroutines.flow.asStateFlow
31 import kotlinx.coroutines.flow.combine
32 
33 @ActivityRetainedScoped
34 class DownloadableWallpaperRepository
35 @Inject
36 constructor(
37     private val liveWallpaperDownloader: LiveWallpaperDownloader,
38 ) {
39 
40     private val _downloadableWallpaperModel =
41         MutableStateFlow(DownloadableWallpaperModel(READY_TO_DOWNLOAD, null))
42     val downloadableWallpaperModel: Flow<DownloadableWallpaperModel> =
43         combine(
44             _downloadableWallpaperModel.asStateFlow(),
45             liveWallpaperDownloader.isDownloaderReady
46         ) { model, isReady ->
47             if (isReady) {
48                 model
49             } else {
50                 DownloadableWallpaperModel(DOWNLOAD_NOT_AVAILABLE, null)
51             }
52         }
53 
54     fun downloadWallpaper(onDownloaded: (wallpaperModel: LiveWallpaperModel) -> Unit) {
55         _downloadableWallpaperModel.value = DownloadableWallpaperModel(DOWNLOADING, null)
56         liveWallpaperDownloader.downloadWallpaper(
57             object : LiveWallpaperDownloader.LiveWallpaperDownloadListener {
58                 override fun onDownloadSuccess(wallpaperModel: LiveWallpaperModel) {
59                     onDownloaded(wallpaperModel)
60                     _downloadableWallpaperModel.value =
61                         DownloadableWallpaperModel(DOWNLOADED, wallpaperModel)
62                 }
63 
64                 override fun onDownloadFailed() {
65                     _downloadableWallpaperModel.value =
66                         DownloadableWallpaperModel(READY_TO_DOWNLOAD, null)
67                 }
68             }
69         )
70     }
71 
72     fun cancelDownloadWallpaper(): Boolean {
73         return liveWallpaperDownloader.cancelDownloadWallpaper()
74     }
75 }
76