DEV Community

Cover image for Building DotShredzilla (Part 1): The Best Sign-In Screen Is the One You See Once
freerave
freerave

Posted on

Building DotShredzilla (Part 1): The Best Sign-In Screen Is the One You See Once

A sign-in screen has an odd job. It's the first thing every new user sees, and the thing a returning user should never see again. Someone opening a workout tracker between sets wants their training, not a login form.

So before redesigning anything, I rebuilt what sits between launching DotShredzilla and seeing your training: signing in, deciding which screen to open, and loading your history.

DotShredzilla is an Android workout tracker from FreeRave (DotSuite), written in Kotlin with Jetpack Compose and Firebase, and open source under Apache License 2.0: github.com/kareem2099/Shredzilla.

One sheet for every way to sign in

The app used the old Google Sign-In SDK, which Google has deprecated. It also needed an ActivityResultLauncher in MainActivity, so the activity, the launcher callback and the navigation code all had to know about each other.

I moved to Credential Manager (androidx.credentials). It shows one system bottom sheet that offers both the user's Google accounts and the passwords saved for the app:

val googleIdOption = GetGoogleIdOption.Builder()
    .setFilterByAuthorizedAccounts(filterByAuthorizedAccounts)
    .setServerClientId(activity.getString(R.string.default_web_client_id))
    .setAutoSelectEnabled(filterByAuthorizedAccounts)
    .build()

val request = GetCredentialRequest.Builder()
    .addCredentialOption(googleIdOption)
    .addCredentialOption(GetPasswordOption(isAutoSelectAllowed = filterByAuthorizedAccounts))
    .build()
Enter fullscreen mode Exit fullscreen mode

Sign-in tries twice, and the order matters:

  1. Returning users first. Only accounts that already used the app are offered, and if there is exactly one, it's picked automatically. For most people, signing in again is one tap or none.
  2. Everyone else. If nothing was found (NoCredentialException), the same request runs again with every Google account on the phone.

Whatever the user picks, the result becomes a Firebase sign-in. A Google ID token goes through GoogleAuthProvider. A saved password goes through Firebase's email and password sign-in, so the sheet works for accounts that never used Google at all.

The best part was what disappeared. Sign-in is now one function that a coroutine can call from anywhere:

suspend fun signInWithGoogle(): Result<FirebaseUser>
Enter fullscreen mode Exit fullscreen mode

No launcher, no activity callback, and no navigation code inside MainActivity. Closing the sheet comes back as a failure the UI quietly ignores, because cancelling isn't an error worth a toast.

Two taps, two requests

On a slow connection, tapping "Create account" twice sent two sign-up requests. The first created the account. The second failed with "email already in use", so a brand-new user saw an error about the account they had just made.

The fix is a lock that's set before anything asynchronous starts:

onRegisterClick = { name, email, password, _ ->
    if (isLoading) return@RegisterScreen  // double-tap guard
    isLoading = true                      // set synchronously, before launch
    scope.launch {
        val result = auth.createUser(name, email, password)
        // navigate or show the error
        isLoading = false
    }
}
Enter fullscreen mode Exit fullscreen mode

The order is the whole point. Setting isLoading inside the coroutine leaves a gap where a second tap still gets through. Set on the main thread, in the click handler itself, the second tap sees the lock immediately. The same flag disables the fields and swaps the button for a progress indicator, so the screen also shows that something is happening.

Opening on the right screen, without a flicker

Every launch has to answer one question: where does this person belong? The answer can be the login screen, the first unfinished onboarding step, or the main app.

The old code answered it twice, in MainActivity and again in the navigation file. The app always started on the login screen and then jumped elsewhere from a LaunchedEffect. You could see the login screen flash before the jump, and the back stack sometimes kept screens that shouldn't be there.

Now the MainViewModel works out the destination once, and the navigation graph starts there instead of jumping:

// MainViewModel
val startDestinationFlow = savedStateHandle.getStateFlow<String?>("startDestination", null)

// MainActivity
val startDestination by mainViewModel.startDestinationFlow.collectAsState()
startDestination?.let { AppNavigationHost(navController, startDestination = it, ...) }
Enter fullscreen mode Exit fullscreen mode

Keeping it in a SavedStateHandle means the decision survives process death. If Android kills the app in the background, it comes back where it was instead of running the whole check again. On sign-out the saved destination goes back to the login screen, so a restored process can't land a signed-out user inside the app.

Loading history in parallel

Exercise history and the progress charts need the last 90 days of logged sets. Firestore stores them per day, so loading meant one request for the list of days and then one request per day, one after another. On a weak mobile connection that was 90 round trips in a row.

The requests don't depend on each other, so they can run at the same time:

val sets = days.map { dayDoc ->
    async(Dispatchers.IO) { dayDoc.reference.collection("recordedSets").get().await() }
}.awaitAll()
Enter fullscreen mode Exit fullscreen mode

The chart calculations then run on Dispatchers.Default, so neither the network nor the math touches the main thread.

A small cleanup with a big effect

The login and register screens each had their own copy of the text field colors, slightly different from each other. Changing the accent color meant finding both. They now share one definition.

That was a two-minute change, but it turned into a rule for the next part of this series: no screen gets its own private copy of a component.

What I'd tell myself before touching sign-in

  • Make sign-in a suspend function that returns a Result. The UI and navigation get much simpler when they don't have to know about activity callbacks.
  • Lock a button in the click handler, not inside the coroutine it starts.
  • Decide the start destination once, in one place, and start there. Don't start somewhere else and jump.
  • Test on a slow connection. Every bug in this part only showed up there.

Sign-in wasn't finished with me, though. Much later, users who had signed in once were sent back to the login screen every time they opened the app, and none of the code in this article was the cause. That story comes later in the series.

Part 2 covers what came next: rebuilding every screen around one glass design system with two palettes.

How does your app decide which screen to open on launch?

Google Play: DotShredzilla

Source: github.com/kareem2099/Shredzilla

Project: DotSuite

Top comments (0)