#22 get tasks from firestore

This commit is contained in:
brreynie 2023-05-02 22:57:35 +02:00
parent e1c05bb0d4
commit 056947e1db
8 changed files with 44 additions and 23 deletions

View file

@ -77,7 +77,7 @@ fun SubjectEntry(
Icon( Icon(
imageVector = Icons.Default.List, contentDescription = "tasks" imageVector = Icons.Default.List, contentDescription = "tasks"
) )
Text(text = subject.tasks.size.toString()) Text(text = "0/0") // TODO
} }
} }
} }

View file

@ -0,0 +1,18 @@
package be.ugent.sel.studeez.data
import be.ugent.sel.studeez.data.local.models.task.Subject
import javax.inject.Inject
import javax.inject.Singleton
/**
* Used to communicate the selected subject from the subject overview to the task overview of that subject.
* Because this is a singleton-class the view-models of both screens observe the same data.
*/
@Singleton
class SelectedSubject @Inject constructor() {
private lateinit var subject: Subject
operator fun invoke() = subject
fun set(subject: Subject) {
this.subject = subject
}
}

View file

@ -5,7 +5,6 @@ import com.google.firebase.firestore.DocumentId
data class Subject( data class Subject(
@DocumentId val id: String = "", @DocumentId val id: String = "",
val name: String = "", val name: String = "",
val tasks: List<Task> = mutableListOf(),
val time: Int = 0, val time: Int = 0,
val argb_color: Long = 0, val argb_color: Long = 0,
) )

View file

@ -5,4 +5,5 @@ object FireBaseCollections {
const val USER_COLLECTION = "users" const val USER_COLLECTION = "users"
const val TIMER_COLLECTION = "timers" const val TIMER_COLLECTION = "timers"
const val SUBJECT_COLLECTION = "subjects" const val SUBJECT_COLLECTION = "subjects"
const val TASK_COLLECTION = "tasks"
} }

View file

@ -4,9 +4,11 @@ import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.data.local.models.task.Task 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.TaskDAO import be.ugent.sel.studeez.domain.TaskDAO
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.snapshots
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
class FireBaseTaskDAO @Inject constructor( class FireBaseTaskDAO @Inject constructor(
@ -14,18 +16,9 @@ class FireBaseTaskDAO @Inject constructor(
private val auth: AccountDAO, private val auth: AccountDAO,
) : TaskDAO { ) : TaskDAO {
override fun getTasks(subject: Subject): Flow<List<Task>> { override fun getTasks(subject: Subject): Flow<List<Task>> {
return flowOf( return selectedSubjectTasksCollection(subject)
listOf( .snapshots()
Task( .map { it.toObjects(Task::class.java) }
name = "Test Task",
completed = false,
),
Task(
name = "Test Task 2",
completed = true,
)
)
)
} }
override fun saveTask(newTask: Task) { override fun saveTask(newTask: Task) {
@ -36,4 +29,10 @@ class FireBaseTaskDAO @Inject constructor(
TODO("Not yet implemented") TODO("Not yet implemented")
} }
private fun selectedSubjectTasksCollection(subject: Subject): CollectionReference =
firestore.collection(FireBaseCollections.USER_COLLECTION)
.document(auth.currentUserId)
.collection(FireBaseCollections.SUBJECT_COLLECTION)
.document(subject.id)
.collection(FireBaseCollections.TASK_COLLECTION)
} }

View file

@ -32,7 +32,7 @@ fun SubjectRoute(
navigationBarActions = navigationBarActions, navigationBarActions = navigationBarActions,
addSubject = { viewModel.addSubject() }, addSubject = { viewModel.addSubject() },
getSubjects = viewModel::getSubjects, getSubjects = viewModel::getSubjects,
onViewSubject = { viewModel.onViewSubject(Subject(), open) }, onViewSubject = { viewModel.onViewSubject(it, open) },
) )
} }
@ -42,7 +42,7 @@ fun SubjectScreen(
navigationBarActions: NavigationBarActions, navigationBarActions: NavigationBarActions,
addSubject: () -> Unit, addSubject: () -> Unit,
getSubjects: () -> Flow<List<Subject>>, getSubjects: () -> Flow<List<Subject>>,
onViewSubject: () -> Unit, onViewSubject: (Subject) -> Unit,
) { ) {
PrimaryScreenTemplate( PrimaryScreenTemplate(
title = resources().getString(R.string.tasks), title = resources().getString(R.string.tasks),
@ -71,7 +71,7 @@ fun SubjectScreen(
items(subjects.value) { items(subjects.value) {
SubjectEntry( SubjectEntry(
subject = it, subject = it,
onViewSubject = onViewSubject, onViewSubject = { onViewSubject(it) },
) )
} }
} }

View file

@ -1,5 +1,7 @@
package be.ugent.sel.studeez.screens.tasks package be.ugent.sel.studeez.screens.tasks
import android.util.Log
import be.ugent.sel.studeez.data.SelectedSubject
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.domain.LogService import be.ugent.sel.studeez.domain.LogService
import be.ugent.sel.studeez.domain.SubjectDAO import be.ugent.sel.studeez.domain.SubjectDAO
@ -12,13 +14,13 @@ import javax.inject.Inject
@HiltViewModel @HiltViewModel
class SubjectViewModel @Inject constructor( class SubjectViewModel @Inject constructor(
private val subjectDAO: SubjectDAO, private val subjectDAO: SubjectDAO,
private val selectedSubject: SelectedSubject,
logService: LogService, logService: LogService,
) : StudeezViewModel(logService) { ) : StudeezViewModel(logService) {
fun addSubject() { fun addSubject() {
subjectDAO.saveSubject( subjectDAO.saveSubject(
Subject( Subject(
name = "Test Subject", name = "Test Subject",
tasks = listOf(),
time = 0, time = 0,
argb_color = 0xFFF44336, argb_color = 0xFFF44336,
) )
@ -30,6 +32,8 @@ class SubjectViewModel @Inject constructor(
} }
fun onViewSubject(subject: Subject, open: (String) -> Unit) { fun onViewSubject(subject: Subject, open: (String) -> Unit) {
Log.v("MYLOG", subject.id)
selectedSubject.set(subject)
open(StudeezDestinations.TASKS_SCREEN) open(StudeezDestinations.TASKS_SCREEN)
} }
} }

View file

@ -1,25 +1,25 @@
package be.ugent.sel.studeez.screens.tasks package be.ugent.sel.studeez.screens.tasks
import be.ugent.sel.studeez.data.local.models.task.Subject import be.ugent.sel.studeez.data.SelectedSubject
import be.ugent.sel.studeez.data.local.models.task.Task import be.ugent.sel.studeez.data.local.models.task.Task
import be.ugent.sel.studeez.domain.LogService import be.ugent.sel.studeez.domain.LogService
import be.ugent.sel.studeez.domain.TaskDAO import be.ugent.sel.studeez.domain.TaskDAO
import be.ugent.sel.studeez.screens.StudeezViewModel import be.ugent.sel.studeez.screens.StudeezViewModel
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
class TaskViewModel @Inject constructor( class TaskViewModel @Inject constructor(
private val taskDAO: TaskDAO, private val taskDAO: TaskDAO,
private val selectedSubject: SelectedSubject,
logService: LogService, logService: LogService,
) : StudeezViewModel(logService) { ) : StudeezViewModel(logService) {
fun addTask() { fun addTask() {
} }
fun getTasks() : Flow<List<Task>> { fun getTasks(): Flow<List<Task>> {
return taskDAO.getTasks(Subject()) return taskDAO.getTasks(selectedSubject())
} }
} }