Composables
Docs Login
Pairel

Profile details

Jetpack Compose Screen

Download
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.ColumnScope
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.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.width
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.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.compose.ripple.rememberRippleIndication
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.ChevronRight
import com.composables.pairel.theme.Icons
import com.composables.pairel.theme.Theme

internal const val YourBio =
    "Weekend hiker, weekday tea person, and always up for trying a new restaurant."
internal const val YourHeightCm = 180
internal val yourInterests = interests("Hiking", "Dogs", "Tea", "Bookshops")

private val yourPhotos =
    listOf(
        Photo.Remote(YourPhotoUrl),
        Photo.Remote(
            "https://images.unsplash.com/photo-1527631746610-bca00a040d60?fit=crop&w=600&h=750&q=85"
        ),
        Photo.Remote(
            "https://images.unsplash.com/photo-1551632811-561732d1e306?fit=crop&w=600&h=750&q=85"
        ),
        Photo.Remote(
            "https://images.unsplash.com/photo-1543466835-00a7907e9de1?fit=crop&w=600&h=750&q=85"
        ),
    )

@Composable
fun ProfileDetailsScreen(
    navigateBack: () -> Unit,
    navigateToEditBio: () -> Unit,
    navigateToEditHeight: () -> Unit,
    navigateToEditInterests: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val photos = remember { mutableStateListOf<Photo>(*yourPhotos.toTypedArray()) }

    ScreenScaffold(
        modifier = modifier,
        topBar = {
            CenteredAppBar(
                title = { Text("Profile details") },
                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(32.dp),
            ) {
                Text(
                    "Ethan, 31",
                    style = Theme.typography.headline.copy(fontSize = 28.sp, lineHeight = 34.sp),
                )

                Section(
                    title = "Photos",
                    description = "Pick a few that show what your days look like.",
                ) {
                    EditablePhotoGrid(photos = photos)
                }

                Section(
                    title = "About you",
                    description = "Help people find things to talk about.",
                ) {
                    Column(
                        Modifier.fillMaxWidth()
                            .clip(Theme.shapes.large)
                            .background(Theme.colors.surface)
                    ) {
                        DetailRow(label = "Bio", value = YourBio, onClick = navigateToEditBio)
                        Separator(
                            SeparatorOrientation.Horizontal,
                            Modifier.padding(horizontal = 20.dp),
                        )
                        DetailRow(
                            label = "Height",
                            value = formatHeight(YourHeightCm),
                            onClick = navigateToEditHeight,
                        )
                        Separator(
                            SeparatorOrientation.Horizontal,
                            Modifier.padding(horizontal = 20.dp),
                        )
                        DetailRow(
                            label = "Interests",
                            value = yourInterests.joinToString(", ") { "${it.emoji} ${it.label}" },
                            onClick = navigateToEditInterests,
                        )
                    }
                }
            }
        }
    }
}

internal fun formatHeight(cm: Int): String {
    val inches = (cm / 2.54).let { kotlin.math.round(it).toInt() }
    return "${inches / 12}'${inches % 12}\" ยท $cm cm"
}

@Composable
private fun Section(
    title: String,
    description: String,
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit,
) {
    Column(modifier, verticalArrangement = Arrangement.spacedBy(12.dp)) {
        Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
            Text(title, style = Theme.typography.title)
            Text(description, color = Theme.colors.onBackgroundMuted)
        }
        content()
    }
}

@Composable
private fun DetailRow(
    label: String,
    value: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Row(
        modifier =
            modifier
                .fillMaxWidth()
                .clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = rememberRippleIndication(color = Theme.colors.onSurface),
                    onClickLabel = "Edit ${label.lowercase()}",
                    onClick = onClick,
                )
                .padding(horizontal = 20.dp, vertical = 16.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) {
            Text(label, style = Theme.typography.label, color = Theme.colors.onSurface)
            Text(
                value,
                color = Theme.colors.onBackgroundMuted,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
            )
        }
        Spacer(Modifier.width(12.dp))
        Icon(
            Icons.ChevronRight,
            contentDescription = null,
            tint = Theme.colors.onBackgroundMuted,
            modifier = Modifier.size(20.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.