Refactor HomeScreen and add ManageActivitiesScreen: streamline navigation, enhance UI layout, and implement habit management features

This commit is contained in:
bobbert committed 2026-03-05 16:59:31 +00:00
1 parent 849ed06fb1
commit 7f92507ab7
4 files changed
+315 -313

No files matched your search

@@ -18,7 +18,7 @@ import com.example.madwellbeingapp.ui.screens.AllActivitiesScreen
import com.example.madwellbeingapp.ui.screens.CalendarScreen import com.example.madwellbeingapp.ui.screens.CalendarScreen
import com.example.madwellbeingapp.ui.screens.HabitDetailScreen import com.example.madwellbeingapp.ui.screens.HabitDetailScreen
import com.example.madwellbeingapp.ui.screens.HomeScreen import com.example.madwellbeingapp.ui.screens.HomeScreen
import com.example.madwellbeingapp.ui.screens.LandingScreen import com.example.madwellbeingapp.ui.screens.ManageActivitiesScreen
import com.example.madwellbeingapp.ui.theme.MadWellbeingAppTheme import com.example.madwellbeingapp.ui.theme.MadWellbeingAppTheme
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
@@ -32,9 +32,9 @@ class MainActivity : ComponentActivity() {
// ── Screen destinations ───────────────────────────────────── // ── Screen destinations ─────────────────────────────────────
sealed class Screen { sealed class Screen {
data object Landing : Screen()
data object AllActivities : Screen()
data object Home : Screen() data object Home : Screen()
data object AllActivities : Screen()
data object ManageActivities : Screen()
data object ActiveOnly : Screen() data object ActiveOnly : Screen()
data object Calendar : Screen() data object Calendar : Screen()
data object AddHabit : Screen() data object AddHabit : Screen()
@@ -44,7 +44,7 @@ sealed class Screen {
@Composable @Composable
fun HabitNavGraph(vm: HabitViewModel = viewModel()) { fun HabitNavGraph(vm: HabitViewModel = viewModel()) {
val backStack = remember { mutableStateListOf<Any>(Screen.Landing) } val backStack = remember { mutableStateListOf<Any>(Screen.Home) }
fun nav(screen: Screen) = backStack.add(screen) fun nav(screen: Screen) = backStack.add(screen)
fun back() { if (backStack.size > 1) backStack.removeLast() } fun back() { if (backStack.size > 1) backStack.removeLast() }
@@ -52,10 +52,10 @@ fun HabitNavGraph(vm: HabitViewModel = viewModel()) {
NavDisplay( NavDisplay(
backStack = backStack, backStack = backStack,
entryProvider = entryProvider { entryProvider = entryProvider {
entry<Screen.Landing> { entry<Screen.Home> {
LandingScreen( HomeScreen(
onAllActivities = { nav(Screen.AllActivities) }, onAllActivities = { nav(Screen.AllActivities) },
onManageActivities = { nav(Screen.Home) }, onManageActivities = { nav(Screen.ManageActivities) },
onActiveOnly = { nav(Screen.ActiveOnly) }, onActiveOnly = { nav(Screen.ActiveOnly) },
onCalendar = { nav(Screen.Calendar) } onCalendar = { nav(Screen.Calendar) }
) )
@@ -63,8 +63,8 @@ fun HabitNavGraph(vm: HabitViewModel = viewModel()) {
entry<Screen.AllActivities> { entry<Screen.AllActivities> {
AllActivitiesScreen(vm, onNavigateBack = ::back, onHabitClick = { nav(Screen.HabitDetail(it)) }) AllActivitiesScreen(vm, onNavigateBack = ::back, onHabitClick = { nav(Screen.HabitDetail(it)) })
} }
entry<Screen.Home> { entry<Screen.ManageActivities> {
HomeScreen(vm, onAddHabit = { nav(Screen.AddHabit) }, onHabitClick = { nav(Screen.HabitDetail(it)) }, onNavigateBack = ::back) ManageActivitiesScreen(vm, onAddHabit = { nav(Screen.AddHabit) }, onHabitClick = { nav(Screen.HabitDetail(it)) }, onNavigateBack = ::back)
} }
entry<Screen.ActiveOnly> { entry<Screen.ActiveOnly> {
ActiveOnlyScreen(vm, onNavigateBack = ::back, onHabitClick = { nav(Screen.HabitDetail(it)) }) ActiveOnlyScreen(vm, onNavigateBack = ::back, onHabitClick = { nav(Screen.HabitDetail(it)) })
@@ -1,254 +1,89 @@
package com.example.madwellbeingapp.ui.screens package com.example.madwellbeingapp.ui.screens
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.example.madwellbeingapp.data.model.Habit
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.math.roundToInt
@Composable
fun HomeScreen(
viewModel: HabitViewModel,
onAddHabit: () -> Unit,
onHabitClick: (Int) -> Unit,
onNavigateBack: () -> Unit
) {
val habits by viewModel.allHabits.collectAsState()
val todayLogs by viewModel.todayLogs.collectAsState()
val completedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet()
val activeHabits = habits.filter { it.isActive }
Scaffold { padding ->
Column(modifier = Modifier.fillMaxSize().padding(padding)) {
ScreenHeader(title = "Manage Activities", onBack = onNavigateBack)
// Summary bar – today only
Column(
modifier = Modifier.fillMaxWidth()
.background(MaterialTheme.colorScheme.primaryContainer)
.padding(horizontal = 20.dp, vertical = 12.dp)
) {
Text(
SimpleDateFormat("EEEE, MMM d", Locale.getDefault()).format(Date()),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
val done = activeHabits.count { completedIds.contains(it.id) }
Text(
"$done / ${activeHabits.size} habits completed today",
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
}
if (activeHabits.isEmpty()) {
Box(
modifier = Modifier.fillMaxSize().weight(1f).padding(32.dp),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("No habits yet!", style = MaterialTheme.typography.titleMedium)
Spacer(modifier = Modifier.height(8.dp))
Text("Tap the button below to create your first habit",
style = MaterialTheme.typography.bodyMedium)
}
}
} else {
LazyColumn(
modifier = Modifier.weight(1f).fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
items(activeHabits, key = { it.id }) { habit ->
val completed = completedIds.contains(habit.id)
SwipeHabitCard(
habit = habit,
completed = completed,
onSwipeRight = { viewModel.toggleTodayLog(habit.id) },
onSwipeLeft = { viewModel.disableHabit(habit) },
onClick = { onHabitClick(habit.id) },
viewModel = viewModel
)
}
}
}
Button(
onClick = onAddHabit,
modifier = Modifier.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp).height(52.dp),
shape = RoundedCornerShape(14.dp)
) {
Text("+ Add New Habit", style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold)
}
}
}
}
/** /**
* Habit card: * Home screen — the first page the user sees.
* • swipe left→right = mark complete (or undo) * Contains navigation buttons to other sections:
* • swipe right→left = disable * 1. All Activities (past + active)
* • streak counter (✓ per consecutive day) * 2. Manage Activities (add / mark done)
* 3. Active Only (still to-do)
* 4. Calendar
*/ */
@Composable @Composable
private fun SwipeHabitCard( fun HomeScreen(
habit: Habit, completed: Boolean, onSwipeRight: () -> Unit, onAllActivities: () -> Unit,
onSwipeLeft: () -> Unit, onClick: () -> Unit, viewModel: HabitViewModel onManageActivities: () -> Unit,
onActiveOnly: () -> Unit,
onCalendar: () -> Unit
) { ) {
// Streak counter
var streak by remember { mutableIntStateOf(0) }
LaunchedEffect(habit.id, completed) {
streak = viewModel.computeStreakForHabit(habit.id)
}
val threshold = 150f
var offsetX by remember { mutableStateOf(0f) }
Box( Box(
modifier = Modifier.fillMaxWidth(), modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
.statusBarsPadding(),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
// Right-swipe reveal (complete / undo) Column(
Box( horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.matchParentSize() verticalArrangement = Arrangement.Center,
.background( modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp)
if (completed) MaterialTheme.colorScheme.error.copy(alpha = 0.2f)
else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
shape = RoundedCornerShape(16.dp)
),
contentAlignment = Alignment.CenterStart
) { ) {
// App title
Text( Text(
if (completed) " ← Undo" else " → Complete", text = "Smart Habit\n& Wellbeing Companion",
modifier = Modifier.padding(start = 16.dp), style = MaterialTheme.typography.headlineMedium,
style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
color = if (completed) MaterialTheme.colorScheme.error color = MaterialTheme.colorScheme.primary,
else MaterialTheme.colorScheme.primary textAlign = TextAlign.Center
) )
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Track your habits. Build your streaks.",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(48.dp))
HomeButton("All Activities", onAllActivities)
Spacer(modifier = Modifier.height(16.dp))
HomeButton("Manage Activities", onManageActivities)
Spacer(modifier = Modifier.height(16.dp))
HomeButton("Active Only", onActiveOnly)
Spacer(modifier = Modifier.height(16.dp))
HomeButton("Calendar", onCalendar)
}
} }
// Left-swipe reveal (disable)
Box(
modifier = Modifier.matchParentSize()
.background(
MaterialTheme.colorScheme.error.copy(alpha = 0.15f),
shape = RoundedCornerShape(16.dp)
),
contentAlignment = Alignment.CenterEnd
) {
Text(
"Disable ← ",
modifier = Modifier.padding(end = 16.dp),
style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.error
)
} }
// Sliding card @Composable
Card( private fun HomeButton(text: String, onClick: () -> Unit) {
Button(
onClick = onClick, onClick = onClick,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth().height(56.dp),
.offset { IntOffset(offsetX.roundToInt(), 0) } shape = RoundedCornerShape(16.dp)
.pointerInput(completed) {
detectHorizontalDragGestures(
onDragEnd = {
if (offsetX > threshold) onSwipeRight()
else if (offsetX < -threshold) onSwipeLeft()
offsetX = 0f
},
onDragCancel = { offsetX = 0f },
onHorizontalDrag = { _, d ->
offsetX = (offsetX + d).coerceIn(-(threshold + 50f), threshold + 50f)
}
)
},
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(2.dp),
colors = CardDefaults.cardColors(
containerColor = if (completed) MaterialTheme.colorScheme.primaryContainer
else MaterialTheme.colorScheme.surface
)
) { ) {
Column(modifier = Modifier.padding(16.dp)) { Text(text = text, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
Row(verticalAlignment = Alignment.CenterVertically) {
Column(modifier = Modifier.weight(1f)) {
Text(habit.displayName, style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold)
Text(
"${habit.targetFrequency}x / week",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
// Streak counter – show ✓ per consecutive day (max 7 visible)
val visibleStreak = streak.coerceAtMost(7)
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(
"✓".repeat(visibleStreak) + if (streak > 7) "+" else "",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary
)
if (streak > 0) {
Text(
"${streak}d streak",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
Spacer(modifier = Modifier.height(4.dp))
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
if (!completed) {
Text("→ complete ← disable",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f))
} else {
Text("→ undo ← disable",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.error.copy(alpha = 0.6f))
}
}
}
}
} }
} }
@@ -1,88 +0,0 @@
package com.example.madwellbeingapp.ui.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
/**
* Landing screen — the first page the user sees.
* Contains 3 centred buttons:
* 1. All Activities (past + active)
* 2. Manage Activities (add / mark done)
* 3. Active Only (still to-do)
*/
@Composable
fun LandingScreen(
onAllActivities: () -> Unit,
onManageActivities: () -> Unit,
onActiveOnly: () -> Unit,
onCalendar: () -> Unit
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
.statusBarsPadding(),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp)
) {
// App title
Text(
text = "Smart Habit\n& Wellbeing Companion",
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Track your habits. Build your streaks.",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(48.dp))
LandingButton("All Activities", onAllActivities)
Spacer(modifier = Modifier.height(16.dp))
LandingButton("Manage Activities", onManageActivities)
Spacer(modifier = Modifier.height(16.dp))
LandingButton("Active Only", onActiveOnly)
Spacer(modifier = Modifier.height(16.dp))
LandingButton("Calendar", onCalendar)
}
}
}
@Composable
private fun LandingButton(text: String, onClick: () -> Unit) {
Button(
onClick = onClick,
modifier = Modifier.fillMaxWidth().height(56.dp),
shape = RoundedCornerShape(16.dp)
) {
Text(text = text, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
}
}
@@ -0,0 +1,255 @@
package com.example.madwellbeingapp.ui.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.input.pointer.pointerInput
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import com.example.madwellbeingapp.data.model.Habit
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.math.roundToInt
@Composable
fun ManageActivitiesScreen(
viewModel: HabitViewModel,
onAddHabit: () -> Unit,
onHabitClick: (Int) -> Unit,
onNavigateBack: () -> Unit
) {
val habits by viewModel.allHabits.collectAsState()
val todayLogs by viewModel.todayLogs.collectAsState()
val completedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet()
val activeHabits = habits.filter { it.isActive }
Scaffold { padding ->
Column(modifier = Modifier.fillMaxSize().padding(padding)) {
ScreenHeader(title = "Manage Activities", onBack = onNavigateBack)
// Summary bar – today only
Column(
modifier = Modifier.fillMaxWidth()
.background(MaterialTheme.colorScheme.primaryContainer)
.padding(horizontal = 20.dp, vertical = 12.dp)
) {
Text(
SimpleDateFormat("EEEE, MMM d", Locale.getDefault()).format(Date()),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
val done = activeHabits.count { completedIds.contains(it.id) }
Text(
"$done / ${activeHabits.size} habits completed today",
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
}
if (activeHabits.isEmpty()) {
Box(
modifier = Modifier.fillMaxSize().weight(1f).padding(32.dp),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("No habits yet!", style = MaterialTheme.typography.titleMedium)
Spacer(modifier = Modifier.height(8.dp))
Text("Tap the button below to create your first habit",
style = MaterialTheme.typography.bodyMedium)
}
}
} else {
LazyColumn(
modifier = Modifier.weight(1f).fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
items(activeHabits, key = { it.id }) { habit ->
val completed = completedIds.contains(habit.id)
SwipeHabitCard(
habit = habit,
completed = completed,
onSwipeRight = { viewModel.toggleTodayLog(habit.id) },
onSwipeLeft = { viewModel.disableHabit(habit) },
onClick = { onHabitClick(habit.id) },
viewModel = viewModel
)
}
}
}
Button(
onClick = onAddHabit,
modifier = Modifier.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp).height(52.dp),
shape = RoundedCornerShape(14.dp)
) {
Text("+ Add New Habit", style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold)
}
}
}
}
/**
* Habit card:
* • swipe left→right = mark complete (or undo)
* • swipe right→left = disable
* • streak counter (✓ per consecutive day)
*/
@Composable
private fun SwipeHabitCard(
habit: Habit, completed: Boolean, onSwipeRight: () -> Unit,
onSwipeLeft: () -> Unit, onClick: () -> Unit, viewModel: HabitViewModel
) {
// Streak counter
var streak by remember { mutableIntStateOf(0) }
LaunchedEffect(habit.id, completed) {
streak = viewModel.computeStreakForHabit(habit.id)
}
val threshold = 150f
var offsetX by remember { mutableStateOf(0f) }
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
// Right-swipe reveal (complete / undo)
Box(
modifier = Modifier.matchParentSize()
.background(
if (completed) MaterialTheme.colorScheme.error.copy(alpha = 0.2f)
else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
shape = RoundedCornerShape(16.dp)
),
contentAlignment = Alignment.CenterStart
) {
Text(
if (completed) " ← Undo" else " → Complete",
modifier = Modifier.padding(start = 16.dp),
style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold,
color = if (completed) MaterialTheme.colorScheme.error
else MaterialTheme.colorScheme.primary
)
}
// Left-swipe reveal (disable)
Box(
modifier = Modifier.matchParentSize()
.background(
MaterialTheme.colorScheme.error.copy(alpha = 0.15f),
shape = RoundedCornerShape(16.dp)
),
contentAlignment = Alignment.CenterEnd
) {
Text(
"Disable ← ",
modifier = Modifier.padding(end = 16.dp),
style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.error
)
}
// Sliding card
Card(
onClick = onClick,
modifier = Modifier.fillMaxWidth()
.offset { IntOffset(offsetX.roundToInt(), 0) }
.pointerInput(completed) {
detectHorizontalDragGestures(
onDragEnd = {
if (offsetX > threshold) onSwipeRight()
else if (offsetX < -threshold) onSwipeLeft()
offsetX = 0f
},
onDragCancel = { offsetX = 0f },
onHorizontalDrag = { _, d ->
offsetX = (offsetX + d).coerceIn(-(threshold + 50f), threshold + 50f)
}
)
},
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(2.dp),
colors = CardDefaults.cardColors(
containerColor = if (completed) MaterialTheme.colorScheme.primaryContainer
else MaterialTheme.colorScheme.surface
)
) {
Column(modifier = Modifier.padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Column(modifier = Modifier.weight(1f)) {
Text(habit.displayName, style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold)
Text(
"${habit.targetFrequency}x / week",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
// Streak counter – show ✓ per consecutive day (max 7 visible)
val visibleStreak = streak.coerceAtMost(7)
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(
"✓".repeat(visibleStreak) + if (streak > 7) "+" else "",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary
)
if (streak > 0) {
Text(
"${streak}d streak",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}
Spacer(modifier = Modifier.height(4.dp))
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
if (!completed) {
Text("→ complete ← disable",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f))
} else {
Text("→ undo ← disable",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.error.copy(alpha = 0.6f))
}
}
}
}
}
}