archive subjects
This commit is contained in:
		
							parent
							
								
									d4c017ef1b
								
							
						
					
					
						commit
						c3424b9996
					
				
					 5 changed files with 26 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -7,10 +7,16 @@ data class Subject(
 | 
			
		|||
    @DocumentId val id: String = "",
 | 
			
		||||
    val name: String = "",
 | 
			
		||||
    val argb_color: Long = 0,
 | 
			
		||||
    var archived: Boolean = false,
 | 
			
		||||
    @get:Exclude @set:Exclude
 | 
			
		||||
    var taskCount: Int = 0,
 | 
			
		||||
    @get:Exclude @set:Exclude
 | 
			
		||||
    var taskCompletedCount: Int = 0,
 | 
			
		||||
    @get:Exclude @set:Exclude
 | 
			
		||||
    var time: Int = 0,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
object SubjectDocument {
 | 
			
		||||
    const val id = "id"
 | 
			
		||||
    const val name = "name"
 | 
			
		||||
    const val archived = "archived"
 | 
			
		||||
    const val argb_color = "argb_color"
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,12 +1,14 @@
 | 
			
		|||
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.SubjectDocument
 | 
			
		||||
import be.ugent.sel.studeez.domain.AccountDAO
 | 
			
		||||
import be.ugent.sel.studeez.domain.SubjectDAO
 | 
			
		||||
import be.ugent.sel.studeez.domain.TaskDAO
 | 
			
		||||
import com.google.firebase.firestore.AggregateSource
 | 
			
		||||
import com.google.firebase.firestore.CollectionReference
 | 
			
		||||
import com.google.firebase.firestore.FirebaseFirestore
 | 
			
		||||
import com.google.firebase.firestore.Query
 | 
			
		||||
import com.google.firebase.firestore.ktx.snapshots
 | 
			
		||||
import com.google.firebase.firestore.ktx.toObject
 | 
			
		||||
import kotlinx.coroutines.flow.Flow
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +23,7 @@ class FireBaseSubjectDAO @Inject constructor(
 | 
			
		|||
) : SubjectDAO {
 | 
			
		||||
    override fun getSubjects(): Flow<List<Subject>> {
 | 
			
		||||
        return currentUserSubjectsCollection()
 | 
			
		||||
            .subjectNotArchived()
 | 
			
		||||
            .snapshots()
 | 
			
		||||
            .map { it.toObjects(Subject::class.java) }
 | 
			
		||||
            .map { subjects ->
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +53,7 @@ class FireBaseSubjectDAO @Inject constructor(
 | 
			
		|||
 | 
			
		||||
    override suspend fun getTaskCount(subject: Subject): Int {
 | 
			
		||||
        return subjectTasksCollection(subject)
 | 
			
		||||
            .nonArchived()
 | 
			
		||||
            .taskNotArchived()
 | 
			
		||||
            .count()
 | 
			
		||||
            .get(AggregateSource.SERVER)
 | 
			
		||||
            .await()
 | 
			
		||||
| 
						 | 
				
			
			@ -59,8 +62,8 @@ class FireBaseSubjectDAO @Inject constructor(
 | 
			
		|||
 | 
			
		||||
    override suspend fun getCompletedTaskCount(subject: Subject): Int {
 | 
			
		||||
        return subjectTasksCollection(subject)
 | 
			
		||||
            .nonArchived()
 | 
			
		||||
            .completed()
 | 
			
		||||
            .taskNotArchived()
 | 
			
		||||
            .taskNotCompleted()
 | 
			
		||||
            .count()
 | 
			
		||||
            .get(AggregateSource.SERVER)
 | 
			
		||||
            .await()
 | 
			
		||||
| 
						 | 
				
			
			@ -83,4 +86,10 @@ class FireBaseSubjectDAO @Inject constructor(
 | 
			
		|||
            .collection(FireBaseCollections.SUBJECT_COLLECTION)
 | 
			
		||||
            .document(subject.id)
 | 
			
		||||
            .collection(FireBaseCollections.TASK_COLLECTION)
 | 
			
		||||
 | 
			
		||||
    fun CollectionReference.subjectNotArchived(): Query =
 | 
			
		||||
        this.whereEqualTo(SubjectDocument.archived, false)
 | 
			
		||||
 | 
			
		||||
    fun Query.subjectNotArchived(): Query =
 | 
			
		||||
        this.whereEqualTo(SubjectDocument.archived, false)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ class FireBaseTaskDAO @Inject constructor(
 | 
			
		|||
) : TaskDAO {
 | 
			
		||||
    override fun getTasks(subject: Subject): Flow<List<Task>> {
 | 
			
		||||
        return selectedSubjectTasksCollection(subject.id)
 | 
			
		||||
            .nonArchived()
 | 
			
		||||
            .taskNotArchived()
 | 
			
		||||
            .snapshots()
 | 
			
		||||
            .map { it.toObjects(Task::class.java) }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -55,14 +55,14 @@ class FireBaseTaskDAO @Inject constructor(
 | 
			
		|||
 | 
			
		||||
// Extend CollectionReference and Query with some filters
 | 
			
		||||
 | 
			
		||||
fun CollectionReference.nonArchived(): Query =
 | 
			
		||||
fun CollectionReference.taskNotArchived(): Query =
 | 
			
		||||
    this.whereEqualTo(TaskDocument.archived, false)
 | 
			
		||||
 | 
			
		||||
fun Query.nonArchived(): Query =
 | 
			
		||||
fun Query.taskNotArchived(): Query =
 | 
			
		||||
    this.whereEqualTo(TaskDocument.archived, false)
 | 
			
		||||
 | 
			
		||||
fun CollectionReference.completed(): Query =
 | 
			
		||||
fun CollectionReference.taskNotCompleted(): Query =
 | 
			
		||||
    this.whereEqualTo(TaskDocument.completed, true)
 | 
			
		||||
 | 
			
		||||
fun Query.completed(): Query =
 | 
			
		||||
fun Query.taskNotCompleted(): Query =
 | 
			
		||||
    this.whereEqualTo(TaskDocument.completed, true)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -70,7 +70,7 @@ class SubjectEditFormViewModel @Inject constructor(
 | 
			
		|||
    )
 | 
			
		||||
 | 
			
		||||
    fun onDelete(openAndPopUp: (String, String) -> Unit) {
 | 
			
		||||
        subjectDAO.deleteSubject(selectedSubject())
 | 
			
		||||
        subjectDAO.updateSubject(selectedSubject().copy(archived = true))
 | 
			
		||||
        openAndPopUp(StudeezDestinations.SUBJECT_SCREEN, StudeezDestinations.EDIT_SUBJECT_FORM)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,7 +27,6 @@ data class TaskActions(
 | 
			
		|||
    val addTask: () -> Unit,
 | 
			
		||||
    val getSubject: () -> Subject,
 | 
			
		||||
    val getTasks: () -> Flow<List<Task>>,
 | 
			
		||||
    val deleteTask: (Task) -> Unit,
 | 
			
		||||
    val onCheckTask: (Task, Boolean) -> Unit,
 | 
			
		||||
    val editSubject: () -> Unit,
 | 
			
		||||
    val startTask: (Task) -> Unit,
 | 
			
		||||
| 
						 | 
				
			
			@ -39,7 +38,6 @@ fun getTaskActions(viewModel: TaskViewModel, open: (String) -> Unit): TaskAction
 | 
			
		|||
        addTask = { viewModel.addTask(open) },
 | 
			
		||||
        getTasks = viewModel::getTasks,
 | 
			
		||||
        getSubject = viewModel::getSelectedSubject,
 | 
			
		||||
        deleteTask = viewModel::deleteTask,
 | 
			
		||||
        onCheckTask = { task, isChecked -> viewModel.toggleTaskCompleted(task, isChecked) },
 | 
			
		||||
        editSubject = { viewModel.editSubject(open) },
 | 
			
		||||
        startTask = { task -> viewModel.startTask(task, open) },
 | 
			
		||||
| 
						 | 
				
			
			@ -110,7 +108,6 @@ fun TaskScreenPreview() {
 | 
			
		|||
            {},
 | 
			
		||||
            { Subject(name = "Test Subject") },
 | 
			
		||||
            { flowOf() },
 | 
			
		||||
            {},
 | 
			
		||||
            { _, _ -> run {} },
 | 
			
		||||
            {},
 | 
			
		||||
            {},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue