• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.customization.ui.viewmodel
18 
19 import androidx.lifecycle.ViewModel
20 import androidx.lifecycle.viewModelScope
21 import com.android.wallpaper.model.Screen
22 import com.android.wallpaper.model.Screen.HOME_SCREEN
23 import com.android.wallpaper.model.Screen.LOCK_SCREEN
24 import com.android.wallpaper.picker.common.preview.ui.viewmodel.BasePreviewViewModel
25 import dagger.hilt.android.lifecycle.HiltViewModel
26 import javax.inject.Inject
27 import kotlinx.coroutines.flow.Flow
28 import kotlinx.coroutines.flow.MutableStateFlow
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.asStateFlow
31 import kotlinx.coroutines.flow.combine
32 import kotlinx.coroutines.flow.distinctUntilChanged
33 import kotlinx.coroutines.flow.map
34 import kotlinx.coroutines.flow.shareIn
35 
36 @HiltViewModel
37 class CustomizationPickerViewModel2
38 @Inject
39 constructor(
40     customizationOptionsViewModelFactory: CustomizationOptionsViewModelFactory,
41     basePreviewViewModelFactory: BasePreviewViewModel.Factory,
42 ) : ViewModel() {
43 
44     val customizationOptionsViewModel =
45         customizationOptionsViewModelFactory.create(viewModelScope = viewModelScope)
46     val basePreviewViewModel = basePreviewViewModelFactory.create(viewModelScope)
47 
48     enum class PickerScreen {
49         MAIN,
50         CUSTOMIZATION_OPTION,
51     }
52 
53     private val _selectedPreviewScreen = MutableStateFlow(LOCK_SCREEN)
54     val selectedPreviewScreen = _selectedPreviewScreen.asStateFlow()
55 
56     fun selectPreviewScreen(screen: Screen) {
57         _selectedPreviewScreen.value = screen
58     }
59 
60     val screen =
61         customizationOptionsViewModel.selectedOption.map {
62             if (it != null) {
63                 Pair(PickerScreen.CUSTOMIZATION_OPTION, it)
64             } else {
65                 Pair(PickerScreen.MAIN, null)
66             }
67         }
68 
69     /** Flow of float that emits to trigger the lock screen preview to animate to an alpha value. */
70     val lockPreviewAnimateToAlpha: Flow<Float> =
71         combine(screen, selectedPreviewScreen, ::Pair)
72             .map { (navigationScreen, previewScreen) ->
73                 when (navigationScreen.first) {
74                     PickerScreen.MAIN ->
75                         if (previewScreen == LOCK_SCREEN) PREVIEW_SHOW_ALPHA else PREVIEW_FADE_ALPHA
76                     PickerScreen.CUSTOMIZATION_OPTION -> {
77                         when (previewScreen) {
78                             LOCK_SCREEN -> PREVIEW_SHOW_ALPHA
79                             HOME_SCREEN -> PREVIEW_HIDE_ALPHA
80                         }
81                     }
82                 }
83             }
84             .distinctUntilChanged()
85             .shareIn(viewModelScope, SharingStarted.WhileSubscribed(), 1)
86 
87     /** Flow of float that emits to trigger the home screen preview to animate to an alpha value. */
88     val homePreviewAnimateToAlpha: Flow<Float> =
89         combine(screen, selectedPreviewScreen, ::Pair)
90             .map { (navigationScreen, previewScreen) ->
91                 when (navigationScreen.first) {
92                     PickerScreen.MAIN ->
93                         if (previewScreen == HOME_SCREEN) PREVIEW_SHOW_ALPHA else PREVIEW_FADE_ALPHA
94                     PickerScreen.CUSTOMIZATION_OPTION -> {
95                         when (previewScreen) {
96                             LOCK_SCREEN -> PREVIEW_HIDE_ALPHA
97                             HOME_SCREEN -> PREVIEW_SHOW_ALPHA
98                         }
99                     }
100                 }
101             }
102             .distinctUntilChanged()
103             .shareIn(viewModelScope, SharingStarted.WhileSubscribed(), 1)
104 
105     val isPreviewClickable: Flow<Boolean> = basePreviewViewModel.wallpapers.map { it != null }
106 
107     val isPagerInteractable: Flow<Boolean> =
108         customizationOptionsViewModel.selectedOption.map { it == null }
109 
110     companion object {
111         const val PREVIEW_SHOW_ALPHA = 1F
112         const val PREVIEW_HIDE_ALPHA = 0F
113         const val PREVIEW_FADE_ALPHA = 0.4F
114     }
115 }
116