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.domain.interactor
18 
19 import com.android.wallpaper.effects.Effect
20 import com.android.wallpaper.effects.EffectsController.EffectEnumInterface
21 import com.android.wallpaper.picker.data.WallpaperModel
22 import com.android.wallpaper.picker.preview.data.repository.CreativeEffectsRepository
23 import com.android.wallpaper.picker.preview.data.repository.ImageEffectsRepository
24 import com.android.wallpaper.picker.preview.data.repository.WallpaperPreviewRepository
25 import com.android.wallpaper.picker.preview.shared.model.LiveWallpaperDownloadResultModel
26 import com.android.wallpaper.widget.floatingsheetcontent.WallpaperEffectsView2
27 import dagger.hilt.android.scopes.ActivityRetainedScoped
28 import javax.inject.Inject
29 import kotlinx.coroutines.flow.Flow
30 import kotlinx.coroutines.flow.MutableStateFlow
31 import kotlinx.coroutines.flow.StateFlow
32 import kotlinx.coroutines.flow.asStateFlow
33 
34 /** This class handles the business logic for Preview screen's action buttons */
35 @ActivityRetainedScoped
36 class PreviewActionsInteractor
37 @Inject
38 constructor(
39     private val wallpaperPreviewRepository: WallpaperPreviewRepository,
40     private val imageEffectsRepository: ImageEffectsRepository,
41     private val creativeEffectsRepository: CreativeEffectsRepository,
42 ) {
43     val wallpaperModel: StateFlow<WallpaperModel?> = wallpaperPreviewRepository.wallpaperModel
44 
45     private val _isDownloadingWallpaper = MutableStateFlow<Boolean>(false)
46     val isDownloadingWallpaper: Flow<Boolean> = _isDownloadingWallpaper.asStateFlow()
47 
48     val imageEffectsModel = imageEffectsRepository.imageEffectsModel
49     val imageEffect = imageEffectsRepository.wallpaperEffect
50     val creativeEffectsModel = creativeEffectsRepository.creativeEffectsModel
51 
turnOnCreativeEffectnull52     suspend fun turnOnCreativeEffect(actionPosition: Int) {
53         creativeEffectsRepository.turnOnCreativeEffect(actionPosition)
54     }
55 
enableImageEffectnull56     fun enableImageEffect(effect: EffectEnumInterface) {
57         imageEffectsRepository.enableImageEffect(effect)
58     }
59 
disableImageEffectnull60     fun disableImageEffect() {
61         imageEffectsRepository.disableImageEffect()
62     }
63 
isTargetEffectnull64     fun isTargetEffect(effect: EffectEnumInterface): Boolean {
65         return imageEffectsRepository.isTargetEffect(effect)
66     }
67 
getEffectTextResnull68     fun getEffectTextRes(): WallpaperEffectsView2.EffectTextRes {
69         return imageEffectsRepository.getEffectTextRes()
70     }
71 
downloadWallpapernull72     suspend fun downloadWallpaper(): LiveWallpaperDownloadResultModel? {
73         _isDownloadingWallpaper.value = true
74         val wallpaperModel = wallpaperPreviewRepository.downloadWallpaper()
75         _isDownloadingWallpaper.value = false
76         return wallpaperModel
77     }
78 
cancelDownloadWallpapernull79     fun cancelDownloadWallpaper(): Boolean = wallpaperPreviewRepository.cancelDownloadWallpaper()
80 
81     fun startEffectsModelDownload(effect: Effect) {
82         imageEffectsRepository.startEffectsModelDownload(effect)
83     }
84 
interruptEffectsModelDownloadnull85     fun interruptEffectsModelDownload(effect: Effect) {
86         imageEffectsRepository.interruptEffectsModelDownload(effect)
87     }
88 }
89