1 /*
<lambda>null2  * 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.wallpaper.picker.customization.ui.binder
18 
19 import android.view.View
20 import androidx.constraintlayout.motion.widget.MotionLayout
21 import androidx.lifecycle.Lifecycle
22 import androidx.lifecycle.LifecycleOwner
23 import androidx.lifecycle.lifecycleScope
24 import androidx.lifecycle.repeatOnLifecycle
25 import androidx.viewpager2.widget.ViewPager2
26 import com.android.wallpaper.R
27 import com.android.wallpaper.model.Screen.HOME_SCREEN
28 import com.android.wallpaper.model.Screen.LOCK_SCREEN
29 import com.android.wallpaper.picker.customization.ui.CustomizationPickerActivity2
30 import com.android.wallpaper.picker.customization.ui.util.CustomizationOptionUtil.CustomizationOption
31 import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationPickerViewModel2
32 import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationPickerViewModel2.PickerScreen.CUSTOMIZATION_OPTION
33 import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationPickerViewModel2.PickerScreen.MAIN
34 import kotlinx.coroutines.launch
35 
36 object CustomizationPickerBinder2 {
37 
38     /**
39      * @return Callback for the [CustomizationPickerActivity2] to set
40      *   [CustomizationPickerViewModel2]'s screen state to null, which infers to the main screen. We
41      *   need this callback to handle the back navigation in [CustomizationPickerActivity2].
42      */
43     fun bind(
44         view: View,
45         lockScreenCustomizationOptionEntries: List<Pair<CustomizationOption, View>>,
46         homeScreenCustomizationOptionEntries: List<Pair<CustomizationOption, View>>,
47         viewModel: CustomizationPickerViewModel2,
48         customizationOptionsBinder: CustomizationOptionsBinder,
49         lifecycleOwner: LifecycleOwner,
50         navigateToPrimary: () -> Unit,
51         navigateToSecondary: (screen: CustomizationOption) -> Unit,
52     ): () -> Boolean {
53         val optionContainer =
54             view.requireViewById<MotionLayout>(R.id.customization_option_container)
55         val pager = view.requireViewById<ViewPager2>(R.id.preview_pager)
56         pager.registerOnPageChangeCallback(
57             object : ViewPager2.OnPageChangeCallback() {
58                 override fun onPageSelected(position: Int) {
59                     viewModel.selectPreviewScreen(if (position == 0) LOCK_SCREEN else HOME_SCREEN)
60                 }
61             }
62         )
63 
64         lifecycleOwner.lifecycleScope.launch {
65             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
66                 launch {
67                     viewModel.screen.collect { (screen, option) ->
68                         when (screen) {
69                             MAIN -> navigateToPrimary()
70                             CUSTOMIZATION_OPTION -> option?.let(navigateToSecondary)
71                         }
72                     }
73                 }
74 
75                 launch {
76                     viewModel.selectedPreviewScreen.collect {
77                         when (it) {
78                             LOCK_SCREEN -> {
79                                 pager.currentItem = 0
80                                 optionContainer.transitionToStart()
81                             }
82                             HOME_SCREEN -> {
83                                 pager.currentItem = 1
84                                 optionContainer.transitionToEnd()
85                             }
86                         }
87                     }
88                 }
89             }
90         }
91 
92         customizationOptionsBinder.bind(
93             view,
94             lockScreenCustomizationOptionEntries,
95             homeScreenCustomizationOptionEntries,
96             viewModel.customizationOptionsViewModel,
97             lifecycleOwner,
98         )
99         return { viewModel.onBackPressed() }
100     }
101 }
102