aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-04-09 00:02:03 -0500
committerIván Ávalos <avalos@disroot.org>2022-04-09 00:02:03 -0500
commita6a95167dbfe48fa0c5d6b61219309d346142979 (patch)
treed515bfc478abafb632c2be8cd19719525b949a89
parent83340f9df0210fa725bf3875d4f7298ab2e7a76b (diff)
downloadetbsa-trackermap-mobile-a6a95167dbfe48fa0c5d6b61219309d346142979.tar.gz
etbsa-trackermap-mobile-a6a95167dbfe48fa0c5d6b61219309d346142979.tar.bz2
etbsa-trackermap-mobile-a6a95167dbfe48fa0c5d6b61219309d346142979.zip
- [shared] Rewrote ReportDates to allow for custom date period.
- [android] Rewrote report handling to use new ReportPeriod. - [ios] Not rewritten yet, it won't build!
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt2
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt34
-rw-r--r--androidApp/src/main/res/menu/report_period_options.xml4
-rw-r--r--androidApp/src/main/res/values-es-rMX/strings.xml1
-rw-r--r--androidApp/src/main/res/values/strings.xml1
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt153
7 files changed, 116 insertions, 81 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt
index e731587..fb0a0bc 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt
@@ -115,7 +115,7 @@ class UnitInformationFragment : Fragment() {
}
private fun displayInformation(unit: UnitInformation) {
- val context = context!!
+ val context = requireContext()
val details: MutableList<Pair<String, String>> = mutableListOf()
unit.device.contact?.let { contact ->
details.add(getString(R.string.unit_info_contact) to contact)
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 60ad531..091cb7b 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
@@ -137,7 +137,7 @@ class UnitReportsFragment : Fragment() {
exportAction = UnitReportsViewModel.ExportAction.ACTION_SHARE
unitReportsViewModel.fetchReportXlsx()
}
- unitReportsViewModel.setReportPeriod(ReportDates.ReportPeriod.TODAY)
+ unitReportsViewModel.setReportPeriod(ReportDates.ReportPeriod.Today())
unitReportsViewModel.setReportType(ReportController.ReportType.POSITIONS)
}
@@ -181,13 +181,14 @@ class UnitReportsFragment : Fragment() {
binding.periodButton.text = context?.getString(
when (period) {
- ReportDates.ReportPeriod.TODAY -> R.string.period_today
- ReportDates.ReportPeriod.LAST_24 -> R.string.period_last_24
- ReportDates.ReportPeriod.YESTERDAY -> R.string.period_yesterday
- ReportDates.ReportPeriod.THIS_WEEK -> R.string.period_this_week
- ReportDates.ReportPeriod.LAST_7 -> R.string.period_last_7
- ReportDates.ReportPeriod.THIS_MONTH -> R.string.period_this_month
- ReportDates.ReportPeriod.LAST_30 -> R.string.period_last_30
+ is ReportDates.ReportPeriod.Today -> R.string.period_today
+ is ReportDates.ReportPeriod.Last24 -> R.string.period_last_24
+ is ReportDates.ReportPeriod.Yesterday -> R.string.period_yesterday
+ is ReportDates.ReportPeriod.ThisWeek -> R.string.period_this_week
+ is ReportDates.ReportPeriod.Last7 -> R.string.period_last_7
+ is ReportDates.ReportPeriod.ThisMonth -> R.string.period_this_month
+ is ReportDates.ReportPeriod.Last30 -> R.string.period_last_30
+ is ReportDates.ReportPeriod.Custom -> R.string.period_custom
}
)
}
@@ -210,14 +211,15 @@ class UnitReportsFragment : Fragment() {
popOver.setOnMenuItemClickListener { item ->
unitReportsViewModel.setReportPeriod(
when (item.itemId) {
- R.id.optionToday -> ReportDates.ReportPeriod.TODAY
- R.id.optionLast24 -> ReportDates.ReportPeriod.LAST_24
- R.id.optionYesterday -> ReportDates.ReportPeriod.YESTERDAY
- R.id.optionWeek -> ReportDates.ReportPeriod.THIS_WEEK
- R.id.optionLast7 -> ReportDates.ReportPeriod.LAST_7
- R.id.optionMonth -> ReportDates.ReportPeriod.THIS_MONTH
- R.id.optionLast30 -> ReportDates.ReportPeriod.LAST_30
- else -> ReportDates.ReportPeriod.TODAY
+ R.id.optionToday -> ReportDates.ReportPeriod.Today()
+ R.id.optionLast24 -> ReportDates.ReportPeriod.Last24()
+ R.id.optionYesterday -> ReportDates.ReportPeriod.Yesterday()
+ R.id.optionWeek -> ReportDates.ReportPeriod.ThisWeek()
+ R.id.optionLast7 -> ReportDates.ReportPeriod.Last7()
+ R.id.optionMonth -> ReportDates.ReportPeriod.ThisMonth()
+ R.id.optionLast30 -> ReportDates.ReportPeriod.Last30()
+ R.id.optionCustom -> ReportDates.ReportPeriod.Custom()
+ else -> ReportDates.ReportPeriod.Today()
}
)
true
diff --git a/androidApp/src/main/res/menu/report_period_options.xml b/androidApp/src/main/res/menu/report_period_options.xml
index 60fb58c..a8fdbad 100644
--- a/androidApp/src/main/res/menu/report_period_options.xml
+++ b/androidApp/src/main/res/menu/report_period_options.xml
@@ -30,4 +30,8 @@
android:id="@+id/optionLast30"
android:title="@string/period_last_30" />
+ <item
+ android:id="@+id/optionCustom"
+ android:title="@string/period_custom" />
+
</menu> \ No newline at end of file
diff --git a/androidApp/src/main/res/values-es-rMX/strings.xml b/androidApp/src/main/res/values-es-rMX/strings.xml
index 8792b08..d07b18d 100644
--- a/androidApp/src/main/res/values-es-rMX/strings.xml
+++ b/androidApp/src/main/res/values-es-rMX/strings.xml
@@ -112,6 +112,7 @@
<string name="period_last_7">Últimos 7d</string>
<string name="period_this_month">Mes</string>
<string name="period_last_30">Últimos 30d</string>
+ <string name="period_custom">Personalizado</string>
<string name="table_event">Evento</string>
<string name="table_datetime">Fecha y hora</string>
diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml
index a6cba19..ce07188 100644
--- a/androidApp/src/main/res/values/strings.xml
+++ b/androidApp/src/main/res/values/strings.xml
@@ -126,6 +126,7 @@
<string name="period_last_7">Last 7d</string>
<string name="period_this_month">Month</string>
<string name="period_last_30">Last 30d</string>
+ <string name="period_custom">Custom</string>
<string name="table_event">Event</string>
<string name="table_datetime">Datetime</string>
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt
index 29c4229..e120e97 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt
@@ -60,7 +60,7 @@ class ReportController(
return
}
- val (currentDate, previousDate) = ReportDates.getDates(reportPeriod)
+ val (currentDate, previousDate) = reportPeriod.getDates()
if (!xlsx) {
reportFlow.value = Report.LoadingReport
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt
index 5672536..64df79d 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt
@@ -22,75 +22,102 @@ import kotlinx.datetime.*
@DelicateCoroutinesApi
class ReportDates {
- enum class ReportPeriod {
- TODAY, LAST_24, YESTERDAY, THIS_WEEK, LAST_7, THIS_MONTH, LAST_30
- }
+ sealed class ReportPeriod {
+ val timezone = TimeZone.currentSystemDefault()
+ val clock = Clock.System
+ val instant = clock.now()
+ val dateTime = instant.toLocalDateTime(timezone)
+ val date = dateTime.date
+
+ abstract fun getDates(): Pair<String, String>
- companion object {
- private fun formatDateTime(dateTime: LocalDateTime, timezone: TimeZone) =
+ fun formatDateTime(dateTime: LocalDateTime) =
dateTime.toInstant(timezone).toString()
- fun getDates(period: ReportPeriod): Pair<String, String> {
- val timezone = TimeZone.currentSystemDefault()
- val clock = Clock.System
- val instant = clock.now()
- val dateTime = instant.toLocalDateTime(timezone)
- val date = dateTime.date
+ class Today : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val from = date.atTime(0, 0)
+ val to = dateTime
+ return formatDateTime(to) to formatDateTime(from)
+ }
+ }
- var currentDate: LocalDateTime? = null
- var previousDate: LocalDateTime? = null
- when (period) {
- ReportPeriod.TODAY -> {
- previousDate = date.atTime(0, 0)
- currentDate = dateTime
- }
- ReportPeriod.LAST_24 -> {
- previousDate = instant
- .minus(1, DateTimeUnit.DAY, timezone)
- .toLocalDateTime(timezone)
- currentDate = dateTime
- }
- ReportPeriod.YESTERDAY -> {
- val yesterday = instant
- .minus(1, DateTimeUnit.DAY, timezone)
- .toLocalDateTime(timezone).date
- previousDate = yesterday.atTime(0, 0)
- currentDate = yesterday.atTime(23, 59)
- }
- ReportPeriod.THIS_WEEK -> {
- previousDate = instant
- .minus(date.dayOfWeek.isoDayNumber - 1, DateTimeUnit.DAY, timezone)
- .toLocalDateTime(timezone).date
- .atTime(0, 0)
- currentDate = dateTime
- }
- ReportPeriod.LAST_7 -> {
- previousDate = instant
- .minus(1, DateTimeUnit.WEEK, timezone)
- .toLocalDateTime(timezone).date
- .atTime(0, 0)
- currentDate = dateTime
- }
- ReportPeriod.THIS_MONTH -> {
- previousDate = instant
- .minus(date.dayOfMonth - 1, DateTimeUnit.DAY, timezone)
- .toLocalDateTime(timezone).date
- .atTime(0, 0)
- currentDate = dateTime
- }
- ReportPeriod.LAST_30 -> {
- previousDate = instant
- .minus(1, DateTimeUnit.MONTH, timezone)
- .toLocalDateTime(timezone).date
- .atTime(0, 0)
- currentDate = dateTime
- }
+ class Last24 : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val from = instant
+ .minus(1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone)
+ val to = dateTime
+ return formatDateTime(to) to formatDateTime(from)
}
+ }
+
+ class Yesterday : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val yesterday = instant
+ .minus(1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone).date
+ val from = yesterday.atTime(0, 0)
+ val to = yesterday.atTime(23, 59)
+ return formatDateTime(to) to formatDateTime(from)
+ }
+ }
+
+ class ThisWeek : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val from = instant
+ .minus(date.dayOfWeek.isoDayNumber - 1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return formatDateTime(to) to formatDateTime(from)
+ }
+ }
- return Pair(
- formatDateTime(currentDate, timezone),
- formatDateTime(previousDate, timezone)
- )
+ class Last7 : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val from = instant
+ .minus(1, DateTimeUnit.WEEK, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return formatDateTime(to) to formatDateTime(from)
+ }
+ }
+
+ class ThisMonth : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val from = instant
+ .minus(date.dayOfMonth - 1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return formatDateTime(to) to formatDateTime(from)
+ }
+ }
+
+ class Last30 : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ val from = instant
+ .minus(1, DateTimeUnit.MONTH, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return formatDateTime(to) to formatDateTime(from)
+ }
+ }
+
+ class Custom (
+ private val from: LocalDateTime? = null,
+ private val to: LocalDateTime? = null
+ ) : ReportPeriod() {
+ override fun getDates(): Pair<String, String> {
+ return formatDateTime(
+ to ?: dateTime
+ ) to formatDateTime(
+ from ?: date.atTime(0, 0)
+ )
+ }
}
}
} \ No newline at end of file