aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
diff options
context:
space:
mode:
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt')
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt22
1 files changed, 20 insertions, 2 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
index 6cc36bd..aab3cfa 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
@@ -38,7 +38,8 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.card.MaterialCardView
import io.ktor.utils.io.*
import kotlinx.coroutines.DelicateCoroutinesApi
-import kotlinx.datetime.LocalDateTime
+import kotlinx.datetime.*
+import kotlinx.datetime.TimeZone
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.UnitDetailsReportsBinding
import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter
@@ -53,7 +54,9 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
import java.io.*
import java.util.*
import kotlin.math.max
+import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
+import kotlin.time.toDuration
@DelicateCoroutinesApi
@ExperimentalTime
@@ -271,7 +274,22 @@ class UnitReportsFragment : Fragment() {
p1, p2
)
(period as? ReportDates.ReportPeriod.Custom)?.let {
- unitReportsViewModel.setReportPeriod(it.withFrom(fromPickedDatetime))
+ val fromInstant = fromPickedDatetime.toInstant(TimeZone.currentSystemDefault())
+ val toInstant = Calendar.getInstance().toInstant().toKotlinInstant()
+ val plusMonth = fromInstant.plus(31.toDuration(DurationUnit.DAYS))
+
+ /* Make ultra sure that user can't select a period longer than a month */
+ unitReportsViewModel.setReportPeriod(
+ if (fromInstant.daysUntil(toInstant, TimeZone.currentSystemDefault()) > 31) {
+ it.withFrom(fromPickedDatetime).withTo(
+ plusMonth.toLocalDateTime(TimeZone.currentSystemDefault()))
+ } else {
+ it.withFrom(fromPickedDatetime)
+ }
+ )
+
+ toDatePicker.datePicker.minDate = fromInstant.toEpochMilliseconds()
+ toDatePicker.datePicker.maxDate = plusMonth.toEpochMilliseconds()
}
}, from.hour, from.minute, false)