Skip to content

Commit ea1c2fb

Browse files
committed
feat: added context AuthUIConfiguration and default string provider
1 parent ed47c1d commit ea1c2fb

File tree

7 files changed

+187
-57
lines changed

7 files changed

+187
-57
lines changed

auth/src/main/java/com/firebase/ui/auth/compose/configuration/AuthUIConfiguration.kt

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,27 @@
1414

1515
package com.firebase.ui.auth.compose.configuration
1616

17+
import android.content.Context
1718
import java.util.Locale
1819
import com.google.firebase.auth.ActionCodeSettings
1920
import androidx.compose.ui.graphics.vector.ImageVector
2021

21-
fun actionCodeSettings(
22-
block: ActionCodeSettings.Builder.() -> Unit
23-
) = ActionCodeSettings.newBuilder().apply(block).build()
22+
fun actionCodeSettings(block: ActionCodeSettings.Builder.() -> Unit) =
23+
ActionCodeSettings.newBuilder().apply(block).build()
2424

25-
fun authUIConfiguration(block: AuthUIConfigurationBuilder.() -> Unit): AuthUIConfiguration {
26-
val builder = AuthUIConfigurationBuilder()
27-
builder.block()
28-
return builder.build()
29-
}
25+
fun authUIConfiguration(block: AuthUIConfigurationBuilder.() -> Unit) =
26+
AuthUIConfigurationBuilder().apply(block).build()
3027

3128
@DslMarker
3229
annotation class AuthUIConfigurationDsl
3330

