1 /* <lambda>null2 * 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.customization.picker.grid.ui.binder 19 20 import android.view.View 21 import android.widget.Button 22 import android.widget.ImageView 23 import androidx.lifecycle.Lifecycle 24 import androidx.lifecycle.LifecycleOwner 25 import androidx.lifecycle.lifecycleScope 26 import androidx.lifecycle.repeatOnLifecycle 27 import androidx.recyclerview.widget.LinearLayoutManager 28 import androidx.recyclerview.widget.RecyclerView 29 import com.android.customization.picker.grid.ui.viewmodel.GridIconViewModel 30 import com.android.customization.picker.grid.ui.viewmodel.GridScreenViewModel 31 import com.android.themepicker.R 32 import com.android.wallpaper.picker.common.ui.view.ItemSpacing 33 import com.android.wallpaper.picker.option.ui.adapter.OptionItemAdapter 34 import com.android.wallpaper.picker.option.ui.binder.OptionItemBinder 35 import kotlinx.coroutines.CoroutineDispatcher 36 import kotlinx.coroutines.launch 37 38 object GridScreenBinder { 39 fun bind( 40 view: View, 41 viewModel: GridScreenViewModel, 42 lifecycleOwner: LifecycleOwner, 43 backgroundDispatcher: CoroutineDispatcher, 44 onOptionsChanged: () -> Unit, 45 isGridApplyButtonEnabled: Boolean, 46 onOptionApplied: () -> Unit, 47 ) { 48 val optionView: RecyclerView = view.requireViewById(com.android.wallpaper.R.id.options) 49 optionView.layoutManager = 50 LinearLayoutManager( 51 view.context, 52 RecyclerView.HORIZONTAL, 53 /* reverseLayout= */ false, 54 ) 55 optionView.addItemDecoration(ItemSpacing(ItemSpacing.ITEM_SPACING_DP)) 56 val adapter = 57 OptionItemAdapter( 58 layoutResourceId = R.layout.grid_option, 59 lifecycleOwner = lifecycleOwner, 60 backgroundDispatcher = backgroundDispatcher, 61 foregroundTintSpec = 62 OptionItemBinder.TintSpec( 63 selectedColor = 64 view.context.getColor(com.android.wallpaper.R.color.system_on_surface), 65 unselectedColor = 66 view.context.getColor(com.android.wallpaper.R.color.system_on_surface), 67 ), 68 bindIcon = { foregroundView: View, gridIcon: GridIconViewModel -> 69 val imageView = foregroundView as? ImageView 70 imageView?.let { GridIconViewBinder.bind(imageView, gridIcon) } 71 } 72 ) 73 optionView.adapter = adapter 74 75 if (isGridApplyButtonEnabled) { 76 val applyButton: Button = view.requireViewById(R.id.apply_button) 77 applyButton.visibility = View.VISIBLE 78 view.requireViewById<View>(R.id.apply_button_note).visibility = View.VISIBLE 79 applyButton.setOnClickListener { onOptionApplied() } 80 } 81 82 lifecycleOwner.lifecycleScope.launch { 83 lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 84 launch { 85 viewModel.optionItems.collect { options -> 86 adapter.setItems(options) 87 onOptionsChanged() 88 } 89 } 90 } 91 } 92 } 93 } 94