diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/ActiveOnlyScreen.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/ActiveOnlyScreen.kt index ddd203b..841e185 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/screens/ActiveOnlyScreen.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/ActiveOnlyScreen.kt @@ -1,25 +1,14 @@ package com.example.madwellbeingapp.ui.screens -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable 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.padding -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.Card -import androidx.compose.material3.CardDefaults -import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text @@ -28,16 +17,10 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp -import com.example.madwellbeingapp.data.model.Habit import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel -/** - * Shows only habits that are still active and NOT yet completed today. - * These are the ones the user still needs to do. - */ @Composable fun ActiveOnlyScreen( viewModel: HabitViewModel, @@ -47,74 +30,32 @@ fun ActiveOnlyScreen( val activeHabits by viewModel.activeHabitsNotCompletedToday.collectAsState() Scaffold { padding -> - Column( - modifier = Modifier - .fillMaxSize() - .padding(padding) - ) { - // Header - Box( - modifier = Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.tertiary) - .padding(horizontal = 16.dp, vertical = 16.dp) - ) { - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = "←", - modifier = Modifier - .clickable(onClick = onNavigateBack) - .padding(end = 16.dp), - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onTertiary, - fontWeight = FontWeight.Bold - ) - Text( - text = "Active — To Do", - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onTertiary, - fontWeight = FontWeight.Bold - ) - } - } + Column(modifier = Modifier.fillMaxSize().padding(padding)) { + ScreenHeader(title = "Active — To Do", onBack = onNavigateBack) if (activeHabits.isEmpty()) { - Box( - modifier = Modifier - .fillMaxSize() - .padding(32.dp), - contentAlignment = Alignment.Center - ) { + Box(modifier = Modifier.fillMaxSize().padding(32.dp), contentAlignment = Alignment.Center) { Column(horizontalAlignment = Alignment.CenterHorizontally) { - Text( - text = "All caught up!", - style = MaterialTheme.typography.titleMedium, - color = MaterialTheme.colorScheme.primary, - fontWeight = FontWeight.Bold - ) + Text("All caught up!", style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold) Spacer(modifier = Modifier.height(8.dp)) - Text( - text = "You've completed all your active habits for today.", + Text("You've completed all your active habits for today.", style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) + color = MaterialTheme.colorScheme.onSurfaceVariant) } } } else { LazyColumn( - modifier = Modifier - .fillMaxSize() - .padding(horizontal = 16.dp, vertical = 12.dp), + modifier = Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(10.dp) ) { items(activeHabits, key = { it.id }) { habit -> - ActiveHabitCard( + HabitCard( habit = habit, + viewModel = viewModel, onClick = { onHabitClick(habit.id) }, - viewModel = viewModel + circleText = if (habit.activityType.isNotBlank()) habit.activityType.first().toString() else "•", + circleTextColor = MaterialTheme.colorScheme.primary ) } } @@ -122,77 +63,3 @@ fun ActiveOnlyScreen( } } } - -@Composable -private fun ActiveHabitCard( - habit: Habit, - onClick: () -> Unit, - viewModel: HabitViewModel -) { - val weeklyProgress by viewModel.weeklyProgress(habit.id) - .collectAsState(initial = 0f) - - Card( - modifier = Modifier - .fillMaxWidth() - .clickable(onClick = onClick), - shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), - colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface) - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .padding(16.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Box( - modifier = Modifier - .size(44.dp) - .clip(CircleShape) - .background(MaterialTheme.colorScheme.surfaceVariant), - contentAlignment = Alignment.Center - ) { - // First letter of activity type - Text( - text = if (habit.activityType.isNotBlank()) habit.activityType.first().toString() else "•", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.Bold, - color = MaterialTheme.colorScheme.primary - ) - } - - Spacer(modifier = Modifier.width(14.dp)) - - Column(modifier = Modifier.weight(1f)) { - Text( - text = habit.displayName, - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold - ) - Spacer(modifier = Modifier.height(2.dp)) - Text( - text = habit.activityType + " • ${habit.targetFrequency}x/week", - style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) - Spacer(modifier = Modifier.height(8.dp)) - LinearProgressIndicator( - progress = { weeklyProgress }, - modifier = Modifier - .fillMaxWidth() - .height(6.dp) - .clip(RoundedCornerShape(3.dp)), - color = MaterialTheme.colorScheme.primary, - trackColor = MaterialTheme.colorScheme.surfaceVariant, - ) - Spacer(modifier = Modifier.height(2.dp)) - Text( - text = "Weekly: ${(weeklyProgress * 100).toInt()}%", - style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) - ) - } - } - } -} diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/AddEditHabitScreen.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/AddEditHabitScreen.kt index 98d93fa..b040ceb 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/screens/AddEditHabitScreen.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/AddEditHabitScreen.kt @@ -15,7 +15,6 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.rememberScrollState @@ -279,34 +278,10 @@ fun AddEditHabitScreen( .fillMaxSize() .padding(padding) ) { - // Header bar - Box( - modifier = Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.primary) - .padding(horizontal = 16.dp, vertical = 16.dp) - ) { - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = "←", - modifier = Modifier - .clickable(onClick = onNavigateBack) - .padding(end = 16.dp), - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - Text( - text = if (isEditing) "Edit Habit" else "New Habit", - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - } - } + ScreenHeader( + title = if (isEditing) "Edit Habit" else "New Habit", + onBack = onNavigateBack + ) Column( modifier = Modifier 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 8a9376d..68f5087 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,25 +1,12 @@ package com.example.madwellbeingapp.ui.screens -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable 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.padding -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.Card -import androidx.compose.material3.CardDefaults -import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text @@ -28,16 +15,10 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import com.example.madwellbeingapp.data.model.Habit import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel -/** - * Shows ALL activities — both active and past/completed. - * Read-only listing; tapping a card navigates to its detail screen. - */ @Composable fun AllActivitiesScreen( viewModel: HabitViewModel, @@ -46,69 +27,37 @@ fun AllActivitiesScreen( ) { val habits by viewModel.allHabits.collectAsState() val todayLogs by viewModel.todayLogs.collectAsState() - val todayCompletedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet() + val completedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet() Scaffold { padding -> - Column( - modifier = Modifier - .fillMaxSize() - .padding(padding) - ) { - // Header - Box( - modifier = Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.primary) - .padding(horizontal = 16.dp, vertical = 16.dp) - ) { - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically - ) { - Text( - text = "←", - modifier = Modifier - .clickable(onClick = onNavigateBack) - .padding(end = 16.dp), - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - Text( - text = "All Activities", - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - } - } + Column(modifier = Modifier.fillMaxSize().padding(padding)) { + ScreenHeader(title = "All Activities", onBack = onNavigateBack) if (habits.isEmpty()) { - Box( - modifier = Modifier - .fillMaxSize() - .padding(32.dp), - contentAlignment = Alignment.Center - ) { - Text( - text = "No activities yet. Go to Manage Activities to create one!", + Box(modifier = Modifier.fillMaxSize().padding(32.dp), contentAlignment = Alignment.Center) { + Text("No activities yet. Go to Manage Activities to create one!", style = MaterialTheme.typography.bodyLarge, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) + color = MaterialTheme.colorScheme.onSurfaceVariant) } } else { LazyColumn( - modifier = Modifier - .fillMaxSize() - .padding(horizontal = 16.dp, vertical = 12.dp), + modifier = Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(10.dp) ) { items(habits, key = { it.id }) { habit -> - AllActivityCard( + val done = completedIds.contains(habit.id) + HabitCard( habit = habit, - isCompletedToday = todayCompletedIds.contains(habit.id), + viewModel = viewModel, onClick = { onHabitClick(habit.id) }, - viewModel = viewModel + circleColor = cardCircleColor(habit, done), + circleText = cardCircleText(habit, done), + circleTextColor = if (done) MaterialTheme.colorScheme.onPrimary + else MaterialTheme.colorScheme.onSurfaceVariant, + containerColor = cardContainerColor(habit, done), + badge = if (!habit.isActive) { + { Text("Retired", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.error) } + } else null ) } } @@ -117,104 +66,23 @@ fun AllActivitiesScreen( } } +/** Resolves the circle background colour for a habit card. */ +@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 cardCircleText(habit: Habit, completedToday: Boolean) = when { + !habit.isActive -> "—" + completedToday -> "✓" + else -> "" +} + @Composable -private fun AllActivityCard( - habit: Habit, - isCompletedToday: Boolean, - onClick: () -> Unit, - viewModel: HabitViewModel -) { - val weeklyProgress by viewModel.weeklyProgress(habit.id) - .collectAsState(initial = 0f) - - Card( - modifier = Modifier - .fillMaxWidth() - .clickable(onClick = onClick), - shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), - colors = CardDefaults.cardColors( - containerColor = when { - !habit.isActive -> MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f) - isCompletedToday -> MaterialTheme.colorScheme.primaryContainer - else -> MaterialTheme.colorScheme.surface - } - ) - ) { - Row( - modifier = Modifier - .fillMaxWidth() - .padding(16.dp), - verticalAlignment = Alignment.CenterVertically - ) { - // Status circle - Box( - modifier = Modifier - .size(44.dp) - .clip(CircleShape) - .background( - when { - !habit.isActive -> MaterialTheme.colorScheme.outline.copy(alpha = 0.4f) - isCompletedToday -> MaterialTheme.colorScheme.primary - else -> MaterialTheme.colorScheme.surfaceVariant - } - ), - contentAlignment = Alignment.Center - ) { - Text( - text = when { - !habit.isActive -> "—" - isCompletedToday -> "✓" - else -> "" - }, - color = if (isCompletedToday) MaterialTheme.colorScheme.onPrimary - else MaterialTheme.colorScheme.onSurfaceVariant, - fontWeight = FontWeight.Bold, - style = MaterialTheme.typography.titleMedium - ) - } - - Spacer(modifier = Modifier.width(14.dp)) - - Column(modifier = Modifier.weight(1f)) { - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - text = habit.displayName, - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold - ) - if (!habit.isActive) { - Spacer(modifier = Modifier.width(8.dp)) - Text( - text = "Retired", - style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.error - ) - } - } - Spacer(modifier = Modifier.height(2.dp)) - Text( - text = habit.activityType + " • ${habit.targetFrequency}x/week", - style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) - Spacer(modifier = Modifier.height(8.dp)) - LinearProgressIndicator( - progress = { weeklyProgress }, - modifier = Modifier - .fillMaxWidth() - .height(6.dp) - .clip(RoundedCornerShape(3.dp)), - color = MaterialTheme.colorScheme.primary, - trackColor = MaterialTheme.colorScheme.surfaceVariant, - ) - Spacer(modifier = Modifier.height(2.dp)) - Text( - text = "Weekly: ${(weeklyProgress * 100).toInt()}%", - style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) - ) - } - } - } +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 } 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 89dbf6a..b08b916 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 @@ -76,383 +76,182 @@ fun HabitDetailScreen( AlertDialog( onDismissRequest = { showDeleteDialog = false }, title = { Text("Delete Habit") }, - text = { Text("Are you sure you want to delete \"${habit!!.displayName}\"? All associated data will be lost.") }, + text = { Text("Delete \"${habit!!.displayName}\"? All data will be lost.") }, confirmButton = { TextButton(onClick = { - Log.i("HabitDetail", "User confirmed DELETE for habit '${habit!!.displayName}' (id=${habit!!.id})") + Log.i("HabitDetail", "DELETE '${habit!!.displayName}' id=${habit!!.id}") viewModel.deleteHabit(habit!!) showDeleteDialog = false onNavigateBack() - }) { - Text("Delete", color = MaterialTheme.colorScheme.error) - } + }) { Text("Delete", color = MaterialTheme.colorScheme.error) } }, - dismissButton = { - TextButton(onClick = { showDeleteDialog = false }) { Text("Cancel") } - } + dismissButton = { TextButton(onClick = { showDeleteDialog = false }) { Text("Cancel") } } ) } Scaffold { padding -> - Column( - modifier = Modifier - .fillMaxSize() - .padding(padding) - ) { - // Custom header bar (replaces TopAppBar which uses material.icons) - Box( - modifier = Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.primary) - .padding(horizontal = 16.dp, vertical = 16.dp) - ) { - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.SpaceBetween - ) { - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - text = "←", - modifier = Modifier - .clickable(onClick = onNavigateBack) - .padding(end = 12.dp), - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - Text( - text = habit?.displayName ?: "Habit Detail", - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - } + Column(modifier = Modifier.fillMaxSize().padding(padding)) { + ScreenHeader( + title = habit?.displayName ?: "Habit Detail", + onBack = onNavigateBack, + trailing = { Row { - Text( - text = "✏️", - modifier = Modifier - .clickable { onEditHabit(habitId) } - .padding(horizontal = 8.dp), - style = MaterialTheme.typography.titleLarge - ) - Text( - text = "🗑️", - modifier = Modifier - .clickable { showDeleteDialog = true } - .padding(horizontal = 8.dp), - style = MaterialTheme.typography.titleLarge - ) + Text("✏️", modifier = Modifier.clickable { onEditHabit(habitId) }.padding(horizontal = 8.dp), + style = MaterialTheme.typography.titleLarge) + Text("🗑️", modifier = Modifier.clickable { showDeleteDialog = true }.padding(horizontal = 8.dp), + style = MaterialTheme.typography.titleLarge) } } - } + ) if (habit == null) { - Box( - modifier = Modifier.fillMaxSize(), - contentAlignment = Alignment.Center - ) { - Text("Loading…") - } + Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Text("Loading…") } return@Scaffold } val h = habit!! - val dateFormat = SimpleDateFormat("MMM d, yyyy", Locale.getDefault()) + val fmt = SimpleDateFormat("MMM d, yyyy", Locale.getDefault()) Column( - modifier = Modifier - .fillMaxSize() - .verticalScroll(rememberScrollState()) - .padding(20.dp), + modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(20.dp), verticalArrangement = Arrangement.spacedBy(16.dp) ) { // Info card - Card( - shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(2.dp), - modifier = Modifier.fillMaxWidth() - ) { + Card(shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), modifier = Modifier.fillMaxWidth()) { Column(modifier = Modifier.padding(16.dp)) { - Text( - text = h.activityType, - style = MaterialTheme.typography.labelLarge, - color = MaterialTheme.colorScheme.primary - ) + Text(h.activityType, style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.primary) Spacer(modifier = Modifier.height(4.dp)) - Text( - text = "Started: ${dateFormat.format(Date(h.startDate))}", - style = MaterialTheme.typography.bodyMedium - ) - Text( - text = "Target: ${h.targetFrequency}x per week", - style = MaterialTheme.typography.bodyMedium - ) + Text("Started: ${fmt.format(Date(h.startDate))}", style = MaterialTheme.typography.bodyMedium) + Text("Target: ${h.targetFrequency}x per week", style = MaterialTheme.typography.bodyMedium) if (h.reminderEnabled) { - Text( - text = "Reminder: ${h.reminderHour.toString().padStart(2, '0')}:${h.reminderMinute.toString().padStart(2, '0')}", - style = MaterialTheme.typography.bodyMedium - ) + Text("Reminder: ${h.reminderHour.toString().padStart(2, '0')}:${h.reminderMinute.toString().padStart(2, '0')}", + style = MaterialTheme.typography.bodyMedium) } } } - // Swipe to mark today as complete (or undo) - SwipeToCompleteBar( - isCompletedToday = isCompletedToday, - onSwipeComplete = { - Log.i("HabitDetail", "Swipe-to-complete triggered for habit '${h.displayName}' (id=${h.id}), isUndo=${isCompletedToday}") - viewModel.toggleTodayLog(habitId) - } - ) + // Swipe bar + SwipeBar(isCompletedToday) { + Log.i("HabitDetail", "Swipe '${h.displayName}' id=${h.id} undo=$isCompletedToday") + viewModel.toggleTodayLog(habitId) + } - // Streak cards - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(12.dp) - ) { - StatCard( - title = "🔥 Current Streak", - value = "$currentStreak day${if (currentStreak != 1) "s" else ""}", - modifier = Modifier.weight(1f) - ) - StatCard( - title = "🏆 Longest Streak", - value = "$longestStreak day${if (longestStreak != 1) "s" else ""}", - modifier = Modifier.weight(1f) - ) + // Streaks + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) { + StatCard("🔥 Current", "$currentStreak day${if (currentStreak != 1) "s" else ""}", Modifier.weight(1f)) + StatCard("🏆 Longest", "$longestStreak day${if (longestStreak != 1) "s" else ""}", Modifier.weight(1f)) } // Weekly progress - Card( - shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(2.dp), - modifier = Modifier.fillMaxWidth() - ) { + Card(shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), modifier = Modifier.fillMaxWidth()) { Column(modifier = Modifier.padding(16.dp)) { - Text( - text = "Weekly Progress", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold - ) + Text("Weekly Progress", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) Spacer(modifier = Modifier.height(12.dp)) - LinearProgressIndicator( - progress = { weeklyProgress }, - modifier = Modifier - .fillMaxWidth() - .height(12.dp) - .clip(RoundedCornerShape(6.dp)), - ) + LinearProgressIndicator(progress = { weeklyProgress }, + modifier = Modifier.fillMaxWidth().height(12.dp).clip(RoundedCornerShape(6.dp))) Spacer(modifier = Modifier.height(6.dp)) - Text( - text = "${(weeklyProgress * 100).toInt()}% of weekly goal", - style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) + Text("${(weeklyProgress * 100).toInt()}% of weekly goal", + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) } } - // Last 14 days heatmap - Text( - text = "Last 14 Days", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold - ) - - val logDates = logs.filter { it.completed }.map { normaliseToDay(it.date) }.toSet() - val today = todayStartMillis() - val last14 = (0..13).map { today - it * 86_400_000L }.reversed() - - LazyRow( - horizontalArrangement = Arrangement.spacedBy(6.dp) - ) { - items(last14) { dayMillis -> - val completed = logDates.contains(dayMillis) - val dayFormat = SimpleDateFormat("dd", Locale.getDefault()) + // Last 14 days + Text("Last 14 Days", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) + val logDates = logs.filter { it.completed }.map { normalise(it.date) }.toSet() + val today = todayMillis() + LazyRow(horizontalArrangement = Arrangement.spacedBy(6.dp)) { + items((0..13).map { today - it * 86_400_000L }.reversed()) { day -> + val done = logDates.contains(day) Column(horizontalAlignment = Alignment.CenterHorizontally) { Box( - modifier = Modifier - .size(36.dp) - .clip(CircleShape) - .background( - if (completed) MaterialTheme.colorScheme.primary - else MaterialTheme.colorScheme.surfaceVariant - ), + modifier = Modifier.size(36.dp).clip(CircleShape).background( + if (done) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant), contentAlignment = Alignment.Center ) { - Text( - text = if (completed) "✓" else "", - color = if (completed) MaterialTheme.colorScheme.onPrimary - else MaterialTheme.colorScheme.onSurfaceVariant, - style = MaterialTheme.typography.labelSmall - ) + Text(if (done) "✓" else "", + color = if (done) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurfaceVariant, + style = MaterialTheme.typography.labelSmall) } Spacer(modifier = Modifier.height(2.dp)) - Text( - text = dayFormat.format(Date(dayMillis)), - style = MaterialTheme.typography.labelSmall, - textAlign = TextAlign.Center - ) + Text(SimpleDateFormat("dd", Locale.getDefault()).format(Date(day)), + style = MaterialTheme.typography.labelSmall, textAlign = TextAlign.Center) } } } // Total completions - val totalCompletions = logs.count { it.completed } - Card( - shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(2.dp), - modifier = Modifier.fillMaxWidth() - ) { - Row( - modifier = Modifier.padding(16.dp), - verticalAlignment = Alignment.CenterVertically - ) { + val total = logs.count { it.completed } + Card(shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), modifier = Modifier.fillMaxWidth()) { + Row(modifier = Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) { Text("📊", style = MaterialTheme.typography.headlineMedium) Spacer(modifier = Modifier.width(12.dp)) Column { - Text( - "Total Completions", - style = MaterialTheme.typography.titleSmall, - fontWeight = FontWeight.SemiBold - ) - Text( - "$totalCompletions time${if (totalCompletions != 1) "s" else ""}", - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) + Text("Total Completions", style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold) + Text("$total time${if (total != 1) "s" else ""}", + style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) } } } - Spacer(modifier = Modifier.height(16.dp)) } } } } -/** - * Swipe-to-complete bar for the detail screen. - * User must swipe right past a threshold to toggle completion. - */ @Composable -private fun SwipeToCompleteBar( - isCompletedToday: Boolean, - onSwipeComplete: () -> Unit -) { - val swipeThreshold = 200f +private fun SwipeBar(isCompleted: Boolean, onSwipe: () -> Unit) { + val threshold = 200f var offsetX by remember { mutableFloatStateOf(0f) } - - Box( - modifier = Modifier - .fillMaxWidth() - .height(56.dp) - .clip(RoundedCornerShape(14.dp)) - ) { - // Background + Box(modifier = Modifier.fillMaxWidth().height(56.dp).clip(RoundedCornerShape(14.dp))) { Box( - modifier = Modifier - .matchParentSize() - .background( - if (isCompletedToday) MaterialTheme.colorScheme.secondaryContainer - else MaterialTheme.colorScheme.primary.copy(alpha = 0.15f) - ), + modifier = Modifier.matchParentSize().background( + if (isCompleted) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.primary.copy(alpha = 0.15f)), contentAlignment = Alignment.Center ) { - Text( - text = if (isCompletedToday) "← Swipe → to Undo" - else "← Swipe → to Complete Today", - style = MaterialTheme.typography.bodyMedium, - fontWeight = FontWeight.Bold, - color = if (isCompletedToday) MaterialTheme.colorScheme.onSecondaryContainer - else MaterialTheme.colorScheme.primary - ) + Text(if (isCompleted) "Swipe → to Undo" else "Swipe → to Complete", + style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold, + color = if (isCompleted) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.primary) } - - // Draggable thumb Box( - modifier = Modifier - .size(width = 80.dp, height = 56.dp) + modifier = Modifier.size(80.dp, 56.dp) .offset { IntOffset(offsetX.roundToInt(), 0) } .clip(RoundedCornerShape(14.dp)) - .background( - if (isCompletedToday) MaterialTheme.colorScheme.secondary - else MaterialTheme.colorScheme.primary - ) - .pointerInput(isCompletedToday) { + .background(MaterialTheme.colorScheme.primary) + .pointerInput(isCompleted) { detectHorizontalDragGestures( - onDragEnd = { - if (offsetX > swipeThreshold) { - Log.d("SwipeGesture", "Detail swipe threshold reached, toggling completion") - onSwipeComplete() - } - offsetX = 0f - }, + onDragEnd = { if (offsetX > threshold) onSwipe(); offsetX = 0f }, onDragCancel = { offsetX = 0f }, - onHorizontalDrag = { _, dragAmount -> - offsetX = (offsetX + dragAmount).coerceIn(0f, swipeThreshold + 50f) - } - ) + onHorizontalDrag = { _, d -> offsetX = (offsetX + d).coerceIn(0f, threshold + 50f) }) }, contentAlignment = Alignment.Center ) { - Text( - text = if (isCompletedToday) "✅" else "→", - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) + Text(if (isCompleted) "✅" else "→", style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Bold) } } } @Composable private fun StatCard(title: String, value: String, modifier: Modifier = Modifier) { - Card( - modifier = modifier, - shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(2.dp), - colors = CardDefaults.cardColors( - containerColor = MaterialTheme.colorScheme.primaryContainer - ) - ) { - Column( - modifier = Modifier.padding(16.dp), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center - ) { - Text( - text = title, - style = MaterialTheme.typography.labelMedium, - textAlign = TextAlign.Center - ) + Card(modifier = modifier, shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), + colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer)) { + Column(modifier = Modifier.padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally) { + Text(title, style = MaterialTheme.typography.labelMedium, textAlign = TextAlign.Center) Spacer(modifier = Modifier.height(6.dp)) - Text( - text = value, - style = MaterialTheme.typography.titleLarge, - fontWeight = FontWeight.Bold, - color = MaterialTheme.colorScheme.primary, - textAlign = TextAlign.Center - ) + Text(value, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold, + color = MaterialTheme.colorScheme.primary, textAlign = TextAlign.Center) } } } -private fun normaliseToDay(millis: Long): Long { - val cal = Calendar.getInstance() - cal.timeInMillis = millis - cal.set(Calendar.HOUR_OF_DAY, 0) - cal.set(Calendar.MINUTE, 0) - cal.set(Calendar.SECOND, 0) - cal.set(Calendar.MILLISECOND, 0) - return cal.timeInMillis +private fun normalise(millis: Long): Long { + val c = Calendar.getInstance(); c.timeInMillis = millis + c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0) + return c.timeInMillis } -private fun todayStartMillis(): Long { - val cal = Calendar.getInstance() - cal.set(Calendar.HOUR_OF_DAY, 0) - cal.set(Calendar.MINUTE, 0) - cal.set(Calendar.SECOND, 0) - cal.set(Calendar.MILLISECOND, 0) - return cal.timeInMillis +private fun todayMillis(): Long { + val c = Calendar.getInstance() + c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0) + return c.timeInMillis } 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 704b24d..47cdc88 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 @@ -47,10 +47,6 @@ import java.util.Date import java.util.Locale import kotlin.math.roundToInt -/** - * Manage Activities screen — the user can add a new activity or - * swipe to mark an existing active habit as done. - */ @Composable fun HomeScreen( viewModel: HabitViewModel, @@ -60,107 +56,64 @@ fun HomeScreen( ) { val habits by viewModel.allHabits.collectAsState() val todayLogs by viewModel.todayLogs.collectAsState() - val todayCompletedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet() - - // Only show active habits on this management screen + val completedIds = todayLogs.filter { it.completed }.map { it.habitId }.toSet() val activeHabits = habits.filter { it.isActive } Scaffold { padding -> - Column( - modifier = Modifier - .fillMaxSize() - .padding(padding) - ) { - // Header with back button + Column(modifier = Modifier.fillMaxSize().padding(padding)) { + // Extended header with summary Column( - modifier = Modifier - .fillMaxWidth() + modifier = Modifier.fillMaxWidth() .background(MaterialTheme.colorScheme.primary) .padding(horizontal = 20.dp, vertical = 24.dp) ) { Row(verticalAlignment = Alignment.CenterVertically) { - Text( - text = "←", - modifier = Modifier - .clickable(onClick = onNavigateBack) - .padding(end = 16.dp), + Text("←", modifier = Modifier.clickable(onClick = onNavigateBack).padding(end = 16.dp), style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) - Text( - text = "Manage Activities", - style = MaterialTheme.typography.headlineSmall, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Bold - ) + color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Bold) + Text("Manage Activities", style = MaterialTheme.typography.headlineSmall, + color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Bold) } Spacer(modifier = Modifier.height(4.dp)) - val dateFormat = SimpleDateFormat("EEEE, MMM d", Locale.getDefault()) - Text( - text = dateFormat.format(Date()), + Text(SimpleDateFormat("EEEE, MMM d", Locale.getDefault()).format(Date()), style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onPrimary.copy(alpha = 0.8f) - ) + color = MaterialTheme.colorScheme.onPrimary.copy(alpha = 0.8f)) Spacer(modifier = Modifier.height(12.dp)) - // Quick summary - val completedToday = activeHabits.count { todayCompletedIds.contains(it.id) } + val done = activeHabits.count { completedIds.contains(it.id) } val total = activeHabits.size - Text( - text = "$completedToday / $total habits completed today", + Text("$done / $total habits completed today", style = MaterialTheme.typography.bodyLarge, - color = MaterialTheme.colorScheme.onPrimary, - fontWeight = FontWeight.Medium - ) + color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Medium) if (total > 0) { Spacer(modifier = Modifier.height(8.dp)) LinearProgressIndicator( - progress = { completedToday.toFloat() / total }, - modifier = Modifier - .fillMaxWidth() - .height(8.dp) - .clip(RoundedCornerShape(4.dp)), + progress = { done.toFloat() / total }, + modifier = Modifier.fillMaxWidth().height(8.dp).clip(RoundedCornerShape(4.dp)), color = MaterialTheme.colorScheme.onPrimary, - trackColor = MaterialTheme.colorScheme.onPrimary.copy(alpha = 0.3f), - ) + trackColor = MaterialTheme.colorScheme.onPrimary.copy(alpha = 0.3f)) } } + // List or empty state if (activeHabits.isEmpty()) { - Box( - modifier = Modifier - .fillMaxSize() - .weight(1f) - .padding(32.dp), - contentAlignment = Alignment.Center - ) { + Box(modifier = Modifier.fillMaxSize().weight(1f).padding(32.dp), contentAlignment = Alignment.Center) { Column(horizontalAlignment = Alignment.CenterHorizontally) { - Text( - text = "No habits yet!", - style = MaterialTheme.typography.titleMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) + Text("No habits yet!", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) Spacer(modifier = Modifier.height(8.dp)) - Text( - text = "Tap the button below to create your first habit", - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) - ) + Text("Tap the button below to create your first habit", style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f)) } } } else { LazyColumn( - modifier = Modifier - .weight(1f) - .fillMaxWidth() - .padding(horizontal = 16.dp, vertical = 12.dp), + modifier = Modifier.weight(1f).fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(10.dp) ) { items(activeHabits, key = { it.id }) { habit -> - SwipeToCompleteHabitCard( + SwipeHabitCard( habit = habit, - isCompletedToday = todayCompletedIds.contains(habit.id), - onSwipeComplete = { viewModel.toggleTodayLog(habit.id) }, + completed = completedIds.contains(habit.id), + onSwipe = { viewModel.toggleTodayLog(habit.id) }, onClick = { onHabitClick(habit.id) }, viewModel = viewModel ) @@ -168,173 +121,93 @@ fun HomeScreen( } } - // Add habit button at the bottom Button( onClick = onAddHabit, - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp, vertical = 12.dp) - .height(52.dp), + modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp).height(52.dp), shape = RoundedCornerShape(14.dp) ) { - Text( - text = "+ Add New Habit", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.Bold - ) + Text("+ Add New Habit", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold) } } } } -/** - * A habit card that requires a horizontal swipe gesture to mark as complete. - * The user must swipe right past a threshold to toggle completion. - * This prevents accidental taps from marking habits complete. - */ +/** Habit card with swipe-to-complete gesture. */ @Composable -private fun SwipeToCompleteHabitCard( - habit: Habit, - isCompletedToday: Boolean, - onSwipeComplete: () -> Unit, - onClick: () -> Unit, - viewModel: HabitViewModel +private fun SwipeHabitCard( + habit: Habit, completed: Boolean, onSwipe: () -> Unit, + onClick: () -> Unit, viewModel: HabitViewModel ) { - val weeklyProgress by viewModel.weeklyProgress(habit.id) - .collectAsState(initial = 0f) - - val swipeThreshold = 150f + val weeklyProgress by viewModel.weeklyProgress(habit.id).collectAsState(initial = 0f) + val threshold = 150f var offsetX by remember { mutableFloatStateOf(0f) } - Box( - modifier = Modifier - .fillMaxWidth() - .clip(RoundedCornerShape(16.dp)) - ) { - // Background revealed on swipe + Box(modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(16.dp))) { + // Swipe background Box( - modifier = Modifier - .matchParentSize() - .background( - if (isCompletedToday) MaterialTheme.colorScheme.error.copy(alpha = 0.2f) - else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f) - ), + modifier = Modifier.matchParentSize().background( + if (completed) MaterialTheme.colorScheme.error.copy(alpha = 0.2f) + else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)), contentAlignment = Alignment.CenterStart ) { Text( - text = if (isCompletedToday) " ← Undo" else " ← Swipe to Complete →", + if (completed) " ← Undo" else " Swipe → to Complete", modifier = Modifier.padding(start = 16.dp), - style = MaterialTheme.typography.bodyMedium, - fontWeight = FontWeight.Bold, - color = if (isCompletedToday) MaterialTheme.colorScheme.error - else MaterialTheme.colorScheme.primary - ) + style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold, + color = if (completed) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary) } - - // Foreground card that slides + // Sliding card Card( - modifier = Modifier - .fillMaxWidth() + modifier = Modifier.fillMaxWidth() .offset { IntOffset(offsetX.roundToInt(), 0) } - .pointerInput(isCompletedToday) { + .pointerInput(completed) { detectHorizontalDragGestures( onDragEnd = { - if (offsetX > swipeThreshold) { - Log.d("SwipeGesture", "Swipe threshold reached for habit '${habit.displayName}' (id=${habit.id}), toggling completion") - onSwipeComplete() + if (offsetX > threshold) { + Log.d("SwipeGesture", "Swiped '${habit.displayName}'") + onSwipe() } offsetX = 0f }, - onDragCancel = { - offsetX = 0f - }, - onHorizontalDrag = { _, dragAmount -> - // Only allow rightward swipe (positive direction) - offsetX = (offsetX + dragAmount).coerceIn(0f, swipeThreshold + 50f) - } + onDragCancel = { offsetX = 0f }, + onHorizontalDrag = { _, d -> offsetX = (offsetX + d).coerceIn(0f, threshold + 50f) } ) - } - .clickable(onClick = onClick), + }.clickable(onClick = onClick), shape = RoundedCornerShape(16.dp), - elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), + elevation = CardDefaults.cardElevation(2.dp), colors = CardDefaults.cardColors( - containerColor = if (isCompletedToday) - MaterialTheme.colorScheme.primaryContainer - else MaterialTheme.colorScheme.surface - ) + containerColor = if (completed) MaterialTheme.colorScheme.primaryContainer + else MaterialTheme.colorScheme.surface) ) { - Row( - modifier = Modifier - .fillMaxWidth() - .padding(16.dp), - verticalAlignment = Alignment.CenterVertically - ) { - // Status indicator circle + Row(modifier = Modifier.fillMaxWidth().padding(16.dp), verticalAlignment = Alignment.CenterVertically) { Box( - modifier = Modifier - .size(44.dp) - .clip(CircleShape) - .background( - if (isCompletedToday) - MaterialTheme.colorScheme.primary - else - MaterialTheme.colorScheme.surfaceVariant - ), + modifier = Modifier.size(44.dp).clip(CircleShape).background( + if (completed) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant), contentAlignment = Alignment.Center ) { - Text( - text = if (isCompletedToday) "✓" else "", - color = if (isCompletedToday) - MaterialTheme.colorScheme.onPrimary - else - MaterialTheme.colorScheme.onSurfaceVariant, - fontWeight = FontWeight.Bold, - style = MaterialTheme.typography.titleMedium - ) + Text(if (completed) "✓" else "", + color = if (completed) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurfaceVariant, + fontWeight = FontWeight.Bold, style = MaterialTheme.typography.titleMedium) } - Spacer(modifier = Modifier.width(14.dp)) - Column(modifier = Modifier.weight(1f)) { - Text( - text = habit.displayName, - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold - ) + Text(habit.displayName, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) Spacer(modifier = Modifier.height(2.dp)) - Text( - text = habit.activityType + " • ${habit.targetFrequency}x/week", - style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant - ) + Text(habit.activityType + " • ${habit.targetFrequency}x/week", + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) Spacer(modifier = Modifier.height(8.dp)) - // Weekly progress bar LinearProgressIndicator( progress = { weeklyProgress }, - modifier = Modifier - .fillMaxWidth() - .height(6.dp) - .clip(RoundedCornerShape(3.dp)), - color = MaterialTheme.colorScheme.primary, - trackColor = MaterialTheme.colorScheme.surfaceVariant, - ) + modifier = Modifier.fillMaxWidth().height(6.dp).clip(RoundedCornerShape(3.dp)), + color = MaterialTheme.colorScheme.primary, trackColor = MaterialTheme.colorScheme.surfaceVariant) Spacer(modifier = Modifier.height(2.dp)) - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween - ) { - Text( - text = "Weekly: ${(weeklyProgress * 100).toInt()}%", + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { + Text("Weekly: ${(weeklyProgress * 100).toInt()}%", style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) - ) - if (!isCompletedToday) { - Text( - text = "Swipe → to complete", - style = MaterialTheme.typography.labelSmall, - color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f) - ) - } + color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f)) + if (!completed) Text("Swipe → to complete", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f)) } } } diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/LandingScreen.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/LandingScreen.kt index f463f5c..75c6210 100644 --- a/app/src/main/java/com/example/madwellbeingapp/ui/screens/LandingScreen.kt +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/LandingScreen.kt @@ -12,7 +12,6 @@ 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.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -45,9 +44,7 @@ fun LandingScreen( Column( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 32.dp) + modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp) ) { // App title Text( @@ -67,64 +64,22 @@ fun LandingScreen( Spacer(modifier = Modifier.height(48.dp)) - // Top button — All Activities - Button( - onClick = onAllActivities, - modifier = Modifier - .fillMaxWidth() - .height(56.dp), - shape = RoundedCornerShape(16.dp), - colors = ButtonDefaults.buttonColors( - containerColor = MaterialTheme.colorScheme.primary - ) - ) { - Text( - text = "All Activities", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.Bold - ) - } - + LandingButton("All Activities", onAllActivities) Spacer(modifier = Modifier.height(16.dp)) - - // Middle button — Manage Activities - Button( - onClick = onManageActivities, - modifier = Modifier - .fillMaxWidth() - .height(56.dp), - shape = RoundedCornerShape(16.dp), - colors = ButtonDefaults.buttonColors( - containerColor = MaterialTheme.colorScheme.secondary - ) - ) { - Text( - text = "Manage Activities", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.Bold - ) - } - + LandingButton("Manage Activities", onManageActivities) Spacer(modifier = Modifier.height(16.dp)) - - // Bottom button — Active Only - Button( - onClick = onActiveOnly, - modifier = Modifier - .fillMaxWidth() - .height(56.dp), - shape = RoundedCornerShape(16.dp), - colors = ButtonDefaults.buttonColors( - containerColor = MaterialTheme.colorScheme.tertiary - ) - ) { - Text( - text = "Active Only", - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.Bold - ) - } + LandingButton("Active Only", onActiveOnly) } } } +@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) + } +} diff --git a/app/src/main/java/com/example/madwellbeingapp/ui/screens/SharedComponents.kt b/app/src/main/java/com/example/madwellbeingapp/ui/screens/SharedComponents.kt new file mode 100644 index 0000000..ea95bfa --- /dev/null +++ b/app/src/main/java/com/example/madwellbeingapp/ui/screens/SharedComponents.kt @@ -0,0 +1,169 @@ +package com.example.madwellbeingapp.ui.screens + +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +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.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults +import androidx.compose.material3.LinearProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +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.font.FontWeight +import androidx.compose.ui.unit.dp +import com.example.madwellbeingapp.data.model.Habit +import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel + +// ───────────────────────────────────────────────────────────── +// Reusable composables shared across multiple screens. +// ───────────────────────────────────────────────────────────── + +/** Standard header bar with a ← back button and a title. */ +@Composable +fun ScreenHeader( + title: String, + onBack: () -> Unit, + trailing: @Composable (() -> Unit)? = null +) { + Box( + modifier = Modifier + .fillMaxWidth() + .background(MaterialTheme.colorScheme.primary) + .padding(horizontal = 16.dp, vertical = 16.dp) + ) { + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween + ) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + text = "←", + modifier = Modifier + .clickable(onClick = onBack) + .padding(end = 12.dp), + style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onPrimary, + fontWeight = FontWeight.Bold + ) + Text( + text = title, + style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onPrimary, + fontWeight = FontWeight.Bold + ) + } + if (trailing != null) trailing() + } + } +} + +/** + * Standard habit card used on list screens. + * + * Shows a status circle, display name, activity type, frequency, and + * a weekly progress bar. Customisable via [circleColor], [circleText], + * and [containerColor]. + */ +@Composable +fun HabitCard( + habit: Habit, + viewModel: HabitViewModel, + onClick: () -> Unit, + circleColor: Color = MaterialTheme.colorScheme.surfaceVariant, + circleText: String = "", + circleTextColor: Color = MaterialTheme.colorScheme.onSurfaceVariant, + containerColor: Color = MaterialTheme.colorScheme.surface, + badge: @Composable (() -> Unit)? = null +) { + val weeklyProgress by viewModel.weeklyProgress(habit.id) + .collectAsState(initial = 0f) + + Card( + modifier = Modifier + .fillMaxWidth() + .clickable(onClick = onClick), + shape = RoundedCornerShape(16.dp), + elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), + colors = CardDefaults.cardColors(containerColor = containerColor) + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + verticalAlignment = Alignment.CenterVertically + ) { + // Status circle + Box( + modifier = Modifier + .size(44.dp) + .clip(CircleShape) + .background(circleColor), + contentAlignment = Alignment.Center + ) { + Text( + text = circleText, + color = circleTextColor, + fontWeight = FontWeight.Bold, + style = MaterialTheme.typography.titleMedium + ) + } + + Spacer(modifier = Modifier.width(14.dp)) + + Column(modifier = Modifier.weight(1f)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + text = habit.displayName, + style = MaterialTheme.typography.titleMedium, + fontWeight = FontWeight.SemiBold + ) + if (badge != null) { + Spacer(modifier = Modifier.width(8.dp)) + badge() + } + } + Spacer(modifier = Modifier.height(2.dp)) + Text( + text = habit.activityType + " • ${habit.targetFrequency}x/week", + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant + ) + Spacer(modifier = Modifier.height(8.dp)) + LinearProgressIndicator( + progress = { weeklyProgress }, + modifier = Modifier + .fillMaxWidth() + .height(6.dp) + .clip(RoundedCornerShape(3.dp)), + color = MaterialTheme.colorScheme.primary, + trackColor = MaterialTheme.colorScheme.surfaceVariant, + ) + Spacer(modifier = Modifier.height(2.dp)) + Text( + text = "Weekly: ${(weeklyProgress * 100).toInt()}%", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) + ) + } + } + } +} +