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