Composables
Docs Login
Vela

Check your email

package com.composables.vela

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.Spacer
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.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.ime
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.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink
import androidx.compose.ui.unit.dp
import com.composables.vela.components.Button
import com.composables.vela.components.ButtonVariant
import com.composables.vela.components.CodeField
import com.composables.vela.components.Icon
import com.composables.vela.components.Page
import com.composables.vela.components.ScreenScaffold
import com.composables.vela.components.Text
import com.composables.vela.components.TextField
import com.composables.vela.theme.AppleLogo
import com.composables.vela.theme.EnvelopeSimple
import com.composables.vela.theme.GoogleLogo
import com.composables.vela.theme.Icons
import com.composables.vela.theme.Space
import com.composables.vela.theme.Theme
import com.composables.vela.theme.VelaMark
import kotlinx.coroutines.delay

@Composable
fun WelcomeScreen(
    navigateHome: () -> Unit,
    navigateToCheckEmail: (email: String) -> Unit,
    navigateToTerms: () -> Unit,
    navigateToPrivacyPolicy: () -> Unit,
    modifier: Modifier = Modifier,
) {
    var email by remember { mutableStateOf("") }

    fun signInWithApple() {
        // sign in with Apple here
        navigateHome()
    }

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

    fun continueWithEmail() {
        // send a sign-in code to the email here
        navigateToCheckEmail(email.trim())
    }

    val validEmail = email.trim().let { it.contains('@') && it.substringAfter('@').contains('.') }
    ScreenScaffold(modifier) {
        BoxWithConstraints(
            Modifier.fillMaxSize()
                .windowInsetsPadding(
                    WindowInsets.systemBars
                        .union(WindowInsets.ime)
                        .union(WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal))
                )
        ) {
            Column(
                Modifier.fillMaxSize()
                    .verticalScroll(rememberScrollState())
                    .heightIn(min = maxHeight)
                    .padding(horizontal = Space * 6, vertical = Space * 6),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.SpaceBetween,
            ) {
                Spacer(Modifier.height(0.dp))
                Column(
                    Modifier.padding(vertical = Space * 10),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(Space * 4),
                ) {
                    Icon(
                        Icons.VelaMark,
                        contentDescription = null,
                        modifier = Modifier.size(56.dp),
                        tint = Theme.colors.primary,
                    )
                    Text("Vela", style = Theme.typography.display)
                    Text(
                        "A calm place to think things through.",
                        color = Theme.colors.onBackgroundMuted,
                        style = Theme.typography.body.copy(textAlign = TextAlign.Center),
                    )
                }
                Column(
                    Modifier.widthIn(max = 420.dp).fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(Space * 3),
                ) {
                    SignInButton("Continue with Apple", Icons.AppleLogo, ::signInWithApple)
                    SignInButton("Continue with Google", Icons.GoogleLogo, ::signInWithGoogle)
                    Row(
                        Modifier.padding(vertical = Space * 2),
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.spacedBy(Space * 3),
                    ) {
                        Box(Modifier.weight(1f).height(1.dp).background(Theme.colors.border))
                        Text(
                            "or",
                            style = Theme.typography.caption,
                            color = Theme.colors.onBackgroundMuted,
                        )
                        Box(Modifier.weight(1f).height(1.dp).background(Theme.colors.border))
                    }
                    TextField(email, { email = it }, label = "Email")
                    Button(
                        onClick = ::continueWithEmail,
                        enabled = validEmail,
                        modifier = Modifier.fillMaxWidth(),
                    ) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.spacedBy(Space * 2),
                        ) {
                            Icon(
                                Icons.EnvelopeSimple,
                                contentDescription = null,
                                modifier = Modifier.size(18.dp),
                            )
                            Text("Continue with email")
                        }
                    }
                    val links =
                        TextLinkStyles(
                            style =
                                SpanStyle(
                                    color = Theme.colors.primary,
                                    textDecoration = TextDecoration.Underline,
                                )
                        )
                    BasicText(
                        text =
                            buildAnnotatedString {
                                append("By continuing, you agree to Vela's ")
                                withLink(
                                    LinkAnnotation.Clickable("terms", links) { navigateToTerms() }
                                ) {
                                    append("Terms")
                                }
                                append(" and ")
                                withLink(
                                    LinkAnnotation.Clickable("privacy", links) {
                                        navigateToPrivacyPolicy()
                                    }
                                ) {
                                    append("Privacy Policy")
                                }
                                append(".")
                            },
                        style =
                            Theme.typography.caption.copy(
                                color = Theme.colors.onBackgroundMuted,
                                textAlign = TextAlign.Center,
                            ),
                        modifier = Modifier.fillMaxWidth().padding(top = Space * 2),
                    )
                }
            }
        }
    }
}

@Composable
private fun SignInButton(
    label: String,
    icon: androidx.compose.ui.graphics.vector.ImageVector,
    onClick: () -> Unit,
) {
    Button(
        onClick = onClick,
        variant = ButtonVariant.Secondary,
        modifier = Modifier.fillMaxWidth(),
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(Space * 2),
        ) {
            Icon(icon, contentDescription = null, modifier = Modifier.size(18.dp))
            Text(label)
        }
    }
}

private const val ResendSeconds = 30

@Composable
fun CheckEmailScreen(
    navigateBack: () -> Unit,
    navigateHome: () -> Unit,
    modifier: Modifier = Modifier,
    email: String = Account.email,
) {
    fun verifyCode(code: String) {
        // verify the code here
        navigateHome()
    }

    fun resendCode() {
        // send a new code here
    }

    var code by remember { mutableStateOf("") }
    var secondsLeft by remember { mutableIntStateOf(ResendSeconds) }
    var resends by remember { mutableIntStateOf(0) }
    LaunchedEffect(resends) {
        secondsLeft = ResendSeconds
        while (secondsLeft > 0) {
            delay(1000)
            secondsLeft--
        }
    }
    LaunchedEffect(code) { if (code.length == 6) verifyCode(code) }
    Page(title = "", onBack = navigateBack, modifier = modifier) {
        Column(verticalArrangement = Arrangement.spacedBy(Space * 3)) {
            Text("Check your email", style = Theme.typography.display)
            Text(
                "We sent a 6-digit code to $email. It expires in 10 minutes.",
                color = Theme.colors.onBackgroundMuted,
            )
        }
        CodeField(code, { code = it })
        if (secondsLeft > 0) {
            Text(
                "You can ask for a new code in 0:${secondsLeft.toString().padStart(2, '0')}",
                style = Theme.typography.caption,
                color = Theme.colors.onBackgroundMuted,
            )
        } else {
            Button(
                onClick = {
                    code = ""
                    resends++
                    resendCode()
                },
                variant = ButtonVariant.Ghost,
            ) {
                Text("Send a new code")
            }
        }
    }
}

More screens from Vela

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.