1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-11-23 17:56:33 -06:00

Fix crash on opening date pickers during holidays (#1456)

This commit is contained in:
Mikołaj Pich 2021-08-29 19:31:28 +02:00 committed by GitHub
parent 4aa6b0b995
commit ea0fb00bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 35 deletions

View File

@ -35,7 +35,8 @@ import java.time.LocalDate
import javax.inject.Inject
@AndroidEntryPoint
class AttendanceFragment : BaseFragment<FragmentAttendanceBinding>(R.layout.fragment_attendance), AttendanceView, MainView.MainChildView,
class AttendanceFragment : BaseFragment<FragmentAttendanceBinding>(R.layout.fragment_attendance),
AttendanceView, MainView.MainChildView,
MainView.TitledView {
@Inject
@ -118,7 +119,9 @@ class AttendanceFragment : BaseFragment<FragmentAttendanceBinding>(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<FragmentAttendanceBinding>(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<FragmentAttendanceBinding>(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()

View File

@ -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()

View File

@ -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<LuckyNumberHistoryView>(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() {

View File

@ -185,20 +185,19 @@ class TimetableFragment : BaseFragment<FragmentTimetableBinding>(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()