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:
parent
4aa6b0b995
commit
ea0fb00bde
@ -35,7 +35,8 @@ import java.time.LocalDate
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@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 {
|
MainView.TitledView {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ -118,7 +119,9 @@ class AttendanceFragment : BaseFragment<FragmentAttendanceBinding>(R.layout.frag
|
|||||||
with(binding) {
|
with(binding) {
|
||||||
attendanceSwipe.setOnRefreshListener(presenter::onSwipeRefresh)
|
attendanceSwipe.setOnRefreshListener(presenter::onSwipeRefresh)
|
||||||
attendanceSwipe.setColorSchemeColors(requireContext().getThemeAttrColor(R.attr.colorPrimary))
|
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() }
|
attendanceErrorRetry.setOnClickListener { presenter.onRetry() }
|
||||||
attendanceErrorDetails.setOnClickListener { presenter.onDetailsClick() }
|
attendanceErrorDetails.setOnClickListener { presenter.onDetailsClick() }
|
||||||
|
|
||||||
@ -208,7 +211,7 @@ class AttendanceFragment : BaseFragment<FragmentAttendanceBinding>(R.layout.frag
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun showNextButton(show: Boolean) {
|
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) {
|
override fun showExcuseButton(show: Boolean) {
|
||||||
@ -220,20 +223,19 @@ class AttendanceFragment : BaseFragment<FragmentAttendanceBinding>(R.layout.frag
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun showDatePickerDialog(currentDate: LocalDate) {
|
override fun showDatePickerDialog(currentDate: LocalDate) {
|
||||||
val now = LocalDate.now()
|
val baseDate = currentDate.schoolYearStart
|
||||||
val startOfSchoolYear = now.schoolYearStart.toTimestamp()
|
val rangeStart = baseDate.toTimestamp()
|
||||||
val endWeek = now.plusWeeks(1).toTimestamp()
|
val rangeEnd = LocalDate.now().plusWeeks(1).toTimestamp()
|
||||||
|
|
||||||
val constraintsBuilder = CalendarConstraints.Builder().apply {
|
val constraintsBuilder = CalendarConstraints.Builder().apply {
|
||||||
setValidator(SchoolDaysValidator(startOfSchoolYear, endWeek))
|
setValidator(SchoolDaysValidator(rangeStart, rangeEnd))
|
||||||
setStart(startOfSchoolYear)
|
setStart(rangeStart)
|
||||||
setEnd(endWeek)
|
setEnd(rangeEnd)
|
||||||
}
|
}
|
||||||
val datePicker =
|
val datePicker = MaterialDatePicker.Builder.datePicker()
|
||||||
MaterialDatePicker.Builder.datePicker()
|
.setCalendarConstraints(constraintsBuilder.build())
|
||||||
.setCalendarConstraints(constraintsBuilder.build())
|
.setSelection(currentDate.toTimestamp())
|
||||||
.setSelection(currentDate.toTimestamp())
|
.build()
|
||||||
.build()
|
|
||||||
|
|
||||||
datePicker.addOnPositiveButtonClickListener {
|
datePicker.addOnPositiveButtonClickListener {
|
||||||
val date = it.toLocalDateTime()
|
val date = it.toLocalDateTime()
|
||||||
|
@ -112,19 +112,19 @@ class LuckyNumberHistoryFragment :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun showDatePickerDialog(currentDate: LocalDate) {
|
override fun showDatePickerDialog(currentDate: LocalDate) {
|
||||||
val now = LocalDate.now()
|
val baseDate = currentDate.schoolYearStart
|
||||||
val startOfSchoolYear = now.schoolYearStart.toTimestamp()
|
val rangeStart = baseDate.toTimestamp()
|
||||||
|
val rangeEnd = LocalDate.now().plusWeeks(1).toTimestamp()
|
||||||
|
|
||||||
val constraintsBuilder = CalendarConstraints.Builder().apply {
|
val constraintsBuilder = CalendarConstraints.Builder().apply {
|
||||||
setValidator(SchoolDaysValidator(startOfSchoolYear, now.toTimestamp()))
|
setValidator(SchoolDaysValidator(rangeStart, rangeEnd))
|
||||||
setStart(startOfSchoolYear)
|
setStart(rangeStart)
|
||||||
setEnd(now.toTimestamp())
|
setEnd(rangeEnd)
|
||||||
}
|
}
|
||||||
val datePicker =
|
val datePicker = MaterialDatePicker.Builder.datePicker()
|
||||||
MaterialDatePicker.Builder.datePicker()
|
.setCalendarConstraints(constraintsBuilder.build())
|
||||||
.setCalendarConstraints(constraintsBuilder.build())
|
.setSelection(currentDate.toTimestamp())
|
||||||
.setSelection(currentDate.toTimestamp())
|
.build()
|
||||||
.build()
|
|
||||||
|
|
||||||
datePicker.addOnPositiveButtonClickListener {
|
datePicker.addOnPositiveButtonClickListener {
|
||||||
val date = it.toLocalDateTime()
|
val date = it.toLocalDateTime()
|
||||||
|
@ -2,18 +2,22 @@ package io.github.wulkanowy.ui.modules.luckynumber.history
|
|||||||
|
|
||||||
import io.github.wulkanowy.data.Status
|
import io.github.wulkanowy.data.Status
|
||||||
import io.github.wulkanowy.data.repositories.LuckyNumberRepository
|
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.data.repositories.StudentRepository
|
||||||
import io.github.wulkanowy.ui.base.BasePresenter
|
import io.github.wulkanowy.ui.base.BasePresenter
|
||||||
import io.github.wulkanowy.ui.base.ErrorHandler
|
import io.github.wulkanowy.ui.base.ErrorHandler
|
||||||
import io.github.wulkanowy.utils.AnalyticsHelper
|
import io.github.wulkanowy.utils.AnalyticsHelper
|
||||||
import io.github.wulkanowy.utils.afterLoading
|
import io.github.wulkanowy.utils.afterLoading
|
||||||
import io.github.wulkanowy.utils.flowWithResource
|
import io.github.wulkanowy.utils.flowWithResource
|
||||||
|
import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday
|
||||||
import io.github.wulkanowy.utils.isHolidays
|
import io.github.wulkanowy.utils.isHolidays
|
||||||
import io.github.wulkanowy.utils.monday
|
import io.github.wulkanowy.utils.monday
|
||||||
import io.github.wulkanowy.utils.previousOrSameSchoolDay
|
import io.github.wulkanowy.utils.previousOrSameSchoolDay
|
||||||
import io.github.wulkanowy.utils.sunday
|
import io.github.wulkanowy.utils.sunday
|
||||||
import io.github.wulkanowy.utils.toFormattedString
|
import io.github.wulkanowy.utils.toFormattedString
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
@ -22,6 +26,7 @@ import javax.inject.Inject
|
|||||||
class LuckyNumberHistoryPresenter @Inject constructor(
|
class LuckyNumberHistoryPresenter @Inject constructor(
|
||||||
errorHandler: ErrorHandler,
|
errorHandler: ErrorHandler,
|
||||||
studentRepository: StudentRepository,
|
studentRepository: StudentRepository,
|
||||||
|
private val semesterRepository: SemesterRepository,
|
||||||
private val luckyNumberRepository: LuckyNumberRepository,
|
private val luckyNumberRepository: LuckyNumberRepository,
|
||||||
private val analytics: AnalyticsHelper
|
private val analytics: AnalyticsHelper
|
||||||
) : BasePresenter<LuckyNumberHistoryView>(errorHandler, studentRepository) {
|
) : BasePresenter<LuckyNumberHistoryView>(errorHandler, studentRepository) {
|
||||||
@ -40,6 +45,19 @@ class LuckyNumberHistoryPresenter @Inject constructor(
|
|||||||
Timber.i("Lucky number history view was initialized")
|
Timber.i("Lucky number history view was initialized")
|
||||||
errorHandler.showErrorMessage = ::showErrorViewOnError
|
errorHandler.showErrorMessage = ::showErrorViewOnError
|
||||||
loadData()
|
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() {
|
private fun loadData() {
|
||||||
|
@ -185,20 +185,19 @@ class TimetableFragment : BaseFragment<FragmentTimetableBinding>(R.layout.fragme
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun showDatePickerDialog(currentDate: LocalDate) {
|
override fun showDatePickerDialog(currentDate: LocalDate) {
|
||||||
val now = LocalDate.now()
|
val baseDate = currentDate.schoolYearStart
|
||||||
val startOfSchoolYear = now.schoolYearStart.toTimestamp()
|
val rangeStart = baseDate.toTimestamp()
|
||||||
val endOfSchoolYear = now.schoolYearEnd.toTimestamp()
|
val rangeEnd = LocalDate.now().schoolYearEnd.toTimestamp()
|
||||||
|
|
||||||
val constraintsBuilder = CalendarConstraints.Builder().apply {
|
val constraintsBuilder = CalendarConstraints.Builder().apply {
|
||||||
setValidator(SchoolDaysValidator(startOfSchoolYear, endOfSchoolYear))
|
setValidator(SchoolDaysValidator(rangeStart, rangeEnd))
|
||||||
setStart(startOfSchoolYear)
|
setStart(rangeStart)
|
||||||
setEnd(endOfSchoolYear)
|
setEnd(rangeEnd)
|
||||||
}
|
}
|
||||||
val datePicker =
|
val datePicker = MaterialDatePicker.Builder.datePicker()
|
||||||
MaterialDatePicker.Builder.datePicker()
|
.setCalendarConstraints(constraintsBuilder.build())
|
||||||
.setCalendarConstraints(constraintsBuilder.build())
|
.setSelection(currentDate.toTimestamp())
|
||||||
.setSelection(currentDate.toTimestamp())
|
.build()
|
||||||
.build()
|
|
||||||
|
|
||||||
datePicker.addOnPositiveButtonClickListener {
|
datePicker.addOnPositiveButtonClickListener {
|
||||||
val date = it.toLocalDateTime()
|
val date = it.toLocalDateTime()
|
||||||
|
Loading…
Reference in New Issue
Block a user