aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-04-14 01:02:56 -0500
committerIván Ávalos <avalos@disroot.org>2022-04-14 01:02:56 -0500
commit796b46d0b3a426eb2ae19272ecf3a97e925565ff (patch)
tree9c5d7d5911a76b27067a446cd9d8ad72aa983376 /shared
parent20068c7e0d20740506ca268b34c2e520385af385 (diff)
parentbd758c32dadb69320a471b005c86b3d8ce393177 (diff)
downloadetbsa-trackermap-mobile-796b46d0b3a426eb2ae19272ecf3a97e925565ff.tar.gz
etbsa-trackermap-mobile-796b46d0b3a426eb2ae19272ecf3a97e925565ff.tar.bz2
etbsa-trackermap-mobile-796b46d0b3a426eb2ae19272ecf3a97e925565ff.zip
Merge branch 'main' of https://git.sr.ht/~avalos/trackermap-mobile
Diffstat (limited to 'shared')
-rw-r--r--shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt23
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt15
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt9
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Formatter.kt5
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/ReportDates.kt169
-rw-r--r--shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt22
-rw-r--r--shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/NSDataByteArray.kt40
7 files changed, 210 insertions, 73 deletions
diff --git a/shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt b/shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt
new file mode 100644
index 0000000..35f1ac6
--- /dev/null
+++ b/shared/src/androidMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt
@@ -0,0 +1,23 @@
+package mx.trackermap.TrackerMap.utils
+
+import android.os.Build
+import kotlinx.datetime.LocalDateTime
+import kotlinx.datetime.toJavaLocalDateTime
+import java.text.SimpleDateFormat
+import java.time.format.DateTimeFormatter
+import java.util.*
+
+actual class DateUtils {
+ actual companion object {
+ actual fun formatDate(date: LocalDateTime): String {
+ val javaDate = date.toJavaLocalDateTime()
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
+ return javaDate.format(formatter)
+ } else {
+ val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
+ return formatter.format(date)
+ }
+ }
+ }
+} \ No newline at end of file
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..bc0a48f 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/ReportController.kt
@@ -60,18 +60,17 @@ class ReportController(
return
}
- val (currentDate, previousDate) = ReportDates.getDates(reportPeriod)
+ val (from, to) = reportPeriod.getStringDates()
if (!xlsx) {
reportFlow.value = Report.LoadingReport
}
- // GlobalScope.launch {
- when (reportType) {
- ReportType.POSITIONS -> fetchPositions(deviceId, previousDate, currentDate, xlsx)
- ReportType.EVENTS -> fetchEvents(deviceId, previousDate, currentDate, eventTypes, xlsx)
- ReportType.STOPS -> fetchStops(deviceId, previousDate, currentDate, xlsx)
- }
- // }
+
+ when (reportType) {
+ ReportType.POSITIONS -> fetchPositions(deviceId, from, to, xlsx)
+ ReportType.EVENTS -> fetchEvents(deviceId, from, to, eventTypes, xlsx)
+ ReportType.STOPS -> fetchStops(deviceId, from, to, xlsx)
+ }
}
private suspend fun fetchPositions(
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt
new file mode 100644
index 0000000..a85ae69
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt
@@ -0,0 +1,9 @@
+package mx.trackermap.TrackerMap.utils
+
+import kotlinx.datetime.LocalDateTime
+
+expect class DateUtils {
+ companion object {
+ fun formatDate(date: LocalDateTime): String
+ }
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Formatter.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Formatter.kt
index 77d0d14..bc7e622 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Formatter.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Formatter.kt
@@ -23,9 +23,8 @@ class Formatter {
companion object {
fun formatDate(str: String): String {
val timezone = TimeZone.currentSystemDefault()
- val date = str.toInstant().toLocalDateTime(timezone).toString()
- return date
- .replace('T', ' ').trim('Z')
+ val datetime = str.toInstant().toLocalDateTime(timezone)
+ return DateUtils.formatDate(datetime)
}
fun formatSpeed(speed: Double, unit: SpeedUnit): String {
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..5298df3 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,120 @@ import kotlinx.datetime.*
@DelicateCoroutinesApi
class ReportDates {
- enum class ReportPeriod {
- TODAY, LAST_24, YESTERDAY, THIS_WEEK, LAST_7, THIS_MONTH, LAST_30
+ enum class PeriodTypes {
+ TODAY,
+ LAST_24,
+ YESTERDAY,
+ THIS_WEEK,
+ LAST_7,
+ THIS_MONTH,
+ LAST_30,
+ CUSTOM
}
- companion object {
- private fun formatDateTime(dateTime: LocalDateTime, timezone: TimeZone) =
+ 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 getObjectDates(): Pair<LocalDateTime, LocalDateTime>
+
+ fun getStringDates(): Pair<String, String> {
+ val (from, to) = getObjectDates()
+ return formatDateTime(from) to formatDateTime(to)
+ }
+
+ 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 getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val from = date.atTime(0, 0)
+ val to = dateTime
+ return from to to
+ }
+ }
+
+ class Last24 : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val from = instant
+ .minus(1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone)
+ val to = dateTime
+ return from to to
+ }
+ }
+
+ class Yesterday : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val yesterday = instant
+ .minus(1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone).date
+ val from = yesterday.atTime(0, 0)
+ val to = yesterday.atTime(23, 59)
+ return from to to
+ }
+ }
+
+ class ThisWeek : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val from = instant
+ .minus(date.dayOfWeek.isoDayNumber - 1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return from to to
+ }
+ }
+
+ class Last7 : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val from = instant
+ .minus(1, DateTimeUnit.WEEK, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return from to to
+ }
+ }
+
+ class ThisMonth : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val from = instant
+ .minus(date.dayOfMonth - 1, DateTimeUnit.DAY, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return from to to
+ }
+ }
+
+ class Last30 : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ val from = instant
+ .minus(1, DateTimeUnit.MONTH, timezone)
+ .toLocalDateTime(timezone).date
+ .atTime(0, 0)
+ val to = dateTime
+ return from to to
+ }
+ }
- 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 Custom (
+ private val from: LocalDateTime? = null,
+ private val to: LocalDateTime? = null
+ ) : ReportPeriod() {
+ override fun getObjectDates(): Pair<LocalDateTime, LocalDateTime> {
+ return Pair(
+ from ?: date.atTime(0, 0),
+ to ?: dateTime
+ )
}
- return Pair(
- formatDateTime(currentDate, timezone),
- formatDateTime(previousDate, timezone)
- )
+ fun withFrom (from: LocalDateTime): Custom = Custom (from, to)
+ fun withTo (to: LocalDateTime): Custom = Custom (from, to)
}
}
} \ No newline at end of file
diff --git a/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt b/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt
new file mode 100644
index 0000000..7286ea5
--- /dev/null
+++ b/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/DateUtils.kt
@@ -0,0 +1,22 @@
+package mx.trackermap.TrackerMap.utils
+
+import kotlinx.datetime.*
+import platform.Foundation.NSDate
+import platform.Foundation.NSDateFormatter
+
+actual class DateUtils {
+ actual companion object {
+ fun iosDateToKotlin(date: NSDate): LocalDateTime {
+ val timezone = TimeZone.currentSystemDefault()
+ return date.toKotlinInstant().toLocalDateTime(timezone)
+ }
+
+ actual fun formatDate(date: LocalDateTime): String {
+ val timezone = TimeZone.currentSystemDefault()
+ val iosDate = date.toInstant(timezone).toNSDate()
+ val formatter = NSDateFormatter()
+ formatter.setDateFormat("yyyy-MM-dd HH:mm")
+ return formatter.stringFromDate(iosDate)
+ }
+ }
+} \ No newline at end of file
diff --git a/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/NSDataByteArray.kt b/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/NSDataByteArray.kt
new file mode 100644
index 0000000..807c63f
--- /dev/null
+++ b/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/utils/NSDataByteArray.kt
@@ -0,0 +1,40 @@
+/**
+ * TrackerMap
+ * Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package mx.trackermap.TrackerMap.utils
+
+import kotlinx.cinterop.addressOf
+import kotlinx.cinterop.allocArrayOf
+import kotlinx.cinterop.memScoped
+import kotlinx.cinterop.usePinned
+import platform.Foundation.NSData
+import platform.Foundation.create
+import platform.posix.memcpy
+
+// Source: https://gist.github.com/noahsark769/61cfb7a8b7231e2069a9dab94cf74a62
+
+fun ByteArray.toData(): NSData = memScoped {
+ NSData.create(
+ bytes = allocArrayOf(this@toData),
+ length = this@toData.size.toULong())
+}
+
+fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt()).apply {
+ usePinned {
+ memcpy(it.addressOf(0), this@toByteArray.bytes, this@toByteArray.length)
+ }
+} \ No newline at end of file