Skip to content

Commit 96ffa30

Browse files
committed
feat: TOS and PP footer, ui tests for AuthMethodPicker
1 parent b6f7bdc commit 96ffa30

File tree

4 files changed

+428
-43
lines changed

4 files changed

+428
-43
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.firebase.ui.auth.compose.ui.method_picker
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import androidx.annotation.StringRes
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.Text
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.res.stringResource
11+
import androidx.compose.ui.text.LinkAnnotation
12+
import androidx.compose.ui.text.SpanStyle
13+
import androidx.compose.ui.text.TextLinkStyles
14+
import androidx.compose.ui.text.buildAnnotatedString
15+
import androidx.compose.ui.text.style.TextAlign
16+
import androidx.compose.ui.text.style.TextDecoration
17+
import androidx.compose.ui.text.withLink
18+
import androidx.core.net.toUri
19+
20+
@Composable
21+
internal fun AnnotatedStringResource(
22+
context: Context,
23+
modifier: Modifier = Modifier,
24+
@StringRes id: Int,
25+
vararg links: Pair<String, String>,
26+
inPreview: Boolean = false,
27+
previewText: String? = null,
28+
) {
29+
val labels = links.map { it.first }.toTypedArray()
30+
31+
val template = if (inPreview && previewText != null) {
32+
previewText
33+
} else {
34+
stringResource(id = id, *labels)
35+
}
36+
37+
val annotated = buildAnnotatedString {
38+
var currentIndex = 0
39+
40+
links.forEach { (label, url) ->
41+
val start = template.indexOf(label, currentIndex).takeIf { it >= 0 } ?: return@forEach
42+
43+
append(template.substring(currentIndex, start))
44+
45+
withLink(
46+
LinkAnnotation.Url(
47+
url,
48+
styles = TextLinkStyles(
49+
style = SpanStyle(
50+
color = MaterialTheme.colorScheme.primary,
51+
textDecoration = TextDecoration.Underline,
52+
)
53+
)
54+
) {
55+
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
56+
context.startActivity(intent)
57+
}
58+
) {
59+
append(label)
60+
}
61+
62+
currentIndex = start + label.length
63+
}
64+
65+
if (currentIndex < template.length) {
66+
append(template.substring(currentIndex))
67+
}
68+
}
69+
70+
Text(
71+
modifier = modifier,
72+
text = annotated,
73+
style = MaterialTheme.typography.bodyMedium,
74+
textAlign = TextAlign.Center
75+
)
76+
}

auth/src/main/java/com/firebase/ui/auth/compose/AuthMethodPicker.kt renamed to auth/src/main/java/com/firebase/ui/auth/compose/ui/method_picker/AuthMethodPicker.kt

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
1-
package com.firebase.ui.auth.compose
1+
package com.firebase.ui.auth.compose.ui.method_picker
22

33
import androidx.compose.foundation.Image
4-
import androidx.compose.foundation.background
5-
import androidx.compose.foundation.layout.Arrangement
64
import androidx.compose.foundation.layout.Box
75
import androidx.compose.foundation.layout.Column
8-
import androidx.compose.foundation.layout.PaddingValues
9-
import androidx.compose.foundation.layout.Spacer
10-
import androidx.compose.foundation.layout.fillMaxHeight
116
import androidx.compose.foundation.layout.fillMaxSize
12-
import androidx.compose.foundation.layout.height
137
import androidx.compose.foundation.layout.padding
14-
import androidx.compose.foundation.layout.safeContentPadding
158
import androidx.compose.foundation.layout.safeDrawingPadding
16-
import androidx.compose.foundation.layout.widthIn
179
import androidx.compose.foundation.lazy.LazyColumn
18-
import androidx.compose.material.icons.Icons
19-
import androidx.compose.material.icons.filled.Favorite
20-
import androidx.compose.material.icons.filled.Star
21-
import androidx.compose.material3.Icon
22-
import androidx.compose.material3.Scaffold
23-
import androidx.compose.material3.Text
2410
import androidx.compose.runtime.Composable
2511
import androidx.compose.ui.Alignment
2612
import androidx.compose.ui.Modifier
27-
import androidx.compose.ui.graphics.Color
2813
import androidx.compose.ui.platform.LocalContext
29-
import androidx.compose.ui.text.style.TextAlign
14+
import androidx.compose.ui.platform.LocalInspectionMode
15+
import androidx.compose.ui.platform.testTag
16+
import androidx.compose.ui.res.stringResource
3017
import androidx.compose.ui.tooling.preview.Preview
3118
import androidx.compose.ui.unit.dp
3219
import com.firebase.ui.auth.R
33-
import com.firebase.ui.auth.compose.AuthProviderButton
3420
import com.firebase.ui.auth.compose.configuration.AuthProvider
35-
import com.firebase.ui.auth.compose.configuration.stringprovider.DefaultAuthUIStringProvider
21+
import com.firebase.ui.auth.compose.configuration.string_provider.DefaultAuthUIStringProvider
3622
import com.firebase.ui.auth.compose.configuration.theme.AuthUIAsset
37-
38-
//AuthMethodPicker(
39-
// providers = listOf(GoogleAuthProvider(), EmailAuthProvider()),
40-
// onProviderSelected = { provider -> /* ... */ }
41-
//)
23+
import com.firebase.ui.auth.compose.ui.components.AuthProviderButton
4224

