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.development.ui.viewmodel 18 19 import androidx.compose.runtime.getValue 20 import com.android.systemui.development.domain.interactor.BuildNumberInteractor 21 import com.android.systemui.development.shared.model.BuildNumber 22 import com.android.systemui.lifecycle.ExclusiveActivatable 23 import com.android.systemui.lifecycle.Hydrator 24 import dagger.assisted.AssistedFactory 25 import dagger.assisted.AssistedInject 26 import kotlinx.coroutines.awaitCancellation 27 import kotlinx.coroutines.channels.Channel 28 import kotlinx.coroutines.coroutineScope 29 import kotlinx.coroutines.flow.receiveAsFlow 30 import kotlinx.coroutines.launch 31 32 /** View model for UI that (optionally) shows the build number and copies it on long press. */ 33 class BuildNumberViewModel 34 @AssistedInject 35 constructor(private val buildNumberInteractor: BuildNumberInteractor) : ExclusiveActivatable() { 36 37 private val hydrator = Hydrator("BuildNumberViewModel") 38 39 private val copyRequests = Channel<Unit>() 40 41 val buildNumber: BuildNumber? by 42 hydrator.hydratedStateOf( 43 traceName = "buildNumber", 44 source = buildNumberInteractor.buildNumber, 45 ) 46 onBuildNumberLongPressnull47 fun onBuildNumberLongPress() { 48 copyRequests.trySend(Unit) 49 } 50 onActivatednull51 override suspend fun onActivated(): Nothing { 52 coroutineScope { 53 launch { hydrator.activate() } 54 launch { 55 copyRequests.receiveAsFlow().collect { buildNumberInteractor.copyBuildNumber() } 56 } 57 awaitCancellation() 58 } 59 } 60 61 @AssistedFactory 62 interface Factory { createnull63 fun create(): BuildNumberViewModel 64 } 65 } 66