aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils
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 /shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils
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!
Diffstat (limited to 'shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt153
1 files changed, 90 insertions, 63 deletions
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