#99 fix subjectentry to use flows for computed values

This commit is contained in:
brreynie 2023-05-10 15:50:54 +02:00
parent b53e3cdc5a
commit de25ed85b5
6 changed files with 40 additions and 36 deletions

View file

@ -31,9 +31,13 @@ import be.ugent.sel.studeez.R.string as AppText
fun SubjectEntry( fun SubjectEntry(
subject: Subject, subject: Subject,
onViewSubject: () -> Unit, onViewSubject: () -> Unit,
getTaskCount: () -> Flow<Int>,
getCompletedTaskCount: () -> Flow<Int>,
getStudyTime: () -> Flow<Int>, getStudyTime: () -> Flow<Int>,
) { ) {
val studytime by getStudyTime().collectAsState(initial = 0) val studytime by getStudyTime().collectAsState(initial = 0)
val taskCount by getTaskCount().collectAsState(initial = 0)
val completedTaskCount by getCompletedTaskCount().collectAsState(initial = 0)
Card( Card(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -80,7 +84,7 @@ fun SubjectEntry(
imageVector = Icons.Default.List, imageVector = Icons.Default.List,
contentDescription = stringResource(id = AppText.tasks) contentDescription = stringResource(id = AppText.tasks)
) )
Text(text = "${subject.taskCompletedCount}/${subject.taskCount}") Text(text = "${completedTaskCount}/${taskCount}")
} }
} }
} }
@ -104,11 +108,11 @@ fun SubjectEntryPreview() {
subject = Subject( subject = Subject(
name = "Test Subject", name = "Test Subject",
argb_color = 0xFFFFD200, argb_color = 0xFFFFD200,
taskCount = 5,
taskCompletedCount = 2,
), ),
onViewSubject = {}, onViewSubject = {},
getStudyTime = { flowOf() } getTaskCount = { flowOf() },
getCompletedTaskCount = { flowOf() },
getStudyTime = { flowOf() },
) )
} }
@ -121,6 +125,8 @@ fun OverflowSubjectEntryPreview() {
argb_color = 0xFFFFD200, argb_color = 0xFFFFD200,
), ),
onViewSubject = {}, onViewSubject = {},
getStudyTime = { flowOf() } getTaskCount = { flowOf() },
getCompletedTaskCount = { flowOf() },
getStudyTime = { flowOf() },
) )
} }

View file

