Add option to change plus and minus modifier separately (#192)

This commit is contained in:
Mikołaj Pich 2018-12-07 00:19:04 +01:00 committed by Rafał Borcz
parent 92baecbd0d
commit 900065d758
9 changed files with 82 additions and 41 deletions

View File

@ -24,8 +24,11 @@ class PreferencesRepository @Inject constructor(
val currentTheme: Int val currentTheme: Int
get() = sharedPref.getString(currentThemeKey, "1")?.toInt() ?: 1 get() = sharedPref.getString(currentThemeKey, "1")?.toInt() ?: 1
val gradeModifier: Double val gradePlusModifier: Double
get() = sharedPref.getString(context.getString(R.string.pref_key_grade_modifier), "0.0")?.toDouble() ?: 0.0 get() = sharedPref.getString(context.getString(R.string.pref_key_grade_modifier_plus), "0.0")?.toDouble() ?: 0.0
val gradeMinusModifier: Double
get() = sharedPref.getString(context.getString(R.string.pref_key_grade_modifier_minus), "0.0")?.toDouble() ?: 0.0
val serviceEnablesKey: String = context.getString(R.string.pref_key_services_enable) val serviceEnablesKey: String = context.getString(R.string.pref_key_services_enable)
val isServiceEnabled: Boolean val isServiceEnabled: Boolean

View File

@ -34,7 +34,7 @@ class GradeDetailsPresenter @Inject constructor(
disposable.add(studentRepository.getCurrentStudent() disposable.add(studentRepository.getCurrentStudent()
.flatMap { semesterRepository.getSemesters(it) } .flatMap { semesterRepository.getSemesters(it) }
.flatMap { gradeRepository.getGrades(it.first { item -> item.semesterId == semesterId }, forceRefresh) } .flatMap { gradeRepository.getGrades(it.first { item -> item.semesterId == semesterId }, forceRefresh) }
.map { it.map { item -> item.changeModifier(preferencesRepository.gradeModifier) } } .map { it.map { item -> item.changeModifier(preferencesRepository.gradePlusModifier, preferencesRepository.gradeMinusModifier) } }
.map { createGradeItems(it.groupBy { grade -> grade.subject }.toSortedMap()) } .map { createGradeItems(it.groupBy { grade -> grade.subject }.toSortedMap()) }
.subscribeOn(schedulers.backgroundThread) .subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread) .observeOn(schedulers.mainThread)

View File

@ -40,7 +40,7 @@ class GradeSummaryPresenter @Inject constructor(
.flatMap { gradesSummary -> .flatMap { gradesSummary ->
gradeRepository.getGrades(it, forceRefresh) gradeRepository.getGrades(it, forceRefresh)
.map { grades -> .map { grades ->
grades.map { item -> item.changeModifier(preferencesRepository.gradeModifier) } grades.map { item -> item.changeModifier(preferencesRepository.gradePlusModifier, preferencesRepository.gradeMinusModifier) }
.groupBy { grade -> grade.subject } .groupBy { grade -> grade.subject }
.mapValues { entry -> entry.value.calcAverage() } .mapValues { entry -> entry.value.calcAverage() }
.filterValues { value -> value != 0.0 } .filterValues { value -> value != 0.0 }

View File

@ -47,9 +47,14 @@ inline val Grade.colorStringId: Int
} }
} }
fun Grade.changeModifier(newModifier: Double): Grade { fun Grade.changeModifier(plusModifier: Double, minusModifier: Double): Grade {
if (modifier != 0.0 && newModifier != 0.0) { if (modifier != 0.0) {
modifier = if (modifier > 0) newModifier else -newModifier if (plusModifier != 0.0 && modifier > 0) {
modifier = plusModifier
}
if (minusModifier != .0 && modifier < 0) {
modifier = -minusModifier
}
} }
return this return this
} }

View File

@ -217,7 +217,8 @@
<string name="pref_view_summary">Pokazuj podsumowanie w ocenach</string> <string name="pref_view_summary">Pokazuj podsumowanie w ocenach</string>
<string name="pref_view_present">Pokazuj obecność we frekwencji</string> <string name="pref_view_present">Pokazuj obecność we frekwencji</string>
<string name="pref_view_theme_dark">Ciemny motyw (Beta)</string> <string name="pref_view_theme_dark">Ciemny motyw (Beta)</string>
<string name="pref_view_grade_modifier">Wartość plusa i minusa</string> <string name="pref_view_grade_modifier_plus">Wartość plusa</string>
<string name="pref_view_grade_modifier_minus">Wartość minusa</string>
<string name="pref_view_expand_grade">Rozwiń oceny</string> <string name="pref_view_expand_grade">Rozwiń oceny</string>
<string name="pref_notify_header">Powiadomienia</string> <string name="pref_notify_header">Powiadomienia</string>

View File

@ -3,7 +3,8 @@
<string name="pref_key_start_menu">start_menu</string> <string name="pref_key_start_menu">start_menu</string>
<string name="pref_key_attendance_present">attendance_present</string> <string name="pref_key_attendance_present">attendance_present</string>
<string name="pref_key_theme">theme</string> <string name="pref_key_theme">theme</string>
<string name="pref_key_grade_modifier">grade_modifier</string> <string name="pref_key_grade_modifier_plus">grade_modifier_plus</string>
<string name="pref_key_grade_modifier_minus">grade_modifier_minus</string>
<string name="pref_key_expand_grade">expand_grade</string> <string name="pref_key_expand_grade">expand_grade</string>
<string name="pref_key_services_enable">services_enable</string> <string name="pref_key_services_enable">services_enable</string>
<string name="pref_key_services_interval">services_interval</string> <string name="pref_key_services_interval">services_interval</string>

View File

@ -199,7 +199,8 @@
<string name="pref_view_summary">Show the summary in the grades</string> <string name="pref_view_summary">Show the summary in the grades</string>
<string name="pref_view_present">Show presence in attendance</string> <string name="pref_view_present">Show presence in attendance</string>
<string name="pref_view_theme_dark">Dark theme (Beta)</string> <string name="pref_view_theme_dark">Dark theme (Beta)</string>
<string name="pref_view_grade_modifier">Value of the plus and minus</string> <string name="pref_view_grade_modifier_plus">Value of the plus</string>
<string name="pref_view_grade_modifier_minus">Value of the minus</string>
<string name="pref_view_expand_grade">Expand grades</string> <string name="pref_view_expand_grade">Expand grades</string>
<string name="pref_notify_header">Notifications</string> <string name="pref_notify_header">Notifications</string>

View File

@ -34,9 +34,17 @@
android:defaultValue="0.0" android:defaultValue="0.0"
android:entries="@array/grade_modifier_entries" android:entries="@array/grade_modifier_entries"
android:entryValues="@array/grade_modifier_value" android:entryValues="@array/grade_modifier_value"
android:key="@string/pref_key_grade_modifier" android:key="@string/pref_key_grade_modifier_plus"
android:summary="%s" android:summary="%s"
android:title="@string/pref_view_grade_modifier" android:title="@string/pref_view_grade_modifier_plus"
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="0.0"
android:entries="@array/grade_modifier_entries"
android:entryValues="@array/grade_modifier_value"
android:key="@string/pref_key_grade_modifier_minus"
android:summary="%s"
android:title="@string/pref_view_grade_modifier_minus"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory

View File

@ -3,31 +3,30 @@ package io.github.wulkanowy.utils
import io.github.wulkanowy.data.db.entities.Grade 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 org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.mockito.Mockito.mock import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
class GradeExtensionTest { class GradeExtensionTest {
@Mock
lateinit var date: LocalDate
@Before
fun before() {
MockitoAnnotations.initMocks(this)
}
@Test @Test
fun calcWeightedAverage() { fun calcWeightedAverage() {
val localDate = mock(LocalDate::class.java)
assertEquals(3.47, listOf( assertEquals(3.47, listOf(
Grade(1, 1, "", "", 5, 0.33 createGrade(5, 6, 0.33),
, "", "", "", "", "", createGrade(5, 5, -0.33),
6, localDate, ""), createGrade(4, 1, 0.0),
Grade(1, 1, "", "", 5, -0.33 createGrade(1, 9, 0.5),
, "", "", "", "", "", createGrade(0, 0, 0.0)
5, localDate, ""),
Grade(1, 1, "", "", 4, 0.0
, "", "", "", "", "",
1, localDate, ""),
Grade(1, 1, "", "", 1, 0.5
, "", "", "", "", "",
9, localDate, ""),
Grade(1, 1, "", "", 0, 0.0
, "", "", "", "", "",
0, localDate, "")
).calcAverage(), 0.005) ).calcAverage(), 0.005)
} }
@ -42,16 +41,39 @@ class GradeExtensionTest {
} }
@Test @Test
fun changeModifierTest() { fun changeModifier_default() {
val localDate = mock(LocalDate::class.java) assertEquals(.33, createGrade(5, 0, .33).changeModifier(.0, .0).modifier, .0)
assertEquals(0.33, Grade(1, 1, "", "", 5, 0.25 assertEquals(-.33, createGrade(5, 0, -.33).changeModifier(.0, .0).modifier, .0)
, "", "", "", "", "", }
6, localDate, "").changeModifier(0.33).modifier, 0.0)
assertEquals(-0.33, Grade(1, 1, "", "", 5, -0.25 @Test
, "", "", "", "", "", fun changeModifier_plus() {
6, localDate, "").changeModifier(0.33).modifier, 0.0) assertEquals(.33, createGrade(5, 0, .25).changeModifier(.33, .50).modifier, .0)
assertEquals(0.25, Grade(1, 1, "", "", 5, 0.25 assertEquals(.25, createGrade(5, 0, .33).changeModifier(.25, .0).modifier, .0)
, "", "", "", "", "", }
6, localDate, "").changeModifier(0.0).modifier, 0.0)
@Test
fun changeModifier_minus() {
assertEquals(-.33, createGrade(5, 0, -.25).changeModifier(.25, .33).modifier, .0)
assertEquals(-.25, createGrade(5, 0, -.33).changeModifier(.0, .25).modifier, .0)
}
private fun createGrade(value: Int, weightValue: Int = 0, modifier: Double = 0.25): Grade {
return Grade(
semesterId = 1,
studentId = 1,
subject = "",
entry = "",
value = value,
modifier = modifier,
comment = "",
color = "",
gradeSymbol = "",
description = "",
weight = "",
weightValue = weightValue,
date = date,
teacher = ""
)
} }
} }