Composables
Docs Login
Tally

Welcome

package com.composables.tally

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.tally.components.Button
import com.composables.tally.components.ButtonVariant
import com.composables.tally.components.Icon
import com.composables.tally.components.ScreenScaffold
import com.composables.tally.components.Text
import com.composables.tally.theme.AppleLogo
import com.composables.tally.theme.Bell
import com.composables.tally.theme.Check
import com.composables.tally.theme.Flame
import com.composables.tally.theme.GoogleLogo
import com.composables.tally.theme.Icons
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme
import kotlin.math.floor

private class WelcomePage(
    val title: String,
    val body: String,
    val icon: ImageVector,
    val color: @Composable () -> Color,
    val accent: @Composable () -> Color,
)

private val pages =
    listOf(
        WelcomePage(
            "Welcome to Tally",
            "Small habits, one mark at a time. Check them off as your day goes.",
            Icons.Check,
            { Theme.colors.amberLight },
            { Theme.colors.amber },
        ),
        WelcomePage(
            "Streaks that stick",
            "Every day you show up adds to your streak. Watch it grow.",
            Icons.Flame,
            { Theme.colors.emberLight },
            { Theme.colors.ember },
        ),
        WelcomePage(
            "Gentle reminders",
            "Pick a time for each habit and Tally nudges you when it's due.",
            Icons.Bell,
            { Theme.colors.skyLight },
            { Theme.colors.sky },
        ),
    )

@Composable
fun WelcomeScreen(
    navigateHome: () -> Unit,
    navigateToTerms: () -> Unit,
    navigateToPrivacyPolicy: () -> Unit,
    modifier: Modifier = Modifier,
) {
    fun continueWithApple() {
        // sign in with Apple here
        navigateHome()
    }

    fun continueWithGoogle() {
        // sign in with Google here
        navigateHome()
    }

    fun continueAsGuest() {
        // start a guest session here
        navigateHome()
    }

    val pager = rememberPagerState { pages.size }
    ScreenScaffold(modifier) {
        BoxWithConstraints(
            Modifier.fillMaxSize()
                .windowInsetsPadding(
                    WindowInsets.systemBars.union(
                        WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal)
                    )
                )
        ) {
            val landscape = maxWidth > maxHeight
            Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) {
                Box(
                    Modifier.fillMaxWidth().padding(Space * 2),
                    contentAlignment = Alignment.CenterEnd,
                ) {
                    Button(onClick = ::continueAsGuest, variant = ButtonVariant.Ghost) {
                        Text("Continue as guest")
                    }
                }
                val actions =
                    @Composable {
                        SignInActions(
                            onApple = ::continueWithApple,
                            onGoogle = ::continueWithGoogle,
                            onTerms = navigateToTerms,
                            onPrivacyPolicy = navigateToPrivacyPolicy,
                        )
                    }
                if (landscape)
                    Row(
                        Modifier.weight(1f).fillMaxWidth(),
                        verticalAlignment = Alignment.CenterVertically,
                    ) {
                        Column(
                            Modifier.weight(1f),
                            horizontalAlignment = Alignment.CenterHorizontally,
                        ) {
                            Pages(pager, compact = true, Modifier.weight(1f))
                            PageDots(pager)
                        }
                        Box(Modifier.weight(1f), contentAlignment = Alignment.Center) { actions() }
                    }
                else {
                    Pages(pager, compact = false, Modifier.weight(1f))
                    PageDots(pager)
                    actions()
                }
            }
        }
    }
}

@Composable
private fun Pages(pager: PagerState, compact: Boolean, modifier: Modifier = Modifier) {
    HorizontalPager(pager, modifier.fillMaxWidth()) { index ->
        val page = pages[index]
        Column(
            Modifier.fillMaxSize().padding(horizontal = Space * 8),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement =
                Arrangement.spacedBy(Space * if (compact) 2 else 4, Alignment.CenterVertically),
        ) {
            Illustration(page.icon, page.color(), page.accent(), if (compact) 120.dp else 220.dp)
            Text(
                page.title,
                style = if (compact) Theme.typography.title else Theme.typography.display,
                textAlign = TextAlign.Center,
            )
            Text(
                page.body,
                color = Theme.colors.onBackgroundMuted,
                textAlign = TextAlign.Center,
                modifier = Modifier.widthIn(max = 320.dp),
            )
        }
    }
}