4325
/**
4426
* Renders the provider selection screen.
@@ -59,6 +41,8 @@ import com.firebase.ui.auth.compose.configuration.theme.AuthUIAsset
5941
* @param logo An optional logo to display.
6042
* @param onProviderSelected A callback when a provider is selected.
6143
* @param customLayout An optional custom layout composable for the provider buttons.
44+
* @param termsOfServiceUrl The URL for the Terms of Service.
45+
* @param privacyPolicyUrl The URL for the Privacy Policy.
6246
*
6347
* @since 10.0.0
6448
*/
@@ -69,8 +53,11 @@ fun AuthMethodPicker(
6953
logo: AuthUIAsset? = null,
7054
onProviderSelected: (AuthProvider) -> Unit,
7155
customLayout: @Composable ((List<AuthProvider>, (AuthProvider) -> Unit) -> Unit)? = null,
56+
termsOfServiceUrl: String? = null,
57+
privacyPolicyUrl: String? = null,
7258
) {
7359
val context = LocalContext.current
60+
val inPreview = LocalInspectionMode.current
7461

7562
Column(
7663
modifier = modifier
@@ -83,7 +70,8 @@ fun AuthMethodPicker(
8370
modifier = Modifier
8471
.weight(0.4f),
8572
painter = it.painter,
86-
contentDescription = "AuthMethodPicker logo",
73+
contentDescription = if (inPreview) ""
74+
else stringResource(R.string.fui_auth_method_picker_logo)
8775
)
8876
}
8977
if (customLayout != null) {
@@ -92,9 +80,9 @@ fun AuthMethodPicker(
9280
LazyColumn(
9381
modifier = Modifier
9482
.fillMaxSize()
95-
.weight(1f),
83+
.weight(1f)
84+
.testTag("AuthMethodPicker LazyColumn"),
9685
horizontalAlignment = Alignment.CenterHorizontally,
97-
contentPadding = PaddingValues(bottom = 64.dp) // Space for text
9886
) {
9987
items(providers.size) { index ->
10088
val provider = providers[index]
@@ -112,14 +100,18 @@ fun AuthMethodPicker(
112100
}
113101
}
114102
}
115-
Text(
116-
"By continuing, you are indicating that you accept our " +
117-
"Terms of Service and Privacy Policy.",
118-
textAlign = TextAlign.Center,
119-
modifier = Modifier
120-
.padding(horizontal = 16.dp, vertical = 16.dp)
121-
)
122103
}
104+
AnnotatedStringResource(
105+
context = context,
106+
inPreview = inPreview,
107+
previewText = "By continuing, you accept our Terms of Service and Privacy Policy.",
108+
modifier = Modifier.padding(vertical = 16.dp),
109+
id = R.string.fui_tos_and_pp,
110+
links = arrayOf(
111+
"Terms of Service" to (termsOfServiceUrl ?: ""),
112+
"Privacy Policy" to (privacyPolicyUrl ?: "")
113+
)
114+
)
123115
}
124116
}
125117

@@ -160,19 +152,12 @@ fun PreviewAuthMethodPicker() {
160152
customParameters = emptyMap()
161153
),
162154
AuthProvider.Anonymous,
163-
AuthProvider.GenericOAuth(
164-
providerId = "google.com",
165-
scopes = emptyList(),
166-
customParameters = emptyMap(),
167-
buttonLabel = "Generic Provider",
168-
buttonIcon = AuthUIAsset.Vector(Icons.Default.Star),
169-
buttonColor = Color.Gray,
170-
contentColor = Color.White
171-
)
172155
),
173156
logo = AuthUIAsset.Resource(R.drawable.fui_ic_check_circle_black_128dp),
174157
onProviderSelected = { provider ->
175158

176159
},
160+
termsOfServiceUrl = "https://example.com/terms",
161+
privacyPolicyUrl = "https://example.com/privacy"
177162
)
178163
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<string name="fui_idp_name_email">Email</string>
2020

2121
<!-- Auth method picker -->
22+
<string name="fui_auth_method_picker_logo" translatable="false">"Auth method picker logo"</string>
2223
<string name="fui_sign_in_with_google" translation_description="Sign in button text">Sign in with Google</string>
2324
<string name="fui_sign_in_with_facebook" translation_description="Sign in button text">Sign in with Facebook</string>
2425
<string name="fui_sign_in_with_twitter" translation_description="Sign in button text">Sign in with Twitter</string>

0 commit comments

Comments
 (0)