From ea0fb00bdebaa48e9c7ac6dfa01ee39bc67e6bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 29 Aug 2021 19:31:28 +0200 Subject: [PATCH] Fix crash on opening date pickers during holidays (#1456) --- .../modules/attendance/AttendanceFragment.kt | 30 ++++++++++--------- .../history/LuckyNumberHistoryFragment.kt | 20 ++++++------- .../history/LuckyNumberHistoryPresenter.kt | 18 +++++++++++ .../ui/modules/timetable/TimetableFragment.kt | 21 +++++++------ 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt index 10cab3df0..7ae1e0586 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt @@ -35,7 +35,8 @@ import java.time.LocalDate import javax.inject.Inject @AndroidEntryPoint -class AttendanceFragment : BaseFragment(R.layout.fragment_attendance), AttendanceView, MainView.MainChildView, +class AttendanceFragment : BaseFragment(R.layout.fragment_attendance), + AttendanceView, MainView.MainChildView, MainView.TitledView { @Inject @@ -118,7 +119,9 @@ class AttendanceFragment : BaseFragment(R.layout.frag with(binding) { attendanceSwipe.setOnRefreshListener(presenter::onSwipeRefresh) attendanceSwipe.setColorSchemeColors(requireContext().getThemeAttrColor(R.attr.colorPrimary)) - attendanceSwipe.setProgressBackgroundColorSchemeColor(requireContext().getThemeAttrColor(R.attr.colorSwipeRefresh)) + attendanceSwipe.setProgressBackgroundColorSchemeColor( + requireContext().getThemeAttrColor(R.attr.colorSwipeRefresh) + ) attendanceErrorRetry.setOnClickListener { presenter.onRetry() } attendanceErrorDetails.setOnClickListener { presenter.onDetailsClick() } @@ -208,7 +211,7 @@ class AttendanceFragment : BaseFragment(R.layout.frag } override fun showNextButton(show: Boolean) { - binding. attendanceNextButton.visibility = if (show) VISIBLE else INVISIBLE + binding.attendanceNextButton.visibility = if (show) VISIBLE else INVISIBLE } override fun showExcuseButton(show: Boolean) { @@ -220,20 +223,19 @@ class AttendanceFragment : BaseFragment(R.layout.frag } override fun showDatePickerDialog(currentDate: LocalDate) { - val now = LocalDate.now() - val startOfSchoolYear = now.schoolYearStart.toTimestamp() - val endWeek = now.plusWeeks(1).toTimestamp() + val baseDate = currentDate.schoolYearStart + val rangeStart = baseDate.toTimestamp() + val rangeEnd = LocalDate.now().plusWeeks(1).toTimestamp() val constraintsBuilder = CalendarConstraints.Builder().apply { - setValidator(SchoolDaysValidator(startOfSchoolYear, endWeek)) - setStart(startOfSchoolYear) - setEnd(endWeek) + setValidator(SchoolDaysValidator(rangeStart, rangeEnd)) + setStart(rangeStart) + setEnd(rangeEnd) } - val datePicker = - MaterialDatePicker.Builder.datePicker() - .setCalendarConstraints(constraintsBuilder.build()) - .setSelection(currentDate.toTimestamp()) - .build() + val datePicker = MaterialDatePicker.Builder.datePicker() + .setCalendarConstraints(constraintsBuilder.build()) + .setSelection(currentDate.toTimestamp()) + .build() datePicker.addOnPositiveButtonClickListener { val date = it.toLocalDateTime() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryFragment.kt index 4981aad25..dc141f8d3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryFragment.kt @@ -112,19 +112,19 @@ class LuckyNumberHistoryFragment : } override fun showDatePickerDialog(currentDate: LocalDate) { - val now = LocalDate.now() - val startOfSchoolYear = now.schoolYearStart.toTimestamp() + val baseDate = currentDate.schoolYearStart + val rangeStart = baseDate.toTimestamp() + val rangeEnd = LocalDate.now().plusWeeks(1).toTimestamp() val constraintsBuilder = CalendarConstraints.Builder().apply { - setValidator(SchoolDaysValidator(startOfSchoolYear, now.toTimestamp())) - setStart(startOfSchoolYear) - setEnd(now.toTimestamp()) + setValidator(SchoolDaysValidator(rangeStart, rangeEnd)) + setStart(rangeStart) + setEnd(rangeEnd) } - val datePicker = - MaterialDatePicker.Builder.datePicker() - .setCalendarConstraints(constraintsBuilder.build()) - .setSelection(currentDate.toTimestamp()) - .build() + val datePicker = MaterialDatePicker.Builder.datePicker() + .setCalendarConstraints(constraintsBuilder.build()) + .setSelection(currentDate.toTimestamp()) + .build() datePicker.addOnPositiveButtonClickListener { val date = it.toLocalDateTime() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryPresenter.kt index 556dda759..c45cb69a7 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/history/LuckyNumberHistoryPresenter.kt @@ -2,18 +2,22 @@ package io.github.wulkanowy.ui.modules.luckynumber.history import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.LuckyNumberRepository +import io.github.wulkanowy.data.repositories.SemesterRepository 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.utils.AnalyticsHelper import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource +import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.previousOrSameSchoolDay import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onEach import timber.log.Timber import java.time.LocalDate @@ -22,6 +26,7 @@ import javax.inject.Inject class LuckyNumberHistoryPresenter @Inject constructor( errorHandler: ErrorHandler, studentRepository: StudentRepository, + private val semesterRepository: SemesterRepository, private val luckyNumberRepository: LuckyNumberRepository, private val analytics: AnalyticsHelper ) : BasePresenter(errorHandler, studentRepository) { @@ -40,6 +45,19 @@ class LuckyNumberHistoryPresenter @Inject constructor( Timber.i("Lucky number history view was initialized") errorHandler.showErrorMessage = ::showErrorViewOnError loadData() + if (currentDate.isHolidays) setBaseDateOnHolidays() + } + + private fun setBaseDateOnHolidays() { + flow { + val student = studentRepository.getCurrentStudent() + emit(semesterRepository.getCurrentSemester(student)) + }.catch { + Timber.i("Loading semester result: An exception occurred") + }.onEach { + currentDate = currentDate.getLastSchoolDayIfHoliday(it.schoolYear) + reloadNavigation() + }.launch("holidays") } private fun loadData() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt index a374e166c..a65d69211 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt @@ -185,20 +185,19 @@ class TimetableFragment : BaseFragment(R.layout.fragme } override fun showDatePickerDialog(currentDate: LocalDate) { - val now = LocalDate.now() - val startOfSchoolYear = now.schoolYearStart.toTimestamp() - val endOfSchoolYear = now.schoolYearEnd.toTimestamp() + val baseDate = currentDate.schoolYearStart + val rangeStart = baseDate.toTimestamp() + val rangeEnd = LocalDate.now().schoolYearEnd.toTimestamp() val constraintsBuilder = CalendarConstraints.Builder().apply { - setValidator(SchoolDaysValidator(startOfSchoolYear, endOfSchoolYear)) - setStart(startOfSchoolYear) - setEnd(endOfSchoolYear) + setValidator(SchoolDaysValidator(rangeStart, rangeEnd)) + setStart(rangeStart) + setEnd(rangeEnd) } - val datePicker = - MaterialDatePicker.Builder.datePicker() - .setCalendarConstraints(constraintsBuilder.build()) - .setSelection(currentDate.toTimestamp()) - .build() + val datePicker = MaterialDatePicker.Builder.datePicker() + .setCalendarConstraints(constraintsBuilder.build()) + .setSelection(currentDate.toTimestamp()) + .build() datePicker.addOnPositiveButtonClickListener { val date = it.toLocalDateTime()