@Composable
private fun PageDots(pager: PagerState) {
    val dot = 8.dp
    val gap = Space * 2
    Box(
        Modifier.padding(vertical = Space * 4).clearAndSetSemantics {
            contentDescription = "Page ${pager.currentPage + 1} of ${pages.size}"
        }
    ) {
        Row(horizontalArrangement = Arrangement.spacedBy(gap)) {
            repeat(pages.size) {
                Box(
                    Modifier.size(dot)
                        .clip(Theme.shapes.fullyRounded)
                        .background(Theme.colors.border)
                )
            }
        }
        // Between two pages the dot stretches to reach the next one, then lets go of the last.
        val position =
            (pager.currentPage + pager.currentPageOffsetFraction).coerceIn(
                0f,
                pages.lastIndex.toFloat(),
            )
        val page = floor(position)
        val progress = position - page
        val step = dot + gap
        val start = step * (page + (progress * 2 - 1).coerceAtLeast(0f))
        val end = step * (page + (progress * 2).coerceAtMost(1f)) + dot
        Box(
            Modifier.offset(x = start)
                .size(width = end - start, height = dot)
                .clip(Theme.shapes.fullyRounded)
                .background(Theme.colors.primary)
        )
    }
}

@Composable
private fun SignInActions(
    onApple: () -> Unit,
    onGoogle: () -> Unit,
    onTerms: () -> Unit,
    onPrivacyPolicy: () -> Unit,
) {
    Column(
        Modifier.widthIn(max = 420.dp).fillMaxWidth().padding(horizontal = Space * 4),
        verticalArrangement = Arrangement.spacedBy(Space * 3),
    ) {
        Button(onClick = onApple, modifier = Modifier.fillMaxWidth()) {
            SignInLabel(Icons.AppleLogo, "Continue with Apple")
        }
        Button(
            onClick = onGoogle,
            variant = ButtonVariant.Secondary,
            modifier = Modifier.fillMaxWidth(),
        ) {
            SignInLabel(Icons.GoogleLogo, "Continue with Google")
        }
        Row(
            Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Button(onClick = onTerms, variant = ButtonVariant.Ghost) { Text("Terms") }
            Button(onClick = onPrivacyPolicy, variant = ButtonVariant.Ghost) {
                Text("Privacy policy")
            }
        }
    }
}

@Composable
private fun SignInLabel(icon: ImageVector, label: String) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(Space * 2),
    ) {
        Icon(icon, contentDescription = null, modifier = Modifier.size(18.dp))
        Text(label)
    }
}

@Composable
private fun Illustration(icon: ImageVector, color: Color, accent: Color, size: Dp) {
    val unit = size / 220
    Box(Modifier.size(size), contentAlignment = Alignment.Center) {
        Dot(color, unit * 200)
        Dot(
            accent,
            unit * 28,
            Modifier.align(Alignment.TopEnd).offset(x = -unit * 16, y = unit * 24),
        )
        Dot(
            color,
            unit * 18,
            Modifier.align(Alignment.BottomStart).offset(x = unit * 20, y = -unit * 28),
        )
        Box(
            Modifier.size(unit * 104).clip(Theme.shapes.fullyRounded).background(accent),
            contentAlignment = Alignment.Center,
        ) {
            Icon(
                icon,
                contentDescription = null,
                modifier = Modifier.size(unit * 52),
                tint = Theme.colors.onBackground,
            )
        }
    }
}

@Composable
private fun Dot(color: Color, size: Dp, modifier: Modifier = Modifier) {
    Box(modifier.size(size).clip(Theme.shapes.fullyRounded).background(color))
}

More screens from Tally

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.