#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

@ -5,4 +5,5 @@ object FireBaseCollections {
const val USER_COLLECTION = "users"
const val TIMER_COLLECTION = "timers"
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.domain.AccountDAO
import be.ugent.sel.studeez.domain.TaskDAO
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.snapshots
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import javax.inject.Inject
class FireBaseTaskDAO @Inject constructor(
@ -14,18 +16,9 @@ class FireBaseTaskDAO @Inject constructor(
private val auth: AccountDAO,
) : TaskDAO {
override fun getTasks(subject: Subject): Flow<List<Task>> {
return flowOf(
listOf(
Task(
name = "Test Task",
completed = false,
),
Task(
name = "Test Task 2",
completed = true,
)
)
)
return selectedSubjectTasksCollection(subject)
.snapshots()
.map { it.toObjects(Task::class.java) }
}
override fun saveTask(newTask: Task) {
@ -36,4 +29,10 @@ class FireBaseTaskDAO @Inject constructor(
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)
}