1 /*
2  * Copyright (C) 2023 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.keyguard.shared.model
18 
19 import androidx.constraintlayout.widget.ConstraintLayout
20 import androidx.constraintlayout.widget.ConstraintSet
21 
22 /** Determines the constraints for the ConstraintSet in the lockscreen root view. */
23 interface KeyguardBlueprint {
24     val id: String
25     val sections: List<KeyguardSection>
26 
27     /**
28      * Removes views of old blueprint and add views of new blueprint.
29      *
30      * Finds sections that no longer exists in the next blueprint and removes those views. Finds
31      * sections that did not exist in the previous blueprint and add the corresponding views.
32      *
33      * @param previousBlueprint: KeyguardBlueprint the blueprint we are transitioning from.
34      * @param constraintLayout: The parent view.
35      * @param bindData: Whether to bind the data or not.
36      */
replaceViewsnull37     fun replaceViews(
38         constraintLayout: ConstraintLayout,
39         previousBlueprint: KeyguardBlueprint? = null,
40         rebuildSections: List<KeyguardSection> = listOf(),
41         bindData: Boolean = true
42     ) {
43         rebuildSections.forEach { it.onRebuildBegin() }
44         val prevSections = previousBlueprint?.sections ?: listOf()
45         val skipSections = sections.intersect(prevSections).subtract(rebuildSections)
46         prevSections.subtract(skipSections).forEach { it.removeViews(constraintLayout) }
47         sections.subtract(skipSections).forEach {
48             it.addViews(constraintLayout)
49             if (bindData) {
50                 it.bindData(constraintLayout)
51             }
52         }
53         rebuildSections.forEach { it.onRebuildEnd() }
54     }
55 
56     /** Rebuilds views for the target sections, or all of them if unspecified. */
rebuildViewsnull57     fun rebuildViews(
58         constraintLayout: ConstraintLayout,
59         rebuildSections: List<KeyguardSection> = sections,
60         bindData: Boolean = true
61     ) {
62         if (rebuildSections.isEmpty()) {
63             return
64         }
65 
66         rebuildSections.forEach { it.onRebuildBegin() }
67         rebuildSections.forEach { it.removeViews(constraintLayout) }
68         rebuildSections.forEach {
69             it.addViews(constraintLayout)
70             if (bindData) {
71                 it.bindData(constraintLayout)
72             }
73         }
74         rebuildSections.forEach { it.onRebuildEnd() }
75     }
76 
applyConstraintsnull77     fun applyConstraints(constraintSet: ConstraintSet) {
78         sections.forEach { it.applyConstraints(constraintSet) }
79     }
80 }
81