mirror of
https://github.com/mudabbir-ahmad/Kotlin-TB2P1.git
synced 2026-12-06 20:13:10 +00:00
Add calendar screen and enhance habit logging features: implement logs retrieval for date ranges, update UI components, and manage habit streaks
This commit is contained in:
1 parent
925f426410
commit
79eb9d4366
18 files changed
+821
-864
No files matched your search
@@ -1,22 +1,21 @@
|
||||
package com.example.madwellbeingapp
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation3.runtime.entryProvider
|
||||
import androidx.navigation3.ui.NavDisplay
|
||||
import com.example.madwellbeingapp.ui.screens.ActiveOnlyScreen
|
||||
import com.example.madwellbeingapp.ui.screens.AddEditHabitScreen
|
||||
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
|
||||
@@ -24,126 +23,67 @@ import com.example.madwellbeingapp.ui.theme.MadWellbeingAppTheme
|
||||
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
private val notificationPermissionLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { /* granted or not */ }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
// Request notification permission on Android 13+
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS)
|
||||
!= PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
}
|
||||
|
||||
setContent {
|
||||
MadWellbeingAppTheme {
|
||||
HabitNavGraph()
|
||||
}
|
||||
}
|
||||
setContent { MadWellbeingAppTheme { HabitNavGraph() } }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple sealed class representing each screen destination.
|
||||
* We avoid using Navigation Compose library and use manual state-based navigation.
|
||||
*/
|
||||
// ── Screen destinations ─────────────────────────────────────
|
||||
sealed class Screen {
|
||||
/** Landing page with 3 navigation buttons — first page the user sees. */
|
||||
data object Landing : Screen()
|
||||
/** All activities — past completed + currently active. */
|
||||
data object AllActivities : Screen()
|
||||
/** Manage activities — add new habits, swipe to complete. */
|
||||
data object Home : Screen()
|
||||
/** Active only — habits still to-do today. */
|
||||
data object ActiveOnly : Screen()
|
||||
data object Calendar : Screen()
|
||||
data object AddHabit : Screen()
|
||||
data class EditHabit(val habitId: Int) : Screen()
|
||||
data class HabitDetail(val habitId: Int) : Screen()
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun HabitNavGraph(viewModel: HabitViewModel = viewModel()) {
|
||||
// Manual back-stack — starts on the Landing screen
|
||||
val backStack = remember { mutableStateListOf<Screen>(Screen.Landing) }
|
||||
val currentScreen = backStack.last()
|
||||
fun HabitNavGraph(vm: HabitViewModel = viewModel()) {
|
||||
val backStack = remember { mutableStateListOf<Any>(Screen.Landing) }
|
||||
|
||||
fun navigateTo(screen: Screen) {
|
||||
backStack.add(screen)
|
||||
}
|
||||
fun nav(screen: Screen) = backStack.add(screen)
|
||||
fun back() { if (backStack.size > 1) backStack.removeLast() }
|
||||
|
||||
fun navigateBack() {
|
||||
if (backStack.size > 1) {
|
||||
backStack.removeAt(backStack.lastIndex)
|
||||
}
|
||||
}
|
||||
|
||||
when (currentScreen) {
|
||||
is Screen.Landing -> {
|
||||
LandingScreen(
|
||||
onAllActivities = { navigateTo(Screen.AllActivities) },
|
||||
onManageActivities = { navigateTo(Screen.Home) },
|
||||
onActiveOnly = { navigateTo(Screen.ActiveOnly) }
|
||||
)
|
||||
}
|
||||
|
||||
is Screen.AllActivities -> {
|
||||
AllActivitiesScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navigateBack() },
|
||||
onHabitClick = { id -> navigateTo(Screen.HabitDetail(id)) }
|
||||
)
|
||||
}
|
||||
|
||||
is Screen.Home -> {
|
||||
HomeScreen(
|
||||
viewModel = viewModel,
|
||||
onAddHabit = { navigateTo(Screen.AddHabit) },
|
||||
onHabitClick = { id -> navigateTo(Screen.HabitDetail(id)) },
|
||||
onNavigateBack = { navigateBack() }
|
||||
)
|
||||
}
|
||||
|
||||
is Screen.ActiveOnly -> {
|
||||
ActiveOnlyScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navigateBack() },
|
||||
onHabitClick = { id -> navigateTo(Screen.HabitDetail(id)) }
|
||||
)
|
||||
}
|
||||
|
||||
is Screen.AddHabit -> {
|
||||
AddEditHabitScreen(
|
||||
viewModel = viewModel,
|
||||
existingHabit = null,
|
||||
onNavigateBack = { navigateBack() }
|
||||
)
|
||||
}
|
||||
|
||||
is Screen.EditHabit -> {
|
||||
val allHabits by viewModel.allHabits.collectAsState()
|
||||
val habit = allHabits.find { it.id == currentScreen.habitId }
|
||||
if (habit != null) {
|
||||
AddEditHabitScreen(
|
||||
viewModel = viewModel,
|
||||
existingHabit = habit,
|
||||
onNavigateBack = { navigateBack() }
|
||||
NavDisplay(
|
||||
backStack = backStack,
|
||||
entryProvider = entryProvider {
|
||||
entry<Screen.Landing> {
|
||||
LandingScreen(
|
||||
onAllActivities = { nav(Screen.AllActivities) },
|
||||
onManageActivities = { nav(Screen.Home) },
|
||||
onActiveOnly = { nav(Screen.ActiveOnly) },
|
||||
onCalendar = { nav(Screen.Calendar) }
|
||||
)
|
||||
}
|
||||
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.ActiveOnly> {
|
||||
ActiveOnlyScreen(vm, onNavigateBack = ::back, onHabitClick = { nav(Screen.HabitDetail(it)) })
|
||||
}
|
||||
entry<Screen.Calendar> {
|
||||
CalendarScreen(vm, onNavigateBack = ::back, onHabitClick = { nav(Screen.HabitDetail(it)) })
|
||||
}
|
||||
entry<Screen.AddHabit> {
|
||||
AddEditHabitScreen(vm, existingHabit = null, onNavigateBack = ::back)
|
||||
}
|
||||
entry<Screen.EditHabit> { key ->
|
||||
val allHabits by vm.allHabits.collectAsState()
|
||||
allHabits.find { it.id == key.habitId }?.let { habit ->
|
||||
AddEditHabitScreen(vm, existingHabit = habit, onNavigateBack = ::back)
|
||||
}
|
||||
}
|
||||
entry<Screen.HabitDetail> { key ->
|
||||
HabitDetailScreen(key.habitId, vm, onNavigateBack = ::back, onEditHabit = { nav(Screen.EditHabit(it)) })
|
||||
}
|
||||
}
|
||||
|
||||
is Screen.HabitDetail -> {
|
||||
HabitDetailScreen(
|
||||
habitId = currentScreen.habitId,
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { navigateBack() },
|
||||
onEditHabit = { id -> navigateTo(Screen.EditHabit(id)) }
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user