#74 feed is now sorted on taskdates

This commit is contained in:
lbarraga 2023-05-07 14:26:07 +02:00
parent 260171a972
commit 3f6d416f39

View file

@ -7,6 +7,7 @@ import be.ugent.sel.studeez.data.local.models.task.Task
import be.ugent.sel.studeez.domain.FeedDAO import be.ugent.sel.studeez.domain.FeedDAO
import be.ugent.sel.studeez.domain.SessionDAO import be.ugent.sel.studeez.domain.SessionDAO
import be.ugent.sel.studeez.domain.TaskDAO import be.ugent.sel.studeez.domain.TaskDAO
import com.google.firebase.Timestamp
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import javax.inject.Inject import javax.inject.Inject
@ -22,13 +23,20 @@ class FirebaseFeedDAO @Inject constructor(
.map { sessionReport -> sessionToFeedEntry(sessionReport) } .map { sessionReport -> sessionToFeedEntry(sessionReport) }
.groupBy { it.taskId } .groupBy { it.taskId }
.map { fuseFeedEntries(it.component2()) } .map { fuseFeedEntries(it.component2()) }
.sortedByDescending { it.endTime }
} }
} }
private fun fuseFeedEntries(entries: List<FeedEntry>): FeedEntry = private fun fuseFeedEntries(entries: List<FeedEntry>): FeedEntry =
entries.fold(entries[0]) { accEntry, newEntry -> entries.fold(entries[0]) { accEntry, newEntry ->
val newStudyTime = accEntry.totalStudyTime + newEntry.totalStudyTime accEntry.copy(
accEntry.copy(totalStudyTime = newStudyTime) totalStudyTime = accEntry.totalStudyTime + newEntry.totalStudyTime,
endTime = getMostRecent(accEntry.endTime, newEntry.endTime)
)
}
private fun getMostRecent(t1: Timestamp, t2: Timestamp): Timestamp {
return if (t1 < t2) t2 else t1
} }
@ -45,7 +53,8 @@ class FirebaseFeedDAO @Inject constructor(
taskName = task.name, taskName = task.name,
taskId = task.id, taskId = task.id,
subjectId = subject.id, subjectId = subject.id,
totalStudyTime = sessionReport.studyTime totalStudyTime = sessionReport.studyTime,
endTime = sessionReport.endTime
) )
} }
} }