From 8644ce32d51721b6835d47bcf1c29e9376e0eb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Mon, 15 Mar 2021 23:58:50 +0100 Subject: [PATCH] Fix semester switch when student have only one semester (#1215) --- app/build.gradle | 2 +- .../ui/modules/grade/GradeFragment.kt | 9 ++++----- .../ui/modules/grade/GradePresenter.kt | 20 +++++++++++++------ .../wulkanowy/ui/modules/grade/GradeView.kt | 3 ++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e6301b2f6..3a449f5ec 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -150,7 +150,7 @@ ext { } dependencies { - implementation "io.github.wulkanowy:sdk:1.1.0" + implementation "io.github.wulkanowy:sdk:b2c274e" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5' diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt index 06b3e9316..b3ef3037a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt @@ -10,6 +10,7 @@ import android.view.View.VISIBLE import androidx.appcompat.app.AlertDialog import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R +import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.databinding.FragmentGradeBinding import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.base.BaseFragmentPagerAdapter @@ -121,11 +122,9 @@ class GradeFragment : BaseFragment(R.layout.fragment_grade semesterSwitchMenu?.isVisible = show } - override fun showSemesterDialog(selectedIndex: Int) { - val choices = arrayOf( - getString(R.string.grade_semester, 1), - getString(R.string.grade_semester, 2) - ) + override fun showSemesterDialog(selectedIndex: Int, semesters: List) { + val choices = semesters.map { getString(R.string.grade_semester, it.semesterName) } + .toTypedArray() AlertDialog.Builder(requireContext()) .setSingleChoiceItems(choices, selectedIndex) { dialog, which -> diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt index bfc504d2c..c467c421e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt @@ -49,7 +49,9 @@ class GradePresenter @Inject constructor( } fun onSemesterSwitch(): Boolean { - if (semesters.isNotEmpty()) view?.showSemesterDialog(selectedIndex - 1) + if (semesters.isNotEmpty()) { + view?.showSemesterDialog(selectedIndex - 1, semesters.slice(0..1)) + } return true } @@ -137,11 +139,17 @@ class GradePresenter @Inject constructor( private fun loadChild(index: Int, forceRefresh: Boolean = false) { Timber.d("Load grade tab child. Selected semester: $selectedIndex, semesters: ${semesters.joinToString { it.semesterName.toString() }}") - semesters.first { it.semesterName == selectedIndex }.semesterId.also { - if (forceRefresh || loadedSemesterId[index] != it) { - Timber.i("Load grade child view index: $index") - view?.notifyChildLoadData(index, it, forceRefresh) - } + + val newSelectedSemesterId = try { + semesters.first { it.semesterName == selectedIndex }.semesterId + } catch (e: NoSuchElementException) { + Timber.e(e, "Selected semester no exists") + return + } + + if (forceRefresh || loadedSemesterId[index] != newSelectedSemesterId) { + Timber.i("Load grade child view index: $index") + view?.notifyChildLoadData(index, newSelectedSemesterId, forceRefresh) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeView.kt index 7b52daa88..104f8505a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeView.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.grade +import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.ui.base.BaseView interface GradeView : BaseView { @@ -18,7 +19,7 @@ interface GradeView : BaseView { fun showSemesterSwitch(show: Boolean) - fun showSemesterDialog(selectedIndex: Int) + fun showSemesterDialog(selectedIndex: Int, semesters: List) fun setCurrentSemesterName(semester: Int, schoolYear: Int)