#22 get tasks from firestore
This commit is contained in:
		
							parent
							
								
									e1c05bb0d4
								
							
						
					
					
						commit
						056947e1db
					
				
					 8 changed files with 44 additions and 23 deletions
				
			
		|  | @ -77,7 +77,7 @@ fun SubjectEntry( | |||
|                             Icon( | ||||
|                                 imageVector = Icons.Default.List, contentDescription = "tasks" | ||||
|                             ) | ||||
|                             Text(text = subject.tasks.size.toString()) | ||||
|                             Text(text = "0/0") // TODO | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|  |  | |||
|  | @ -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 | ||||
|     } | ||||
| } | ||||
|  | @ -5,7 +5,6 @@ import com.google.firebase.firestore.DocumentId | |||
| data class Subject( | ||||
|     @DocumentId val id: String = "", | ||||
|     val name: String = "", | ||||
|     val tasks: List<Task> = mutableListOf(), | ||||
|     val time: Int = 0, | ||||
|     val argb_color: Long = 0, | ||||
| ) | ||||
|  | @ -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" | ||||
| } | ||||
|  | @ -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) | ||||
| } | ||||
|  | @ -32,7 +32,7 @@ fun SubjectRoute( | |||
|         navigationBarActions = navigationBarActions, | ||||
|         addSubject = { viewModel.addSubject() }, | ||||
|         getSubjects = viewModel::getSubjects, | ||||
|         onViewSubject = { viewModel.onViewSubject(Subject(), open) }, | ||||
|         onViewSubject = { viewModel.onViewSubject(it, open) }, | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
|  | @ -42,7 +42,7 @@ fun SubjectScreen( | |||
|     navigationBarActions: NavigationBarActions, | ||||
|     addSubject: () -> Unit, | ||||
|     getSubjects: () -> Flow<List<Subject>>, | ||||
|     onViewSubject: () -> Unit, | ||||
|     onViewSubject: (Subject) -> Unit, | ||||
| ) { | ||||
|     PrimaryScreenTemplate( | ||||
|         title = resources().getString(R.string.tasks), | ||||
|  | @ -71,7 +71,7 @@ fun SubjectScreen( | |||
|                 items(subjects.value) { | ||||
|                     SubjectEntry( | ||||
|                         subject = it, | ||||
|                         onViewSubject = onViewSubject, | ||||
|                         onViewSubject = { onViewSubject(it) }, | ||||
|                     ) | ||||
|                 } | ||||
|             } | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| 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.domain.LogService | ||||
| import be.ugent.sel.studeez.domain.SubjectDAO | ||||
|  | @ -12,13 +14,13 @@ import javax.inject.Inject | |||
| @HiltViewModel | ||||
| class SubjectViewModel @Inject constructor( | ||||
|     private val subjectDAO: SubjectDAO, | ||||
|     private val selectedSubject: SelectedSubject, | ||||
|     logService: LogService, | ||||
| ) : StudeezViewModel(logService) { | ||||
|     fun addSubject() { | ||||
|         subjectDAO.saveSubject( | ||||
|             Subject( | ||||
|                 name = "Test Subject", | ||||
|                 tasks = listOf(), | ||||
|                 time = 0, | ||||
|                 argb_color = 0xFFF44336, | ||||
|             ) | ||||
|  | @ -30,6 +32,8 @@ class SubjectViewModel @Inject constructor( | |||
|     } | ||||
| 
 | ||||
|     fun onViewSubject(subject: Subject, open: (String) -> Unit) { | ||||
|         Log.v("MYLOG", subject.id) | ||||
|         selectedSubject.set(subject) | ||||
|         open(StudeezDestinations.TASKS_SCREEN) | ||||
|     } | ||||
| } | ||||
|  | @ -1,25 +1,25 @@ | |||
| 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.domain.LogService | ||||
| import be.ugent.sel.studeez.domain.TaskDAO | ||||
| import be.ugent.sel.studeez.screens.StudeezViewModel | ||||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||||
| import kotlinx.coroutines.flow.Flow | ||||
| import kotlinx.coroutines.flow.flowOf | ||||
| import javax.inject.Inject | ||||
| 
 | ||||
| @HiltViewModel | ||||
| class TaskViewModel @Inject constructor( | ||||
|     private val taskDAO: TaskDAO, | ||||
|     private val selectedSubject: SelectedSubject, | ||||
|     logService: LogService, | ||||
| ) : StudeezViewModel(logService) { | ||||
|     fun addTask() { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     fun getTasks() : Flow<List<Task>> { | ||||
|         return taskDAO.getTasks(Subject()) | ||||
|     fun getTasks(): Flow<List<Task>> { | ||||
|         return taskDAO.getTasks(selectedSubject()) | ||||
|     } | ||||
| } | ||||
		Reference in a new issue
	
	 brreynie
						brreynie