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.biometrics.ui.binder
19 
20 import androidx.lifecycle.Lifecycle
21 import androidx.lifecycle.repeatOnLifecycle
22 import com.airbnb.lottie.LottieAnimationView
23 import com.android.systemui.biometrics.ui.viewmodel.PromptFingerprintIconViewModel
24 import com.android.systemui.lifecycle.repeatWhenAttached
25 import kotlinx.coroutines.launch
26 
27 /** Sub-binder for [BiometricPromptLayout.iconView]. */
28 object PromptFingerprintIconViewBinder {
29 
30     /** Binds [BiometricPromptLayout.iconView] to [PromptFingerprintIconViewModel]. */
31     @JvmStatic
32     fun bind(view: LottieAnimationView, viewModel: PromptFingerprintIconViewModel) {
33         view.repeatWhenAttached {
34             repeatOnLifecycle(Lifecycle.State.STARTED) {
35                 viewModel.onConfigurationChanged(view.context.resources.configuration)
36                 launch {
37                     viewModel.iconAsset.collect { iconAsset ->
38                         if (iconAsset != -1) {
39                             view.setAnimation(iconAsset)
40                             // TODO: must replace call below once non-sfps asset logic and
41                             // shouldAnimateIconView logic is migrated to this ViewModel.
42                             view.playAnimation()
43                         }
44                     }
45                 }
46             }
47         }
48     }
49 }
50