|
| 1 | +package com.firebase.ui.auth.compose |
| 2 | + |
| 3 | +import androidx.compose.foundation.Image |
| 4 | +import androidx.compose.material3.Icon |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.Spacer |
| 9 | +import androidx.compose.foundation.layout.fillMaxSize |
| 10 | +import androidx.compose.foundation.layout.width |
| 11 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 12 | +import androidx.compose.material.icons.Icons |
| 13 | +import androidx.compose.material.icons.filled.Star |
| 14 | +import androidx.compose.material3.Button |
| 15 | +import androidx.compose.material3.ButtonDefaults |
| 16 | +import androidx.compose.material3.Text |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.ui.Alignment |
| 19 | +import androidx.compose.ui.Modifier |
| 20 | +import androidx.compose.ui.graphics.Color |
| 21 | +import androidx.compose.ui.platform.LocalContext |
| 22 | +import androidx.compose.ui.tooling.preview.Preview |
| 23 | +import androidx.compose.ui.unit.dp |
| 24 | +import com.firebase.ui.auth.compose.configuration.AuthProvider |
| 25 | +import com.firebase.ui.auth.compose.configuration.Provider |
| 26 | +import com.firebase.ui.auth.compose.configuration.stringprovider.AuthUIStringProvider |
| 27 | +import com.firebase.ui.auth.compose.configuration.stringprovider.DefaultAuthUIStringProvider |
| 28 | +import com.firebase.ui.auth.compose.configuration.theme.AuthUIAsset |
| 29 | +import com.firebase.ui.auth.compose.configuration.theme.AuthUITheme |
| 30 | + |
| 31 | +/** |
| 32 | + * A customizable button for an authentication provider. |
| 33 | + * |
| 34 | + * This button displays the icon and name of an authentication provider (e.g., Google, Facebook). |
| 35 | + * It is designed to be used within a list of sign-in options. The button's appearance can be |
| 36 | + * customized using the [style] parameter, and its text is localized via the [stringProvider]. |
| 37 | + * |
| 38 | + * **Example usage:** |
| 39 | + * ```kotlin |
| 40 | + * AuthProviderButton( |
| 41 | + * provider = AuthProvider.Facebook(), |
| 42 | + * onClick = { /* Handle Facebook sign-in */ }, |
| 43 | + * stringProvider = DefaultAuthUIStringProvider(LocalContext.current) |
| 44 | + * ) |
| 45 | + * ``` |
| 46 | + * |
| 47 | + * @param modifier A modifier for the button |
| 48 | + * @param provider The provider to represent. |
| 49 | + * @param onClick A callback when the button is clicked |
| 50 | + * @param enabled If the button is enabled. Defaults to true. |
| 51 | + * @param style Optional custom styling for the button. |
| 52 | + * @param stringProvider The [AuthUIStringProvider] for localized strings |
| 53 | + * |
| 54 | + * @since 10.0.0 |
| 55 | + */ |
| 56 | +@Composable |
| 57 | +fun AuthProviderButton( |
| 58 | + modifier: Modifier = Modifier, |
| 59 | + provider: AuthProvider, |
| 60 | + onClick: () -> Unit, |
| 61 | + enabled: Boolean = true, |
| 62 | + style: AuthUITheme.ProviderStyle? = null, |
| 63 | + stringProvider: AuthUIStringProvider, |
| 64 | +) { |
| 65 | + val providerStyle = resolveProviderStyle(provider, style) |
| 66 | + val providerText = resolveProviderLabel(provider, stringProvider) |
| 67 | + |
| 68 | + Button( |
| 69 | + modifier = modifier, |
| 70 | + colors = ButtonDefaults.buttonColors( |
| 71 | + containerColor = providerStyle.backgroundColor, |
| 72 | + contentColor = providerStyle.contentColor, |
| 73 | + ), |
| 74 | + shape = providerStyle.shape, |
| 75 | + elevation = ButtonDefaults.buttonElevation( |
| 76 | + defaultElevation = providerStyle.elevation |
| 77 | + ), |
| 78 | + onClick = onClick, |
| 79 | + enabled = enabled, |
| 80 | + ) { |
| 81 | + Row( |
| 82 | + verticalAlignment = Alignment.CenterVertically |
| 83 | + ) { |
| 84 | + val providerIcon = providerStyle.icon |
| 85 | + if (providerIcon != null) { |
| 86 | + val iconTint = providerStyle.iconTint |
| 87 | + if (iconTint != null) { |
| 88 | + Icon( |
| 89 | + painter = providerIcon.painter, |
| 90 | + contentDescription = providerText, |
| 91 | + tint = iconTint |
| 92 | + ) |
| 93 | + } else { |
| 94 | + Image( |
| 95 | + painter = providerIcon.painter, |
| 96 | + contentDescription = providerText |
| 97 | + ) |
| 98 | + } |
| 99 | + Spacer(modifier = Modifier.width(8.dp)) |
| 100 | + } |
| 101 | + Text( |
| 102 | + text = providerText |
| 103 | + ) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +internal fun resolveProviderStyle( |
| 109 | + provider: AuthProvider, |
| 110 | + style: AuthUITheme.ProviderStyle?, |
| 111 | +): AuthUITheme.ProviderStyle { |
| 112 | + if (style != null) return style |
| 113 | + |
| 114 | + val defaultStyle = |
| 115 | + AuthUITheme.Default.providerStyles[provider.providerId] ?: AuthUITheme.ProviderStyle.Empty |
| 116 | + |
| 117 | + return if (provider is AuthProvider.GenericOAuth) { |
| 118 | + AuthUITheme.ProviderStyle( |
| 119 | + icon = provider.buttonIcon ?: defaultStyle.icon, |
| 120 | + backgroundColor = provider.buttonColor ?: defaultStyle.backgroundColor, |
| 121 | + contentColor = provider.contentColor ?: defaultStyle.contentColor, |
| 122 | + ) |
| 123 | + } else { |
| 124 | + defaultStyle |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +internal fun resolveProviderLabel( |
| 129 | + provider: AuthProvider, |
| 130 | + stringProvider: AuthUIStringProvider |
| 131 | +): String = when (provider) { |
| 132 | + is AuthProvider.GenericOAuth -> provider.buttonLabel |
| 133 | + else -> when (Provider.fromId(provider.providerId)) { |
| 134 | + Provider.GOOGLE -> stringProvider.signInWithGoogle |
| 135 | + Provider.FACEBOOK -> stringProvider.signInWithFacebook |
| 136 | + Provider.TWITTER -> stringProvider.signInWithTwitter |
| 137 | + Provider.GITHUB -> stringProvider.signInWithGithub |
| 138 | + Provider.EMAIL -> stringProvider.signInWithEmail |
| 139 | + Provider.PHONE -> stringProvider.signInWithPhone |
| 140 | + Provider.ANONYMOUS -> stringProvider.signInAnonymously |
| 141 | + Provider.MICROSOFT -> stringProvider.signInWithMicrosoft |
| 142 | + Provider.YAHOO -> stringProvider.signInWithYahoo |
| 143 | + Provider.APPLE -> stringProvider.signInWithApple |
| 144 | + null -> "Unknown Provider" |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +@Preview(showBackground = true) |
| 149 | +@Composable |
| 150 | +private fun PreviewAuthProviderButton() { |
| 151 | + val context = LocalContext.current |
| 152 | + Column( |
| 153 | + modifier = Modifier |
| 154 | + .fillMaxSize(), |
| 155 | + verticalArrangement = Arrangement.Center, |
| 156 | + horizontalAlignment = Alignment.CenterHorizontally |
| 157 | + ) { |
| 158 | + AuthProviderButton( |
| 159 | + provider = AuthProvider.Email( |
| 160 | + actionCodeSettings = null, |
| 161 | + passwordValidationRules = emptyList() |
| 162 | + ), |
| 163 | + onClick = {}, |
| 164 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 165 | + ) |
| 166 | + AuthProviderButton( |
| 167 | + provider = AuthProvider.Phone( |
| 168 | + defaultNumber = null, |
| 169 | + defaultCountryCode = null, |
| 170 | + allowedCountries = null, |
| 171 | + ), |
| 172 | + onClick = {}, |
| 173 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 174 | + ) |
| 175 | + AuthProviderButton( |
| 176 | + provider = AuthProvider.Google( |
| 177 | + scopes = emptyList(), |
| 178 | + serverClientId = null |
| 179 | + ), |
| 180 | + onClick = {}, |
| 181 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 182 | + ) |
| 183 | + AuthProviderButton( |
| 184 | + provider = AuthProvider.Facebook(), |
| 185 | + onClick = {}, |
| 186 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 187 | + ) |
| 188 | + AuthProviderButton( |
| 189 | + provider = AuthProvider.Twitter( |
| 190 | + customParameters = emptyMap() |
| 191 | + ), |
| 192 | + onClick = {}, |
| 193 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 194 | + ) |
| 195 | + AuthProviderButton( |
| 196 | + provider = AuthProvider.Github( |
| 197 | + customParameters = emptyMap() |
| 198 | + ), |
| 199 | + onClick = {}, |
| 200 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 201 | + ) |
| 202 | + AuthProviderButton( |
| 203 | + provider = AuthProvider.Microsoft( |
| 204 | + tenant = null, |
| 205 | + customParameters = emptyMap() |
| 206 | + ), |
| 207 | + onClick = {}, |
| 208 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 209 | + ) |
| 210 | + AuthProviderButton( |
| 211 | + provider = AuthProvider.Yahoo( |
| 212 | + customParameters = emptyMap() |
| 213 | + ), |
| 214 | + onClick = {}, |
| 215 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 216 | + ) |
| 217 | + AuthProviderButton( |
| 218 | + provider = AuthProvider.Apple( |
| 219 | + locale = null, |
| 220 | + customParameters = emptyMap() |
| 221 | + ), |
| 222 | + onClick = {}, |
| 223 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 224 | + ) |
| 225 | + AuthProviderButton( |
| 226 | + provider = AuthProvider.Anonymous, |
| 227 | + onClick = {}, |
| 228 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 229 | + ) |
| 230 | + AuthProviderButton( |
| 231 | + provider = AuthProvider.GenericOAuth( |
| 232 | + providerId = "google.com", |
| 233 | + scopes = emptyList(), |
| 234 | + customParameters = emptyMap(), |
| 235 | + buttonLabel = "Generic Provider", |
| 236 | + buttonIcon = AuthUIAsset.Vector(Icons.Default.Star), |
| 237 | + buttonColor = Color.Gray, |
| 238 | + contentColor = Color.White |
| 239 | + ), |
| 240 | + onClick = {}, |
| 241 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 242 | + ) |
| 243 | + AuthProviderButton( |
| 244 | + provider = AuthProvider.GenericOAuth( |
| 245 | + providerId = "google.com", |
| 246 | + scopes = emptyList(), |
| 247 | + customParameters = emptyMap(), |
| 248 | + buttonLabel = "Custom Style", |
| 249 | + buttonIcon = AuthUIAsset.Vector(Icons.Default.Star), |
| 250 | + buttonColor = Color.Gray, |
| 251 | + contentColor = Color.White |
| 252 | + ), |
| 253 | + onClick = {}, |
| 254 | + style = AuthUITheme.ProviderStyle( |
| 255 | + icon = AuthUITheme.Default.providerStyles[Provider.MICROSOFT.id]?.icon, |
| 256 | + backgroundColor = AuthUITheme.Default.providerStyles[Provider.MICROSOFT.id]!!.backgroundColor, |
| 257 | + contentColor = AuthUITheme.Default.providerStyles[Provider.MICROSOFT.id]!!.contentColor, |
| 258 | + iconTint = Color.Red, |
| 259 | + shape = RoundedCornerShape(24.dp), |
| 260 | + elevation = 6.dp |
| 261 | + ), |
| 262 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 263 | + ) |
| 264 | + AuthProviderButton( |
| 265 | + provider = AuthProvider.GenericOAuth( |
| 266 | + providerId = "unknown_provider", |
| 267 | + scopes = emptyList(), |
| 268 | + customParameters = emptyMap(), |
| 269 | + buttonLabel = "Unsupported Provider", |
| 270 | + buttonIcon = null, |
| 271 | + buttonColor = null, |
| 272 | + contentColor = null, |
| 273 | + ), |
| 274 | + onClick = {}, |
| 275 | + stringProvider = DefaultAuthUIStringProvider(context) |
| 276 | + ) |
| 277 | + } |
| 278 | +} |
0 commit comments