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.systemui.keyguard.ui.viewmodel
18 
19 import com.android.app.animation.Interpolators.EMPHASIZED
20 import com.android.systemui.common.ui.domain.interactor.ConfigurationInteractor
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.keyguard.shared.model.Edge
23 import com.android.systemui.keyguard.shared.model.KeyguardState.DREAMING
24 import com.android.systemui.keyguard.shared.model.KeyguardState.GLANCEABLE_HUB
25 import com.android.systemui.keyguard.ui.KeyguardTransitionAnimationFlow
26 import com.android.systemui.keyguard.ui.transitions.DeviceEntryIconTransition
27 import com.android.systemui.res.R
28 import com.android.systemui.scene.shared.model.Scenes
29 import javax.inject.Inject
30 import kotlin.time.Duration.Companion.milliseconds
31 import kotlin.time.Duration.Companion.seconds
32 import kotlinx.coroutines.ExperimentalCoroutinesApi
33 import kotlinx.coroutines.flow.Flow
34 import kotlinx.coroutines.flow.flatMapLatest
35 import kotlinx.coroutines.flow.map
36 
37 @OptIn(ExperimentalCoroutinesApi::class)
38 @SysUISingleton
39 class DreamingToGlanceableHubTransitionViewModel
40 @Inject
41 constructor(
42     animationFlow: KeyguardTransitionAnimationFlow,
43     configurationInteractor: ConfigurationInteractor,
44 ) : DeviceEntryIconTransition {
45     private val transitionAnimation =
46         animationFlow
47             .setup(
48                 duration = TO_GLANCEABLE_HUB_DURATION,
49                 edge = Edge.create(from = DREAMING, to = Scenes.Communal),
50             )
51             .setupWithoutSceneContainer(
52                 edge = Edge.create(from = DREAMING, to = GLANCEABLE_HUB),
53             )
54 
55     val dreamOverlayTranslationX: Flow<Float> =
56         configurationInteractor
57             .dimensionPixelSize(R.dimen.dreaming_to_hub_transition_dream_overlay_translation_x)
58             .flatMapLatest { translatePx ->
59                 transitionAnimation.sharedFlow(
60                     duration = TO_GLANCEABLE_HUB_DURATION,
61                     onStep = { value -> value * translatePx },
62                     interpolator = EMPHASIZED,
63                     onCancel = { 0f },
64                     name = "DREAMING->GLANCEABLE_HUB: overlayTranslationX",
65                 )
66             }
67 
68     // Keep the dream visible while the hub swipes in over the dream.
69     val dreamAlpha: Flow<Float> = transitionAnimation.immediatelyTransitionTo(1f)
70 
71     val dreamOverlayAlpha: Flow<Float> =
72         transitionAnimation.sharedFlow(
73             duration = 167.milliseconds,
74             onStep = { 1f - it },
75             name = "DREAMING->GLANCEABLE_HUB: dreamOverlayAlpha",
76         )
77 
78     // Show UMO once the transition starts.
79     val showUmo: Flow<Boolean> =
80         transitionAnimation
81             .sharedFlow(
82                 duration = TO_GLANCEABLE_HUB_DURATION,
83                 onStep = { it },
84                 onCancel = { 0f },
85                 onFinish = { 1f },
86             )
87             .map { step -> step != 0f }
88 
89     override val deviceEntryParentViewAlpha: Flow<Float> =
90         transitionAnimation.sharedFlow(
91             startTime = 167.milliseconds,
92             duration = 167.milliseconds,
93             onStep = { it },
94             onCancel = { 0f },
95             onFinish = { 1f },
96         )
97 
98     private companion object {
99         val TO_GLANCEABLE_HUB_DURATION = 1.seconds
100     }
101 }
102