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.preview.ui.binder
18 
19 import android.content.Context
20 import android.graphics.Point
21 import android.view.View
22 import android.widget.Button
23 import androidx.constraintlayout.motion.widget.MotionLayout
24 import androidx.core.view.isVisible
25 import androidx.lifecycle.Lifecycle
26 import androidx.lifecycle.LifecycleOwner
27 import androidx.lifecycle.lifecycleScope
28 import androidx.lifecycle.repeatOnLifecycle
29 import androidx.transition.Transition
30 import com.android.wallpaper.R
31 import com.android.wallpaper.model.Screen
32 import com.android.wallpaper.picker.preview.ui.view.ClickableMotionLayout
33 import com.android.wallpaper.picker.preview.ui.viewmodel.FullPreviewConfigViewModel
34 import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel
35 import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel.Companion.PreviewScreen
36 import com.android.wallpaper.util.wallpaperconnection.WallpaperConnectionUtils
37 import kotlinx.coroutines.CompletableDeferred
38 import kotlinx.coroutines.CoroutineScope
39 import kotlinx.coroutines.flow.combine
40 import kotlinx.coroutines.launch
41 
42 object SmallPreviewScreenBinder {
43     fun bind(
44         applicationContext: Context,
45         mainScope: CoroutineScope,
46         lifecycleOwner: LifecycleOwner,
47         fragmentLayout: MotionLayout,
48         viewModel: WallpaperPreviewViewModel,
49         previewDisplaySize: Point,
50         transition: Transition?,
51         transitionConfig: FullPreviewConfigViewModel?,
52         wallpaperConnectionUtils: WallpaperConnectionUtils,
53         isFirstBindingDeferred: CompletableDeferred<Boolean>,
54         isFoldable: Boolean,
55         navigate: (View) -> Unit,
56     ) {
57         val previewPager = fragmentLayout.requireViewById<ClickableMotionLayout>(R.id.preview_pager)
58         val previewPagerContainer =
59             fragmentLayout.requireViewById<MotionLayout>(R.id.small_preview_container)
60         val nextButton = fragmentLayout.requireViewById<Button>(R.id.button_next)
61 
62         PreviewPagerBinder2.bind(
63             applicationContext,
64             mainScope,
65             lifecycleOwner,
66             previewPagerContainer,
67             viewModel,
68             previewDisplaySize,
69             transition,
70             transitionConfig,
71             wallpaperConnectionUtils,
72             isFirstBindingDeferred,
73             isFoldable,
74             navigate,
75         )
76 
77         lifecycleOwner.lifecycleScope.launch {
78             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
79                 launch {
80                     combine(
81                             viewModel.currentPreviewScreen,
82                             viewModel.smallPreviewSelectedTab,
83                             viewModel.previewActionsViewModel.isActionChecked,
84                         ) { screen, tab, actionChecked ->
85                             Triple(screen, tab, actionChecked)
86                         }
87                         .collect { (screen, tab, isActionChecked) ->
88                             when (screen) {
89                                 PreviewScreen.SMALL_PREVIEW -> {
90                                     fragmentLayout.transitionToState(R.id.show_full_page)
91                                     previewPagerContainer.transitionToState(
92                                         if (isActionChecked) R.id.floating_sheet_visible
93                                         else R.id.floating_sheet_gone
94                                     )
95                                     // TODO(b/367374790): Use jumpToState for shared element
96                                     //  transition back from PreviewScreen.FULL_PREVIEW, until full
97                                     //  preview fragment is removed.
98                                     previewPager.transitionToState(
99                                         if (tab == Screen.LOCK_SCREEN) R.id.lock_preview_selected
100                                         else R.id.home_preview_selected
101                                     )
102                                 }
103                                 PreviewScreen.FULL_PREVIEW -> {
104                                     // TODO(b/367374790): Transition to full preview
105                                 }
106                                 PreviewScreen.APPLY_WALLPAPER -> {
107                                     fragmentLayout.transitionToState(R.id.hide_page_header)
108                                     previewPagerContainer.transitionToState(
109                                         R.id.show_apply_wallpaper
110                                     )
111                                     previewPager.transitionToState(
112                                         if (isFoldable) R.id.apply_wallpaper_lock_preview_selected
113                                         else R.id.apply_wallpaper_preview_only
114                                     )
115                                 }
116                             }
117                         }
118                 }
119 
120                 launch {
121                     viewModel.shouldEnableClickOnPager.collect {
122                         previewPager.shouldInterceptTouch = it
123                     }
124                 }
125 
126                 launch {
127                     viewModel.isSetWallpaperButtonVisible.collect { nextButton.isVisible = it }
128                 }
129 
130                 launch {
131                     viewModel.isSetWallpaperButtonEnabled.collect { nextButton.isEnabled = it }
132                 }
133 
134                 launch {
135                     viewModel.onNextButtonClicked.collect { onClicked ->
136                         nextButton.setOnClickListener(
137                             if (onClicked != null) {
138                                 { onClicked() }
139                             } else {
140                                 null
141                             }
142                         )
143                     }
144                 }
145             }
146         }
147     }
148 }
149