@ -8,10 +8,6 @@ data class Subject(
val name: String = "", val name: String = "",
val argb_color: Long = 0, val argb_color: Long = 0,
var archived: Boolean = false, var archived: Boolean = false,
@get:Exclude @set:Exclude
var taskCount: Int = 0,
@get:Exclude @set:Exclude
var taskCompletedCount: Int = 0,
) )
object SubjectDocument { object SubjectDocument {

View file

@ -13,8 +13,8 @@ interface SubjectDAO {
fun updateSubject(newSubject: Subject) fun updateSubject(newSubject: Subject)
suspend fun getTaskCount(subject: Subject): Int fun getTaskCount(subject: Subject): Flow<Int>
suspend fun getCompletedTaskCount(subject: Subject): Int fun getCompletedTaskCount(subject: Subject): Flow<Int>
fun getStudyTime(subject: Subject): Flow<Int> fun getStudyTime(subject: Subject): Flow<Int>
suspend fun getSubject(subjectId: String): Subject? suspend fun getSubject(subjectId: String): Subject?

View file

@ -2,10 +2,10 @@ package be.ugent.sel.studeez.domain.implementation
import be.ugent.sel.studeez.data.local.models.task.Subject import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.data.local.models.task.SubjectDocument import be.ugent.sel.studeez.data.local.models.task.SubjectDocument
import be.ugent.sel.studeez.data.local.models.task.Task
import be.ugent.sel.studeez.domain.AccountDAO import be.ugent.sel.studeez.domain.AccountDAO
import be.ugent.sel.studeez.domain.SubjectDAO import be.ugent.sel.studeez.domain.SubjectDAO
import be.ugent.sel.studeez.domain.TaskDAO import be.ugent.sel.studeez.domain.TaskDAO
import com.google.firebase.firestore.AggregateSource
import com.google.firebase.firestore.CollectionReference import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query import com.google.firebase.firestore.Query
@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.tasks.await import kotlinx.coroutines.tasks.await
import javax.inject.Inject import javax.inject.Inject
import kotlin.collections.count
class FireBaseSubjectDAO @Inject constructor( class FireBaseSubjectDAO @Inject constructor(
private val firestore: FirebaseFirestore, private val firestore: FirebaseFirestore,
@ -26,13 +27,6 @@ class FireBaseSubjectDAO @Inject constructor(
.subjectNotArchived() .subjectNotArchived()
.snapshots() .snapshots()
.map { it.toObjects(Subject::class.java) } .map { it.toObjects(Subject::class.java) }
.map { subjects ->
subjects.map { subject ->
subject.taskCount = getTaskCount(subject)
subject.taskCompletedCount = getCompletedTaskCount(subject)
subject
}
}
} }
override suspend fun getSubject(subjectId: String): Subject? { override suspend fun getSubject(subjectId: String): Subject? {
@ -51,23 +45,14 @@ class FireBaseSubjectDAO @Inject constructor(
currentUserSubjectsCollection().document(newSubject.id).set(newSubject) currentUserSubjectsCollection().document(newSubject.id).set(newSubject)
} }
override suspend fun getTaskCount(subject: Subject): Int { override fun getTaskCount(subject: Subject): Flow<Int> {
return subjectTasksCollection(subject) return taskDAO.getTasks(subject)
.taskNotArchived() .map(List<Task>::count)
.count()
.get(AggregateSource.SERVER)
.await()
.count.toInt()
} }
override suspend fun getCompletedTaskCount(subject: Subject): Int { override fun getCompletedTaskCount(subject: Subject): Flow<Int> {
return subjectTasksCollection(subject) return taskDAO.getTasks(subject)
.taskNotArchived() .map { tasks -> tasks.count { it.completed && !it.archived } }
.taskNotCompleted()
.count()
.get(AggregateSource.SERVER)
.await()
.count.toInt()
} }
override fun getStudyTime(subject: Subject): Flow<Int> { override fun getStudyTime(subject: Subject): Flow<Int> {

View file

@ -36,6 +36,8 @@ fun SubjectRoute(
navigationBarActions = navigationBarActions, navigationBarActions = navigationBarActions,
onAddSubject = { viewModel.onAddSubject(open) }, onAddSubject = { viewModel.onAddSubject(open) },
onViewSubject = { viewModel.onViewSubject(it, open) }, onViewSubject = { viewModel.onViewSubject(it, open) },
getTaskCount = viewModel::getTaskCount,
getCompletedTaskCount = viewModel::getCompletedTaskCount,
getStudyTime = viewModel::getStudyTime, getStudyTime = viewModel::getStudyTime,
uiState, uiState,
) )
@ -47,6 +49,8 @@ fun SubjectScreen(
navigationBarActions: NavigationBarActions, navigationBarActions: NavigationBarActions,
onAddSubject: () -> Unit, onAddSubject: () -> Unit,
onViewSubject: (Subject) -> Unit, onViewSubject: (Subject) -> Unit,
getTaskCount: (Subject) -> Flow<Int>,
getCompletedTaskCount: (Subject) -> Flow<Int>,
getStudyTime: (Subject) -> Flow<Int>, getStudyTime: (Subject) -> Flow<Int>,
uiState: SubjectUiState, uiState: SubjectUiState,
) { ) {
@ -76,6 +80,8 @@ fun SubjectScreen(
SubjectEntry( SubjectEntry(
subject = it, subject = it,
onViewSubject = { onViewSubject(it) }, onViewSubject = { onViewSubject(it) },
getTaskCount = { getTaskCount(it) },
getCompletedTaskCount = { getCompletedTaskCount(it) },
getStudyTime = { getStudyTime(it) }, getStudyTime = { getStudyTime(it) },
) )
} }
@ -94,13 +100,14 @@ fun SubjectScreenPreview() {
navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}), navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}),
onAddSubject = {}, onAddSubject = {},
onViewSubject = {}, onViewSubject = {},
getTaskCount = { flowOf() },
getCompletedTaskCount = { flowOf() },
getStudyTime = { flowOf() }, getStudyTime = { flowOf() },
uiState = SubjectUiState.Succes( uiState = SubjectUiState.Succes(
listOf( listOf(
Subject( Subject(
name = "Test Subject", name = "Test Subject",
argb_color = 0xFFFFD200, argb_color = 0xFFFFD200,
taskCount = 5, taskCompletedCount = 2,
) )
) )
) )
@ -115,7 +122,9 @@ fun SubjectScreenLoadingPreview() {
navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}), navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}),
onAddSubject = {}, onAddSubject = {},
onViewSubject = {}, onViewSubject = {},
getTaskCount = { flowOf() },
getCompletedTaskCount = { flowOf() },
getStudyTime = { flowOf() }, getStudyTime = { flowOf() },
uiState = SubjectUiState.Loading uiState = SubjectUiState.Loading,
) )
} }

View file

@ -30,6 +30,14 @@ class SubjectViewModel @Inject constructor(
open(StudeezDestinations.ADD_SUBJECT_FORM) open(StudeezDestinations.ADD_SUBJECT_FORM)
} }
fun getTaskCount(subject: Subject): Flow<Int> {
return subjectDAO.getTaskCount(subject)
}
fun getCompletedTaskCount(subject: Subject): Flow<Int> {
return subjectDAO.getCompletedTaskCount(subject)
}
fun getStudyTime(subject: Subject): Flow<Int> { fun getStudyTime(subject: Subject): Flow<Int> {
return subjectDAO.getStudyTime(subject) return subjectDAO.getStudyTime(subject)
} }