1 /* 2 * Copyright 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 package com.android.wallpaper.picker.preview.data.repository 18 19 import com.android.wallpaper.module.WallpaperPreferences 20 import com.android.wallpaper.picker.data.WallpaperModel 21 import com.android.wallpaper.picker.di.modules.BackgroundDispatcher 22 import com.android.wallpaper.picker.preview.data.util.LiveWallpaperDownloader 23 import com.android.wallpaper.picker.preview.shared.model.LiveWallpaperDownloadResultCode.SUCCESS 24 import com.android.wallpaper.picker.preview.shared.model.LiveWallpaperDownloadResultModel 25 import dagger.hilt.android.scopes.ActivityRetainedScoped 26 import javax.inject.Inject 27 import kotlinx.coroutines.CoroutineDispatcher 28 import kotlinx.coroutines.flow.MutableStateFlow 29 import kotlinx.coroutines.flow.StateFlow 30 import kotlinx.coroutines.flow.asStateFlow 31 import kotlinx.coroutines.withContext 32 33 /** This repository class manages the [WallpaperModel] for the preview screen */ 34 @ActivityRetainedScoped 35 class WallpaperPreviewRepository 36 @Inject 37 constructor( 38 private val liveWallpaperDownloader: LiveWallpaperDownloader, 39 private val preferences: WallpaperPreferences, 40 @BackgroundDispatcher private val bgDispatcher: CoroutineDispatcher, 41 ) { 42 /** This [WallpaperModel] represents the current selected wallpaper */ 43 private val _wallpaperModel = MutableStateFlow<WallpaperModel?>(null) 44 val wallpaperModel: StateFlow<WallpaperModel?> = _wallpaperModel.asStateFlow() 45 setWallpaperModelnull46 fun setWallpaperModel(wallpaperModel: WallpaperModel) { 47 _wallpaperModel.value = wallpaperModel 48 } 49 50 private val _hasSmallPreviewTooltipBeenShown: MutableStateFlow<Boolean> = 51 MutableStateFlow(preferences.getHasFullPreviewTooltipBeenShown()) 52 val hasSmallPreviewTooltipBeenShown: StateFlow<Boolean> = 53 _hasSmallPreviewTooltipBeenShown.asStateFlow() 54 hideSmallPreviewTooltipnull55 fun hideSmallPreviewTooltip() { 56 _hasSmallPreviewTooltipBeenShown.value = true 57 preferences.setHasSmallPreviewTooltipBeenShown(true) 58 } 59 60 private val _hasFullPreviewTooltipBeenShown: MutableStateFlow<Boolean> = 61 MutableStateFlow(preferences.getHasFullPreviewTooltipBeenShown()) 62 val hasFullPreviewTooltipBeenShown: StateFlow<Boolean> = 63 _hasFullPreviewTooltipBeenShown.asStateFlow() 64 hideFullPreviewTooltipnull65 fun hideFullPreviewTooltip() { 66 _hasFullPreviewTooltipBeenShown.value = true 67 preferences.setHasFullPreviewTooltipBeenShown(true) 68 } 69 downloadWallpapernull70 suspend fun downloadWallpaper(): LiveWallpaperDownloadResultModel? = 71 withContext(bgDispatcher) { 72 val result = liveWallpaperDownloader.downloadWallpaper() 73 if (result?.code == SUCCESS && result.wallpaperModel != null) { 74 // If download success, update repo's WallpaperModel to render the live wallpaper. 75 _wallpaperModel.value = result.wallpaperModel 76 result 77 } else { 78 result 79 } 80 } 81 cancelDownloadWallpapernull82 fun cancelDownloadWallpaper(): Boolean = liveWallpaperDownloader.cancelDownloadWallpaper() 83 } 84