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.wallpaper.picker.common.dialog.ui.viewbinder 19 20 import android.app.Dialog 21 import android.content.Context 22 import android.view.LayoutInflater 23 import android.view.ViewGroup 24 import android.widget.ImageView 25 import android.widget.TextView 26 import androidx.annotation.LayoutRes 27 import androidx.appcompat.app.AlertDialog 28 import androidx.core.view.isVisible 29 import com.android.wallpaper.R 30 import com.android.wallpaper.picker.common.button.ui.viewbinder.ButtonViewBinder 31 import com.android.wallpaper.picker.common.dialog.ui.viewmodel.DialogViewModel 32 import com.android.wallpaper.picker.common.icon.ui.viewbinder.IconViewBinder 33 import com.android.wallpaper.picker.common.text.ui.viewbinder.TextViewBinder 34 35 object DialogViewBinder { 36 /** Returns a shown dialog that's bound to the given [DialogViewModel]. */ 37 fun show( 38 context: Context, 39 viewModel: DialogViewModel, 40 onDismissed: (() -> Unit)? = null, 41 @LayoutRes dialogLayoutResourceId: Int = R.layout.dialog_view, 42 @LayoutRes buttonLayoutResourceId: Int = R.layout.dialog_button, 43 ): Dialog { 44 val view = LayoutInflater.from(context).inflate(dialogLayoutResourceId, null) 45 val icon: ImageView = view.requireViewById(R.id.icon) 46 val title: TextView = view.requireViewById(R.id.title) 47 val message: TextView = view.requireViewById(R.id.message) 48 val buttonContainer: ViewGroup = view.requireViewById(R.id.button_container) 49 50 viewModel.icon?.let { 51 IconViewBinder.bind( 52 view = icon, 53 viewModel = it, 54 ) 55 icon.isVisible = true 56 } 57 ?: run { icon.isVisible = false } 58 59 viewModel.title?.let { 60 TextViewBinder.bind( 61 view = title, 62 viewModel = it, 63 ) 64 title.isVisible = true 65 } 66 ?: run { title.isVisible = false } 67 68 viewModel.message?.let { 69 TextViewBinder.bind( 70 view = message, 71 viewModel = it, 72 ) 73 message.isVisible = true 74 } 75 ?: run { message.isVisible = false } 76 77 val dialog = 78 AlertDialog.Builder(context, R.style.LightDialogTheme) 79 .setView(view) 80 .apply { 81 if (viewModel.onDismissed != null || onDismissed != null) { 82 setOnDismissListener { 83 onDismissed?.invoke() 84 viewModel.onDismissed?.invoke() 85 } 86 } 87 } 88 .create() 89 90 buttonContainer.removeAllViews() 91 viewModel.buttons.forEach { buttonViewModel -> 92 buttonContainer.addView( 93 ButtonViewBinder.create( 94 parent = buttonContainer, 95 viewModel = 96 buttonViewModel.copy( 97 onClicked = { 98 buttonViewModel.onClicked?.invoke() 99 dialog.dismiss() 100 }, 101 ), 102 buttonLayoutResourceId = buttonLayoutResourceId, 103 ) 104 ) 105 } 106 107 dialog.show() 108 return dialog 109 } 110 } 111