1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-11-23 12:46:02 -06:00

Fix student average calculation error in grade statistics (#1981)

Co-authored-by: Mikołaj Pich <m.pich@outlook.com>
This commit is contained in:
Michael 2022-09-28 23:33:05 +02:00 committed by GitHub
parent b271c12ebc
commit 354f51dd70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 16 deletions

View File

@ -186,7 +186,7 @@ ext {
}
dependencies {
implementation "io.github.wulkanowy:sdk:1.7.5"
implementation "io.github.wulkanowy:sdk:2840d9d6d0"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.8'

View File

@ -10,6 +10,7 @@ import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.databinding.ItemGradeSummaryBinding
import io.github.wulkanowy.databinding.ScrollableHeaderGradeSummaryBinding
import io.github.wulkanowy.sdk.scrapper.grades.isGradeValid
import io.github.wulkanowy.utils.calcFinalAverage
import java.util.Locale
import javax.inject.Inject
@ -61,7 +62,7 @@ class GradeSummaryAdapter @Inject constructor(
if (items.isEmpty()) return
val context = binding.root.context
val finalItemsCount = items.count { it.finalGrade.matches("[0-6][+-]?".toRegex()) }
val finalItemsCount = items.count { isGradeValid(it.finalGrade) }
val calculatedItemsCount = items.count { value -> value.average != 0.0 }
val allItemsCount = items.count { !it.subject.equals("zachowanie", true) }
val finalAverage = items.calcFinalAverage(

View File

@ -4,6 +4,7 @@ import io.github.wulkanowy.R
import io.github.wulkanowy.data.db.entities.Grade
import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.enums.GradeColorTheme
import io.github.wulkanowy.sdk.scrapper.grades.getGradeValueWithModifier
import io.github.wulkanowy.sdk.scrapper.grades.isGradeValid
fun List<Grade>.calcAverage(isOptionalArithmeticAverage: Boolean): Double {
@ -20,20 +21,15 @@ fun List<Grade>.calcAverage(isOptionalArithmeticAverage: Boolean): Double {
}
fun List<GradeSummary>.calcFinalAverage(plusModifier: Double, minusModifier: Double) = asSequence()
.mapNotNull {
if (it.finalGrade.matches("[0-6][+-]?".toRegex())) {
.mapNotNull { summary ->
val (gradeValue, gradeModifier) = getGradeValueWithModifier(summary.finalGrade)
if (gradeValue == null || gradeModifier == null) return@mapNotNull null
when {
it.finalGrade.endsWith('+') -> {
it.finalGrade.removeSuffix("+").toDouble() + plusModifier
gradeModifier > 0 -> gradeValue + plusModifier
gradeModifier < 0 -> gradeValue - minusModifier
else -> gradeValue + 0.0
}
it.finalGrade.endsWith('-') -> {
it.finalGrade.removeSuffix("-").toDouble() - minusModifier
}
else -> {
it.finalGrade.toDouble()
}
}
} else null
}
.average()
.let { if (it.isNaN()) 0.0 else it }