Skip to content

Commit

Permalink
Add progress indicator on GoogleLoginButton
Browse files Browse the repository at this point in the history
  • Loading branch information
jja08111 committed Jan 28, 2024
1 parent ec674da commit e87c662
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
14 changes: 11 additions & 3 deletions feature/auth/ui/src/main/java/io/foundy/auth/ui/LoginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.Status
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
Expand Down Expand Up @@ -61,7 +60,9 @@ fun LoginRoute(
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
val signInWithGoogleLauncher = rememberSignInWithGoogleLauncher(
onCancelled = { viewModel.setInProgressGoogleSignIn(false) },
onFailure = {
viewModel.setInProgressGoogleSignIn(false)
val message = context.getString(R.string.failed_to_sign_in, it.statusCode)
scope.launch {
snackbarHostState.showSnackbar(message)
Expand Down Expand Up @@ -90,6 +91,7 @@ fun LoginRoute(
.build()
val googleSignInClient = GoogleSignIn.getClient(context, options)
signInWithGoogleLauncher.launch(googleSignInClient.signInIntent)
viewModel.setInProgressGoogleSignIn(true)
}
)
}
Expand Down Expand Up @@ -126,19 +128,25 @@ fun LoginScreen(
)
)
Spacer(modifier = Modifier.height(36.dp))
GoogleLoginButton(onClick = onGoogleLoginClick)
GoogleLoginButton(
showProgressIndicator = uiState.inProgressGoogleSignIn,
enabled = !uiState.inProgressGoogleSignIn,
onClick = onGoogleLoginClick
)
}
}
}

@Composable
private fun rememberSignInWithGoogleLauncher(
onCancelled: () -> Unit,
onFailure: (ApiException) -> Unit
): ManagedActivityResultLauncher<Intent, ActivityResult> {
val startActivityForResult = ActivityResultContracts.StartActivityForResult()
return rememberLauncherForActivityResult(startActivityForResult) remember@{ result ->
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
if (task.isSuccessful.not()) {
onCancelled()
return@remember
}
try {
Expand All @@ -156,7 +164,7 @@ private fun rememberSignInWithGoogleLauncher(
@Composable
fun LoginScreenPreview() {
LoginScreen(
LoginUiState,
LoginUiState(),
snackbarHostState = SnackbarHostState(),
onGoogleLoginClick = {}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package io.foundy.auth.ui

object LoginUiState
data class LoginUiState(
val inProgressGoogleSignIn: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.foundy.auth.domain.usecase.GetCurrentUserIdStreamUseCase
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
import org.orbitmvi.orbit.syntax.simple.reduce
import org.orbitmvi.orbit.viewmodel.container
import javax.inject.Inject

Expand All @@ -16,7 +17,7 @@ class LoginViewModel @Inject constructor(
private val existsInitInfoUseCase: ExistsInitInfoUseCase
) : ViewModel(), ContainerHost<LoginUiState, LoginSideEffect> {

override val container = container<LoginUiState, LoginSideEffect>(LoginUiState)
override val container = container<LoginUiState, LoginSideEffect>(LoginUiState())

init {
intent {
Expand All @@ -41,4 +42,10 @@ class LoginViewModel @Inject constructor(
}
}
}

fun setInProgressGoogleSignIn(inProgress: Boolean) = intent {
reduce {
state.copy(inProgressGoogleSignIn = inProgress)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.foundy.auth.ui.componenet

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
Expand All @@ -9,9 +10,12 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -28,7 +32,8 @@ private val ButtonShape = RoundedCornerShape(6.dp)
@Composable
fun GoogleLoginButton(
onClick: () -> Unit,
enabled: Boolean = true
enabled: Boolean = true,
showProgressIndicator: Boolean = false,
) {
Box(
modifier = Modifier
Expand All @@ -52,6 +57,12 @@ fun GoogleLoginButton(
modifier = Modifier.align(Alignment.Center),
verticalAlignment = Alignment.CenterVertically
) {
AnimatedVisibility(visible = showProgressIndicator) {
Row {
CircularProgressIndicator(modifier = Modifier.size(20.dp))
Spacer(modifier = Modifier.width(12.dp))
}
}
Image(
painter = painterResource(id = R.drawable.mark_google),
contentDescription = null
Expand All @@ -72,6 +83,7 @@ fun GoogleLoginButton(
fun GoogleLoginButtonPreview() {
CamstudyTheme {
GoogleLoginButton(
showProgressIndicator = true,
onClick = {}
)
}
Expand Down

0 comments on commit e87c662

Please sign in to comment.