1 /*
2  * Copyright (C) 2022 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.dagger.SysUISingleton
21 import com.android.systemui.keyguard.domain.interactor.FromDreamingTransitionInteractor
22 import com.android.systemui.keyguard.domain.interactor.FromDreamingTransitionInteractor.Companion.TO_LOCKSCREEN_DURATION
23 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
24 import com.android.systemui.keyguard.shared.model.TransitionState
25 import com.android.systemui.keyguard.ui.KeyguardTransitionAnimationFlow
26 import javax.inject.Inject
27 import kotlin.time.Duration.Companion.milliseconds
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.filter
30 
31 /**
32  * Breaks down DREAMING->LOCKSCREEN transition into discrete steps for corresponding views to
33  * consume.
34  */
35 @SysUISingleton
36 class DreamingToLockscreenTransitionViewModel
37 @Inject
38 constructor(
39     keyguardTransitionInteractor: KeyguardTransitionInteractor,
40     private val fromDreamingTransitionInteractor: FromDreamingTransitionInteractor
41 ) {
42     fun startTransition() = fromDreamingTransitionInteractor.startToLockscreenTransition()
43 
44     private val transitionAnimation =
45         KeyguardTransitionAnimationFlow(
46             transitionDuration = TO_LOCKSCREEN_DURATION,
47             transitionFlow = keyguardTransitionInteractor.dreamingToLockscreenTransition,
48         )
49 
50     val transitionEnded =
51         keyguardTransitionInteractor.fromDreamingTransition.filter { step ->
52             step.transitionState == TransitionState.FINISHED ||
53                 step.transitionState == TransitionState.CANCELED
54         }
55 
56     /** Dream overlay y-translation on exit */
57     fun dreamOverlayTranslationY(translatePx: Int): Flow<Float> {
58         return transitionAnimation.createFlow(
59             duration = TO_LOCKSCREEN_DURATION,
60             onStep = { it * translatePx },
61             interpolator = EMPHASIZED,
62         )
63     }
64 
65     /** Dream overlay views alpha - fade out */
66     val dreamOverlayAlpha: Flow<Float> =
67         transitionAnimation.createFlow(
68             duration = 250.milliseconds,
69             onStep = { 1f - it },
70         )
71 
72     /** Lockscreen views y-translation */
73     fun lockscreenTranslationY(translatePx: Int): Flow<Float> {
74         return transitionAnimation.createFlow(
75             duration = TO_LOCKSCREEN_DURATION,
76             onStep = { value -> -translatePx + value * translatePx },
77             // Reset on cancel or finish
78             onFinish = { 0f },
79             onCancel = { 0f },
80             interpolator = EMPHASIZED,
81         )
82     }
83 
84     /** Lockscreen views alpha */
85     val lockscreenAlpha: Flow<Float> =
86         transitionAnimation.createFlow(
87             startTime = 233.milliseconds,
88             duration = 250.milliseconds,
89             onStep = { it },
90         )
91 }
92