forked from github/wulkanowy-mirror
Fix all year average
This commit is contained in:
parent
ae2a697e01
commit
43e95cfdc6
@ -68,7 +68,7 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
forceRefresh: Boolean,
|
forceRefresh: Boolean,
|
||||||
averageMode: GradeAverageMode
|
averageMode: GradeAverageMode
|
||||||
): Flow<Resource<List<GradeSubject>>> {
|
): Flow<Resource<List<GradeSubject>>> {
|
||||||
val gradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc
|
val isGradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc
|
||||||
val selectedSemester = semesters.single { it.semesterId == semesterId }
|
val selectedSemester = semesters.single { it.semesterId == semesterId }
|
||||||
val firstSemester =
|
val firstSemester =
|
||||||
semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 }
|
semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 }
|
||||||
@ -78,8 +78,7 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
|
|
||||||
if (selectedSemester == firstSemester) return selectedSemesterGradeSubjects
|
if (selectedSemester == firstSemester) return selectedSemesterGradeSubjects
|
||||||
|
|
||||||
val firstSemesterGradeSubjects =
|
val firstSemesterGradeSubjects = getGradeSubjects(student, firstSemester, forceRefresh)
|
||||||
getGradeSubjects(student, firstSemester, forceRefresh)
|
|
||||||
|
|
||||||
return selectedSemesterGradeSubjects.combine(firstSemesterGradeSubjects) { secondSemesterGradeSubject, firstSemesterGradeSubject ->
|
return selectedSemesterGradeSubjects.combine(firstSemesterGradeSubjects) { secondSemesterGradeSubject, firstSemesterGradeSubject ->
|
||||||
if (firstSemesterGradeSubject.status == Status.ERROR) {
|
if (firstSemesterGradeSubject.status == Status.ERROR) {
|
||||||
@ -87,9 +86,9 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val isAnyVulcanAverageInFirstSemester =
|
val isAnyVulcanAverageInFirstSemester =
|
||||||
firstSemesterGradeSubject.data.orEmpty().any { it.average != .0 }
|
firstSemesterGradeSubject.data.orEmpty().any { it.isVulcanAverage }
|
||||||
val isAnyVulcanAverageInSecondSemester =
|
val isAnyVulcanAverageInSecondSemester =
|
||||||
secondSemesterGradeSubject.data.orEmpty().any { it.average != .0 }
|
secondSemesterGradeSubject.data.orEmpty().any { it.isVulcanAverage }
|
||||||
|
|
||||||
val updatedData = secondSemesterGradeSubject.data?.map { secondSemesterSubject ->
|
val updatedData = secondSemesterGradeSubject.data?.map { secondSemesterSubject ->
|
||||||
val firstSemesterSubject = firstSemesterGradeSubject.data.orEmpty()
|
val firstSemesterSubject = firstSemesterGradeSubject.data.orEmpty()
|
||||||
@ -99,7 +98,7 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
calculateAllYearAverage(
|
calculateAllYearAverage(
|
||||||
student = student,
|
student = student,
|
||||||
isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester,
|
isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester,
|
||||||
gradeAverageForceCalc = gradeAverageForceCalc,
|
isGradeAverageForceCalc = isGradeAverageForceCalc,
|
||||||
secondSemesterSubject = secondSemesterSubject,
|
secondSemesterSubject = secondSemesterSubject,
|
||||||
firstSemesterSubject = firstSemesterSubject
|
firstSemesterSubject = firstSemesterSubject
|
||||||
)
|
)
|
||||||
@ -107,7 +106,7 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
calculateBothSemestersAverage(
|
calculateBothSemestersAverage(
|
||||||
student = student,
|
student = student,
|
||||||
isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester,
|
isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester,
|
||||||
gradeAverageForceCalc = gradeAverageForceCalc,
|
isGradeAverageForceCalc = isGradeAverageForceCalc,
|
||||||
secondSemesterSubject = secondSemesterSubject,
|
secondSemesterSubject = secondSemesterSubject,
|
||||||
firstSemesterSubject = firstSemesterSubject
|
firstSemesterSubject = firstSemesterSubject
|
||||||
)
|
)
|
||||||
@ -121,10 +120,10 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
private fun calculateAllYearAverage(
|
private fun calculateAllYearAverage(
|
||||||
student: Student,
|
student: Student,
|
||||||
isAnyVulcanAverage: Boolean,
|
isAnyVulcanAverage: Boolean,
|
||||||
gradeAverageForceCalc: Boolean,
|
isGradeAverageForceCalc: Boolean,
|
||||||
secondSemesterSubject: GradeSubject,
|
secondSemesterSubject: GradeSubject,
|
||||||
firstSemesterSubject: GradeSubject?
|
firstSemesterSubject: GradeSubject?
|
||||||
) = if (!isAnyVulcanAverage || gradeAverageForceCalc) {
|
) = if (!isAnyVulcanAverage || isGradeAverageForceCalc) {
|
||||||
val updatedSecondSemesterGrades =
|
val updatedSecondSemesterGrades =
|
||||||
secondSemesterSubject.grades.updateModifiers(student)
|
secondSemesterSubject.grades.updateModifiers(student)
|
||||||
val updatedFirstSemesterGrades =
|
val updatedFirstSemesterGrades =
|
||||||
@ -138,13 +137,13 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
private fun calculateBothSemestersAverage(
|
private fun calculateBothSemestersAverage(
|
||||||
student: Student,
|
student: Student,
|
||||||
isAnyVulcanAverage: Boolean,
|
isAnyVulcanAverage: Boolean,
|
||||||
gradeAverageForceCalc: Boolean,
|
isGradeAverageForceCalc: Boolean,
|
||||||
secondSemesterSubject: GradeSubject,
|
secondSemesterSubject: GradeSubject,
|
||||||
firstSemesterSubject: GradeSubject?
|
firstSemesterSubject: GradeSubject?
|
||||||
): Double {
|
): Double {
|
||||||
val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1
|
val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1
|
||||||
|
|
||||||
return if (!isAnyVulcanAverage || gradeAverageForceCalc) {
|
return if (!isAnyVulcanAverage || isGradeAverageForceCalc) {
|
||||||
val secondSemesterAverage =
|
val secondSemesterAverage =
|
||||||
secondSemesterSubject.grades.updateModifiers(student).calcAverage()
|
secondSemesterSubject.grades.updateModifiers(student).calcAverage()
|
||||||
val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student)
|
val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student)
|
||||||
@ -161,7 +160,7 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
semester: Semester,
|
semester: Semester,
|
||||||
forceRefresh: Boolean
|
forceRefresh: Boolean
|
||||||
): Flow<Resource<List<GradeSubject>>> {
|
): Flow<Resource<List<GradeSubject>>> {
|
||||||
val gradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc
|
val isGradeAverageForceCalc = preferencesRepository.gradeAverageForceCalc
|
||||||
|
|
||||||
return gradeRepository.getGrades(student, semester, forceRefresh = forceRefresh)
|
return gradeRepository.getGrades(student, semester, forceRefresh = forceRefresh)
|
||||||
.map { res ->
|
.map { res ->
|
||||||
@ -170,20 +169,21 @@ class GradeAverageProvider @Inject constructor(
|
|||||||
val allGrades = details.orEmpty().groupBy { it.subject }
|
val allGrades = details.orEmpty().groupBy { it.subject }
|
||||||
|
|
||||||
val items = summaries?.emulateEmptySummaries(
|
val items = summaries?.emulateEmptySummaries(
|
||||||
student,
|
student = student,
|
||||||
semester,
|
semester = semester,
|
||||||
allGrades.toList(),
|
grades = allGrades.toList(),
|
||||||
isAnyAverage
|
calcAverage = isAnyAverage
|
||||||
)?.map { summary ->
|
)?.map { summary ->
|
||||||
val grades = allGrades[summary.subject].orEmpty()
|
val grades = allGrades[summary.subject].orEmpty()
|
||||||
GradeSubject(
|
GradeSubject(
|
||||||
subject = summary.subject,
|
subject = summary.subject,
|
||||||
average = if (!isAnyAverage || gradeAverageForceCalc) {
|
average = if (!isAnyAverage || isGradeAverageForceCalc) {
|
||||||
grades.updateModifiers(student).calcAverage()
|
grades.updateModifiers(student).calcAverage()
|
||||||
} else summary.average,
|
} else summary.average,
|
||||||
points = summary.pointsSum,
|
points = summary.pointsSum,
|
||||||
summary = summary,
|
summary = summary,
|
||||||
grades = grades
|
grades = grades,
|
||||||
|
isVulcanAverage = isAnyAverage
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,5 +8,6 @@ data class GradeSubject(
|
|||||||
val average: Double,
|
val average: Double,
|
||||||
val points: String,
|
val points: String,
|
||||||
val summary: GradeSummary,
|
val summary: GradeSummary,
|
||||||
val grades: List<Grade>
|
val grades: List<Grade>,
|
||||||
|
val isVulcanAverage: Boolean
|
||||||
)
|
)
|
||||||
|
@ -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(2, items.size)
|
||||||
assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0
|
assertEquals(
|
||||||
assertEquals(3.25, items.single { it.subject == "Fizyka" }.average, .0) // (from details): 3,5 + 3,0 → 3,25
|
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
|
@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.gradeAverageForceCalc } returns true
|
||||||
every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR
|
every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR
|
||||||
coEvery { semesterRepository.getSemesters(student) } returns semesters
|
coEvery { semesterRepository.getSemesters(student) } returns semesters
|
||||||
@ -490,17 +546,39 @@ class GradeAverageProviderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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.gradeAverageForceCalc } returns true
|
||||||
every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS
|
every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS
|
||||||
|
|
||||||
coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to emptyList() }
|
coEvery {
|
||||||
coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to emptyList() }
|
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(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
|
assertEquals(3.25, items.single { it.subject == "Fizyka" }.average, .0) // (from details): 3,5 + 3,0 → 3,25
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user