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.qs.panels.ui.viewmodel 18 19 import androidx.compose.runtime.derivedStateOf 20 import androidx.compose.runtime.getValue 21 import com.android.systemui.lifecycle.ExclusiveActivatable 22 import com.android.systemui.lifecycle.Hydrator 23 import com.android.systemui.media.controls.ui.controller.MediaLocation 24 import com.android.systemui.qs.panels.domain.interactor.QSColumnsInteractor 25 import dagger.assisted.Assisted 26 import dagger.assisted.AssistedFactory 27 import dagger.assisted.AssistedInject 28 import kotlinx.coroutines.awaitCancellation 29 import kotlinx.coroutines.coroutineScope 30 import kotlinx.coroutines.launch 31 32 /** 33 * View model for the number of columns that should be shown in a QS grid. 34 * * Create it with a [MediaLocation] to halve the number of columns when media should show in a row 35 * with the tiles. 36 * * Create it with a `null` [MediaLocation] to ignore media visibility (useful for edit mode). 37 */ 38 class QSColumnsViewModel 39 @AssistedInject 40 constructor( 41 interactor: QSColumnsInteractor, 42 mediaInRowInLandscapeViewModelFactory: MediaInRowInLandscapeViewModel.Factory, 43 @Assisted @MediaLocation mediaLocation: Int?, 44 ) : ExclusiveActivatable() { 45 46 private val hydrator = Hydrator("QSColumnsViewModelWithMedia") 47 <lambda>null48 val columns by derivedStateOf { 49 if (mediaInRowInLandscapeViewModel?.shouldMediaShowInRow == true) { 50 columnsWithoutMedia / 2 51 } else { 52 columnsWithoutMedia 53 } 54 } 55 56 private val mediaInRowInLandscapeViewModel = <lambda>null57 mediaLocation?.let { mediaInRowInLandscapeViewModelFactory.create(it) } 58 59 private val columnsWithoutMedia by 60 hydrator.hydratedStateOf(traceName = "columnsWithoutMedia", source = interactor.columns) 61 onActivatednull62 override suspend fun onActivated(): Nothing { 63 coroutineScope { 64 launch { hydrator.activate() } 65 launch { mediaInRowInLandscapeViewModel?.activate() } 66 awaitCancellation() 67 } 68 } 69 70 @AssistedFactory 71 interface Factory { createnull72 fun create(mediaLocation: Int?): QSColumnsViewModel 73 74 fun createWithoutMediaTracking() = create(null) 75 } 76 } 77