1 /*
2  * Copyright (C) 2024 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 package com.android.settings.widget
18 
19 import android.content.Context
20 import android.content.res.Resources
21 import android.util.AttributeSet
22 import androidx.compose.ui.graphics.vector.ImageVector
23 import androidx.compose.ui.res.vectorResource
24 import com.android.settings.spa.preference.ComposePreference
25 import com.android.settingslib.spa.widget.card.CardButton
26 import com.android.settingslib.spa.widget.card.CardModel
27 import com.android.settingslib.spa.widget.card.SettingsCard
28 
29 /** A preference for settings banner tips card. */
30 class TipCardPreference
31 @JvmOverloads
32 constructor(
33     context: Context,
34     attr: AttributeSet? = null,
35 ) : ComposePreference(context, attr) {
36 
37     /** A icon resource id for displaying icon on tips card. */
38     var iconResId: Int? = null
39 
40     /** The primary button's text. */
41     var primaryButtonText: String = ""
42 
43     /** The accessibility content description of the primary button. */
44     var primaryButtonContentDescription: String? = null
45 
46     /** The action for click on primary button. */
<lambda>null47     var primaryButtonAction: () -> Unit = {}
48 
49     /** The visibility of primary button on tips card. The default value is `false`. */
50     var primaryButtonVisibility: Boolean = false
51 
52     /** The text on the second button of this [SettingsCard]. */
53     var secondaryButtonText: String = ""
54 
55     /** The accessibility content description of the secondary button. */
56     var secondaryButtonContentDescription: String? = null
57 
58     /** The action for click on secondary button. */
<lambda>null59     var secondaryButtonAction: () -> Unit = {}
60 
61     /** The visibility of secondary button on tips card. The default value is `false`. */
62     var secondaryButtonVisibility: Boolean = false
63 
64     var onClick: (() -> Unit)? = null
65 
66     /** The callback for click on card preference itself. */
67     private var onDismiss: (() -> Unit)? = null
68 
69     /** Enable the dismiss button on tips card. */
enableDismissnull70     fun enableDismiss(enable: Boolean) =
71         if (enable) onDismiss = { isVisible = false } else onDismiss = null
72 
73     /** Clear layout state if needed. */
resetLayoutStatenull74     fun resetLayoutState() {
75         primaryButtonVisibility = false
76         secondaryButtonVisibility = false
77         notifyChanged()
78     }
79 
80     /** Build the tips card content to apply any changes of this card's property. */
buildContentnull81     fun buildContent() {
82         setContent {
83             SettingsCard(
84                 CardModel(
85                     title = title?.toString() ?: "",
86                     text = summary?.toString() ?: "",
87                     buttons = listOfNotNull(configPrimaryButton(), configSecondaryButton()),
88                     onDismiss = onDismiss,
89                     imageVector =
90                     iconResId
91                         ?.takeIf { it != Resources.ID_NULL }
92                         ?.let { ImageVector.vectorResource(it) },
93                     onClick = onClick,
94                 )
95             )
96         }
97     }
98 
configPrimaryButtonnull99     private fun configPrimaryButton(): CardButton? {
100         return if (primaryButtonVisibility)
101             CardButton(
102                 text = primaryButtonText,
103                 contentDescription = primaryButtonContentDescription,
104                 onClick = primaryButtonAction,
105             )
106         else null
107     }
108 
configSecondaryButtonnull109     private fun configSecondaryButton(): CardButton? {
110         return if (secondaryButtonVisibility)
111             CardButton(
112                 text = secondaryButtonText,
113                 contentDescription = secondaryButtonContentDescription,
114                 onClick = secondaryButtonAction,
115             )
116         else null
117     }
118 
notifyChangednull119     override fun notifyChanged() {
120         buildContent()
121         super.notifyChanged()
122     }
123 }
124