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 package com.android.systemui.keyguard.ui.viewmodel
18 
19 import android.content.Context
20 import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor
21 import com.android.systemui.keyguard.shared.model.ClockSizeSetting
22 import com.android.systemui.res.R
23 import javax.inject.Inject
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.StateFlow
26 import kotlinx.coroutines.flow.combine
27 import kotlinx.coroutines.flow.map
28 
29 /** View model for the smartspace. */
30 class KeyguardPreviewSmartspaceViewModel
31 @Inject
32 constructor(
33     interactor: KeyguardClockInteractor,
34     val smartspaceViewModel: KeyguardSmartspaceViewModel,
35     val clockViewModel: KeyguardClockViewModel,
36 ) {
37 
38     val selectedClockSize: StateFlow<ClockSizeSetting> = interactor.selectedClockSize
39 
40     val shouldHideSmartspace: Flow<Boolean> =
41         combine(
42                 interactor.selectedClockSize,
43                 interactor.currentClockId,
44                 ::Pair,
45             )
46             .map { (size, currentClockId) ->
47                 when (size) {
48                     // TODO (b/284122375) This is temporary. We should use clockController
49                     //      .largeClock.config.hasCustomWeatherDataDisplay instead, but
50                     //      ClockRegistry.createCurrentClock is not reliable.
51                     ClockSizeSetting.DYNAMIC -> currentClockId == "DIGITAL_CLOCK_WEATHER"
52                     ClockSizeSetting.SMALL -> false
53                 }
54             }
55 
56     fun getSmartspaceStartPadding(context: Context): Int {
57         return KeyguardSmartspaceViewModel.getSmartspaceStartMargin(context)
58     }
59 
60     fun getSmartspaceEndPadding(context: Context): Int {
61         return KeyguardSmartspaceViewModel.getSmartspaceEndMargin(context)
62     }
63 
64     fun getSmallClockSmartspaceTopPadding(splitShadePreview: Boolean, context: Context): Int {
65         return getSmallClockTopPadding(splitShadePreview, context) +
66             context.resources.getDimensionPixelSize(
67                 com.android.systemui.customization.R.dimen.small_clock_height
68             )
69     }
70 
71     fun getLargeClockSmartspaceTopPadding(splitShadePreview: Boolean, context: Context): Int {
72         return getSmallClockTopPadding(splitShadePreview, context)
73     }
74 
75     /*
76      * SmallClockTopPadding decides the top position of smartspace
77      */
78     private fun getSmallClockTopPadding(splitShadePreview: Boolean, context: Context): Int {
79         return with(context.resources) {
80             if (splitShadePreview) {
81                 getDimensionPixelSize(R.dimen.keyguard_split_shade_top_margin)
82             } else {
83                 getDimensionPixelSize(R.dimen.keyguard_clock_top_margin) +
84                     getDimensionPixelSize(R.dimen.status_bar_header_height_keyguard) +
85                     getDimensionPixelSize(R.dimen.keyguard_smartspace_top_offset)
86             }
87         }
88     }
89 }
90