forked from github/wulkanowy-mirror
Add final average grades calculation with modifiers (#1125)
This commit is contained in:
parent
a063aabc7c
commit
bad0776cab
@ -5,14 +5,21 @@ import android.view.LayoutInflater
|
|||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
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.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.ItemGradeSummaryBinding
|
||||||
import io.github.wulkanowy.databinding.ScrollableHeaderGradeSummaryBinding
|
import io.github.wulkanowy.databinding.ScrollableHeaderGradeSummaryBinding
|
||||||
|
import io.github.wulkanowy.sdk.Sdk
|
||||||
import io.github.wulkanowy.utils.calcAverage
|
import io.github.wulkanowy.utils.calcAverage
|
||||||
|
import io.github.wulkanowy.utils.changeModifier
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import javax.inject.Inject
|
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) {
|
private enum class ViewType(val id: Int) {
|
||||||
HEADER(1),
|
HEADER(1),
|
||||||
@ -49,7 +56,7 @@ class GradeSummaryAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerV
|
|||||||
if (items.isEmpty()) return
|
if (items.isEmpty()) return
|
||||||
|
|
||||||
with(binding) {
|
with(binding) {
|
||||||
gradeSummaryScrollableHeaderFinal.text = formatAverage(items.calcAverage())
|
gradeSummaryScrollableHeaderFinal.text = formatAverage(items.calcAverage(preferencesRepository.gradePlusModifier, preferencesRepository.gradeMinusModifier))
|
||||||
gradeSummaryScrollableHeaderCalculated.text = formatAverage(items
|
gradeSummaryScrollableHeaderCalculated.text = formatAverage(items
|
||||||
.filter { value -> value.average != 0.0 }
|
.filter { value -> value.average != 0.0 }
|
||||||
.map { values -> values.average }
|
.map { values -> values.average }
|
||||||
|
@ -16,10 +16,20 @@ fun List<Grade>.calcAverage(): Double {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmName("calcSummaryAverage")
|
@JvmName("calcSummaryAverage")
|
||||||
fun List<GradeSummary>.calcAverage() = asSequence()
|
fun List<GradeSummary>.calcAverage(plusModifier: Double, minusModifier: Double) = asSequence()
|
||||||
.mapNotNull {
|
.mapNotNull {
|
||||||
if (it.finalGrade.matches("[0-6]".toRegex())) {
|
if (it.finalGrade.matches("[0-6][+-]?".toRegex())) {
|
||||||
it.finalGrade.toDouble()
|
when {
|
||||||
|
it.finalGrade.endsWith('+') -> {
|
||||||
|
it.finalGrade.removeSuffix("+").toDouble() + plusModifier
|
||||||
|
}
|
||||||
|
it.finalGrade.endsWith('-') -> {
|
||||||
|
it.finalGrade.removeSuffix("-").toDouble() - minusModifier
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
it.finalGrade.toDouble()
|
||||||
|
}
|
||||||
|
}
|
||||||
} else null
|
} else null
|
||||||
}
|
}
|
||||||
.average()
|
.average()
|
||||||
|
@ -33,12 +33,13 @@ class GradeExtensionTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun calcSummaryAverage() {
|
fun calcSummaryAverage() {
|
||||||
assertEquals(2.5, listOf(
|
assertEquals(3.5, listOf(
|
||||||
createGradeSummary("5"),
|
createGradeSummary("4"),
|
||||||
createGradeSummary("-5"),
|
createGradeSummary("5+"),
|
||||||
|
createGradeSummary("5-"),
|
||||||
createGradeSummary("test"),
|
createGradeSummary("test"),
|
||||||
createGradeSummary("0")
|
createGradeSummary("0")
|
||||||
).calcAverage(), 0.005)
|
).calcAverage(0.5, 0.5), 0.005)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user