1 /* 2 * 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 com.android.systemui.touchpad.tutorial.ui.gesture.GestureState 21 import com.android.systemui.touchpad.tutorial.ui.gesture.handleTouchpadMotionEvent 22 import java.util.function.Consumer 23 import kotlinx.coroutines.channels.Channel 24 import kotlinx.coroutines.flow.filter 25 import kotlinx.coroutines.flow.map 26 import kotlinx.coroutines.flow.merge 27 import kotlinx.coroutines.flow.onStart 28 import kotlinx.coroutines.flow.receiveAsFlow 29 30 class EasterEggGestureViewModel(val gestureRecognizer: GestureRecognizerAdapter) : 31 Consumer<MotionEvent> { 32 <lambda>null33 private val gestureDone = gestureRecognizer.gestureState.filter { it == GestureState.Finished } 34 35 private val easterEggFinished = Channel<Unit>() 36 37 val easterEggTriggered = 38 merge( <lambda>null39 gestureDone.map { Event.GestureFinished }, <lambda>null40 easterEggFinished.receiveAsFlow().map { Event.StateRestarted }, 41 ) <lambda>null42 .map { 43 when (it) { 44 Event.GestureFinished -> true 45 Event.StateRestarted -> false 46 } 47 } <lambda>null48 .onStart { emit(false) } 49 acceptnull50 override fun accept(event: MotionEvent) { 51 gestureRecognizer.handleTouchpadMotionEvent(event) 52 } 53 onEasterEggFinishednull54 fun onEasterEggFinished() { 55 easterEggFinished.trySend(Unit) 56 } 57 58 private sealed interface Event { 59 data object GestureFinished : Event 60 61 data object StateRestarted : Event 62 } 63 } 64