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.touchpad.tutorial.ui.viewmodel
18
19 import android.view.MotionEvent
20 import androidx.annotation.RawRes
21 import com.android.systemui.inputdevice.tutorial.ui.composable.TutorialActionState
22 import com.android.systemui.touchpad.tutorial.ui.gesture.GestureState
23 import com.android.systemui.touchpad.tutorial.ui.gesture.GestureState.Finished
24 import com.android.systemui.touchpad.tutorial.ui.gesture.GestureState.InProgress
25 import com.android.systemui.touchpad.tutorial.ui.gesture.GestureState.NotStarted
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.flow
28
29 interface TouchpadTutorialScreenViewModel {
30 val tutorialState: Flow<TutorialActionState>
31
32 fun handleEvent(event: MotionEvent): Boolean
33 }
34
35 data class TutorialAnimationProperties(
36 val progressStartMarker: String,
37 val progressEndMarker: String,
38 @RawRes val successAnimation: Int,
39 )
40
mapToTutorialStatenull41 fun Flow<Pair<GestureState, TutorialAnimationProperties>>.mapToTutorialState():
42 Flow<TutorialActionState> {
43 return flow<TutorialActionState> {
44 var lastState: TutorialActionState = TutorialActionState.NotStarted
45 collect { (gestureState, animationProperties) ->
46 val newState = gestureState.toTutorialActionState(animationProperties, lastState)
47 lastState = newState
48 emit(newState)
49 }
50 }
51 }
52
toTutorialActionStatenull53 fun GestureState.toTutorialActionState(
54 properties: TutorialAnimationProperties,
55 previousState: TutorialActionState,
56 ): TutorialActionState {
57 return when (this) {
58 NotStarted -> TutorialActionState.NotStarted
59 is InProgress -> {
60 val inProgress =
61 TutorialActionState.InProgress(
62 progress = progress,
63 startMarker = properties.progressStartMarker,
64 endMarker = properties.progressEndMarker,
65 )
66 if (
67 previousState is TutorialActionState.InProgressAfterError ||
68 previousState is TutorialActionState.Error
69 ) {
70 TutorialActionState.InProgressAfterError(inProgress)
71 } else {
72 inProgress
73 }
74 }
75 is Finished -> TutorialActionState.Finished(properties.successAnimation)
76 GestureState.Error -> TutorialActionState.Error
77 }
78 }
79