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