Skip to content

Commit

Permalink
Merge branch 'main' into confetti
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Yerkeshev committed Nov 29, 2024
2 parents bfce018 + 8f16de7 commit 17ec886
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 55 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/com/example/androidproject/data/AppDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import com.example.androidproject.data.models.CheckpointEntity
import com.example.androidproject.data.models.QuestEntity
import com.example.androidproject.data.models.TaskEntity
import kotlinx.coroutines.flow.firstOrNull
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter


@Database(entities = [CheckpointEntity::class, TaskEntity::class, QuestEntity::class], version = 1, exportSchema = false)
abstract class AppDB : RoomDatabase() {

abstract fun checkpointDao(): CheckpointDao
abstract fun taskDao(): TaskDao
abstract fun questDao(): QuestDao
Expand Down Expand Up @@ -76,8 +80,9 @@ abstract class AppDB : RoomDatabase() {
val checkpointDao = db.checkpointDao() // Get the CheckpointDao instance
val taskDao = db.taskDao() // Get the TaskDao instance

// Add test data for quests and checkpoints
val yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000
val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")
val currentDate = LocalDateTime.now().format(formatter)


questDao.insert(
QuestEntity(
Expand Down Expand Up @@ -119,7 +124,7 @@ abstract class AppDB : RoomDatabase() {
description = "Completed Quest Example",
category = "Test",
current = false,
completedAt = yesterday.toString()
completedAt = currentDate
)
)

Expand All @@ -129,7 +134,7 @@ abstract class AppDB : RoomDatabase() {
description = "BULL RUN 2025",
category = "Test2",
current = false,
completedAt = yesterday.toString()
completedAt = currentDate
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.androidproject.ui.screens

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
Expand Down Expand Up @@ -31,30 +30,33 @@ import com.example.androidproject.data.models.QuestEntity
import com.example.androidproject.ui.navigation.Screens
import com.example.androidproject.ui.viewmodels.QuestViewModel


// val viewModel = QuestViewModel()
// using viewModel() instead of manually creating new instance of ViewModel(). viewModel() gets the
// ViewModel from the provider, and persists through recomposition of the composable. It means when
// the composable recomposes, the ViewModel stays the same, with all its state variables/data
// val viewModel: QuestViewModel = viewModel()

@Composable
fun QuestsListScreen(
modifier: Modifier = Modifier,
navCtrl: NavController,
questViewModel: QuestViewModel
) {

// val viewModel = QuestViewModel()

// using viewModel() instead of manually creating new instance of ViewModel(). viewModel() gets the
// ViewModel from the provider, and persists through recomposition of the composable. It means when
// the composable recomposes, the ViewModel stays the same, with all its state variables/data
// val viewModel: QuestViewModel = viewModel()

val questsWithCheckpoints by questViewModel.questsWithCheckpoints.collectAsState()
val completedQuests by questViewModel.completedQuests.collectAsState()

// Filter out completed quests
val uncompletedQuestsWithCheckpoints = questsWithCheckpoints.filter {
it.quest.completedAt == null
}

LazyColumn(
modifier = modifier
.fillMaxSize()
.padding(bottom = 56.dp),
contentPadding = PaddingValues(16.dp)
) {
items(questsWithCheckpoints) { questWithCheckpoints ->
items(uncompletedQuestsWithCheckpoints) { questWithCheckpoints ->
var isExpanded by remember { mutableStateOf(questWithCheckpoints.quest.current) }

QuestItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.androidproject.ui.screens

import android.content.Context
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.runtime.Composable
import androidx.compose.material3.Text
Expand All @@ -11,8 +10,6 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -95,65 +92,64 @@ fun WelcomeScreen(
}
}

Spacer(modifier = Modifier.height(16.dp))

Text(
text = "Completed Quests",
style = MaterialTheme.typography.titleLarge,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.align(Alignment.Start)
)

LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(top = 8.dp)
) {
val chunkedQuests = completedQuests.chunked(2)
Log.d("CompletedQuests", "Chunked quests: $chunkedQuests")

items(chunkedQuests) { rowQuests ->
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(4.dp)
) {
rowQuests.forEach { quest ->
CompletedQuestCard(
quest = quest,
modifier = Modifier
.weight(1f)
.padding(8.dp)
.wrapContentHeight()
)
}
if (rowQuests.size < 2) {
Spacer(modifier = Modifier.weight(1f))
}
}
items(completedQuests) { quest ->
CompletedQuestItem(
quest = quest,
completedDate = quest.completedAt ?: "Unknown",
onNavigateToMap = { /* Add navigation logic */ },
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp)
)
}
}
}
}
}

@Composable
fun CompletedQuestCard(quest: QuestEntity, modifier: Modifier = Modifier) {
Card(
fun CompletedQuestItem(
quest: QuestEntity,
completedDate: String,
onNavigateToMap: () -> Unit,
modifier: Modifier = Modifier
) {
Row(
modifier = modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
.height(120.dp),
elevation = CardDefaults.elevatedCardElevation(defaultElevation = 4.dp),
shape = MaterialTheme.shapes.medium
.background(MaterialTheme.colorScheme.surface, RoundedCornerShape(8.dp))
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center
modifier = Modifier.weight(1f)
) {
Text(
text = "Route: ${quest.description}",
style = MaterialTheme.typography.titleMedium
text = quest.description,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurface
)

Spacer(modifier = Modifier.height(8.dp))
Spacer(modifier = Modifier.height(4.dp))

Text(
text = "Status: ${if (quest.completedAt != null) "Completed" else "Not Completed"}",
style = MaterialTheme.typography.bodyMedium,
color = if (quest.completedAt != null) MaterialTheme.colorScheme.secondary else MaterialTheme.colorScheme.error
text = "Completed on: $completedDate",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.secondary
)
}
}
Expand Down

0 comments on commit 17ec886

Please sign in to comment.