From d2aa940d4670696dac8bb0255b0227a4f99ae731 Mon Sep 17 00:00:00 2001 From: Michael <5672750+mibac138@users.noreply.github.com> Date: Sat, 27 Nov 2021 10:11:17 +0100 Subject: [PATCH] Convert from a stringly typed grade color to enum GradeColorTheme (#1672) --- .../data/db/migrations/Migration41.kt | 2 +- .../wulkanowy/data/enums/GradeColorTheme.kt | 13 ++++++ .../wulkanowy/data/enums/GradeExpandMode.kt | 11 +++++ .../wulkanowy/data/enums/GradeSortingMode.kt | 10 +++++ .../repositories/PreferencesRepository.kt | 15 ++++--- .../ui/modules/dashboard/DashboardAdapter.kt | 3 +- .../dashboard/DashboardGradesAdapter.kt | 5 ++- .../ui/modules/dashboard/DashboardItem.kt | 3 +- .../ui/modules/grade/GradeExpandMode.kt | 9 ---- .../ui/modules/grade/GradeSortingMode.kt | 10 ----- .../grade/details/GradeDetailsAdapter.kt | 7 +-- .../grade/details/GradeDetailsDialog.kt | 13 +++--- .../grade/details/GradeDetailsFragment.kt | 11 ++--- .../grade/details/GradeDetailsPresenter.kt | 6 +-- .../modules/grade/details/GradeDetailsView.kt | 7 +-- .../statistics/GradeStatisticsAdapter.kt | 7 +-- .../statistics/GradeStatisticsFragment.kt | 5 ++- .../grade/statistics/GradeStatisticsView.kt | 3 +- .../github/wulkanowy/utils/GradeExtension.kt | 45 ++++++++++--------- .../wulkanowy/utils/GradeExtensionTest.kt | 24 ++++++++-- 20 files changed, 127 insertions(+), 82 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/data/enums/GradeColorTheme.kt create mode 100644 app/src/main/java/io/github/wulkanowy/data/enums/GradeExpandMode.kt create mode 100644 app/src/main/java/io/github/wulkanowy/data/enums/GradeSortingMode.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeExpandMode.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSortingMode.kt diff --git a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration41.kt b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration41.kt index 0080e057c..ccaf85755 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration41.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration41.kt @@ -3,7 +3,7 @@ package io.github.wulkanowy.data.db.migrations import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import io.github.wulkanowy.data.db.SharedPrefProvider -import io.github.wulkanowy.ui.modules.grade.GradeExpandMode +import io.github.wulkanowy.data.enums.GradeExpandMode class Migration41(private val sharedPrefProvider: SharedPrefProvider) : Migration(40, 41) { diff --git a/app/src/main/java/io/github/wulkanowy/data/enums/GradeColorTheme.kt b/app/src/main/java/io/github/wulkanowy/data/enums/GradeColorTheme.kt new file mode 100644 index 000000000..24b095d0e --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/enums/GradeColorTheme.kt @@ -0,0 +1,13 @@ +package io.github.wulkanowy.data.enums + +import java.io.Serializable + +enum class GradeColorTheme(val value: String) : Serializable { + VULCAN("vulcan"), + MATERIAL("material"), + GRADE_COLOR("grade_color"); + + companion object { + fun getByValue(value: String) = values().find { it.value == value } ?: VULCAN + } +} \ No newline at end of file diff --git a/app/src/main/java/io/github/wulkanowy/data/enums/GradeExpandMode.kt b/app/src/main/java/io/github/wulkanowy/data/enums/GradeExpandMode.kt new file mode 100644 index 000000000..96e4a174d --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/enums/GradeExpandMode.kt @@ -0,0 +1,11 @@ +package io.github.wulkanowy.data.enums + +enum class GradeExpandMode(val value: String) { + ONE("one"), + UNLIMITED("any"), + ALWAYS_EXPANDED("always"); + + companion object { + fun getByValue(value: String) = values().find { it.value == value } ?: ONE + } +} \ No newline at end of file diff --git a/app/src/main/java/io/github/wulkanowy/data/enums/GradeSortingMode.kt b/app/src/main/java/io/github/wulkanowy/data/enums/GradeSortingMode.kt new file mode 100644 index 000000000..c5c0196c9 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/enums/GradeSortingMode.kt @@ -0,0 +1,10 @@ +package io.github.wulkanowy.data.enums + +enum class GradeSortingMode(val value: String) { + ALPHABETIC("alphabetic"), + DATE("date"); + + companion object { + fun getByValue(value: String) = values().find { it.value == value } ?: ALPHABETIC + } +} \ No newline at end of file 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 11f38c3f4..f4fb77d89 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 @@ -7,11 +7,12 @@ import com.fredporciuncula.flow.preferences.FlowSharedPreferences import com.fredporciuncula.flow.preferences.Preference import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R +import io.github.wulkanowy.data.enums.GradeColorTheme +import io.github.wulkanowy.data.enums.GradeExpandMode +import io.github.wulkanowy.data.enums.GradeSortingMode import io.github.wulkanowy.sdk.toLocalDate import io.github.wulkanowy.ui.modules.dashboard.DashboardItem import io.github.wulkanowy.ui.modules.grade.GradeAverageMode -import io.github.wulkanowy.ui.modules.grade.GradeExpandMode -import io.github.wulkanowy.ui.modules.grade.GradeSortingMode import io.github.wulkanowy.utils.toLocalDateTime import io.github.wulkanowy.utils.toTimestamp import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -75,10 +76,12 @@ class PreferencesRepository @Inject constructor( val appTheme: String get() = getString(appThemeKey, R.string.pref_default_app_theme) - val gradeColorTheme: String - get() = getString( - R.string.pref_key_grade_color_scheme, - R.string.pref_default_grade_color_scheme + val gradeColorTheme: GradeColorTheme + get() = GradeColorTheme.getByValue( + getString( + R.string.pref_key_grade_color_scheme, + R.string.pref_default_grade_color_scheme + ) ) val appLanguageKey = context.getString(R.string.pref_key_app_language) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/dashboard/DashboardAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/dashboard/DashboardAdapter.kt index 1c09c2a74..6dfecd620 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/dashboard/DashboardAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/dashboard/DashboardAdapter.kt @@ -20,6 +20,7 @@ import io.github.wulkanowy.data.db.entities.AdminMessage import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.db.entities.TimetableHeader +import io.github.wulkanowy.data.enums.GradeColorTheme import io.github.wulkanowy.databinding.ItemDashboardAccountBinding import io.github.wulkanowy.databinding.ItemDashboardAdminMessageBinding import io.github.wulkanowy.databinding.ItemDashboardAnnouncementsBinding @@ -262,7 +263,7 @@ class DashboardAdapter @Inject constructor() : RecyclerView.Adapter>>() - var gradeTheme = "" + lateinit var gradeColorTheme: GradeColorTheme override fun getItemCount() = items.size @@ -36,7 +37,7 @@ class DashboardGradesAdapter : RecyclerView.Adapter>? = null, - val gradeTheme: String? = null, + val gradeTheme: GradeColorTheme? = null, override val error: Throwable? = null, override val isLoading: Boolean = false ) : DashboardItem(Type.GRADES) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeExpandMode.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeExpandMode.kt deleted file mode 100644 index 722e986ee..000000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeExpandMode.kt +++ /dev/null @@ -1,9 +0,0 @@ -package io.github.wulkanowy.ui.modules.grade - -enum class GradeExpandMode(val value: String) { - ONE("one"), UNLIMITED("any"), ALWAYS_EXPANDED("always"); - - companion object { - fun getByValue(value: String) = values().firstOrNull { it.value == value } ?: ONE - } -} \ No newline at end of file diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSortingMode.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSortingMode.kt deleted file mode 100644 index 1e6b26e8c..000000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeSortingMode.kt +++ /dev/null @@ -1,10 +0,0 @@ -package io.github.wulkanowy.ui.modules.grade - -enum class GradeSortingMode(val value: String) { - ALPHABETIC("alphabetic"), - DATE("date"); - - companion object { - fun getByValue(value: String) = values().firstOrNull { it.value == value } ?: ALPHABETIC - } -} \ No newline at end of file diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt index d96ac0928..e5c3bb63e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsAdapter.kt @@ -11,10 +11,11 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.NO_POSITION import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade +import io.github.wulkanowy.data.enums.GradeColorTheme +import io.github.wulkanowy.data.enums.GradeExpandMode import io.github.wulkanowy.databinding.HeaderGradeDetailsBinding import io.github.wulkanowy.databinding.ItemGradeDetailsBinding import io.github.wulkanowy.ui.base.BaseExpandableAdapter -import io.github.wulkanowy.ui.modules.grade.GradeExpandMode import io.github.wulkanowy.utils.getBackgroundColor import io.github.wulkanowy.utils.toFormattedString import timber.log.Timber @@ -33,7 +34,7 @@ class GradeDetailsAdapter @Inject constructor() : BaseExpandableAdapter Unit = { _, _ -> } - var colorTheme = "" + lateinit var gradeColorTheme: GradeColorTheme fun setDataItems(data: List, expandMode: GradeExpandMode = this.expandMode) { headers = data.filter { it.viewType == ViewType.HEADER }.toMutableList() @@ -202,7 +203,7 @@ class GradeDetailsAdapter @Inject constructor() : BaseExpandableAdapter grade.description diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsDialog.kt index 286194464..a9d9039dd 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsDialog.kt @@ -8,6 +8,7 @@ import android.view.ViewGroup import androidx.fragment.app.DialogFragment import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade +import io.github.wulkanowy.data.enums.GradeColorTheme import io.github.wulkanowy.databinding.DialogGradeBinding import io.github.wulkanowy.utils.colorStringId import io.github.wulkanowy.utils.getBackgroundColor @@ -21,19 +22,19 @@ class GradeDetailsDialog : DialogFragment() { private lateinit var grade: Grade - private lateinit var colorScheme: String + private lateinit var gradeColorTheme: GradeColorTheme companion object { private const val ARGUMENT_KEY = "Item" - private const val COLOR_SCHEME_KEY = "Scheme" + private const val COLOR_THEME_KEY = "Theme" - fun newInstance(grade: Grade, colorScheme: String) = + fun newInstance(grade: Grade, colorTheme: GradeColorTheme) = GradeDetailsDialog().apply { arguments = Bundle().apply { putSerializable(ARGUMENT_KEY, grade) - putString(COLOR_SCHEME_KEY, colorScheme) + putSerializable(COLOR_THEME_KEY, colorTheme) } } } @@ -43,7 +44,7 @@ class GradeDetailsDialog : DialogFragment() { setStyle(STYLE_NO_TITLE, 0) arguments?.run { grade = getSerializable(ARGUMENT_KEY) as Grade - colorScheme = getString(COLOR_SCHEME_KEY) ?: "default" + gradeColorTheme = getSerializable(COLOR_THEME_KEY) as GradeColorTheme } } @@ -76,7 +77,7 @@ class GradeDetailsDialog : DialogFragment() { gradeDialogValue.run { text = grade.entry - setBackgroundResource(grade.getBackgroundColor(colorScheme)) + setBackgroundResource(grade.getBackgroundColor(gradeColorTheme)) } gradeDialogTeacherValue.text = if (grade.teacher.isBlank()) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt index c93600d49..81f3226ad 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt @@ -12,7 +12,8 @@ import androidx.recyclerview.widget.LinearLayoutManager import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade -import io.github.wulkanowy.ui.modules.grade.GradeExpandMode +import io.github.wulkanowy.data.enums.GradeColorTheme +import io.github.wulkanowy.data.enums.GradeExpandMode import io.github.wulkanowy.databinding.FragmentGradeDetailsBinding import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.grade.GradeFragment @@ -80,9 +81,9 @@ class GradeDetailsFragment : else false } - override fun updateData(data: List, expandMode: GradeExpandMode, gradeColorTheme: String) { + override fun updateData(data: List, expandMode: GradeExpandMode, gradeColorTheme: GradeColorTheme) { with(gradeDetailsAdapter) { - colorTheme = gradeColorTheme + this.gradeColorTheme = gradeColorTheme setDataItems(data, expandMode) notifyDataSetChanged() } @@ -143,8 +144,8 @@ class GradeDetailsFragment : binding.gradeDetailsSwipe.isRefreshing = show } - override fun showGradeDialog(grade: Grade, colorScheme: String) { - (activity as? MainActivity)?.showDialogFragment(GradeDetailsDialog.newInstance(grade, colorScheme)) + override fun showGradeDialog(grade: Grade, colorTheme: GradeColorTheme) { + (activity as? MainActivity)?.showDialogFragment(GradeDetailsDialog.newInstance(grade, colorTheme)) } override fun onParentLoadData(semesterId: Int, forceRefresh: Boolean) { 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 54d4f461e..ce36f8ac4 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 @@ -2,6 +2,9 @@ package io.github.wulkanowy.ui.modules.grade.details import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Grade +import io.github.wulkanowy.data.enums.GradeExpandMode +import io.github.wulkanowy.data.enums.GradeSortingMode.ALPHABETIC +import io.github.wulkanowy.data.enums.GradeSortingMode.DATE import io.github.wulkanowy.data.repositories.GradeRepository import io.github.wulkanowy.data.repositories.PreferencesRepository import io.github.wulkanowy.data.repositories.SemesterRepository @@ -9,9 +12,6 @@ import io.github.wulkanowy.data.repositories.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider -import io.github.wulkanowy.ui.modules.grade.GradeExpandMode -import io.github.wulkanowy.ui.modules.grade.GradeSortingMode.ALPHABETIC -import io.github.wulkanowy.ui.modules.grade.GradeSortingMode.DATE import io.github.wulkanowy.ui.modules.grade.GradeSubject import io.github.wulkanowy.utils.AnalyticsHelper import io.github.wulkanowy.utils.afterLoading diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsView.kt index 556332290..491bf3003 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsView.kt @@ -1,7 +1,8 @@ package io.github.wulkanowy.ui.modules.grade.details import io.github.wulkanowy.data.db.entities.Grade -import io.github.wulkanowy.ui.modules.grade.GradeExpandMode +import io.github.wulkanowy.data.enums.GradeColorTheme +import io.github.wulkanowy.data.enums.GradeExpandMode import io.github.wulkanowy.ui.base.BaseView interface GradeDetailsView : BaseView { @@ -10,7 +11,7 @@ interface GradeDetailsView : BaseView { fun initView() - fun updateData(data: List, expandMode: GradeExpandMode, gradeColorTheme: String) + fun updateData(data: List, expandMode: GradeExpandMode, gradeColorTheme: GradeColorTheme) fun updateItem(item: Grade, position: Int) @@ -22,7 +23,7 @@ interface GradeDetailsView : BaseView { fun collapseAllItems() - fun showGradeDialog(grade: Grade, colorScheme: String) + fun showGradeDialog(grade: Grade, colorTheme: GradeColorTheme) fun showContent(show: Boolean) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsAdapter.kt index bf0b20142..6be2d969f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsAdapter.kt @@ -20,6 +20,7 @@ import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.GradePartialStatistics import io.github.wulkanowy.data.db.entities.GradePointsStatistics import io.github.wulkanowy.data.db.entities.GradeSemesterStatistics +import io.github.wulkanowy.data.enums.GradeColorTheme import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.databinding.ItemGradeStatisticsBarBinding import io.github.wulkanowy.databinding.ItemGradeStatisticsHeaderBinding @@ -34,7 +35,7 @@ class GradeStatisticsAdapter @Inject constructor() : var items = emptyList() - var theme: String = "vulcan" + lateinit var gradeColorTheme: GradeColorTheme var showAllSubjectsOnList: Boolean = false @@ -156,8 +157,8 @@ class GradeStatisticsAdapter @Inject constructor() : visibility = if (items.size == 1 || !showAllSubjectsOnList) GONE else VISIBLE } - val gradeColors = when (theme) { - "vulcan" -> vulcanGradeColors + val gradeColors = when (gradeColorTheme) { + GradeColorTheme.VULCAN -> vulcanGradeColors else -> materialGradeColors } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt index 16162d119..35d007749 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt @@ -7,6 +7,7 @@ import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R +import io.github.wulkanowy.data.enums.GradeColorTheme import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.databinding.FragmentGradeStatisticsBinding import io.github.wulkanowy.ui.base.BaseFragment @@ -90,12 +91,12 @@ class GradeStatisticsFragment : override fun updateData( newItems: List, - newTheme: String, + newTheme: GradeColorTheme, showAllSubjectsOnStatisticsList: Boolean ) { with(statisticsAdapter) { showAllSubjectsOnList = showAllSubjectsOnStatisticsList - theme = newTheme + gradeColorTheme = newTheme items = newItems notifyDataSetChanged() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsView.kt index 405118178..8e9a206bc 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsView.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.grade.statistics +import io.github.wulkanowy.data.enums.GradeColorTheme import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.ui.base.BaseView @@ -15,7 +16,7 @@ interface GradeStatisticsView : BaseView { fun updateData( newItems: List, - newTheme: String, + newTheme: GradeColorTheme, showAllSubjectsOnStatisticsList: Boolean ) 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 1be3093fe..ff65d6376 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/GradeExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/GradeExtension.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.utils import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary +import io.github.wulkanowy.data.enums.GradeColorTheme import io.github.wulkanowy.sdk.scrapper.grades.isGradeValid fun List.calcAverage(isOptionalArithmeticAverage: Boolean): Double { @@ -37,28 +38,6 @@ fun List.calcFinalAverage(plusModifier: Double, minusModifier: Dou .average() .let { if (it.isNaN()) 0.0 else it } -fun Grade.getBackgroundColor(theme: String) = when (theme) { - "grade_color" -> getGradeColor() - "material" -> when (value.toInt()) { - 6 -> R.color.grade_material_six - 5 -> R.color.grade_material_five - 4 -> R.color.grade_material_four - 3 -> R.color.grade_material_three - 2 -> R.color.grade_material_two - 1 -> R.color.grade_material_one - else -> R.color.grade_material_default - } - else -> when (value.toInt()) { - 6 -> R.color.grade_vulcan_six - 5 -> R.color.grade_vulcan_five - 4 -> R.color.grade_vulcan_four - 3 -> R.color.grade_vulcan_three - 2 -> R.color.grade_vulcan_two - 1 -> R.color.grade_vulcan_one - else -> R.color.grade_vulcan_default - } -} - fun Grade.getGradeColor() = when (color) { "000000" -> R.color.grade_black "F04C4C" -> R.color.grade_red @@ -83,3 +62,25 @@ fun Grade.changeModifier(plusModifier: Double, minusModifier: Double) = when { modifier < 0 -> copy(modifier = -minusModifier) else -> this } + +fun Grade.getBackgroundColor(theme: GradeColorTheme) = when (theme) { + GradeColorTheme.GRADE_COLOR -> getGradeColor() + GradeColorTheme.MATERIAL -> when (value.toInt()) { + 6 -> R.color.grade_material_six + 5 -> R.color.grade_material_five + 4 -> R.color.grade_material_four + 3 -> R.color.grade_material_three + 2 -> R.color.grade_material_two + 1 -> R.color.grade_material_one + else -> R.color.grade_material_default + } + GradeColorTheme.VULCAN -> when (value.toInt()) { + 6 -> R.color.grade_vulcan_six + 5 -> R.color.grade_vulcan_five + 4 -> R.color.grade_vulcan_four + 3 -> R.color.grade_vulcan_three + 2 -> R.color.grade_vulcan_two + 1 -> R.color.grade_vulcan_one + else -> R.color.grade_vulcan_default + } +} diff --git a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt index 39d4c3bdb..35dc4e5ba 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.utils import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary +import io.github.wulkanowy.data.enums.GradeColorTheme import io.mockk.MockKAnnotations import io.mockk.impl.annotations.MockK import org.junit.Assert.assertEquals @@ -46,10 +47,25 @@ class GradeExtensionTest { @Test fun getBackgroundColor() { - assertEquals(R.color.grade_material_five, createGrade(5.0).getBackgroundColor("material")) - assertEquals(R.color.grade_material_five, createGrade(5.5).getBackgroundColor("material")) - assertEquals(R.color.grade_material_five, createGrade(5.9).getBackgroundColor("material")) - assertEquals(R.color.grade_vulcan_five, createGrade(5.9).getBackgroundColor("whatever")) + assertEquals( + R.color.grade_material_five, createGrade(5.0).getBackgroundColor( + GradeColorTheme.MATERIAL + ) + ) + assertEquals( + R.color.grade_material_five, createGrade(5.5).getBackgroundColor( + GradeColorTheme.MATERIAL + ) + ) + assertEquals( + R.color.grade_material_five, createGrade(5.9).getBackgroundColor( + GradeColorTheme.MATERIAL + ) + ) + assertEquals( + R.color.grade_vulcan_five, + createGrade(5.9).getBackgroundColor(GradeColorTheme.VULCAN) + ) } @Test