From b302286990bc84340c1d61f9c04f20d1625a2b8d Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Wed, 4 Mar 2026 18:25:06 +0000 Subject: [PATCH] Enhance habit tracking features: implement swipeable cards for habit actions, add monthly progress toggle, and refactor UI components for better usability --- .../ui/screens/AllActivitiesScreen.kt | 225 +++++++++++++----- .../ui/screens/HabitDetailScreen.kt | 70 +++--- .../madwellbeingapp/ui/screens/HomeScreen.kt | 79 ++++-- .../ui/viewmodel/HabitViewModel.kt | 12 + 4 files changed, 273 insertions(+), 113 deletions(-) diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/AllActivitiesScreen.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/AllActivitiesScreen.kt index ac95b76..118eab0 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/screens/AllActivitiesScreen.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/AllActivitiesScreen.kt @@ -1,26 +1,42 @@ 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.layout.size import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material3.Button +import androidx.compose.foundation.shape.RoundedCornerShape +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 kotlin.math.roundToInt @Composable fun AllActivitiesScreen( @@ -50,46 +66,19 @@ fun AllActivitiesScreen( verticalArrangement = Arrangement.spacedBy(10.dp) ) { items(habits, key = { it.id }) { habit -> - val done = completedIds.contains(habit.id) - HabitCard( + val completed = completedIds.contains(habit.id) + AllActivitiesSwipeCard( habit = habit, + completed = completed, viewModel = viewModel, - onClick = { onHabitClick(habit.id) }, - circleColor = cardCircleColor(habit, done), - circleText = cardCircleText(habit, done), - circleTextColor = if (!habit.isActive) - MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f) - else if (done) MaterialTheme.colorScheme.onPrimary - else MaterialTheme.colorScheme.onSurfaceVariant, - containerColor = cardContainerColor(habit, done), - contentAlpha = if (!habit.isActive) 0.5f else 1f, - badge = if (!habit.isActive) { - { Text("Disabled", style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.error) } - } else null, - trailing = if (!habit.isActive) { - { - Button( - onClick = { viewModel.enableHabit(habit) }, - modifier = Modifier.size(32.dp), - contentPadding = androidx.compose.foundation.layout.PaddingValues(0.dp) - ) { - Text("+", fontWeight = FontWeight.Bold, - style = MaterialTheme.typography.titleMedium) - } - } - } else { - { - Button( - onClick = { viewModel.disableHabit(habit) }, - modifier = Modifier.size(32.dp), - contentPadding = androidx.compose.foundation.layout.PaddingValues(0.dp) - ) { - Text("−", fontWeight = FontWeight.Bold, - style = MaterialTheme.typography.titleMedium) - } - } - } + onSwipeRight = { + if (habit.isActive) viewModel.toggleTodayLog(habit.id) + }, + onSwipeLeft = { + if (habit.isActive) viewModel.disableHabit(habit) + else viewModel.enableHabit(habit) + }, + onClick = { onHabitClick(habit.id) } ) } } @@ -98,22 +87,148 @@ fun AllActivitiesScreen( } } +/** + * Swipeable card for All Activities screen. + * • Active habits: swipe left→right = complete/undo, right→left = disable + * • Disabled habits: shown greyed out, right→left = re-enable + */ @Composable -private fun cardCircleColor(habit: Habit, completedToday: Boolean) = when { - !habit.isActive -> MaterialTheme.colorScheme.outline.copy(alpha = 0.4f) - completedToday -> MaterialTheme.colorScheme.primary - else -> MaterialTheme.colorScheme.surfaceVariant -} +private fun AllActivitiesSwipeCard( + habit: Habit, + completed: Boolean, + viewModel: HabitViewModel, + onSwipeRight: () -> Unit, + onSwipeLeft: () -> Unit, + onClick: () -> Unit +) { + var streak by remember { mutableIntStateOf(0) } + LaunchedEffect(habit.id, completed) { + streak = viewModel.computeStreakForHabit(habit.id) + } -private fun cardCircleText(habit: Habit, completedToday: Boolean) = when { - !habit.isActive -> "—" - completedToday -> "✓" - else -> "" -} + val threshold = 150f + var offsetX by remember { mutableStateOf(0f) } + val isActive = habit.isActive + val contentAlpha = if (isActive) 1f else 0.45f -@Composable -private fun cardContainerColor(habit: Habit, completedToday: Boolean) = when { - !habit.isActive -> MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f) - completedToday -> MaterialTheme.colorScheme.primaryContainer - else -> MaterialTheme.colorScheme.surface + Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) { + // Right-swipe reveal (complete/undo – only for active habits) + if (isActive) { + 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 / re-enable) + Box( + modifier = Modifier.matchParentSize().background( + if (isActive) MaterialTheme.colorScheme.error.copy(alpha = 0.15f) + else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f), + shape = RoundedCornerShape(16.dp) + ), + contentAlignment = Alignment.CenterEnd + ) { + Text( + if (isActive) "Disable ← " else "Enable ← ", + modifier = Modifier.padding(end = 16.dp), + style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold, + color = if (isActive) MaterialTheme.colorScheme.error + else MaterialTheme.colorScheme.primary + ) + } + + Card( + onClick = onClick, + modifier = Modifier.fillMaxWidth() + .offset { IntOffset(offsetX.roundToInt(), 0) } + .pointerInput(isActive, completed) { + detectHorizontalDragGestures( + onDragEnd = { + if (offsetX > threshold) onSwipeRight() + else if (offsetX < -threshold) onSwipeLeft() + offsetX = 0f + }, + onDragCancel = { offsetX = 0f }, + onHorizontalDrag = { _, d -> + val minBound = -(threshold + 50f) + val maxBound = if (isActive) threshold + 50f else 0f + offsetX = (offsetX + d).coerceIn(minBound, maxBound) + } + ) + }, + shape = RoundedCornerShape(16.dp), + elevation = CardDefaults.cardElevation(2.dp), + colors = CardDefaults.cardColors( + containerColor = when { + !isActive -> MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f) + completed -> MaterialTheme.colorScheme.primaryContainer + else -> MaterialTheme.colorScheme.surface + } + ) + ) { + Column(modifier = Modifier.padding(16.dp)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Column(modifier = Modifier.weight(1f)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + habit.displayName, + style = MaterialTheme.typography.titleMedium, + fontWeight = FontWeight.SemiBold, + color = MaterialTheme.colorScheme.onSurface.copy(alpha = contentAlpha) + ) + if (!isActive) { + Text( + " Disabled", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.error + ) + } + } + Text( + habit.activityType + " • ${habit.targetFrequency}x/week", + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = contentAlpha) + ) + } + // Streak counter + if (isActive && streak > 0) { + 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.copy(alpha = contentAlpha) + ) + Text( + "${streak}d streak", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = contentAlpha) + ) + } + } + } + Spacer(modifier = Modifier.height(4.dp)) + Text( + if (!isActive) "← swipe to re-enable" + else if (completed) "→ undo ← disable" + else "→ complete ← disable", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f) + ) + } + } + } } diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/HabitDetailScreen.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/HabitDetailScreen.kt index f0739a8..a20414d 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/screens/HabitDetailScreen.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/HabitDetailScreen.kt @@ -13,7 +13,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults @@ -51,29 +50,11 @@ fun HabitDetailScreen( val logs by viewModel.selectedHabitLogs.collectAsState() val currentStreak by viewModel.currentStreak.collectAsState() val longestStreak by viewModel.longestStreak.collectAsState() - val weeklyProgress by viewModel.weeklyProgress(habitId).collectAsState(initial = 0f) val todayLogs by viewModel.todayLogs.collectAsState() val isCompletedToday = todayLogs.any { it.habitId == habitId && it.completed } - var showDisableDialog by remember { mutableStateOf(false) } + var showMonth by remember { mutableStateOf(false) } - if (showDisableDialog && habit != null) { - AlertDialog( - onDismissRequest = { showDisableDialog = false }, - title = { Text("Disable Habit") }, - text = { Text("Disable \"${habit!!.displayName}\"? It will be greyed out but data is kept.") }, - confirmButton = { - TextButton(onClick = { - viewModel.disableHabit(habit!!) - showDisableDialog = false - onNavigateBack() - }) { Text("Disable", color = MaterialTheme.colorScheme.error) } - }, - dismissButton = { - TextButton(onClick = { showDisableDialog = false }) { Text("Cancel") } - } - ) - } Scaffold { padding -> Column(modifier = Modifier.fillMaxSize().padding(padding)) { @@ -81,21 +62,8 @@ fun HabitDetailScreen( title = habit?.displayName ?: "Habit Detail", onBack = onNavigateBack, trailing = { - Row { - TextButton(onClick = { onEditHabit(habitId) }) { - Text("Edit", color = MaterialTheme.colorScheme.onPrimary) - } - if (habit?.isActive == true) { - TextButton(onClick = { showDisableDialog = true }) { - Text("−", color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold) - } - } else { - TextButton(onClick = { if (habit != null) viewModel.enableHabit(habit!!) }) { - Text("+", color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold) - } - } + TextButton(onClick = { onEditHabit(habitId) }) { + Text("Edit", color = MaterialTheme.colorScheme.onPrimary) } } ) @@ -156,15 +124,35 @@ fun HabitDetailScreen( Modifier.weight(1f)) } - // Weekly progress + // Weekly/Monthly progress with toggle Card(modifier = Modifier.fillMaxWidth(), elevation = CardDefaults.cardElevation(2.dp)) { Column(modifier = Modifier.padding(16.dp)) { - Text("Weekly Progress", style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold) - Spacer(modifier = Modifier.height(8.dp)) - Text("${(weeklyProgress * 100).toInt()}% of weekly goal", + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically + ) { + Text( + if (showMonth) "Monthly Progress" else "Weekly Progress", + style = MaterialTheme.typography.titleMedium, + fontWeight = FontWeight.SemiBold + ) + TextButton(onClick = { showMonth = !showMonth }) { + Text( + if (showMonth) "Week ▲" else "Month ▼", + style = MaterialTheme.typography.labelMedium, + fontWeight = FontWeight.Bold, + color = MaterialTheme.colorScheme.primary + ) + } + } + val detailProgress by (if (showMonth) viewModel.monthlyProgress(habitId) + else viewModel.weeklyProgress(habitId)).collectAsState(initial = 0f) + Text( + "${(detailProgress * 100).toInt()}% of ${if (showMonth) "monthly" else "weekly"} goal", style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant) + color = MaterialTheme.colorScheme.onSurfaceVariant + ) } } diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/HomeScreen.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/HomeScreen.kt index 6bc77ce..d42b277 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/screens/HomeScreen.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/HomeScreen.kt @@ -5,7 +5,6 @@ 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.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize @@ -13,7 +12,6 @@ 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.layout.size import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape @@ -23,9 +21,12 @@ import androidx.compose.material3.CardDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text +import androidx.compose.material3.TextButton 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 @@ -54,6 +55,9 @@ fun HomeScreen( val completedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet() val activeHabits = habits.filter { it.isActive } + // Toggle between "Week" and "Month" view for the progress dropdown + var showMonth by remember { mutableStateOf(false) } + Scaffold { padding -> Column(modifier = Modifier.fillMaxSize().padding(padding)) { ScreenHeader(title = "Manage Activities", onBack = onNavigateBack) @@ -76,6 +80,25 @@ fun HomeScreen( fontWeight = FontWeight.Medium, color = MaterialTheme.colorScheme.onPrimaryContainer ) + // Week / Month toggle + Row( + modifier = Modifier.fillMaxWidth().padding(top = 4.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Text( + if (showMonth) "Showing: Month" else "Showing: Week", + style = MaterialTheme.typography.labelMedium, + color = MaterialTheme.colorScheme.onPrimaryContainer + ) + TextButton(onClick = { showMonth = !showMonth }) { + Text( + if (showMonth) "Switch to Week ▲" else "Expand to Month ▼", + style = MaterialTheme.typography.labelMedium, + fontWeight = FontWeight.Bold, + color = MaterialTheme.colorScheme.primary + ) + } + } } if (activeHabits.isEmpty()) { @@ -104,7 +127,8 @@ fun HomeScreen( onSwipeRight = { viewModel.toggleTodayLog(habit.id) }, onSwipeLeft = { viewModel.disableHabit(habit) }, onClick = { onHabitClick(habit.id) }, - viewModel = viewModel + viewModel = viewModel, + showMonth = showMonth ) } } @@ -123,13 +147,27 @@ fun HomeScreen( } } -/** Habit card with swipe-right-to-complete, swipe-left-to-disable, and a − button. */ +/** + * Habit card: + * • swipe left→right = mark complete (or undo) + * • swipe right→left = disable + * • streak counter (✓ per consecutive day) replaces the old − button + */ @Composable private fun SwipeHabitCard( habit: Habit, completed: Boolean, onSwipeRight: () -> Unit, - onSwipeLeft: () -> Unit, onClick: () -> Unit, viewModel: HabitViewModel + onSwipeLeft: () -> Unit, onClick: () -> Unit, viewModel: HabitViewModel, + showMonth: Boolean ) { - val weeklyProgress by viewModel.weeklyProgress(habit.id).collectAsState(initial = 0f) + val progress by (if (showMonth) viewModel.monthlyProgress(habit.id) + else viewModel.weeklyProgress(habit.id)).collectAsState(initial = 0f) + + // 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) } @@ -208,20 +246,27 @@ private fun SwipeHabitCard( color = MaterialTheme.colorScheme.onSurfaceVariant ) Text( - "Weekly: ${(weeklyProgress * 100).toInt()}%", + "${if (showMonth) "Monthly" else "Weekly"}: ${(progress * 100).toInt()}%", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant ) } - // Minus button for quick disable - Button( - onClick = onSwipeLeft, - modifier = Modifier.size(36.dp), - shape = RoundedCornerShape(8.dp), - contentPadding = PaddingValues(0.dp) - ) { - Text("−", style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.Bold) + // 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)) @@ -231,7 +276,7 @@ private fun SwipeHabitCard( style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f)) } else { - Text("→ undo", + Text("→ undo ← disable", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.error.copy(alpha = 0.6f)) } diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/viewmodel/HabitViewModel.kt b/app/src/main/java/com/example/madwellbeingapp/ui/viewmodel/HabitViewModel.kt index 0901970..fc063cf 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/viewmodel/HabitViewModel.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/viewmodel/HabitViewModel.kt @@ -179,6 +179,18 @@ class HabitViewModel(application: Application) : AndroidViewModel(application) { } } + // ── Monthly progress for a habit ──────────────────────── + fun monthlyProgress(habitId: Int): Flow { + return repository.getLogsForHabit(habitId).map { logs -> + val monthAgo = todayStart - 29 * 86_400_000L + val thisMonthCount = logs.count { it.completed && it.date >= monthAgo } + val habit = repository.getHabitByIdOnce(habitId) + val weeklyTarget = habit?.targetFrequency ?: 7 + val monthlyTarget = (weeklyTarget * 4.3).toInt().coerceAtLeast(1) + (thisMonthCount.toFloat() / monthlyTarget).coerceIn(0f, 1f) + } + } + // ── Duplicate check ───────────────────────────────────── suspend fun isDuplicate(name: String, details: String, excludeId: Int? = null): Boolean { val existing = repository.findDuplicate(name.trim(), details.trim())