From 5e3b89636fc3cb9c83720738549f9f493196f4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sat, 13 Feb 2021 12:33:53 +0100 Subject: [PATCH] Add checking vulcan average from both semesters (#1140) --- .../ui/modules/grade/GradeAverageProvider.kt | 35 +++++++++++-------- .../modules/grade/GradeAverageProviderTest.kt | 13 +++++++ gradle/wrapper/gradle-wrapper.properties | 2 +- 3 files changed, 35 insertions(+), 15 deletions(-) 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 36c3c4f87..7e9b56b94 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 @@ -86,7 +86,11 @@ class GradeAverageProvider @Inject constructor( return@combine firstSemesterGradeSubject } - val isAnyAverage = secondSemesterGradeSubject.data.orEmpty().any { it.average != .0 } + val isAnyVulcanAverageInFirstSemester = + firstSemesterGradeSubject.data.orEmpty().any { it.average != .0 } + val isAnyVulcanAverageInSecondSemester = + secondSemesterGradeSubject.data.orEmpty().any { it.average != .0 } + val updatedData = secondSemesterGradeSubject.data?.map { secondSemesterSubject -> val firstSemesterSubject = firstSemesterGradeSubject.data.orEmpty() .singleOrNull { it.subject == secondSemesterSubject.subject } @@ -94,7 +98,7 @@ class GradeAverageProvider @Inject constructor( val updatedAverage = if (averageMode == ALL_YEAR) { calculateAllYearAverage( student = student, - isAnyAverage = isAnyAverage, + isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester, gradeAverageForceCalc = gradeAverageForceCalc, secondSemesterSubject = secondSemesterSubject, firstSemesterSubject = firstSemesterSubject @@ -102,7 +106,7 @@ class GradeAverageProvider @Inject constructor( } else { calculateBothSemestersAverage( student = student, - isAnyAverage = isAnyAverage, + isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester, gradeAverageForceCalc = gradeAverageForceCalc, secondSemesterSubject = secondSemesterSubject, firstSemesterSubject = firstSemesterSubject @@ -116,11 +120,11 @@ class GradeAverageProvider @Inject constructor( private fun calculateAllYearAverage( student: Student, - isAnyAverage: Boolean, + isAnyVulcanAverage: Boolean, gradeAverageForceCalc: Boolean, secondSemesterSubject: GradeSubject, firstSemesterSubject: GradeSubject? - ) = if (!isAnyAverage || gradeAverageForceCalc) { + ) = if (!isAnyVulcanAverage || gradeAverageForceCalc) { val updatedSecondSemesterGrades = secondSemesterSubject.grades.updateModifiers(student) val updatedFirstSemesterGrades = @@ -133,20 +137,23 @@ class GradeAverageProvider @Inject constructor( private fun calculateBothSemestersAverage( student: Student, - isAnyAverage: Boolean, + isAnyVulcanAverage: Boolean, gradeAverageForceCalc: Boolean, secondSemesterSubject: GradeSubject, firstSemesterSubject: GradeSubject? - ) = if (!isAnyAverage || gradeAverageForceCalc) { - val secondSemesterAverage = - secondSemesterSubject.grades.updateModifiers(student).calcAverage() - val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student) - ?.calcAverage() ?: secondSemesterAverage + ): Double { val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1 - (secondSemesterAverage + firstSemesterAverage) / divider - } else { - (secondSemesterSubject.average + (firstSemesterSubject?.average ?: secondSemesterSubject.average)) / 2 + return if (!isAnyVulcanAverage || gradeAverageForceCalc) { + val secondSemesterAverage = + secondSemesterSubject.grades.updateModifiers(student).calcAverage() + val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student) + ?.calcAverage() ?: secondSemesterAverage + + (secondSemesterAverage + firstSemesterAverage) / divider + } else { + (secondSemesterSubject.average + (firstSemesterSubject?.average ?: secondSemesterSubject.average)) / divider + } } private fun getGradeSubjects( 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 0da02cf52..f3d92d2ee 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 @@ -198,6 +198,19 @@ class GradeAverageProviderTest { assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0) } + @Test + fun `calc both semesters average with no grade in second semester but with average in first semester`() { + every { preferencesRepository.gradeAverageForceCalc } returns false + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS + + coEvery { gradeRepository.getGrades(student, semesters[1], false) } returns flowWithResource { secondGradeWithModifier to secondSummariesWithModifier } + coEvery { gradeRepository.getGrades(student, semesters[2], false) } returns flowWithResource { emptyList() to listOf(getSummary(24, "Język polski", .0))} + + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, false).getResult() } + + assertEquals(3.49, items.single { it.subject == "Język polski" }.average, .0) + } + @Test fun `force calc average on no grades`() { every { preferencesRepository.gradeAverageForceCalc } returns true diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1c4bcc29e..25d326531 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists