changed some DAOs to take ids and added a friends feed getter
This commit is contained in:
parent
4d2abaec12
commit
43b46dd4a1
6 changed files with 75 additions and 36 deletions
|
@ -7,4 +7,7 @@ interface FeedDAO {
|
|||
|
||||
fun getFeedEntries(): Flow<Map<String, List<FeedEntry>>>
|
||||
|
||||
suspend fun getFeedEntriesFromUser(id: String): Map<String, List<FeedEntry>>
|
||||
|
||||
fun getFriendsSessions(): Flow<Map<String, List<Pair<String, FeedEntry>>>>
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package be.ugent.sel.studeez.domain
|
||||
|
||||
import be.ugent.sel.studeez.data.local.models.FeedEntry
|
||||
import be.ugent.sel.studeez.data.local.models.SessionReport
|
||||
import be.ugent.sel.studeez.data.local.models.User
|
||||
import be.ugent.sel.studeez.data.local.models.task.Task
|
||||
|
@ -11,11 +12,6 @@ interface SessionDAO {
|
|||
fun getSessions(): Flow<List<SessionReport>>
|
||||
suspend fun getSessionsOfUser(userId: String): List<SessionReport>
|
||||
|
||||
/**
|
||||
* Return a list of pairs, containing the username and all the studysessions of that user.
|
||||
*/
|
||||
fun getFriendsSessions(): Flow<List<Pair<String, List<Task>>>>
|
||||
|
||||
fun saveSession(newSessionReport: SessionReport)
|
||||
|
||||
fun deleteSession(newTimer: TimerInfo)
|
||||
|
|
|
@ -20,4 +20,5 @@ interface SubjectDAO {
|
|||
fun getStudyTime(subject: Subject): Flow<Int>
|
||||
|
||||
suspend fun getSubject(subjectId: String): Subject?
|
||||
suspend fun getSubjectOfUSer(subjectId: String, userId: String): Subject
|
||||
}
|
|
@ -5,19 +5,19 @@ import be.ugent.sel.studeez.data.local.models.FeedEntry
|
|||
import be.ugent.sel.studeez.data.local.models.SessionReport
|
||||
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.FeedDAO
|
||||
import be.ugent.sel.studeez.domain.SessionDAO
|
||||
import be.ugent.sel.studeez.domain.SubjectDAO
|
||||
import be.ugent.sel.studeez.domain.TaskDAO
|
||||
import be.ugent.sel.studeez.domain.*
|
||||
import com.google.firebase.Timestamp
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
|
||||
class FirebaseFeedDAO @Inject constructor(
|
||||
private val friendshipDAO: FriendshipDAO,
|
||||
private val sessionDAO: SessionDAO,
|
||||
private val taskDAO: TaskDAO,
|
||||
private val subjectDAO: SubjectDAO
|
||||
private val subjectDAO: SubjectDAO,
|
||||
private val auth: AccountDAO,
|
||||
private val userDAO: UserDAO,
|
||||
) : FeedDAO {
|
||||
|
||||
/**
|
||||
|
@ -37,6 +37,45 @@ class FirebaseFeedDAO @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a map as with key the day and value a list of feedentries for that day.
|
||||
*/
|
||||
override suspend fun getFeedEntriesFromUser(id: String): Map<String, List<FeedEntry>> {
|
||||
return sessionDAO.getSessionsOfUser(id)
|
||||
.map { sessionReport -> sessionToFeedEntryFromUser(sessionReport, id) }
|
||||
.sortedByDescending { it.endTime }
|
||||
.groupBy { getFormattedTime(it) }
|
||||
.mapValues { (_, entries) ->
|
||||
entries
|
||||
.groupBy { it.taskId }
|
||||
.map { fuseFeedEntries(it.component2()) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun getFriendsSessions(): Flow<Map<String, List<Pair<String, FeedEntry>>>> {
|
||||
return friendshipDAO.getAllFriendships(auth.currentUserId)
|
||||
.map { friendships ->
|
||||
friendships.map { friendship ->
|
||||
val userId: String = friendship.friendId
|
||||
val username = userDAO.getUsername(userId)
|
||||
val friendFeed = getFeedEntriesFromUser(userId)
|
||||
Pair(username, friendFeed)
|
||||
}
|
||||
}.map {
|
||||
mergeNameAndEntries(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun mergeNameAndEntries(l: List<Pair<String, Map<String, List<FeedEntry>>>>): Map<String, List<Pair<String, FeedEntry>>> {
|
||||
val new: MutableMap<String, List<Pair<String, FeedEntry>>> = mutableMapOf()
|
||||
for ((name, map) in l) {
|
||||
for ((day, feedEntries: List<FeedEntry>) in map) {
|
||||
new[day] = new.getOrDefault(day, listOf()) + feedEntries.map { Pair(name, it) }
|
||||
}
|
||||
}
|
||||
return new
|
||||
}
|
||||
|
||||
private fun getFormattedTime(entry: FeedEntry): String {
|
||||
return DateFormat.getDateInstance().format(entry.endTime.toDate())
|
||||
}
|
||||
|
@ -67,6 +106,10 @@ class FirebaseFeedDAO @Inject constructor(
|
|||
val task: Task = taskDAO.getTask(subjectId, taskId)
|
||||
val subject: Subject = subjectDAO.getSubject(subjectId)!!
|
||||
|
||||
return makeFeedEntry(sessionReport, subject, task)
|
||||
}
|
||||
|
||||
private fun makeFeedEntry(sessionReport: SessionReport, subject: Subject, task: Task): FeedEntry {
|
||||
return FeedEntry(
|
||||
argb_color = subject.argb_color,
|
||||
subJectName = subject.name,
|
||||
|
@ -78,4 +121,17 @@ class FirebaseFeedDAO @Inject constructor(
|
|||
isArchived = task.archived || subject.archived
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a sessionReport to a feedEntry. Fetch Task and Subject to get names
|
||||
*/
|
||||
private suspend fun sessionToFeedEntryFromUser(sessionReport: SessionReport, id: String): FeedEntry {
|
||||
val subjectId: String = sessionReport.subjectId
|
||||
val taskId: String = sessionReport.taskId
|
||||
|
||||
val task: Task = taskDAO.getTaskFromUser(subjectId, taskId, id)
|
||||
val subject: Subject = subjectDAO.getSubjectOfUSer(subjectId, id)
|
||||
|
||||
return makeFeedEntry(sessionReport, subject, task)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package be.ugent.sel.studeez.domain.implementation
|
||||
|
||||
import be.ugent.sel.studeez.data.local.models.FeedEntry
|
||||
import be.ugent.sel.studeez.data.local.models.SessionReport
|
||||
import be.ugent.sel.studeez.data.local.models.User
|
||||
import be.ugent.sel.studeez.data.local.models.task.Task
|
||||
|
@ -25,9 +26,6 @@ import javax.inject.Inject
|
|||
class FirebaseSessionDAO @Inject constructor(
|
||||
private val firestore: FirebaseFirestore,
|
||||
private val auth: AccountDAO,
|
||||
private val userDAO: UserDAO,
|
||||
private val friendshipDAO: FriendshipDAO,
|
||||
private val taskDAO: TaskDAO,
|
||||
) : SessionDAO {
|
||||
|
||||
override fun getSessions(): Flow<List<SessionReport>> {
|
||||
|
@ -44,24 +42,6 @@ class FirebaseSessionDAO @Inject constructor(
|
|||
.map { it.toObject(SessionReport::class.java) }
|
||||
}
|
||||
|
||||
override fun getFriendsSessions(): Flow<List<Pair<String, List<Task>>>> {
|
||||
return friendshipDAO.getAllFriendships(auth.currentUserId)
|
||||
.map { friendships ->
|
||||
friendships.map { friendship ->
|
||||
val userId: String = friendship.friendId
|
||||
val username = userDAO.getUsername(userId)
|
||||
val userTasks = getSessionsOfUser(userId)
|
||||
.map { sessionReport ->
|
||||
taskDAO.getTaskFromUser(
|
||||
sessionReport.subjectId,
|
||||
sessionReport.taskId,
|
||||
userId
|
||||
)
|
||||
}
|
||||
Pair(username, userTasks)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun saveSession(newSessionReport: SessionReport) {
|
||||
currentUserSessionsCollection().add(newSessionReport)
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package be.ugent.sel.studeez.domain.implementation
|
||||
|
||||
import android.util.Log
|
||||
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.Task
|
||||
|
@ -35,6 +34,10 @@ class FirebaseSubjectDAO @Inject constructor(
|
|||
return currentUserSubjectsCollection().document(subjectId).get().await().toObject()
|
||||
}
|
||||
|
||||
override suspend fun getSubjectOfUSer(subjectId: String, userId: String): Subject {
|
||||
return currentUserSubjectsCollection(userId).document(subjectId).get().await().toObject()!!
|
||||
}
|
||||
|
||||
override fun saveSubject(newSubject: Subject) {
|
||||
currentUserSubjectsCollection().add(newSubject)
|
||||
}
|
||||
|
@ -74,14 +77,14 @@ class FirebaseSubjectDAO @Inject constructor(
|
|||
.map { tasks -> tasks.sumOf { it.time } }
|
||||
}
|
||||
|
||||
private fun currentUserSubjectsCollection(): CollectionReference =
|
||||
private fun currentUserSubjectsCollection(id: String = auth.currentUserId): CollectionReference =
|
||||
firestore.collection(FirebaseCollections.USER_COLLECTION)
|
||||
.document(auth.currentUserId)
|
||||
.document(id)
|
||||
.collection(FirebaseCollections.SUBJECT_COLLECTION)
|
||||
|
||||
private fun subjectTasksCollection(subject: Subject): CollectionReference =
|
||||
private fun subjectTasksCollection(subject: Subject, id: String = auth.currentUserId): CollectionReference =
|
||||
firestore.collection(FirebaseCollections.USER_COLLECTION)
|
||||
.document(auth.currentUserId)
|
||||
.document(id)
|
||||
.collection(FirebaseCollections.SUBJECT_COLLECTION)
|
||||
.document(subject.id)
|
||||
.collection(FirebaseCollections.TASK_COLLECTION)
|
||||
|
|
Reference in a new issue