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.volume.panel.ui.layout
18 
19 import com.android.systemui.volume.panel.dagger.scope.VolumePanelScope
20 import com.android.systemui.volume.panel.shared.model.VolumePanelComponentKey
21 import com.android.systemui.volume.panel.ui.BottomBar
22 import com.android.systemui.volume.panel.ui.FooterComponents
23 import com.android.systemui.volume.panel.ui.HeaderComponents
24 import com.android.systemui.volume.panel.ui.viewmodel.ComponentState
25 import com.android.systemui.volume.panel.ui.viewmodel.VolumePanelState
26 import javax.inject.Inject
27 
28 @VolumePanelScope
29 class DefaultComponentsLayoutManager
30 @Inject
31 constructor(
32     @BottomBar private val bottomBar: VolumePanelComponentKey,
33     @HeaderComponents
34     private val headerComponents: Collection<VolumePanelComponentKey> = emptyList(),
35     @FooterComponents
36     private val footerComponents: Collection<VolumePanelComponentKey> = emptyList(),
37 ) : ComponentsLayoutManager {
38 
layoutnull39     override fun layout(
40         volumePanelState: VolumePanelState,
41         components: Collection<ComponentState>
42     ): ComponentsLayout {
43         val contentComponents =
44             components.filter {
45                 !headerComponents.contains(it.key) &&
46                     !footerComponents.contains(it.key) &&
47                     it.key != bottomBar
48             }
49         val headerComponents =
50             components
51                 .filter { it.key in headerComponents }
52                 .sortedBy { headerComponents.indexOf(it.key) }
53         val footerComponents =
54             components
55                 .filter { it.key in footerComponents }
56                 .sortedBy { footerComponents.indexOf(it.key) }
57         return ComponentsLayout(
58             headerComponents = headerComponents,
59             contentComponents = contentComponents.sortedBy { it.key },
60             footerComponents = footerComponents,
61             bottomBarComponent = components.find { it.key == bottomBar }
62                     ?: error(
63                         "VolumePanelComponents.BOTTOM_BAR must be present in the default " +
64                             "components layout."
65                     )
66         )
67     }
68 }
69