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 
18 package com.android.systemui.keyguard.ui.binder
19 
20 import android.os.Trace
21 import android.util.Log
22 import androidx.constraintlayout.widget.ConstraintLayout
23 import androidx.constraintlayout.widget.ConstraintSet
24 import androidx.lifecycle.Lifecycle
25 import androidx.lifecycle.repeatOnLifecycle
26 import com.android.systemui.keyguard.ui.viewmodel.KeyguardBlueprintViewModel
27 import com.android.systemui.lifecycle.repeatWhenAttached
28 import kotlinx.coroutines.launch
29 
30 class KeyguardBlueprintViewBinder {
31     companion object {
32         private const val TAG = "KeyguardBlueprintViewBinder"
33 
34         fun bind(constraintLayout: ConstraintLayout, viewModel: KeyguardBlueprintViewModel) {
35             constraintLayout.repeatWhenAttached {
36                 repeatOnLifecycle(Lifecycle.State.CREATED) {
37                     launch {
38                         viewModel.blueprint.collect { blueprint ->
39                             Trace.beginSection("KeyguardBlueprintController#applyBlueprint")
40                             Log.d(TAG, "applying blueprint: $blueprint")
41                             ConstraintSet().apply {
42                                 clone(constraintLayout)
43                                 val emptyLayout = ConstraintSet.Layout()
44                                 knownIds.forEach { getConstraint(it).layout.copyFrom(emptyLayout) }
45                                 blueprint?.apply(this)
46                                 applyTo(constraintLayout)
47                             }
48                             Trace.endSection()
49                         }
50                     }
51                 }
52             }
53         }
54     }
55 }
56