3431
@AuthUIConfigurationDsl
3532
class AuthUIConfigurationBuilder {
33+
var context: Context? = null
3634
private val providers = mutableListOf<AuthProvider>()
3735
var theme: AuthUITheme = AuthUITheme.Default
38-
var stringProvider: AuthUIStringProvider? = null
3936
var locale: Locale? = null
37+
var stringProvider: AuthUIStringProvider? = null
4038
var isCredentialManagerEnabled: Boolean = true
4139
var isMfaEnabled: Boolean = true
4240
var isAnonymousUpgradeEnabled: Boolean = false
@@ -48,36 +46,18 @@ class AuthUIConfigurationBuilder {
4846
var isDisplayNameRequired: Boolean = true
4947
var isProviderChoiceAlwaysShown: Boolean = false
5048

51-
fun providers(block: AuthProvidersBuilder.() -> Unit) {
52-
val builder = AuthProvidersBuilder()
53-
builder.block()
54-
providers.addAll(builder.build())
55-
}
49+
fun providers(block: AuthProvidersBuilder.() -> Unit) =
50+
providers.addAll(AuthProvidersBuilder().apply(block).build())
5651

5752
internal fun build(): AuthUIConfiguration {
58-
validate()
59-
return AuthUIConfiguration(
60-
providers = providers.toList(),
61-
theme = theme,
62-
stringProvider = stringProvider,
63-
locale = locale,
64-
isCredentialManagerEnabled = isCredentialManagerEnabled,
65-
isMfaEnabled = isMfaEnabled,
66-
isAnonymousUpgradeEnabled = isAnonymousUpgradeEnabled,
67-
tosUrl = tosUrl,
68-
privacyPolicyUrl = privacyPolicyUrl,
69-
logo = logo,
70-
actionCodeSettings = actionCodeSettings,
71-
isNewEmailAccountsAllowed = isNewEmailAccountsAllowed,
72-
isDisplayNameRequired = isDisplayNameRequired,
73-
isProviderChoiceAlwaysShown = isProviderChoiceAlwaysShown
74-
)
75-
}
53+
// Context is not null
54+
val context = requireNotNull(context) {
55+
"Application context is required"
56+
}
7657

77-
private fun validate() {
7858
// At least one provider
79-
if (providers.isEmpty()) {
80-
throw IllegalArgumentException("At least one provider must be configured")
59+
require(providers.isNotEmpty()) {
60+
"At least one provider must be configured"
8161
}
8262

8363
// No unsupported providers
@@ -113,13 +93,36 @@ class AuthUIConfigurationBuilder {
11393
else -> null
11494
}
11595
}
96+
97+
return AuthUIConfiguration(
98+
context = context,
99+
providers = providers.toList(),
100+
theme = theme,
101+
locale = locale,
102+
stringProvider = stringProvider ?: DefaultAuthUIStringProvider(context, locale),
103+
isCredentialManagerEnabled = isCredentialManagerEnabled,
104+
isMfaEnabled = isMfaEnabled,
105+
isAnonymousUpgradeEnabled = isAnonymousUpgradeEnabled,
106+
tosUrl = tosUrl,
107+
privacyPolicyUrl = privacyPolicyUrl,
108+
logo = logo,
109+
actionCodeSettings = actionCodeSettings,
110+
isNewEmailAccountsAllowed = isNewEmailAccountsAllowed,
111+
isDisplayNameRequired = isDisplayNameRequired,
112+
isProviderChoiceAlwaysShown = isProviderChoiceAlwaysShown
113+
)
116114
}
117115
}
118116

119117
/**
120118
* Configuration object for the authentication flow.
121119
*/
122120
class AuthUIConfiguration(
121+
/**
122+
* Application context
123+
*/
124+
val context: Context,
125+
123126
/**
124127
* The list of enabled authentication providers.
125128
*/
@@ -131,14 +134,14 @@ class AuthUIConfiguration(
131134
val theme: AuthUITheme = AuthUITheme.Default,
132135

133136
/**
134-
* A custom provider for localized strings.
137+
* The locale for internationalization.
135138
*/
136-
val stringProvider: AuthUIStringProvider? = null,
139+
val locale: Locale? = null,
137140

138141
/**
139-
* The locale for internationalization.
142+
* A custom provider for localized strings.
140143
*/
141-
val locale: Locale? = null,
144+
val stringProvider: AuthUIStringProvider = DefaultAuthUIStringProvider(context, locale),
142145

143146
/**
144147
* Enables integration with Android's Credential Manager API. Defaults to true.

auth/src/main/java/com/firebase/ui/auth/compose/configuration/AuthUIStringProvider.kt

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package com.firebase.ui.auth.compose.configuration
1616

1717
import android.content.Context
18+
import android.content.res.Configuration
1819
import com.firebase.ui.auth.R
20+
import java.util.Locale
1921

2022
/**
2123
* An interface for providing localized string resources. This interface defines methods for all
@@ -29,6 +31,33 @@ interface AuthUIStringProvider {
2931
/** Button text for Google sign-in option */
3032
val signInWithGoogle: String
3133

34+
/** Button text for Facebook sign-in option */
35+
val signInWithFacebook: String
36+
37+
/** Button text for Twitter sign-in option */
38+
val signInWithTwitter: String
39+
40+
/** Button text for Github sign-in option */
41+
val signInWithGithub: String
42+
43+
/** Button text for Email sign-in option */
44+
val signInWithEmail: String
45+
46+
/** Button text for Phone sign-in option */
47+
val signInWithPhone: String
48+
49+
/** Button text for Anonymous sign-in option */
50+
val signInAnonymously: String
51+
52+
/** Button text for Apple sign-in option */
53+
val signInWithApple: String
54+
55+
/** Button text for Microsoft sign-in option */
56+
val signInWithMicrosoft: String
57+
58+
/** Button text for Yahoo sign-in option */
59+
val signInWithYahoo: String
60+
3261
/** Error message when email address field is empty */
3362
val missingEmailAddress: String
3463

@@ -57,25 +86,72 @@ interface AuthUIStringProvider {
5786
val passwordMissingSpecialCharacter: String
5887
}
5988

60-
internal class DefaultAuthUIStringProvider(private val context: Context) : AuthUIStringProvider {
61-
override val initializing: String get() = ""
89+
internal class DefaultAuthUIStringProvider(
90+
private val context: Context,
91+
private val locale: Locale? = null,
92+
) : AuthUIStringProvider {
93+
94+
private val localizedContext = locale?.let { locale ->
95+
context.createConfigurationContext(
96+
Configuration(context.resources.configuration).apply {
97+
setLocale(locale)
98+
}
99+
)
100+
} ?: context
101+
102+
/**
103+
* General Strings
104+
*/
105+
override val initializing: String
106+
get() = ""
107+
108+
/**
109+
* Auth Provider Button Strings
110+
*/
62111
override val signInWithGoogle: String
63-
get() = context.getString(R.string.fui_sign_in_with_google)
112+
get() = localizedContext.getString(R.string.fui_sign_in_with_google)
113+
override val signInWithFacebook: String
114+
get() = localizedContext.getString(R.string.fui_sign_in_with_facebook)
115+
override val signInWithTwitter: String
116+
get() = localizedContext.getString(R.string.fui_sign_in_with_twitter)
117+
override val signInWithGithub: String
118+
get() = localizedContext.getString(R.string.fui_sign_in_with_github)
119+
override val signInWithEmail: String
120+
get() = localizedContext.getString(R.string.fui_sign_in_with_email)
121+
override val signInWithPhone: String
122+
get() = localizedContext.getString(R.string.fui_sign_in_with_phone)
123+
override val signInAnonymously: String
124+
get() = localizedContext.getString(R.string.fui_sign_in_anonymously)
125+
override val signInWithApple: String
126+
get() = localizedContext.getString(R.string.fui_sign_in_with_apple)
127+
override val signInWithMicrosoft: String
128+
get() = localizedContext.getString(R.string.fui_sign_in_with_microsoft)
129+
override val signInWithYahoo: String
130+
get() = localizedContext.getString(R.string.fui_sign_in_with_yahoo)
131+
132+
/**
133+
* Email Validator Strings
134+
*/
64135
override val missingEmailAddress: String
65-
get() = context.getString(R.string.fui_missing_email_address)
136+
get() = localizedContext.getString(R.string.fui_missing_email_address)
66137
override val invalidEmailAddress: String
67-
get() = context.getString(R.string.fui_invalid_email_address)
138+
get() = localizedContext.getString(R.string.fui_invalid_email_address)
139+
140+
/**
141+
* Password Validator Strings
142+
*/
68143
override val invalidPassword: String
69-
get() = context.getString(R.string.fui_error_invalid_password)
70-
override val passwordsDoNotMatch: String get() = ""
144+
get() = localizedContext.getString(R.string.fui_error_invalid_password)
145+
override val passwordsDoNotMatch: String
146+
get() = localizedContext.getString(R.string.fui_passwords_do_not_match)
71147
override val passwordTooShort: String
72-
get() = context.getString(R.string.fui_error_password_too_short)
148+
get() = localizedContext.getString(R.string.fui_error_password_too_short)
73149
override val passwordMissingUppercase: String
74-
get() = context.getString(R.string.fui_error_password_missing_uppercase)
150+
get() = localizedContext.getString(R.string.fui_error_password_missing_uppercase)
75151
override val passwordMissingLowercase: String
76-
get() = context.getString(R.string.fui_error_password_missing_lowercase)
152+
get() = localizedContext.getString(R.string.fui_error_password_missing_lowercase)
77153
override val passwordMissingDigit: String
78-
get() = context.getString(R.string.fui_error_password_missing_digit)
154+
get() = localizedContext.getString(R.string.fui_error_password_missing_digit)
79155
override val passwordMissingSpecialCharacter: String
80-
get() = context.getString(R.string.fui_error_password_missing_special_character)
156+
get() = localizedContext.getString(R.string.fui_error_password_missing_special_character)
81157
}

auth/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<string name="fui_error_invalid_password" translation_description="Error message for when the user enters an invalid or wrong password.">Incorrect password.</string>
9595

9696
<!-- Password validation messages -->
97+
<string name="fui_passwords_do_not_match" translation_description="Error message when password don't match">Passwords do not match</string>
9798
<string name="fui_error_password_too_short" translation_description="Error message when password is too short">Password must be at least %1$d characters long</string>
9899
<string name="fui_error_password_missing_uppercase" translation_description="Error message when password is missing uppercase letter">Password must contain at least one uppercase letter</string>
99100
<string name="fui_error_password_missing_lowercase" translation_description="Error message when password is missing lowercase letter">Password must contain at least one lowercase letter</string>

0 commit comments

Comments
 (0)