From 2f24b25399b6ebcee79c924ac15d3253dba2ac61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sun, 4 Nov 2018 13:23:34 +0100 Subject: [PATCH] Add preference for the grades modifier (#177) Resolve #163 --- .../repositories/PreferencesRepository.kt | 3 ++ .../grade/details/GradeDetailsPresenter.kt | 6 ++- .../grade/summary/GradeSummaryPresenter.kt | 6 ++- .../github/wulkanowy/utils/GradeExtension.kt | 10 +++- app/src/main/res/values-pl/strings.xml | 1 + .../main/res/values-pl/value_prefernces.xml | 6 +++ app/src/main/res/values/preferences_keys.xml | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/value_prefernces.xml | 13 +++++ app/src/main/res/xml/scheme_preferences.xml | 8 +++ .../wulkanowy/utils/GradeExtensionTest.kt | 52 ++++++++++++------- 11 files changed, 84 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt index 2c4a1242..2efea046 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt @@ -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) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt index 46539aec..a24e395f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt @@ -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(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) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt index a6479e4d..874e3247 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt @@ -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(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 -> diff --git a/app/src/main/java/io/github/wulkanowy/utils/GradeExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/GradeExtension.kt index c9119cc3..b75cca1a 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/GradeExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/GradeExtension.kt @@ -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 } - } \ No newline at end of file + } + +fun Grade.changeModifier(newModifier: Double): Grade { + if (modifier != 0.0 && newModifier != 0.0) { + modifier = if (modifier > 0) newModifier else -newModifier + } + return this +} \ No newline at end of file diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 4a7ee27a..ca9d3707 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -134,6 +134,7 @@ Pokazuj podsumowanie w ocenach Pokazuj obecność we frekwencji Ciemny motyw (Beta) + Wartość plusa i minusa Powiadomienia Pokazuj powiadomienia diff --git a/app/src/main/res/values-pl/value_prefernces.xml b/app/src/main/res/values-pl/value_prefernces.xml index 611add4b..c15fa89e 100644 --- a/app/src/main/res/values-pl/value_prefernces.xml +++ b/app/src/main/res/values-pl/value_prefernces.xml @@ -15,4 +15,10 @@ Automatyczny Używaj ustawień systemowych + + Domyślna + 0,25 + 0,33 + 0,5 + diff --git a/app/src/main/res/values/preferences_keys.xml b/app/src/main/res/values/preferences_keys.xml index 974d8242..cc829354 100644 --- a/app/src/main/res/values/preferences_keys.xml +++ b/app/src/main/res/values/preferences_keys.xml @@ -7,4 +7,5 @@ services_interval services_disable_wifi_only notifications_enable + grade_modifier diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 61e45d6c..1f68f54d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -127,6 +127,7 @@ Show the summary in the grades Show presence in attendance Dark theme (Beta) + Value of the plus and minus Notifications Show notifications diff --git a/app/src/main/res/values/value_prefernces.xml b/app/src/main/res/values/value_prefernces.xml index bf07b515..56c5de0e 100644 --- a/app/src/main/res/values/value_prefernces.xml +++ b/app/src/main/res/values/value_prefernces.xml @@ -44,4 +44,17 @@ 720 1440 + + + Default + 0,25 + 0,33 + 0,5 + + + 0.0 + 0.25 + 0.33 + 0.5 + diff --git a/app/src/main/res/xml/scheme_preferences.xml b/app/src/main/res/xml/scheme_preferences.xml index e3912555..61717155 100644 --- a/app/src/main/res/xml/scheme_preferences.xml +++ b/app/src/main/res/xml/scheme_preferences.xml @@ -25,6 +25,14 @@ android:key="@string/pref_key_attendance_present" android:title="@string/pref_view_present" app:iconSpaceReserved="false" /> +