diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt index 7e9b56b9..d83b1490 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt @@ -68,7 +68,7 @@ class GradeAverageProvider @Inject constructor( forceRefresh: Boolean, averageMode: GradeAverageMode ): Flow>> { - val gradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc + val isGradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc val selectedSemester = semesters.single { it.semesterId == semesterId } val firstSemester = semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 } @@ -78,8 +78,7 @@ class GradeAverageProvider @Inject constructor( if (selectedSemester == firstSemester) return selectedSemesterGradeSubjects - val firstSemesterGradeSubjects = - getGradeSubjects(student, firstSemester, forceRefresh) + val firstSemesterGradeSubjects = getGradeSubjects(student, firstSemester, forceRefresh) return selectedSemesterGradeSubjects.combine(firstSemesterGradeSubjects) { secondSemesterGradeSubject, firstSemesterGradeSubject -> if (firstSemesterGradeSubject.status == Status.ERROR) { @@ -87,9 +86,9 @@ class GradeAverageProvider @Inject constructor( } val isAnyVulcanAverageInFirstSemester = - firstSemesterGradeSubject.data.orEmpty().any { it.average != .0 } + firstSemesterGradeSubject.data.orEmpty().any { it.isVulcanAverage } val isAnyVulcanAverageInSecondSemester = - secondSemesterGradeSubject.data.orEmpty().any { it.average != .0 } + secondSemesterGradeSubject.data.orEmpty().any { it.isVulcanAverage } val updatedData = secondSemesterGradeSubject.data?.map { secondSemesterSubject -> val firstSemesterSubject = firstSemesterGradeSubject.data.orEmpty() @@ -99,7 +98,7 @@ class GradeAverageProvider @Inject constructor( calculateAllYearAverage( student = student, isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester, - gradeAverageForceCalc = gradeAverageForceCalc, + isGradeAverageForceCalc = isGradeAverageForceCalc, secondSemesterSubject = secondSemesterSubject, firstSemesterSubject = firstSemesterSubject ) @@ -107,7 +106,7 @@ class GradeAverageProvider @Inject constructor( calculateBothSemestersAverage( student = student, isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester, - gradeAverageForceCalc = gradeAverageForceCalc, + isGradeAverageForceCalc = isGradeAverageForceCalc, secondSemesterSubject = secondSemesterSubject, firstSemesterSubject = firstSemesterSubject ) @@ -121,10 +120,10 @@ class GradeAverageProvider @Inject constructor( private fun calculateAllYearAverage( student: Student, isAnyVulcanAverage: Boolean, - gradeAverageForceCalc: Boolean, + isGradeAverageForceCalc: Boolean, secondSemesterSubject: GradeSubject, firstSemesterSubject: GradeSubject? - ) = if (!isAnyVulcanAverage || gradeAverageForceCalc) { + ) = if (!isAnyVulcanAverage || isGradeAverageForceCalc) { val updatedSecondSemesterGrades = secondSemesterSubject.grades.updateModifiers(student) val updatedFirstSemesterGrades = @@ -138,13 +137,13 @@ class GradeAverageProvider @Inject constructor( private fun calculateBothSemestersAverage( student: Student, isAnyVulcanAverage: Boolean, - gradeAverageForceCalc: Boolean, + isGradeAverageForceCalc: Boolean, secondSemesterSubject: GradeSubject, firstSemesterSubject: GradeSubject? ): Double { val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1 - return if (!isAnyVulcanAverage || gradeAverageForceCalc) { + return if (!isAnyVulcanAverage || isGradeAverageForceCalc) { val secondSemesterAverage = secondSemesterSubject.grades.updateModifiers(student).calcAverage() val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student) @@ -161,7 +160,7 @@ class GradeAverageProvider @Inject constructor( semester: Semester, forceRefresh: Boolean ): Flow>> { - val gradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc + val isGradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc return gradeRepository.getGrades(student, semester, forceRefresh = forceRefresh) .map { res -> @@ -170,20 +169,21 @@ class GradeAverageProvider @Inject constructor( val allGrades = details.orEmpty().groupBy { it.subject } val items = summaries?.emulateEmptySummaries( - student, - semester, - allGrades.toList(), - isAnyAverage + student = student, + semester = semester, + grades = allGrades.toList(), + calcAverage = isAnyAverage )?.map { summary -> val grades = allGrades[summary.subject].orEmpty() GradeSubject( subject = summary.subject, - average = if (!isAnyAverage || gradeAverageForceCalc) { + average = if (!isAnyAverage || isGradeAverageForceCalc) { grades.updateModifiers(student).calcAverage() } else summary.average, points = summary.pointsSum, summary = summary, - grades = grades + grades = grades, + isVulcanAverage = isAnyAverage ) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSubject.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSubject.kt index ee4266c5..57be55ee 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSubject.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSubject.kt @@ -8,5 +8,6 @@ data class GradeSubject( val average: Double, val points: String, val summary: GradeSummary, - val grades: List + val grades: List, + val isVulcanAverage: Boolean ) diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt index f3d92d2e..3878503a 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt @@ -440,15 +440,71 @@ class GradeAverageProviderTest { ) } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } + val items = runBlocking { + gradeAverageProvider.getGradesDetailsWithAverage( + student, + semesters[2].semesterId, + true + ).getResult() + } assertEquals(2, items.size) - assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 - assertEquals(3.25, items.single { it.subject == "Fizyka" }.average, .0) // (from details): 3,5 + 3,0 → 3,25 + assertEquals( + 3.0, + items.single { it.subject == "Matematyka" }.average, + .0 + ) // (from details): 3,5 + 2,5 → 3,0 + assertEquals( + 3.25, + items.single { it.subject == "Fizyka" }.average, + .0 + ) // (from details): 3,5 + 3,0 → 3,25 } @Test - fun `calc full year average when current is second with load from cache sequence`() { + fun `calc all year average`() { + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR + every { preferencesRepository.gradeAverageForceCalc } returns false + coEvery { + gradeRepository.getGrades( + student, + semesters[1], + true + ) + } returns flowWithResource { + firstGrades to listOf( + getSummary(22, "Matematyka", .0), + getSummary(22, "Fizyka", .0) + ) + } + coEvery { + gradeRepository.getGrades( + student, + semesters[2], + true + ) + } returns flowWithResource { + secondGrades to listOf( + getSummary(22, "Matematyka", .0), + getSummary(22, "Fizyka", .0) + ) + } + + val items = runBlocking { + gradeAverageProvider.getGradesDetailsWithAverage( + student, + semesters[2].semesterId, + true + ).getResult() + } + + assertEquals(2, items.size) + assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) + assertEquals(3.25, items.single { it.subject == "Fizyka" }.average, .0) + } + + @Test + fun `force calc full year average when current is second with load from cache sequence`() { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR coEvery { semesterRepository.getSemesters(student) } returns semesters @@ -490,17 +546,39 @@ class GradeAverageProviderTest { } @Test - fun `calc both semesters average when no summaries`() { + fun `force calc both semesters average when no summaries`() { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to emptyList() } - coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to emptyList() } + coEvery { + gradeRepository.getGrades( + student, + semesters[1], + true + ) + } returns flowWithResource { firstGrades to emptyList() } + coEvery { + gradeRepository.getGrades( + student, + semesters[2], + true + ) + } returns flowWithResource { secondGrades to emptyList() } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } + val items = runBlocking { + gradeAverageProvider.getGradesDetailsWithAverage( + student, + semesters[2].semesterId, + true + ).getResult() + } assertEquals(2, items.size) - assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 + assertEquals( + 3.0, + items.single { it.subject == "Matematyka" }.average, + .0 + ) // (from details): 3,5 + 2,5 → 3,0 assertEquals(3.25, items.single { it.subject == "Fizyka" }.average, .0) // (from details): 3,5 + 3,0 → 3,25 }