1 /* <lambda>null2 * Copyright (C) 2022 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.wallpaper.picker.customization.ui.binder 19 20 import android.view.View 21 import androidx.lifecycle.Lifecycle 22 import androidx.lifecycle.LifecycleOwner 23 import androidx.lifecycle.lifecycleScope 24 import androidx.lifecycle.repeatOnLifecycle 25 import com.android.wallpaper.R 26 import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationPickerViewModel 27 import com.android.wallpaper.widget.DuoTabs 28 import kotlinx.coroutines.flow.combine 29 import kotlinx.coroutines.launch 30 31 /** Binds view to view-model for the customization picker tabs. */ 32 object CustomizationPickerTabsBinder { 33 @JvmStatic 34 fun bind( 35 view: View, 36 viewModel: CustomizationPickerViewModel, 37 lifecycleOwner: LifecycleOwner, 38 ) { 39 val tabs: DuoTabs = view.requireViewById(R.id.duo_tabs) 40 tabs.setTabText( 41 view.context.getString(R.string.lock_screen_tab), 42 view.context.getString(R.string.home_screen_tab), 43 ) 44 45 lifecycleOwner.lifecycleScope.launch { 46 lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 47 launch { 48 combine(viewModel.lockScreenTab, viewModel.homeScreenTab) { 49 lockScreenTabViewModel, 50 homeScreenTabViewModel -> 51 lockScreenTabViewModel to homeScreenTabViewModel 52 } 53 .collect { (lockScreenTabViewModel, homeScreenTabViewModel) -> 54 tabs.setOnTabSelectedListener(null) 55 tabs.selectTab( 56 when { 57 lockScreenTabViewModel.isSelected -> DuoTabs.TAB_PRIMARY 58 else -> DuoTabs.TAB_SECONDARY 59 }, 60 ) 61 tabs.setOnTabSelectedListener { tabId -> 62 when (tabId) { 63 DuoTabs.TAB_PRIMARY -> 64 lockScreenTabViewModel.onClicked?.invoke() 65 DuoTabs.TAB_SECONDARY -> 66 homeScreenTabViewModel.onClicked?.invoke() 67 } 68 } 69 } 70 } 71 } 72 } 73 } 74 } 75