Skip to content

Commit 079668d

Browse files
committed
feat(mobile): add Google Sign-In button to LoginScreen
- Add "Continue with Google" OutlinedButton with inline Google G logo (four-colour vector, no resource file required) above the email/password fields with an OR divider - Wire onSignInWithGoogle callback through AppNavGraph; activity reference obtained from LocalContext so LoginScreen stays activity-independent - Add onSignInWithGoogle parameter to LoginScreen and update all call sites and previews - Add instrumented tests: button visibility, click callback, existing tests updated with new parameter
1 parent fefd49c commit 079668d

3 files changed

Lines changed: 133 additions & 69 deletions

File tree

‎mobile/app/src/androidTest/java/com/company/template/auth/LoginScreenTest.kt‎

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.company.template.auth
22

33
import androidx.compose.ui.test.assertIsDisplayed
4-
import androidx.compose.ui.test.assertIsEnabled
5-
import androidx.compose.ui.test.assertIsNotEnabled
64
import androidx.compose.ui.test.junit4.createComposeRule
75
import androidx.compose.ui.test.onNodeWithText
86
import androidx.compose.ui.test.performClick
@@ -20,81 +18,62 @@ class LoginScreenTest {
2018
@get:Rule
2119
val composeTestRule = createComposeRule()
2220

23-
@Test
24-
fun loginScreen_displaysSignInHeading() {
21+
private fun setContent(
22+
uiState: AuthUiState = AuthUiState.Idle,
23+
onSignIn: (String, String) -> Unit = { _, _ -> },
24+
onSignInWithGoogle: () -> Unit = {},
25+
onNavigateToRegister: () -> Unit = {},
26+
onClearError: () -> Unit = {}
27+
) {
2528
composeTestRule.setContent {
2629
TemplateTheme {
2730
LoginScreen(
28-
uiState = AuthUiState.Idle,
29-
onSignIn = { _, _ -> },
30-
onNavigateToRegister = {},
31-
onClearError = {}
31+
uiState = uiState,
32+
onSignIn = onSignIn,
33+
onSignInWithGoogle = onSignInWithGoogle,
34+
onNavigateToRegister = onNavigateToRegister,
35+
onClearError = onClearError
3236
)
3337
}
3438
}
39+
}
40+
41+
@Test
42+
fun loginScreen_displaysSignInHeading() {
43+
setContent()
3544
composeTestRule.onNodeWithText("Sign In").assertIsDisplayed()
3645
}
3746

3847
@Test
39-
fun loginScreen_signInButtonDisabledWhenFieldsEmpty() {
40-
composeTestRule.setContent {
41-
TemplateTheme {
42-
LoginScreen(
43-
uiState = AuthUiState.Idle,
44-
onSignIn = { _, _ -> },
45-
onNavigateToRegister = {},
46-
onClearError = {}
47-
)
48-
}
49-
}
50-
// Button with text "Sign In" that is a Button (not the heading Text)
51-
composeTestRule.onNodeWithText("Sign In", useUnmergedTree = false)
52-
.assertIsDisplayed()
48+
fun loginScreen_displaysGoogleSignInButton() {
49+
setContent()
50+
composeTestRule.onNodeWithText("Continue with Google").assertIsDisplayed()
51+
}
52+
53+
@Test
54+
fun loginScreen_googleButton_invokesCallback() {
55+
var called = false
56+
setContent(onSignInWithGoogle = { called = true })
57+
composeTestRule.onNodeWithText("Continue with Google").performClick()
58+
assertTrue(called)
5359
}
5460

5561
@Test
5662
fun loginScreen_displaysErrorMessage() {
57-
composeTestRule.setContent {
58-
TemplateTheme {
59-
LoginScreen(
60-
uiState = AuthUiState.Error("Invalid credentials"),
61-
onSignIn = { _, _ -> },
62-
onNavigateToRegister = {},
63-
onClearError = {}
64-
)
65-
}
66-
}
63+
setContent(uiState = AuthUiState.Error("Invalid credentials"))
6764
composeTestRule.onNodeWithText("Invalid credentials").assertIsDisplayed()
6865
}
6966

7067
@Test
7168
fun loginScreen_displaysRegisterLink() {
72-
composeTestRule.setContent {
73-
TemplateTheme {
74-
LoginScreen(
75-
uiState = AuthUiState.Idle,
76-
onSignIn = { _, _ -> },
77-
onNavigateToRegister = {},
78-
onClearError = {}
79-
)
80-
}
81-
}
69+
setContent()
8270
composeTestRule.onNodeWithText("Register").assertIsDisplayed()
8371
}
8472

8573
@Test
8674
fun loginScreen_clickRegister_invokesCallback() {
8775
var navigateCalled = false
88-
composeTestRule.setContent {
89-
TemplateTheme {
90-
LoginScreen(
91-
uiState = AuthUiState.Idle,
92-
onSignIn = { _, _ -> },
93-
onNavigateToRegister = { navigateCalled = true },
94-
onClearError = {}
95-
)
96-
}
97-
}
76+
setContent(onNavigateToRegister = { navigateCalled = true })
9877
composeTestRule.onNodeWithText("Register").performClick()
9978
assertTrue(navigateCalled)
10079
}
@@ -103,19 +82,7 @@ class LoginScreenTest {
10382
fun loginScreen_typingEmailAndPassword_thenSignInInvoked() {
10483
var signInEmail = ""
10584
var signInPassword = ""
106-
composeTestRule.setContent {
107-
TemplateTheme {
108-
LoginScreen(
109-
uiState = AuthUiState.Idle,
110-
onSignIn = { e, p ->
111-
signInEmail = e
112-
signInPassword = p
113-
},
114-
onNavigateToRegister = {},
115-
onClearError = {}
116-
)
117-
}
118-
}
85+
setContent(onSignIn = { e, p -> signInEmail = e; signInPassword = p })
11986
composeTestRule.onNodeWithText("Email").performTextInput("test@example.com")
12087
composeTestRule.onNodeWithText("Password").performTextInput("secret123")
12188
composeTestRule.onNodeWithText("Sign In").performClick()

‎mobile/app/src/main/java/com/company/template/auth/LoginScreen.kt‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import androidx.compose.foundation.layout.fillMaxSize
88
import androidx.compose.foundation.layout.fillMaxWidth
99
import androidx.compose.foundation.layout.height
1010
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.layout.size
1112
import androidx.compose.foundation.text.KeyboardOptions
1213
import androidx.compose.material3.Button
1314
import androidx.compose.material3.CircularProgressIndicator
15+
import androidx.compose.material3.HorizontalDivider
16+
import androidx.compose.material3.Icon
1417
import androidx.compose.material3.MaterialTheme
18+
import androidx.compose.material3.OutlinedButton
1519
import androidx.compose.material3.OutlinedTextField
1620
import androidx.compose.material3.Text
1721
import androidx.compose.material3.TextButton
@@ -22,22 +26,83 @@ import androidx.compose.runtime.saveable.rememberSaveable
2226
import androidx.compose.runtime.setValue
2327
import androidx.compose.ui.Alignment
2428
import androidx.compose.ui.Modifier
29+
import androidx.compose.ui.graphics.Color
30+
import androidx.compose.ui.graphics.vector.ImageVector
31+
import androidx.compose.ui.graphics.vector.path
2532
import androidx.compose.ui.text.input.KeyboardType
2633
import androidx.compose.ui.text.input.PasswordVisualTransformation
2734
import androidx.compose.ui.tooling.preview.Preview
2835
import androidx.compose.ui.unit.dp
2936
import com.company.template.ui.theme.TemplateTheme
3037

38+
// Google "G" logo as a vector — colours from the brand guidelines
39+
private val GoogleIcon: ImageVector
40+
get() = ImageVector.Builder(
41+
name = "GoogleIcon",
42+
defaultWidth = 24.dp,
43+
defaultHeight = 24.dp,
44+
viewportWidth = 24f,
45+
viewportHeight = 24f
46+
).apply {
47+
path(fill = androidx.compose.ui.graphics.SolidColor(Color(0xFF4285F4))) {
48+
moveTo(22.56f, 12.25f)
49+
curveToRelative(0f, -0.78f, -0.07f, -1.53f, -0.2f, -2.25f)
50+
horizontalLineTo(12f)
51+
verticalLineToRelative(4.26f)
52+
horizontalLineToRelative(5.92f)
53+
curveToRelative(-0.26f, 1.37f, -1.04f, 2.53f, -2.21f, 3.31f)
54+
verticalLineToRelative(2.77f)
55+
horizontalLineToRelative(3.57f)
56+
curveToRelative(2.08f, -1.92f, 3.28f, -4.74f, 3.28f, -8.09f)
57+
close()
58+
}
59+
path(fill = androidx.compose.ui.graphics.SolidColor(Color(0xFF34A853))) {
60+
moveTo(12f, 23f)
61+
curveToRelative(2.97f, 0f, 5.46f, -0.98f, 7.28f, -2.66f)
62+
lineToRelative(-3.57f, -2.77f)
63+
curveToRelative(-0.98f, 0.66f, -2.23f, 1.06f, -3.71f, 1.06f)
64+
curveToRelative(-2.86f, 0f, -5.29f, -1.93f, -6.16f, -4.53f)
65+
horizontalLineTo(2.18f)
66+
verticalLineToRelative(2.84f)
67+
curveTo(3.99f, 20.53f, 7.7f, 23f, 12f, 23f)
68+
close()
69+
}
70+
path(fill = androidx.compose.ui.graphics.SolidColor(Color(0xFFFBBC05))) {
71+
moveTo(5.84f, 14.09f)
72+
curveToRelative(-0.22f, -0.66f, -0.35f, -1.36f, -0.35f, -2.09f)
73+
reflectiveCurveToRelative(0.13f, -1.43f, 0.35f, -2.09f)
74+
verticalLineTo(7.07f)
75+
horizontalLineTo(2.18f)
76+
curveTo(1.43f, 8.55f, 1f, 10.22f, 1f, 12f)
77+
reflectiveCurveToRelative(0.43f, 3.45f, 1.18f, 4.93f)
78+
lineToRelative(2.85f, -2.22f)
79+
lineToRelative(0.81f, -0.62f)
80+
close()
81+
}
82+
path(fill = androidx.compose.ui.graphics.SolidColor(Color(0xFFEA4335))) {
83+
moveTo(12f, 5.38f)
84+
curveToRelative(1.62f, 0f, 3.06f, 0.56f, 4.21f, 1.64f)
85+
lineToRelative(3.15f, -3.15f)
86+
curveTo(17.45f, 2.09f, 14.97f, 1f, 12f, 1f)
87+
curveTo(7.7f, 1f, 3.99f, 3.47f, 2.18f, 7.07f)
88+
lineToRelative(3.66f, 2.84f)
89+
curveToRelative(0.87f, -2.6f, 3.3f, -4.53f, 6.16f, -4.53f)
90+
close()
91+
}
92+
}.build()
93+
3194
@Composable
3295
fun LoginScreen(
3396
uiState: AuthUiState,
3497
onSignIn: (email: String, password: String) -> Unit,
98+
onSignInWithGoogle: () -> Unit,
3599
onNavigateToRegister: () -> Unit,
36100
onClearError: () -> Unit,
37101
modifier: Modifier = Modifier
38102
) {
39103
var email by rememberSaveable { mutableStateOf("") }
40104
var password by rememberSaveable { mutableStateOf("") }
105+
val isLoading = uiState is AuthUiState.Loading
41106

42107
Column(
43108
modifier = modifier
@@ -52,6 +117,34 @@ fun LoginScreen(
52117
color = MaterialTheme.colorScheme.primary
53118
)
54119
Spacer(modifier = Modifier.height(32.dp))
120+
OutlinedButton(
121+
onClick = onSignInWithGoogle,
122+
enabled = !isLoading,
123+
modifier = Modifier.fillMaxWidth()
124+
) {
125+
Icon(
126+
imageVector = GoogleIcon,
127+
contentDescription = null,
128+
tint = Color.Unspecified,
129+
modifier = Modifier.size(18.dp)
130+
)
131+
Spacer(modifier = Modifier.size(8.dp))
132+
Text(text = "Continue with Google")
133+
}
134+
Spacer(modifier = Modifier.height(20.dp))
135+
Row(
136+
verticalAlignment = Alignment.CenterVertically,
137+
modifier = Modifier.fillMaxWidth()
138+
) {
139+
HorizontalDivider(modifier = Modifier.weight(1f))
140+
Text(
141+
text = " or ",
142+
style = MaterialTheme.typography.bodySmall,
143+
color = MaterialTheme.colorScheme.outline
144+
)
145+
HorizontalDivider(modifier = Modifier.weight(1f))
146+
}
147+
Spacer(modifier = Modifier.height(20.dp))
55148
OutlinedTextField(
56149
value = email,
57150
onValueChange = {
@@ -85,7 +178,7 @@ fun LoginScreen(
85178
)
86179
}
87180
Spacer(modifier = Modifier.height(24.dp))
88-
if (uiState is AuthUiState.Loading) {
181+
if (isLoading) {
89182
CircularProgressIndicator()
90183
} else {
91184
Button(
@@ -120,6 +213,7 @@ fun LoginScreenPreview() {
120213
LoginScreen(
121214
uiState = AuthUiState.Idle,
122215
onSignIn = { _, _ -> },
216+
onSignInWithGoogle = {},
123217
onNavigateToRegister = {},
124218
onClearError = {}
125219
)
@@ -133,6 +227,7 @@ fun LoginScreenErrorPreview() {
133227
LoginScreen(
134228
uiState = AuthUiState.Error("Invalid credentials"),
135229
onSignIn = { _, _ -> },
230+
onSignInWithGoogle = {},
136231
onNavigateToRegister = {},
137232
onClearError = {}
138233
)

‎mobile/app/src/main/java/com/company/template/navigation/AppNavGraph.kt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.company.template.navigation
22

3+
import android.app.Activity
34
import androidx.compose.runtime.Composable
45
import androidx.compose.runtime.LaunchedEffect
56
import androidx.compose.runtime.getValue
67
import androidx.compose.ui.Modifier
8+
import androidx.compose.ui.platform.LocalContext
79
import androidx.lifecycle.compose.collectAsStateWithLifecycle
810
import androidx.navigation.NavHostController
911
import androidx.navigation.compose.NavHost
@@ -32,6 +34,7 @@ fun AppNavGraph(
3234
val startDestination by appViewModel.startDestination.collectAsStateWithLifecycle()
3335
val authUiState by authViewModel.uiState.collectAsStateWithLifecycle()
3436
val currentUser: FirebaseUser? by authViewModel.currentUser.collectAsStateWithLifecycle()
37+
val activity = LocalContext.current as Activity
3538

3639
// Navigate away from auth screens when the user successfully signs in
3740
LaunchedEffect(authUiState) {
@@ -67,9 +70,8 @@ fun AppNavGraph(
6770
LoginScreen(
6871
uiState = authUiState,
6972
onSignIn = { email, password -> authViewModel.signIn(email, password) },
70-
onNavigateToRegister = {
71-
navController.navigate(ROUTE_REGISTER)
72-
},
73+
onSignInWithGoogle = { authViewModel.signInWithGoogle(activity) },
74+
onNavigateToRegister = { navController.navigate(ROUTE_REGISTER) },
7375
onClearError = authViewModel::clearError
7476
)
7577
}

0 commit comments

Comments
 (0)