Add preference for the grades modifier (#177)

Resolve #163
This commit is contained in:
Rafał Borcz 2018-11-04 13:23:34 +01:00 committed by Mikołaj Pich
parent 533637d32a
commit 2f24b25399
11 changed files with 84 additions and 23 deletions

View File

@ -22,6 +22,9 @@ class PreferencesRepository @Inject constructor(
val currentTheme: Int
get() = sharedPref.getString(currentThemeKey, "1")?.toInt() ?: 1
val gradeModifier: Double
get() = sharedPref.getString(context.getString(R.string.pref_key_grade_modifier), "0.0")?.toDouble() ?: 0.0
val serviceEnablesKey: String = context.getString(R.string.pref_key_services_enable)
val serviceEnabled: Boolean
get() = sharedPref.getBoolean(serviceEnablesKey, true)

View File

@ -4,10 +4,12 @@ import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import io.github.wulkanowy.data.ErrorHandler
import io.github.wulkanowy.data.db.entities.Grade
import io.github.wulkanowy.data.repositories.GradeRepository
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.SessionRepository
import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.calcAverage
import io.github.wulkanowy.utils.changeModifier
import io.github.wulkanowy.utils.logEvent
import io.github.wulkanowy.utils.valueColor
import timber.log.Timber
@ -17,7 +19,8 @@ class GradeDetailsPresenter @Inject constructor(
private val errorHandler: ErrorHandler,
private val schedulers: SchedulersProvider,
private val gradeRepository: GradeRepository,
private val sessionRepository: SessionRepository
private val sessionRepository: SessionRepository,
private val preferencesRepository: PreferencesRepository
) : BasePresenter<GradeDetailsView>(errorHandler) {
override fun onAttachView(view: GradeDetailsView) {
@ -28,6 +31,7 @@ class GradeDetailsPresenter @Inject constructor(
fun onParentViewLoadData(semesterId: Int, forceRefresh: Boolean) {
disposable.add(sessionRepository.getSemesters()
.flatMap { gradeRepository.getGrades(it.first { item -> item.semesterId == semesterId }, forceRefresh) }
.map { it.map { item -> item.changeModifier(preferencesRepository.gradeModifier) } }
.map { createGradeItems(it.groupBy { grade -> grade.subject }.toSortedMap()) }
.subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread)

View File

@ -4,10 +4,12 @@ import io.github.wulkanowy.data.ErrorHandler
import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.repositories.GradeRepository
import io.github.wulkanowy.data.repositories.GradeSummaryRepository
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.SessionRepository
import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.calcAverage
import io.github.wulkanowy.utils.changeModifier
import io.github.wulkanowy.utils.logEvent
import java.lang.String.format
import java.util.Locale.FRANCE
@ -18,6 +20,7 @@ class GradeSummaryPresenter @Inject constructor(
private val gradeSummaryRepository: GradeSummaryRepository,
private val gradeRepository: GradeRepository,
private val sessionRepository: SessionRepository,
private val preferencesRepository: PreferencesRepository,
private val schedulers: SchedulersProvider
) : BasePresenter<GradeSummaryView>(errorHandler) {
@ -34,7 +37,8 @@ class GradeSummaryPresenter @Inject constructor(
.flatMap { gradesSummary ->
gradeRepository.getGrades(it, forceRefresh)
.map { grades ->
grades.groupBy { grade -> grade.subject }
grades.map { item -> item.changeModifier(preferencesRepository.gradeModifier) }
.groupBy { grade -> grade.subject }
.mapValues { entry -> entry.value.calcAverage() }
.filterValues { value -> value != 0.0 }
.let { averages ->

View File

@ -33,7 +33,6 @@ inline val Grade.valueColor: Int
1 -> R.color.grade_one
else -> R.color.grade_default
}
}
inline val Grade.colorStringId: Int
@ -46,4 +45,11 @@ inline val Grade.colorStringId: Int
"B16CF1" -> R.string.all_purple
else -> R.string.all_empty_color
}
}
}
fun Grade.changeModifier(newModifier: Double): Grade {
if (modifier != 0.0 && newModifier != 0.0) {
modifier = if (modifier > 0) newModifier else -newModifier
}
return this
}

View File

@ -134,6 +134,7 @@
<string name="pref_view_summary">Pokazuj podsumowanie w ocenach</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_grade_modifier">Wartość plusa i minusa</string>
<string name="pref_notify_header">Powiadomienia</string>
<string name="pref_notify_switch">Pokazuj powiadomienia</string>

View File

@ -15,4 +15,10 @@
<item>Automatyczny</item>
<item>Używaj ustawień systemowych</item>
</string-array>
<string-array name="grade_modifier_entries">
<item>Domyślna</item>
<item>0,25</item>
<item>0,33</item>
<item>0,5</item>
</string-array>
</resources>

View File

@ -7,4 +7,5 @@
<string name="pref_key_services_interval">services_interval</string>
<string name="pref_key_services_wifi_only">services_disable_wifi_only</string>
<string name="pref_key_notifications_enable">notifications_enable</string>
<string name="pref_key_grade_modifier">grade_modifier</string>
</resources>

View File

@ -127,6 +127,7 @@
<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_theme_dark">Dark theme (Beta)</string>
<string name="pref_view_grade_modifier">Value of the plus and minus</string>
<string name="pref_notify_header">Notifications</string>
<string name="pref_notify_switch">Show notifications</string>

View File

@ -44,4 +44,17 @@
<item>720</item>
<item>1440</item>
</string-array>
<string-array name="grade_modifier_entries">
<item>Default</item>
<item>0,25</item>
<item>0,33</item>
<item>0,5</item>
</string-array>
<string-array name="grade_modifier_value" translatable="false">
<item>0.0</item>
<item>0.25</item>
<item>0.33</item>
<item>0.5</item>
</string-array>
</resources>

View File

@ -25,6 +25,14 @@
android:key="@string/pref_key_attendance_present"
android:title="@string/pref_view_present"
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"
android:summary="%s"
android:title="@string/pref_view_grade_modifier"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_services_header"

View File

@ -13,31 +13,45 @@ class GradeExtensionTest {
fun calcWeightedAverage() {
val localDate = mock(LocalDate::class.java)
assertEquals(3.47, listOf(
Grade(1, 1, "", "", 5, 0.33
, "", "", "", "", "",
6, localDate, ""),
Grade(1, 1, "", "", 5, -0.33
, "", "", "", "", "",
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, "")
Grade(1, 1, "", "", 5, 0.33
, "", "", "", "", "",
6, localDate, ""),
Grade(1, 1, "", "", 5, -0.33
, "", "", "", "", "",
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)
}
@Test
fun calcSummaryAverage() {
assertEquals(2.5, listOf(
GradeSummary(1, 1, "", "", "5"),
GradeSummary(1, 1, "", "", "-5"),
GradeSummary(1, 1, "", "", "test"),
GradeSummary(1, 1, "", "", "0")
GradeSummary(1, 1, "", "", "5"),
GradeSummary(1, 1, "", "", "-5"),
GradeSummary(1, 1, "", "", "test"),
GradeSummary(1, 1, "", "", "0")
).calcAverage(), 0.005)
}
@Test
fun changeModifierTest() {
val localDate = mock(LocalDate::class.java)
assertEquals(0.33, Grade(1, 1, "", "", 5, 0.25
, "", "", "", "", "",
6, localDate, "").changeModifier(0.33).modifier, 0.0)
assertEquals(-0.33, Grade(1, 1, "", "", 5, -0.25
, "", "", "", "", "",
6, localDate, "").changeModifier(0.33).modifier, 0.0)
assertEquals(0.25, Grade(1, 1, "", "", 5, 0.25
, "", "", "", "", "",
6, localDate, "").changeModifier(0.0).modifier, 0.0)
}
}