Composables
Docs Login
Pairel

Account

package com.composables.pairel

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
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.navigationBars
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.union
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.components.AlertDialog
import com.composables.pairel.components.Button
import com.composables.pairel.components.CenteredAppBar
import com.composables.pairel.components.Icon
import com.composables.pairel.components.IconButton
import com.composables.pairel.components.ScreenScaffold
import com.composables.pairel.components.Separator
import com.composables.pairel.components.SeparatorOrientation
import com.composables.pairel.components.Text
import com.composables.pairel.theme.ArrowLeft
import com.composables.pairel.theme.Icons
import com.composables.pairel.theme.Theme

@Composable
fun AccountManagementScreen(
    navigateBack: () -> Unit,
    navigateToJoin: () -> Unit,
    modifier: Modifier = Modifier,
) {
    fun signOut() {
        // sign out here
        navigateToJoin()
    }

    fun deleteAccount() {
        // delete the account here
        navigateToJoin()
    }

    var confirmingDelete by remember { mutableStateOf(false) }

    ScreenScaffold(
        modifier = modifier,
        topBar = {
            CenteredAppBar(
                title = { Text("Account") },
                navigation = {
                    IconButton(onClick = navigateBack) {
                        Icon(
                            Icons.ArrowLeft,
                            contentDescription = "Back",
                            modifier = Modifier.size(24.dp),
                        )
                    }
                },
            )
        },
    ) {
        Column(
            Modifier.fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(top = 8.dp, bottom = 24.dp)
                .windowInsetsPadding(
                    WindowInsets.navigationBars.union(
                        WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal)
                    )
                )
                .padding(horizontal = 20.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Column(
                Modifier.widthIn(max = 560.dp).fillMaxWidth(),
                verticalArrangement = Arrangement.spacedBy(24.dp),
            ) {
                AccountGroup {
                    AccountDetail(label = "Phone number", value = "+1 (718) 555-0142")
                    Separator(SeparatorOrientation.Horizontal, Modifier.padding(horizontal = 20.dp))
                    AccountDetail(label = "Name", value = "Ethan")
                    Separator(SeparatorOrientation.Horizontal, Modifier.padding(horizontal = 20.dp))
                    AccountDetail(label = "Birthday", value = "March 14, 1997")
                }
                AccountGroup {
                    AccountAction(
                        label = "Sign out",
                        color = Theme.colors.onSurface,
                        onClick = ::signOut,
                    )
                    Separator(SeparatorOrientation.Horizontal, Modifier.padding(horizontal = 20.dp))
                    AccountAction(
                        label = "Delete account",
                        color = Theme.colors.destructive,
                        onClick = { confirmingDelete = true },
                    )
                }
            }
        }

        AlertDialog(
            visible = confirmingDelete,
            title = "Delete your account?",
            onDismissRequest = { confirmingDelete = false },
            body = {
                Text(
                    "Your profile, matches, and conversations will be removed. This can't be undone."
                )
            },
            positiveAction = {
                Button(onClick = { confirmingDelete = false }, modifier = Modifier.fillMaxWidth()) {
                    Text("Keep my account")
                }
            },
            negativeAction = {
                AccountAction(
                    label = "Delete account",
                    color = Theme.colors.destructive,
                    textAlign = TextAlign.Center,
                    onClick = {
                        confirmingDelete = false
                        deleteAccount()
                    },
                    modifier = Modifier.clip(Theme.shapes.large),
                )
            },
        )
    }
}

@Composable
private fun AccountGroup(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
    Column(modifier.fillMaxWidth().clip(Theme.shapes.large).background(Theme.colors.surface)) {
        content()
    }
}

@Composable
private fun AccountDetail(label: String, value: String, modifier: Modifier = Modifier) {
    Row(
        modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 16.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Text(
            label,
            style = Theme.typography.label,
            color = Theme.colors.onSurface,
            modifier = Modifier.weight(1f),
        )
        Text(value, color = Theme.colors.onBackgroundMuted)
    }
}

@Composable
private fun AccountAction(
    label: String,
    color: Color,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    textAlign: TextAlign = TextAlign.Start,
) {
    Text(
        label,
        style = Theme.typography.label.copy(textAlign = textAlign),
        color = color,
        modifier =
            modifier
                .fillMaxWidth()
                .clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = rememberRippleIndication(color = color),
                    onClick = onClick,
                )
                .padding(horizontal = 20.dp, vertical = 16.dp),
    )
}

More screens from Pairel

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.