1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-11-23 19:36:16 -06:00

Add final average grades calculation with modifiers (#1125)

This commit is contained in:
MRmlik12 2021-02-05 10:50:35 +01:00 committed by GitHub
parent a063aabc7c
commit bad0776cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 9 deletions

View File

@ -5,14 +5,21 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import io.github.wulkanowy.data.db.entities.Grade
import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.databinding.ItemGradeSummaryBinding
import io.github.wulkanowy.databinding.ScrollableHeaderGradeSummaryBinding
import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.calcAverage
import io.github.wulkanowy.utils.changeModifier
import java.util.Locale
import javax.inject.Inject
class GradeSummaryAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class GradeSummaryAdapter @Inject constructor(
private val preferencesRepository: PreferencesRepository
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private enum class ViewType(val id: Int) {
HEADER(1),
@ -49,7 +56,7 @@ class GradeSummaryAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerV
if (items.isEmpty()) return
with(binding) {
gradeSummaryScrollableHeaderFinal.text = formatAverage(items.calcAverage())
gradeSummaryScrollableHeaderFinal.text = formatAverage(items.calcAverage(preferencesRepository.gradePlusModifier, preferencesRepository.gradeMinusModifier))
gradeSummaryScrollableHeaderCalculated.text = formatAverage(items
.filter { value -> value.average != 0.0 }
.map { values -> values.average }

View File

@ -16,10 +16,20 @@ fun List<Grade>.calcAverage(): Double {
}
@JvmName("calcSummaryAverage")
fun List<GradeSummary>.calcAverage() = asSequence()
fun List<GradeSummary>.calcAverage(plusModifier: Double, minusModifier: Double) = asSequence()
.mapNotNull {
if (it.finalGrade.matches("[0-6]".toRegex())) {
it.finalGrade.toDouble()
if (it.finalGrade.matches("[0-6][+-]?".toRegex())) {
when {
it.finalGrade.endsWith('+') -> {
it.finalGrade.removeSuffix("+").toDouble() + plusModifier
}
it.finalGrade.endsWith('-') -> {
it.finalGrade.removeSuffix("-").toDouble() - minusModifier
}
else -> {
it.finalGrade.toDouble()
}
}
} else null
}
.average()

View File

@ -33,12 +33,13 @@ class GradeExtensionTest {
@Test
fun calcSummaryAverage() {
assertEquals(2.5, listOf(
createGradeSummary("5"),
createGradeSummary("-5"),
assertEquals(3.5, listOf(
createGradeSummary("4"),
createGradeSummary("5+"),
createGradeSummary("5-"),
createGradeSummary("test"),
createGradeSummary("0")
).calcAverage(), 0.005)
).calcAverage(0.5, 0.5), 0.005)
}
@Test