1
0

Implement a toggleable setting to count an arithmetic average of grades when all weights are equal to zero (#1186)

This commit is contained in:
Kamil Studziński
2021-03-30 13:59:36 +02:00
committed by GitHub
parent f2130998ec
commit 3071e19584
9 changed files with 97 additions and 11 deletions

View File

@ -146,6 +146,12 @@ class PreferencesRepository @Inject constructor(
R.bool.pref_default_subjects_without_grades
)
val isOptionalArithmeticAverage: Boolean
get() = getBoolean(
R.string.pref_key_optional_arithmetic_average,
R.bool.pref_default_optional_arithmetic_average
)
var isKitkatDialogDisabled: Boolean
get() = sharedPref.getBoolean("kitkat_dialog_disabled", false)
set(value) = sharedPref.edit { putBoolean("kitkat_dialog_disabled", value) }

View File

@ -34,6 +34,8 @@ class GradeAverageProvider @Inject constructor(
private val minusModifier get() = preferencesRepository.gradeMinusModifier
private val isOptionalArithmeticAverage get() = preferencesRepository.isOptionalArithmeticAverage
fun getGradesDetailsWithAverage(student: Student, semesterId: Int, forceRefresh: Boolean) =
flowWithResourceIn {
val semesters = semesterRepository.getSemesters(student)
@ -130,7 +132,7 @@ class GradeAverageProvider @Inject constructor(
val updatedFirstSemesterGrades =
firstSemesterSubject?.grades?.updateModifiers(student).orEmpty()
(updatedSecondSemesterGrades + updatedFirstSemesterGrades).calcAverage()
(updatedSecondSemesterGrades + updatedFirstSemesterGrades).calcAverage(isOptionalArithmeticAverage)
} else {
secondSemesterSubject.average
}
@ -146,9 +148,9 @@ class GradeAverageProvider @Inject constructor(
return if (!isAnyVulcanAverage || gradeAverageForceCalc) {
val secondSemesterAverage =
secondSemesterSubject.grades.updateModifiers(student).calcAverage()
secondSemesterSubject.grades.updateModifiers(student).calcAverage(isOptionalArithmeticAverage)
val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student)
?.calcAverage() ?: secondSemesterAverage
?.calcAverage(isOptionalArithmeticAverage) ?: secondSemesterAverage
(secondSemesterAverage + firstSemesterAverage) / divider
} else {
@ -179,7 +181,7 @@ class GradeAverageProvider @Inject constructor(
GradeSubject(
subject = summary.subject,
average = if (!isAnyAverage || gradeAverageForceCalc) {
grades.updateModifiers(student).calcAverage()
grades.updateModifiers(student).calcAverage(isOptionalArithmeticAverage)
} else summary.average,
points = summary.pointsSum,
summary = summary,
@ -211,7 +213,7 @@ class GradeAverageProvider @Inject constructor(
proposedPoints = "",
finalPoints = "",
pointsSum = "",
average = if (calcAverage) details.updateModifiers(student).calcAverage() else .0
average = if (calcAverage) details.updateModifiers(student).calcAverage(isOptionalArithmeticAverage) else .0
)
}
}

View File

@ -3,14 +3,17 @@ package io.github.wulkanowy.utils
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.sdk.scrapper.grades.*
fun List<Grade>.calcAverage(): Double {
fun List<Grade>.calcAverage(isOptionalArithmeticAverage: Boolean): Double {
val isArithmeticAverage = isOptionalArithmeticAverage && !any { it.weightValue != .0 }
var counter = 0.0
var denominator = 0.0
forEach {
counter += (it.value + it.modifier) * it.weightValue
denominator += it.weightValue
val weight = if (isArithmeticAverage && isGradeValid(it.entry)) 1.0 else it.weightValue
counter += (it.value + it.modifier) * weight
denominator += weight
}
return if (denominator != 0.0) counter / denominator else 0.0
}

View File

@ -24,4 +24,5 @@
<bool name="pref_default_timetable_show_timers">false</bool>
<bool name="pref_default_homework_fullscreen">false</bool>
<bool name="pref_default_subjects_without_grades">false</bool>
<bool name="pref_default_optional_arithmetic_average">false</bool>
</resources>

View File

@ -26,4 +26,5 @@
<string name="pref_key_timetable_show_timers">timetable_show_timers</string>
<string name="pref_key_homework_fullscreen">homework_fullscreen</string>
<string name="pref_key_subjects_without_grades">subjects_without_grades</string>
<string name="pref_key_optional_arithmetic_average">optional_arithmetic_average</string>
</resources>

View File

@ -507,6 +507,7 @@
<string name="pref_other_grade_modifier_plus">Value of the plus</string>
<string name="pref_other_grade_modifier_minus">Value of the minus</string>
<string name="pref_other_fill_message_content">Reply with message history</string>
<string name="pref_other_optional_arithmetic_average">Show arithmetic average when no weights provided</string>
<string name="pref_settings_advanced_title">Advanced</string>
<string name="pref_settings_appearance_title">Appearance &amp; Behavior</string>

View File

@ -25,6 +25,12 @@
app:key="@string/pref_key_grade_average_force_calc"
app:singleLineTitle="false"
app:title="@string/pref_view_grade_average_force_calc" />
<SwitchPreferenceCompat
app:defaultValue="@bool/pref_default_optional_arithmetic_average"
app:iconSpaceReserved="false"
app:key="@string/pref_key_optional_arithmetic_average"
app:singleLineTitle="false"
app:title="@string/pref_other_optional_arithmetic_average" />
<ListPreference
app:defaultValue="@string/pref_default_grade_average_mode"
app:entries="@array/grade_average_mode_entries"