Refactor UI components: replace header implementations with reusable ScreenHeader and simplify button layouts in habit screens

This commit is contained in:
bobbert committed 2026-03-02 21:12:26 +00:00
1 parent 7061cf00cc
commit 925f426410
7 files changed
+386 -880

No files matched your search

@@ -1,25 +1,14 @@
package com.example.madwellbeingapp.ui.screens 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.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.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding 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.LazyColumn
import androidx.compose.foundation.lazy.items 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.MaterialTheme
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text import androidx.compose.material3.Text
@@ -28,16 +17,10 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
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 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 @Composable
fun ActiveOnlyScreen( fun ActiveOnlyScreen(
viewModel: HabitViewModel, viewModel: HabitViewModel,
@@ -47,74 +30,32 @@ fun ActiveOnlyScreen(
val activeHabits by viewModel.activeHabitsNotCompletedToday.collectAsState() val activeHabits by viewModel.activeHabitsNotCompletedToday.collectAsState()
Scaffold { padding -> Scaffold { padding ->
Column( Column(modifier = Modifier.fillMaxSize().padding(padding)) {
modifier = Modifier ScreenHeader(title = "Active — To Do", onBack = onNavigateBack)
.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
)
}
}
if (activeHabits.isEmpty()) { if (activeHabits.isEmpty()) {
Box( Box(modifier = Modifier.fillMaxSize().padding(32.dp), contentAlignment = Alignment.Center) {
modifier = Modifier
.fillMaxSize()
.padding(32.dp),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) { Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text( Text("All caught up!", style = MaterialTheme.typography.titleMedium,
text = "All caught up!", color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold)
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.primary,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(8.dp)) 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, style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant color = MaterialTheme.colorScheme.onSurfaceVariant)
)
} }
} }
} else { } else {
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp),
.fillMaxSize()
.padding(horizontal = 16.dp, vertical = 12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp) verticalArrangement = Arrangement.spacedBy(10.dp)
) { ) {
items(activeHabits, key = { it.id }) { habit -> items(activeHabits, key = { it.id }) { habit ->
ActiveHabitCard( HabitCard(
habit = habit, habit = habit,
viewModel = viewModel,
onClick = { onHabitClick(habit.id) }, 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)
)
}
}
}
}
@@ -15,7 +15,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
@@ -279,34 +278,10 @@ fun AddEditHabitScreen(
.fillMaxSize() .fillMaxSize()
.padding(padding) .padding(padding)
) { ) {
// Header bar ScreenHeader(
Box( title = if (isEditing) "Edit Habit" else "New Habit",
modifier = Modifier onBack = onNavigateBack
.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
)
}
}
Column( Column(
modifier = Modifier modifier = Modifier
@@ -1,25 +1,12 @@
package com.example.madwellbeingapp.ui.screens 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.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.fillMaxSize 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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items 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.MaterialTheme
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text import androidx.compose.material3.Text
@@ -28,16 +15,10 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier 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 androidx.compose.ui.unit.dp
import com.example.madwellbeingapp.data.model.Habit import com.example.madwellbeingapp.data.model.Habit
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel 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 @Composable
fun AllActivitiesScreen( fun AllActivitiesScreen(
viewModel: HabitViewModel, viewModel: HabitViewModel,
@@ -46,69 +27,37 @@ fun AllActivitiesScreen(
) { ) {
val habits by viewModel.allHabits.collectAsState() val habits by viewModel.allHabits.collectAsState()
val todayLogs by viewModel.todayLogs.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 -> Scaffold { padding ->
Column( Column(modifier = Modifier.fillMaxSize().padding(padding)) {
modifier = Modifier ScreenHeader(title = "All Activities", onBack = onNavigateBack)
.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
)
}
}
if (habits.isEmpty()) { if (habits.isEmpty()) {
Box( Box(modifier = Modifier.fillMaxSize().padding(32.dp), contentAlignment = Alignment.Center) {
modifier = Modifier Text("No activities yet. Go to Manage Activities to create one!",
.fillMaxSize()
.padding(32.dp),
contentAlignment = Alignment.Center
) {
Text(
text = "No activities yet. Go to Manage Activities to create one!",
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant color = MaterialTheme.colorScheme.onSurfaceVariant)
)
} }
} else { } else {
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp),
.fillMaxSize()
.padding(horizontal = 16.dp, vertical = 12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp) verticalArrangement = Arrangement.spacedBy(10.dp)
) { ) {
items(habits, key = { it.id }) { habit -> items(habits, key = { it.id }) { habit ->
AllActivityCard( val done = completedIds.contains(habit.id)
HabitCard(
habit = habit, habit = habit,
isCompletedToday = todayCompletedIds.contains(habit.id), viewModel = viewModel,
onClick = { onHabitClick(habit.id) }, 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 @Composable
private fun AllActivityCard( private fun cardContainerColor(habit: Habit, completedToday: Boolean) = when {
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) !habit.isActive -> MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
isCompletedToday -> MaterialTheme.colorScheme.primaryContainer completedToday -> MaterialTheme.colorScheme.primaryContainer
else -> MaterialTheme.colorScheme.surface 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)
)
}
}
}
}
@@ -76,383 +76,182 @@ fun HabitDetailScreen(
AlertDialog( AlertDialog(
onDismissRequest = { showDeleteDialog = false }, onDismissRequest = { showDeleteDialog = false },
title = { Text("Delete Habit") }, 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 = { confirmButton = {
TextButton(onClick = { 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!!) viewModel.deleteHabit(habit!!)
showDeleteDialog = false showDeleteDialog = false
onNavigateBack() onNavigateBack()
}) { }) { Text("Delete", color = MaterialTheme.colorScheme.error) }
Text("Delete", color = MaterialTheme.colorScheme.error)
}
}, },
dismissButton = { dismissButton = { TextButton(onClick = { showDeleteDialog = false }) { Text("Cancel") } }
TextButton(onClick = { showDeleteDialog = false }) { Text("Cancel") }
}
) )
} }
Scaffold { padding -> Scaffold { padding ->
Column( Column(modifier = Modifier.fillMaxSize().padding(padding)) {
modifier = Modifier ScreenHeader(
.fillMaxSize() title = habit?.displayName ?: "Habit Detail",
.padding(padding) onBack = onNavigateBack,
) { trailing = {
// 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
)
}
Row { Row {
Text( Text("✏️", modifier = Modifier.clickable { onEditHabit(habitId) }.padding(horizontal = 8.dp),
text = "✏️", style = MaterialTheme.typography.titleLarge)
modifier = Modifier Text("🗑️", modifier = Modifier.clickable { showDeleteDialog = true }.padding(horizontal = 8.dp),
.clickable { onEditHabit(habitId) } style = MaterialTheme.typography.titleLarge)
.padding(horizontal = 8.dp), }
style = MaterialTheme.typography.titleLarge }
) )
Text(
text = "🗑️",
modifier = Modifier
.clickable { showDeleteDialog = true }
.padding(horizontal = 8.dp),
style = MaterialTheme.typography.titleLarge
)
}
}
}
if (habit == null) { if (habit == null) {
Box( Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Text("Loading…") }
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text("Loading…")
}
return@Scaffold return@Scaffold
} }
val h = habit!! val h = habit!!
val dateFormat = SimpleDateFormat("MMM d, yyyy", Locale.getDefault()) val fmt = SimpleDateFormat("MMM d, yyyy", Locale.getDefault())
Column( Column(
modifier = Modifier modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(20.dp),
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(16.dp) verticalArrangement = Arrangement.spacedBy(16.dp)
) { ) {
// Info card // Info card
Card( Card(shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), modifier = Modifier.fillMaxWidth()) {
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(2.dp),
modifier = Modifier.fillMaxWidth()
) {
Column(modifier = Modifier.padding(16.dp)) { 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)) Spacer(modifier = Modifier.height(4.dp))
Text( Text("Started: ${fmt.format(Date(h.startDate))}", style = MaterialTheme.typography.bodyMedium)
text = "Started: ${dateFormat.format(Date(h.startDate))}", Text("Target: ${h.targetFrequency}x per week", style = MaterialTheme.typography.bodyMedium)
style = MaterialTheme.typography.bodyMedium
)
Text(
text = "Target: ${h.targetFrequency}x per week",
style = MaterialTheme.typography.bodyMedium
)
if (h.reminderEnabled) { if (h.reminderEnabled) {
Text( Text("Reminder: ${h.reminderHour.toString().padStart(2, '0')}:${h.reminderMinute.toString().padStart(2, '0')}",
text = "Reminder: ${h.reminderHour.toString().padStart(2, '0')}:${h.reminderMinute.toString().padStart(2, '0')}", style = MaterialTheme.typography.bodyMedium)
style = MaterialTheme.typography.bodyMedium
)
} }
} }
} }
// Swipe to mark today as complete (or undo) // Swipe bar
SwipeToCompleteBar( SwipeBar(isCompletedToday) {
isCompletedToday = isCompletedToday, Log.i("HabitDetail", "Swipe '${h.displayName}' id=${h.id} undo=$isCompletedToday")
onSwipeComplete = {
Log.i("HabitDetail", "Swipe-to-complete triggered for habit '${h.displayName}' (id=${h.id}), isUndo=${isCompletedToday}")
viewModel.toggleTodayLog(habitId) viewModel.toggleTodayLog(habitId)
} }
)
// Streak cards // Streaks
Row( Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) {
modifier = Modifier.fillMaxWidth(), StatCard("🔥 Current", "$currentStreak day${if (currentStreak != 1) "s" else ""}", Modifier.weight(1f))
horizontalArrangement = Arrangement.spacedBy(12.dp) StatCard("🏆 Longest", "$longestStreak day${if (longestStreak != 1) "s" else ""}", Modifier.weight(1f))
) {
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)
)
} }
// Weekly progress // Weekly progress
Card( Card(shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), modifier = Modifier.fillMaxWidth()) {
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(2.dp),
modifier = Modifier.fillMaxWidth()
) {
Column(modifier = Modifier.padding(16.dp)) { 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)) Spacer(modifier = Modifier.height(12.dp))
LinearProgressIndicator( LinearProgressIndicator(progress = { weeklyProgress },
progress = { weeklyProgress }, modifier = Modifier.fillMaxWidth().height(12.dp).clip(RoundedCornerShape(6.dp)))
modifier = Modifier
.fillMaxWidth()
.height(12.dp)
.clip(RoundedCornerShape(6.dp)),
)
Spacer(modifier = Modifier.height(6.dp)) Spacer(modifier = Modifier.height(6.dp))
Text( Text("${(weeklyProgress * 100).toInt()}% of weekly goal",
text = "${(weeklyProgress * 100).toInt()}% of weekly goal", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
} }
} }
// Last 14 days heatmap // Last 14 days
Text( Text("Last 14 Days", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
text = "Last 14 Days", val logDates = logs.filter { it.completed }.map { normalise(it.date) }.toSet()
style = MaterialTheme.typography.titleMedium, val today = todayMillis()
fontWeight = FontWeight.SemiBold LazyRow(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
) items((0..13).map { today - it * 86_400_000L }.reversed()) { day ->
val done = logDates.contains(day)
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())
Column(horizontalAlignment = Alignment.CenterHorizontally) { Column(horizontalAlignment = Alignment.CenterHorizontally) {
Box( Box(
modifier = Modifier modifier = Modifier.size(36.dp).clip(CircleShape).background(
.size(36.dp) if (done) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant),
.clip(CircleShape)
.background(
if (completed) MaterialTheme.colorScheme.primary
else MaterialTheme.colorScheme.surfaceVariant
),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Text(if (done) "✓" else "",
text = if (completed) "✓" else "", color = if (done) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurfaceVariant,
color = if (completed) MaterialTheme.colorScheme.onPrimary style = MaterialTheme.typography.labelSmall)
else MaterialTheme.colorScheme.onSurfaceVariant,
style = MaterialTheme.typography.labelSmall
)
} }
Spacer(modifier = Modifier.height(2.dp)) Spacer(modifier = Modifier.height(2.dp))
Text( Text(SimpleDateFormat("dd", Locale.getDefault()).format(Date(day)),
text = dayFormat.format(Date(dayMillis)), style = MaterialTheme.typography.labelSmall, textAlign = TextAlign.Center)
style = MaterialTheme.typography.labelSmall,
textAlign = TextAlign.Center
)
} }
} }
} }
// Total completions // Total completions
val totalCompletions = logs.count { it.completed } val total = logs.count { it.completed }
Card( Card(shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp), modifier = Modifier.fillMaxWidth()) {
shape = RoundedCornerShape(16.dp), Row(modifier = Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) {
elevation = CardDefaults.cardElevation(2.dp),
modifier = Modifier.fillMaxWidth()
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text("📊", style = MaterialTheme.typography.headlineMedium) Text("📊", style = MaterialTheme.typography.headlineMedium)
Spacer(modifier = Modifier.width(12.dp)) Spacer(modifier = Modifier.width(12.dp))
Column { Column {
Text( Text("Total Completions", style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold)
"Total Completions", Text("$total time${if (total != 1) "s" else ""}",
style = MaterialTheme.typography.titleSmall, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
fontWeight = FontWeight.SemiBold
)
Text(
"$totalCompletions time${if (totalCompletions != 1) "s" else ""}",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
} }
} }
} }
Spacer(modifier = Modifier.height(16.dp)) 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 @Composable
private fun SwipeToCompleteBar( private fun SwipeBar(isCompleted: Boolean, onSwipe: () -> Unit) {
isCompletedToday: Boolean, val threshold = 200f
onSwipeComplete: () -> Unit
) {
val swipeThreshold = 200f
var offsetX by remember { mutableFloatStateOf(0f) } var offsetX by remember { mutableFloatStateOf(0f) }
Box(modifier = Modifier.fillMaxWidth().height(56.dp).clip(RoundedCornerShape(14.dp))) {
Box( Box(
modifier = Modifier modifier = Modifier.matchParentSize().background(
.fillMaxWidth() if (isCompleted) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.primary.copy(alpha = 0.15f)),
.height(56.dp)
.clip(RoundedCornerShape(14.dp))
) {
// Background
Box(
modifier = Modifier
.matchParentSize()
.background(
if (isCompletedToday) MaterialTheme.colorScheme.secondaryContainer
else MaterialTheme.colorScheme.primary.copy(alpha = 0.15f)
),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Text(if (isCompleted) "Swipe → to Undo" else "Swipe → to Complete",
text = if (isCompletedToday) "← Swipe → to Undo" style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold,
else "← Swipe → to Complete Today", color = if (isCompleted) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.primary)
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Bold,
color = if (isCompletedToday) MaterialTheme.colorScheme.onSecondaryContainer
else MaterialTheme.colorScheme.primary
)
} }
// Draggable thumb
Box( Box(
modifier = Modifier modifier = Modifier.size(80.dp, 56.dp)
.size(width = 80.dp, height = 56.dp)
.offset { IntOffset(offsetX.roundToInt(), 0) } .offset { IntOffset(offsetX.roundToInt(), 0) }
.clip(RoundedCornerShape(14.dp)) .clip(RoundedCornerShape(14.dp))
.background( .background(MaterialTheme.colorScheme.primary)
if (isCompletedToday) MaterialTheme.colorScheme.secondary .pointerInput(isCompleted) {
else MaterialTheme.colorScheme.primary
)
.pointerInput(isCompletedToday) {
detectHorizontalDragGestures( detectHorizontalDragGestures(
onDragEnd = { onDragEnd = { if (offsetX > threshold) onSwipe(); offsetX = 0f },
if (offsetX > swipeThreshold) {
Log.d("SwipeGesture", "Detail swipe threshold reached, toggling completion")
onSwipeComplete()
}
offsetX = 0f
},
onDragCancel = { offsetX = 0f }, onDragCancel = { offsetX = 0f },
onHorizontalDrag = { _, dragAmount -> onHorizontalDrag = { _, d -> offsetX = (offsetX + d).coerceIn(0f, threshold + 50f) })
offsetX = (offsetX + dragAmount).coerceIn(0f, swipeThreshold + 50f)
}
)
}, },
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Text(if (isCompleted) "✅" else "→", style = MaterialTheme.typography.titleLarge,
text = if (isCompletedToday) "✅" else "→", color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Bold)
style = MaterialTheme.typography.titleLarge,
color = MaterialTheme.colorScheme.onPrimary,
fontWeight = FontWeight.Bold
)
} }
} }
} }
@Composable @Composable
private fun StatCard(title: String, value: String, modifier: Modifier = Modifier) { private fun StatCard(title: String, value: String, modifier: Modifier = Modifier) {
Card( Card(modifier = modifier, shape = RoundedCornerShape(16.dp), elevation = CardDefaults.cardElevation(2.dp),
modifier = modifier, colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer)) {
shape = RoundedCornerShape(16.dp), Column(modifier = Modifier.padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally) {
elevation = CardDefaults.cardElevation(2.dp), Text(title, style = MaterialTheme.typography.labelMedium, textAlign = TextAlign.Center)
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
)
Spacer(modifier = Modifier.height(6.dp)) Spacer(modifier = Modifier.height(6.dp))
Text( Text(value, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold,
text = value, color = MaterialTheme.colorScheme.primary, textAlign = TextAlign.Center)
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary,
textAlign = TextAlign.Center
)
} }
} }
} }
private fun normaliseToDay(millis: Long): Long { private fun normalise(millis: Long): Long {
val cal = Calendar.getInstance() val c = Calendar.getInstance(); c.timeInMillis = millis
cal.timeInMillis = millis c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0)
cal.set(Calendar.HOUR_OF_DAY, 0) return c.timeInMillis
cal.set(Calendar.MINUTE, 0)
cal.set(Calendar.SECOND, 0)
cal.set(Calendar.MILLISECOND, 0)
return cal.timeInMillis
} }
private fun todayStartMillis(): Long { private fun todayMillis(): Long {
val cal = Calendar.getInstance() val c = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY, 0) c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0)
cal.set(Calendar.MINUTE, 0) return c.timeInMillis
cal.set(Calendar.SECOND, 0)
cal.set(Calendar.MILLISECOND, 0)
return cal.timeInMillis
} }
@@ -47,10 +47,6 @@ import java.util.Date
import java.util.Locale import java.util.Locale
import kotlin.math.roundToInt 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 @Composable
fun HomeScreen( fun HomeScreen(
viewModel: HabitViewModel, viewModel: HabitViewModel,
@@ -60,107 +56,64 @@ fun HomeScreen(
) { ) {
val habits by viewModel.allHabits.collectAsState() val habits by viewModel.allHabits.collectAsState()
val todayLogs by viewModel.todayLogs.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()
// Only show active habits on this management screen
val activeHabits = habits.filter { it.isActive } val activeHabits = habits.filter { it.isActive }
Scaffold { padding -> Scaffold { padding ->
Column(modifier = Modifier.fillMaxSize().padding(padding)) {
// Extended header with summary
Column( Column(
modifier = Modifier modifier = Modifier.fillMaxWidth()
.fillMaxSize()
.padding(padding)
) {
// Header with back button
Column(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.primary) .background(MaterialTheme.colorScheme.primary)
.padding(horizontal = 20.dp, vertical = 24.dp) .padding(horizontal = 20.dp, vertical = 24.dp)
) { ) {
Row(verticalAlignment = Alignment.CenterVertically) { 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, style = MaterialTheme.typography.titleLarge,
color = MaterialTheme.colorScheme.onPrimary, color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Bold)
fontWeight = FontWeight.Bold Text("Manage Activities", style = MaterialTheme.typography.headlineSmall,
) color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Bold)
Text(
text = "Manage Activities",
style = MaterialTheme.typography.headlineSmall,
color = MaterialTheme.colorScheme.onPrimary,
fontWeight = FontWeight.Bold
)
} }
Spacer(modifier = Modifier.height(4.dp)) Spacer(modifier = Modifier.height(4.dp))
val dateFormat = SimpleDateFormat("EEEE, MMM d", Locale.getDefault()) Text(SimpleDateFormat("EEEE, MMM d", Locale.getDefault()).format(Date()),
Text(
text = dateFormat.format(Date()),
style = MaterialTheme.typography.bodyMedium, 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)) Spacer(modifier = Modifier.height(12.dp))
// Quick summary val done = activeHabits.count { completedIds.contains(it.id) }
val completedToday = activeHabits.count { todayCompletedIds.contains(it.id) }
val total = activeHabits.size val total = activeHabits.size
Text( Text("$done / $total habits completed today",
text = "$completedToday / $total habits completed today",
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onPrimary, color = MaterialTheme.colorScheme.onPrimary, fontWeight = FontWeight.Medium)
fontWeight = FontWeight.Medium
)
if (total > 0) { if (total > 0) {
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
LinearProgressIndicator( LinearProgressIndicator(
progress = { completedToday.toFloat() / total }, progress = { done.toFloat() / total },
modifier = Modifier modifier = Modifier.fillMaxWidth().height(8.dp).clip(RoundedCornerShape(4.dp)),
.fillMaxWidth()
.height(8.dp)
.clip(RoundedCornerShape(4.dp)),
color = MaterialTheme.colorScheme.onPrimary, 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()) { if (activeHabits.isEmpty()) {
Box( Box(modifier = Modifier.fillMaxSize().weight(1f).padding(32.dp), contentAlignment = Alignment.Center) {
modifier = Modifier
.fillMaxSize()
.weight(1f)
.padding(32.dp),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) { 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)) Spacer(modifier = Modifier.height(8.dp))
Text( Text("Tap the button below to create your first habit", style = MaterialTheme.typography.bodyMedium,
text = "Tap the button below to create your first habit", color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f))
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f)
)
} }
} }
} else { } else {
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier.weight(1f).fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp),
.weight(1f)
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp),
verticalArrangement = Arrangement.spacedBy(10.dp) verticalArrangement = Arrangement.spacedBy(10.dp)
) { ) {
items(activeHabits, key = { it.id }) { habit -> items(activeHabits, key = { it.id }) { habit ->
SwipeToCompleteHabitCard( SwipeHabitCard(
habit = habit, habit = habit,
isCompletedToday = todayCompletedIds.contains(habit.id), completed = completedIds.contains(habit.id),
onSwipeComplete = { viewModel.toggleTodayLog(habit.id) }, onSwipe = { viewModel.toggleTodayLog(habit.id) },
onClick = { onHabitClick(habit.id) }, onClick = { onHabitClick(habit.id) },
viewModel = viewModel viewModel = viewModel
) )
@@ -168,173 +121,93 @@ fun HomeScreen(
} }
} }
// Add habit button at the bottom
Button( Button(
onClick = onAddHabit, onClick = onAddHabit,
modifier = Modifier modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp).height(52.dp),
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp)
.height(52.dp),
shape = RoundedCornerShape(14.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
)
} }
} }
} }
} }
/** /** Habit card with swipe-to-complete gesture. */
* 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.
*/
@Composable @Composable
private fun SwipeToCompleteHabitCard( private fun SwipeHabitCard(
habit: Habit, habit: Habit, completed: Boolean, onSwipe: () -> Unit,
isCompletedToday: Boolean, onClick: () -> Unit, viewModel: HabitViewModel
onSwipeComplete: () -> Unit,
onClick: () -> Unit,
viewModel: HabitViewModel
) { ) {
val weeklyProgress by viewModel.weeklyProgress(habit.id) val weeklyProgress by viewModel.weeklyProgress(habit.id).collectAsState(initial = 0f)
.collectAsState(initial = 0f) val threshold = 150f
val swipeThreshold = 150f
var offsetX by remember { mutableFloatStateOf(0f) } var offsetX by remember { mutableFloatStateOf(0f) }
Box(modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(16.dp))) {
// Swipe background
Box( Box(
modifier = Modifier modifier = Modifier.matchParentSize().background(
.fillMaxWidth() if (completed) MaterialTheme.colorScheme.error.copy(alpha = 0.2f)
.clip(RoundedCornerShape(16.dp)) else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)),
) {
// Background revealed on swipe
Box(
modifier = Modifier
.matchParentSize()
.background(
if (isCompletedToday) MaterialTheme.colorScheme.error.copy(alpha = 0.2f)
else MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)
),
contentAlignment = Alignment.CenterStart contentAlignment = Alignment.CenterStart
) { ) {
Text( Text(
text = if (isCompletedToday) " ← Undo" else " ← Swipe to Complete →", if (completed) " ← Undo" else " Swipe → to Complete",
modifier = Modifier.padding(start = 16.dp), modifier = Modifier.padding(start = 16.dp),
style = MaterialTheme.typography.bodyMedium, style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold,
fontWeight = FontWeight.Bold, color = if (completed) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary)
color = if (isCompletedToday) MaterialTheme.colorScheme.error
else MaterialTheme.colorScheme.primary
)
} }
// Sliding card
// Foreground card that slides
Card( Card(
modifier = Modifier modifier = Modifier.fillMaxWidth()
.fillMaxWidth()
.offset { IntOffset(offsetX.roundToInt(), 0) } .offset { IntOffset(offsetX.roundToInt(), 0) }
.pointerInput(isCompletedToday) { .pointerInput(completed) {
detectHorizontalDragGestures( detectHorizontalDragGestures(
onDragEnd = { onDragEnd = {
if (offsetX > swipeThreshold) { if (offsetX > threshold) {
Log.d("SwipeGesture", "Swipe threshold reached for habit '${habit.displayName}' (id=${habit.id}), toggling completion") Log.d("SwipeGesture", "Swiped '${habit.displayName}'")
onSwipeComplete() onSwipe()
} }
offsetX = 0f offsetX = 0f
}, },
onDragCancel = { onDragCancel = { offsetX = 0f },
offsetX = 0f onHorizontalDrag = { _, d -> offsetX = (offsetX + d).coerceIn(0f, threshold + 50f) }
},
onHorizontalDrag = { _, dragAmount ->
// Only allow rightward swipe (positive direction)
offsetX = (offsetX + dragAmount).coerceIn(0f, swipeThreshold + 50f)
}
) )
} }.clickable(onClick = onClick),
.clickable(onClick = onClick),
shape = RoundedCornerShape(16.dp), shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), elevation = CardDefaults.cardElevation(2.dp),
colors = CardDefaults.cardColors( colors = CardDefaults.cardColors(
containerColor = if (isCompletedToday) containerColor = if (completed) MaterialTheme.colorScheme.primaryContainer
MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.surface)
else MaterialTheme.colorScheme.surface
)
) { ) {
Row( Row(modifier = Modifier.fillMaxWidth().padding(16.dp), verticalAlignment = Alignment.CenterVertically) {
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
// Status indicator circle
Box( Box(
modifier = Modifier modifier = Modifier.size(44.dp).clip(CircleShape).background(
.size(44.dp) if (completed) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant),
.clip(CircleShape)
.background(
if (isCompletedToday)
MaterialTheme.colorScheme.primary
else
MaterialTheme.colorScheme.surfaceVariant
),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Text(if (completed) "✓" else "",
text = if (isCompletedToday) "✓" else "", color = if (completed) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurfaceVariant,
color = if (isCompletedToday) fontWeight = FontWeight.Bold, style = MaterialTheme.typography.titleMedium)
MaterialTheme.colorScheme.onPrimary
else
MaterialTheme.colorScheme.onSurfaceVariant,
fontWeight = FontWeight.Bold,
style = MaterialTheme.typography.titleMedium
)
} }
Spacer(modifier = Modifier.width(14.dp)) Spacer(modifier = Modifier.width(14.dp))
Column(modifier = Modifier.weight(1f)) { 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)) Spacer(modifier = Modifier.height(2.dp))
Text( Text(habit.activityType + " • ${habit.targetFrequency}x/week",
text = habit.activityType + " • ${habit.targetFrequency}x/week", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
// Weekly progress bar
LinearProgressIndicator( LinearProgressIndicator(
progress = { weeklyProgress }, progress = { weeklyProgress },
modifier = Modifier modifier = Modifier.fillMaxWidth().height(6.dp).clip(RoundedCornerShape(3.dp)),
.fillMaxWidth() color = MaterialTheme.colorScheme.primary, trackColor = MaterialTheme.colorScheme.surfaceVariant)
.height(6.dp)
.clip(RoundedCornerShape(3.dp)),
color = MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
Spacer(modifier = Modifier.height(2.dp)) Spacer(modifier = Modifier.height(2.dp))
Row( Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
modifier = Modifier.fillMaxWidth(), Text("Weekly: ${(weeklyProgress * 100).toInt()}%",
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = "Weekly: ${(weeklyProgress * 100).toInt()}%",
style = MaterialTheme.typography.labelSmall, style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f))
) if (!completed) Text("Swipe → to complete",
if (!isCompletedToday) {
Text(
text = "Swipe → to complete",
style = MaterialTheme.typography.labelSmall, style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f) color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f))
)
}
} }
} }
} }
@@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.layout.statusBarsPadding
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.ButtonDefaults
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@@ -45,9 +44,7 @@ fun LandingScreen(
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
modifier = Modifier modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp)
.fillMaxWidth()
.padding(horizontal = 32.dp)
) { ) {
// App title // App title
Text( Text(
@@ -67,64 +64,22 @@ fun LandingScreen(
Spacer(modifier = Modifier.height(48.dp)) Spacer(modifier = Modifier.height(48.dp))
// Top button — All Activities LandingButton("All Activities", onAllActivities)
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
)
}
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
LandingButton("Manage Activities", onManageActivities)
// 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
)
}
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
LandingButton("Active Only", onActiveOnly)
}
}
}
// Bottom button — Active Only @Composable
private fun LandingButton(text: String, onClick: () -> Unit) {
Button( Button(
onClick = onActiveOnly, onClick = onClick,
modifier = Modifier modifier = Modifier.fillMaxWidth().height(56.dp),
.fillMaxWidth() shape = RoundedCornerShape(16.dp)
.height(56.dp),
shape = RoundedCornerShape(16.dp),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.tertiary
)
) { ) {
Text( Text(text = text, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
text = "Active Only",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
} }
} }
}
}
@@ -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)
)
}
}
}
}