Composables
Docs Login
Tally

Habit Row

Jetpack Compose Component

Download
package com.composables.tally.components

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.tally.theme.Check
import com.composables.tally.theme.Flame
import com.composables.tally.theme.Icons
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme
import com.composeunstyled.UnstyledButton

@Composable
fun HabitRow(
    name: String,
    count: Int,
    goal: Int,
    unit: String,
    streak: Int,
    color: Color,
    onCheckIn: () -> Unit,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val done = count >= goal
    UnstyledButton(
        onClick = onClick,
        modifier = modifier.fillMaxWidth().heightIn(min = 64.dp),
        contentPadding =
            PaddingValues(start = Space * 2, end = Space * 4, top = Space * 2, bottom = Space * 2),
        indication = rememberRippleIndication(color = Theme.colors.onSurface),
        interactionSource = remember { MutableInteractionSource() },
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(Space * 2),
        ) {
            UnstyledButton(
                onClick = onCheckIn,
                modifier =
                    Modifier.size(48.dp).semantics {
                        contentDescription = if (done) "Undo $name" else "Check in $name"
                        stateDescription = "$count of $goal"
                    },
                contentAlignment = Alignment.Center,
                indication =
                    rememberRippleIndication(color = color, bounded = false, radius = 24.dp),
                interactionSource = remember { MutableInteractionSource() },
            ) {
                CheckRing(progress = count.toFloat() / goal, color = color)
            }
            Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(Space * 0.5f)) {
                Text(
                    name,
                    style =
                        Theme.typography.body.copy(
                            textDecoration = if (done) TextDecoration.LineThrough else null
                        ),
                    color = if (done) Theme.colors.onBackgroundMuted else Theme.colors.onSurface,
                )
                Text(
                    "$count / $goal $unit",
                    style = Theme.typography.caption,
                    color = Theme.colors.onBackgroundMuted,
                )
            }
            if (streak > 0) {
                Row(
                    Modifier.semantics(mergeDescendants = true) {
                        contentDescription = "$streak day streak"
                    },
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(Space),
                ) {
                    Icon(
                        Icons.Flame,
                        contentDescription = null,
                        modifier = Modifier.size(16.dp),
                        tint = Theme.colors.onBackgroundMuted,
                    )
                    Text(
                        streak.toString(),
                        style = Theme.typography.label,
                        color = Theme.colors.onBackgroundMuted,
                    )
                }
            }
        }
    }
}

@Composable
private fun CheckRing(progress: Float, color: Color) {
    val animated by animateFloatAsState(progress.coerceIn(0f, 1f))
    val track = Theme.colors.border
    Box(Modifier.size(30.dp), contentAlignment = Alignment.Center) {
        Canvas(Modifier.fillMaxSize()) {
            val stroke = 3.dp.toPx()
            if (animated >= 1f) {
                drawCircle(color)
            } else {
                val inset = stroke / 2
                val arcSize = Size(size.width - stroke, size.height - stroke)
                drawArc(
                    track,
                    0f,
                    360f,
                    false,
                    Offset(inset, inset),
                    arcSize,
                    style = Stroke(stroke),
                )
                if (animated > 0f)
                    drawArc(
                        color,
                        -90f,
                        360f * animated,
                        false,
                        Offset(inset, inset),
                        arcSize,
                        style = Stroke(stroke, cap = StrokeCap.Round),
                    )
            }
        }
        if (animated >= 1f)
            Icon(
                Icons.Check,
                contentDescription = null,
                modifier = Modifier.size(16.dp),
                tint = Theme.colors.onBackground,
            )
    }
}

More components 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.