/* * Copyright (C) 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.wallpaper.picker.preview.ui.binder import android.content.Context import android.graphics.Point import android.view.View import android.widget.Button import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.view.isVisible import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import androidx.transition.Transition import com.android.customization.picker.clock.shared.ClockSize import com.android.wallpaper.R import com.android.wallpaper.model.Screen import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination import com.android.wallpaper.picker.data.WallpaperModel import com.android.wallpaper.picker.preview.ui.view.ClickableMotionLayout import com.android.wallpaper.picker.preview.ui.viewmodel.FullPreviewConfigViewModel import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel.Companion.PreviewScreen import com.android.wallpaper.util.wallpaperconnection.WallpaperConnectionUtils import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.launch object SmallPreviewScreenBinder { fun bind( applicationContext: Context, mainScope: CoroutineScope, lifecycleOwner: LifecycleOwner, fragmentLayout: MotionLayout, viewModel: WallpaperPreviewViewModel, previewDisplaySize: Point, transition: Transition?, transitionConfig: FullPreviewConfigViewModel?, wallpaperConnectionUtils: WallpaperConnectionUtils, isFirstBindingDeferred: CompletableDeferred, isFoldable: Boolean, onPreviewReady: ((Screen) -> Unit)? = null, onStartTransition: (() -> Unit)? = null, onPreviewSurfaceDestroyed: ((Screen) -> Unit)? = null, navigate: (View) -> Unit, ): PreviewPagerBinder2.Binding { val previewPager = fragmentLayout.requireViewById(R.id.preview_pager) previewPager.jumpToState( if (viewModel.smallPreviewSelectedTab.value == Screen.LOCK_SCREEN) R.id.lock_preview_selected else R.id.home_preview_selected ) val previewPagerContainer = fragmentLayout.requireViewById(R.id.small_preview_container) val nextButton = fragmentLayout.requireViewById