diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-04-14 01:02:56 -0500 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-04-14 01:02:56 -0500 |
commit | 796b46d0b3a426eb2ae19272ecf3a97e925565ff (patch) | |
tree | 9c5d7d5911a76b27067a446cd9d8ad72aa983376 | |
parent | 20068c7e0d20740506ca268b34c2e520385af385 (diff) | |
parent | bd758c32dadb69320a471b005c86b3d8ce393177 (diff) | |
download | etbsa-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
29 files changed, 1040 insertions, 331 deletions
diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 0ae667e..a6f61bc 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -14,8 +14,8 @@ android { versionCode = 1300 versionName = "1.2" ndk { - abiFilters.clear() - abiFilters += listOf("armeabi-v7a", "arm64-v8a") + // abiFilters.clear() + // abiFilters += listOf("armeabi-v7a", "arm64-v8a") } } buildTypes { 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..60222ff 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 @@ -18,6 +18,8 @@ package mx.trackermap.TrackerMap.android.details.reports import android.app.Activity +import android.app.DatePickerDialog +import android.app.TimePickerDialog import android.content.Intent import android.os.Build import android.os.Bundle @@ -27,14 +29,16 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.PopupMenu -import android.widget.TableRow -import android.widget.TextView +import android.widget.* import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.setMargins import androidx.fragment.app.Fragment import androidx.fragment.app.commit +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 mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitDetailsReportsBinding import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter @@ -47,6 +51,7 @@ import mx.trackermap.TrackerMap.utils.Formatter import mx.trackermap.TrackerMap.utils.ReportDates import org.koin.androidx.viewmodel.ext.android.viewModel import java.io.* +import java.util.* import kotlin.math.max import kotlin.time.ExperimentalTime @@ -59,12 +64,22 @@ class UnitReportsFragment : Fragment() { private val unitReportsViewModel: UnitReportsViewModel by viewModel() private lateinit var mapFragment: MapWrapperFragment + private lateinit var bottomSheetBehavior: BottomSheetBehavior<MaterialCardView> private var reportFile: ReportController.Report.XlsxReport? = null private var exportAction: UnitReportsViewModel.ExportAction? = null + private lateinit var fromPickedDatetime: LocalDateTime + private lateinit var fromDatePicker: DatePickerDialog + private lateinit var fromTimePicker: TimePickerDialog + + private lateinit var toPickedDatetime: LocalDateTime + private lateinit var toDatePicker: DatePickerDialog + private lateinit var toTimePicker: TimePickerDialog + private companion object { - const val XLSX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + const val XLSX_MIME_TYPE = + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } override fun onCreateView( @@ -80,7 +95,12 @@ class UnitReportsFragment : Fragment() { super.onViewCreated(view, savedInstanceState) unitReportsViewModel.setDeviceId( - arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) ?: 0) + arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) ?: 0 + ) + + bottomSheetBehavior = BottomSheetBehavior.from(binding.bottomSheet) + bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED + setupEvents() initializeMap() } @@ -129,6 +149,12 @@ class UnitReportsFragment : Fragment() { binding.periodButton.setOnClickListener { showPeriodPopUp(it) } + binding.fromEditText.setOnClickListener { + fromDatePicker.show() + } + binding.toEditText.setOnClickListener { + toDatePicker.show() + } binding.exportButton.setOnClickListener { exportAction = UnitReportsViewModel.ExportAction.ACTION_SAVE unitReportsViewModel.fetchReportXlsx() @@ -137,7 +163,27 @@ class UnitReportsFragment : Fragment() { exportAction = UnitReportsViewModel.ExportAction.ACTION_SHARE unitReportsViewModel.fetchReportXlsx() } - unitReportsViewModel.setReportPeriod(ReportDates.ReportPeriod.TODAY) + binding.toggleSheet.setOnClickListener { + if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED) { + bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED + } else { + bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED + } + } + bottomSheetBehavior.addBottomSheetCallback(object: BottomSheetBehavior.BottomSheetCallback() { + override fun onStateChanged(bottomSheet: View, newState: Int) { + if (newState == BottomSheetBehavior.STATE_EXPANDED) { + binding.toggleSheet.setImageResource(R.drawable.icon_down) + binding.toggleSheet.contentDescription = getString(R.string.shared_slide_down) + } else { + binding.toggleSheet.setImageResource(R.drawable.icon_up) + binding.toggleSheet.contentDescription = getString(R.string.shared_slide_up) + } + } + + override fun onSlide(bottomSheet: View, slideOffset: Float) {} + }) + unitReportsViewModel.setReportPeriod(ReportDates.ReportPeriod.Today()) unitReportsViewModel.setReportType(ReportController.ReportType.POSITIONS) } @@ -149,7 +195,11 @@ class UnitReportsFragment : Fragment() { when (report) { is ReportController.Report.PositionsReport -> { mapFragment.display(unitReportsViewModel.geofences.value!!) - mapFragment.display(report.positions.toTypedArray(), isReport = true, center = true) + mapFragment.display( + report.positions.toTypedArray(), + isReport = true, + center = true + ) showMap(true) } is ReportController.Report.EventsReport -> { @@ -181,15 +231,68 @@ 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 } ) + + binding.rangeLayout.visibility = + if (period is ReportDates.ReportPeriod.Custom) View.VISIBLE else View.GONE + + (period as? ReportDates.ReportPeriod.Custom)?.let { + val (from, to) = it.getStringDates() + binding.fromEditText.setText(Formatter.formatDate(from)) + binding.toEditText.setText(Formatter.formatDate(to)) + } + + /** + * Initialize date and time pickers + */ + + val (from, to) = period.getObjectDates() + + fromDatePicker = DatePickerDialog(requireContext(), { p0, p1, p2, p3 -> + fromPickedDatetime = LocalDateTime(p1, p2, p3, from.hour, from.minute) + fromTimePicker.show() + }, from.year, from.monthNumber, from.dayOfMonth) + fromDatePicker.datePicker.maxDate = Calendar.getInstance().timeInMillis + + fromTimePicker = TimePickerDialog(requireContext(), { p0, p1, p2 -> + fromPickedDatetime = LocalDateTime( + fromPickedDatetime.year, + fromPickedDatetime.monthNumber, + fromPickedDatetime.dayOfMonth, + p1, p2 + ) + (period as? ReportDates.ReportPeriod.Custom)?.let { + unitReportsViewModel.setReportPeriod(it.withFrom(fromPickedDatetime)) + } + }, from.hour, from.minute, false) + + toDatePicker = DatePickerDialog(requireContext(), { p0, p1, p2, p3 -> + toPickedDatetime = LocalDateTime(p1, p2, p3, from.hour, from.minute) + toTimePicker.show() + }, to.year, to.monthNumber, to.dayOfMonth) + toDatePicker.datePicker.maxDate = Calendar.getInstance().timeInMillis + + toTimePicker = TimePickerDialog(requireContext(), { p0, p1, p2 -> + toPickedDatetime = LocalDateTime( + toPickedDatetime.year, + toPickedDatetime.monthNumber, + toPickedDatetime.dayOfMonth, + p1, p2 + ) + (period as? ReportDates.ReportPeriod.Custom)?.let { + unitReportsViewModel.setReportPeriod(it.withTo(toPickedDatetime)) + } + }, to.hour, to.minute, false) + } unitReportsViewModel.geofences.observe(viewLifecycleOwner) { geofences -> @@ -210,14 +313,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 @@ -278,9 +382,10 @@ class UnitReportsFragment : Fragment() { EventInformation.Type.MAINTENANCE -> R.string.event_maintenance EventInformation.Type.TEXT_MESSAGE -> R.string.event_text_message EventInformation.Type.DRIVER_CHANGED -> R.string.event_driver_changed - EventInformation.Type.UNKNOWN -> R.string.event_unknown + EventInformation.Type.UNKNOWN -> R.string.event_unknown else -> R.string.event_unknown - }) + } + ) } event.geofence?.let { geofenceText.text = it.name diff --git a/androidApp/src/main/res/drawable/icon_down.xml b/androidApp/src/main/res/drawable/icon_down.xml new file mode 100644 index 0000000..884bee1 --- /dev/null +++ b/androidApp/src/main/res/drawable/icon_down.xml @@ -0,0 +1,10 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24" + android:tint="?attr/colorControlNormal"> + <path + android:fillColor="@android:color/white" + android:pathData="M7.41,8.59L12,13.17l4.59,-4.58L18,10l-6,6 -6,-6 1.41,-1.41z"/> +</vector> diff --git a/androidApp/src/main/res/drawable/icon_up.xml b/androidApp/src/main/res/drawable/icon_up.xml new file mode 100644 index 0000000..9b15755 --- /dev/null +++ b/androidApp/src/main/res/drawable/icon_up.xml @@ -0,0 +1,10 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24" + android:tint="?attr/colorControlNormal"> + <path + android:fillColor="@android:color/white" + android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/> +</vector> diff --git a/androidApp/src/main/res/layout/unit_details_reports.xml b/androidApp/src/main/res/layout/unit_details_reports.xml index 903c2c3..c29e4b0 100644 --- a/androidApp/src/main/res/layout/unit_details_reports.xml +++ b/androidApp/src/main/res/layout/unit_details_reports.xml @@ -1,194 +1,297 @@ <?xml version="1.0" encoding="utf-8"?> -<androidx.constraintlayout.widget.ConstraintLayout +<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" - android:layout_height="match_parent"> - - <androidx.fragment.app.FragmentContainerView - android:id="@+id/reportsMapContainer" - android:layout_width="0dp" - android:layout_height="0dp" - android:layout_marginBottom="@dimen/margin" - app:layout_constraintBottom_toTopOf="@id/periodSection" - app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" - android:visibility="gone"/> - - <ScrollView - android:id="@+id/eventsScroll" - android:layout_width="0dp" - android:layout_height="0dp" - android:layout_marginBottom="@dimen/margin" - android:visibility="gone" - app:layout_constraintBottom_toTopOf="@id/periodSection" - app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent"> - - <HorizontalScrollView - android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="match_parent" + xmlns:tools="http://schemas.android.com/tools"> - <TableLayout - android:id="@+id/eventsTable" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:isScrollContainer="true" - android:scrollbars="vertical" - android:stretchColumns="*"> + <!-- Main content --> + <androidx.constraintlayout.widget.ConstraintLayout + android:layout_width="match_parent" + android:layout_height="match_parent"> - <TableRow android:background="@color/colorPrimary"> + <androidx.fragment.app.FragmentContainerView + android:id="@+id/reportsMapContainer" + android:layout_width="0dp" + android:layout_height="0dp" + android:layout_marginBottom="@dimen/margin" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + android:visibility="gone"/> + + <ScrollView + android:id="@+id/eventsScroll" + android:layout_width="0dp" + android:layout_height="0dp" + android:layout_marginBottom="@dimen/bottom_sheet_peak" + android:visibility="visible" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + <HorizontalScrollView + android:layout_width="match_parent" + android:layout_height="wrap_content"> - <TextView - android:paddingHorizontal="@dimen/padding" - android:text="@string/table_datetime" - android:textColor="@color/background" - android:padding="@dimen/padding" /> + <TableLayout + android:id="@+id/eventsTable" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:isScrollContainer="true" + android:scrollbars="vertical" + android:stretchColumns="*"> - <TextView - android:paddingHorizontal="@dimen/padding" - android:text="@string/table_event" - android:textColor="@color/background" - android:padding="@dimen/padding" /> + <TableRow android:background="@color/colorPrimary"> - <TextView - android:paddingHorizontal="@dimen/padding" - android:text="@string/table_geofence" - android:textColor="@color/background" - android:padding="@dimen/padding" /> + <TextView + android:paddingHorizontal="@dimen/padding" + android:text="@string/table_datetime" + android:textColor="@color/background" + android:padding="@dimen/padding" /> - <TextView - android:paddingHorizontal="@dimen/padding" - android:text="@string/table_address" - android:textColor="@color/background" - android:padding="@dimen/padding" /> + <TextView + android:paddingHorizontal="@dimen/padding" + android:text="@string/table_event" + android:textColor="@color/background" + android:padding="@dimen/padding" /> - </TableRow> + <TextView + android:paddingHorizontal="@dimen/padding" + android:text="@string/table_geofence" + android:textColor="@color/background" + android:padding="@dimen/padding" /> - </TableLayout> + <TextView + android:paddingHorizontal="@dimen/padding" + android:text="@string/table_address" + android:textColor="@color/background" + android:padding="@dimen/padding" /> - </HorizontalScrollView> + </TableRow> - </ScrollView> + </TableLayout> - <LinearLayout - android:id="@+id/periodSection" + </HorizontalScrollView> + + </ScrollView> + + <include + android:id="@+id/reportLoading" + android:layout_width="0dp" + android:layout_height="0dp" + layout="@layout/loading_indicator" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintBottom_toBottomOf="parent" + android:visibility="gone"/> + + </androidx.constraintlayout.widget.ConstraintLayout> + + <!-- Bottom sheet --> + <com.google.android.material.card.MaterialCardView + android:id="@+id/bottomSheet" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_margin="@dimen/margin" + app:contentPadding="@dimen/card_padding" android:orientation="vertical" - app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintStart_toStartOf="parent"> + app:behavior_hideable="false" + app:behavior_peekHeight="@dimen/bottom_sheet_peak" + app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" + style="@style/BottomSheetCardStyle"> <LinearLayout + android:id="@+id/periodSection" android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="horizontal"> + android:orientation="vertical"> - <TextView - android:layout_width="0dp" - android:layout_height="match_parent" - android:layout_weight="1" - android:gravity="center_vertical" - android:text="@string/period" - android:textColor="@color/colorPrimaryDark" - android:textSize="22sp" /> - - <com.google.android.material.button.MaterialButton - android:id="@+id/periodButton" - android:layout_width="wrap_content" + <androidx.constraintlayout.widget.ConstraintLayout + android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginEnd="@dimen/fields_spacing" - android:text="@string/select_period" - android:textColor="@color/colorPrimaryDark" - app:backgroundTint="@color/darkBackground" /> + android:layout_marginBottom="@dimen/margin" + android:orientation="horizontal"> - </LinearLayout> + <!-- Report type --> + <com.addisonelliott.segmentedbutton.SegmentedButtonGroup + android:id="@+id/reportType" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginEnd="@dimen/fields_spacing" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@id/toggleSheet" + app:borderWidth="1dp" + app:dividerPadding="10dp" + app:dividerWidth="1dp" + app:position="0" + app:radius="30dp" + app:ripple="true" + app:selectedBackground="@color/colorPrimary"> - <com.addisonelliott.segmentedbutton.SegmentedButtonGroup - android:id="@+id/reportType" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_marginTop="@dimen/margin" - app:borderWidth="1dp" - app:dividerPadding="10dp" - app:dividerWidth="1dp" - app:position="0" - app:radius="30dp" - app:ripple="true" - app:selectedBackground="@color/colorPrimary"> - - <com.addisonelliott.segmentedbutton.SegmentedButton - android:layout_width="0dp" - android:layout_height="wrap_content" - android:layout_weight="1" - android:padding="10dp" - app:drawableGravity="top" - app:selectedTextColor="@color/darkBackground" - app:text="@string/positions" - app:textColor="@color/colorPrimaryDark" /> - - <com.addisonelliott.segmentedbutton.SegmentedButton - android:layout_width="0dp" - android:layout_height="wrap_content" - android:layout_weight="1" - android:padding="10dp" - app:drawableGravity="top" - app:selectedTextColor="@color/darkBackground" - app:text="@string/events" - app:textColor="@color/colorPrimaryDark" /> - - <com.addisonelliott.segmentedbutton.SegmentedButton - android:layout_width="0dp" + <com.addisonelliott.segmentedbutton.SegmentedButton + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:padding="10dp" + app:drawableGravity="top" + app:selectedTextColor="@color/darkBackground" + app:text="@string/positions" + app:textColor="@color/colorPrimaryDark" /> + + <com.addisonelliott.segmentedbutton.SegmentedButton + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:padding="10dp" + app:drawableGravity="top" + app:selectedTextColor="@color/darkBackground" + app:text="@string/events" + app:textColor="@color/colorPrimaryDark" /> + + <com.addisonelliott.segmentedbutton.SegmentedButton + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:padding="10dp" + app:drawableGravity="top" + app:selectedTextColor="@color/darkBackground" + app:text="@string/stops" + app:textColor="@color/colorPrimaryDark" /> + + </com.addisonelliott.segmentedbutton.SegmentedButtonGroup> + + <ImageButton + android:id="@+id/toggleSheet" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/icon_down" + android:contentDescription="@string/shared_slide_down" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintBottom_toBottomOf="parent" + android:background="?selectableItemBackgroundBorderless" /> + + </androidx.constraintlayout.widget.ConstraintLayout> + + <!-- Report period --> + <LinearLayout + android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_weight="1" - android:padding="10dp" - app:drawableGravity="top" - app:selectedTextColor="@color/darkBackground" - app:text="@string/stops" - app:textColor="@color/colorPrimaryDark" /> + android:orientation="horizontal"> - </com.addisonelliott.segmentedbutton.SegmentedButtonGroup> + <TextView + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="1" + android:gravity="center_vertical" + android:text="@string/period" + android:textColor="@color/colorPrimaryDark" + android:textSize="22sp" /> - <LinearLayout - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="horizontal" - android:layout_marginTop="@dimen/margin"> + <com.google.android.material.button.MaterialButton + android:id="@+id/periodButton" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/select_period" + android:textColor="@color/colorPrimaryDark" + app:backgroundTint="@color/darkBackground" + style="@style/Widget.AppCompat.Button.Small" /> + </LinearLayout> - <com.google.android.material.button.MaterialButton - android:id="@+id/exportButton" + <!-- Custom period ranges --> + <LinearLayout + android:id="@+id/rangeLayout" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="@string/export_report" - android:layout_weight="1" - android:layout_marginEnd="@dimen/fields_spacing" - style="?android:buttonStyleSmall"/> + android:gravity="center_vertical" + android:weightSum="2" + android:orientation="horizontal"> + + <com.google.android.material.textfield.TextInputLayout + android:id="@+id/fromInputLayout" + style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginEnd="@dimen/fields_spacing" + android:hint="@string/period_from" + android:layout_weight="1"> + + <com.google.android.material.textfield.TextInputEditText + android:id="@+id/fromEditText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="@dimen/datetime_field" + android:inputType="none" + android:clickable="false" + android:focusable="false" + tools:text="2022-04-11 00:00" + /> - <com.google.android.material.button.MaterialButton - android:id="@+id/shareButton" + </com.google.android.material.textfield.TextInputLayout> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/period_separator" /> + + <com.google.android.material.textfield.TextInputLayout + android:id="@+id/toInputLayout" + style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/period_to" + android:layout_marginStart="@dimen/fields_spacing" + android:layout_weight="1"> + + <com.google.android.material.textfield.TextInputEditText + android:id="@+id/toEditText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="@dimen/datetime_field" + android:inputType="none" + android:clickable="false" + android:focusable="false" + tools:text="2022-04-11 00:39" + /> + + </com.google.android.material.textfield.TextInputLayout> + + </LinearLayout> + + <!-- Action buttons --> + <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_weight="1" - android:text="@string/share_report" - style="?android:buttonStyleSmall"/> + android:orientation="horizontal" + android:layout_marginTop="@dimen/margin"> - </LinearLayout> + <com.google.android.material.button.MaterialButton + android:id="@+id/exportButton" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/export_report" + android:layout_weight="1" + android:layout_marginEnd="@dimen/fields_spacing" + style="?android:buttonStyleSmall"/> + + <com.google.android.material.button.MaterialButton + android:id="@+id/shareButton" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@string/share_report" + style="?android:buttonStyleSmall"/> - </LinearLayout> + </LinearLayout> + + </LinearLayout> - <include - android:id="@+id/reportLoading" - android:layout_width="0dp" - android:layout_height="0dp" - layout="@layout/loading_indicator" - app:layout_constraintTop_toTopOf="parent" - app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintBottom_toTopOf="@id/periodSection" - android:visibility="gone"/> + </com.google.android.material.card.MaterialCardView> -</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file +</androidx.coordinatorlayout.widget.CoordinatorLayout>
\ No newline at end of file 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 eeb7149..26e0cad 100644 --- a/androidApp/src/main/res/values-es-rMX/strings.xml +++ b/androidApp/src/main/res/values-es-rMX/strings.xml @@ -6,6 +6,8 @@ <string name="shared_close">Cerrar</string> <string name="shared_send">Enviar</string> <string name="shared_loading">Cargando</string> + <string name="shared_slide_up">Deslizar hacia arriba</string> + <string name="shared_slide_down">Deslizar hacia abajo</string> <!-- LoginActivity --> <string name="login_username">Nombre de usuario</string> @@ -113,6 +115,10 @@ <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="period_separator">—</string> + <string name="period_from">Inicio</string> + <string name="period_to">Fin</string> <string name="table_event">Evento</string> <string name="table_datetime">Fecha y hora</string> diff --git a/androidApp/src/main/res/values/dimen.xml b/androidApp/src/main/res/values/dimen.xml index 92d5242..8c8c5f5 100644 --- a/androidApp/src/main/res/values/dimen.xml +++ b/androidApp/src/main/res/values/dimen.xml @@ -35,6 +35,12 @@ <dimen name="report_label_width">10dp</dimen> <dimen name="attribution_text_size">11sp</dimen> + <!-- Reports bottom sheet --> + <dimen name="bottom_sheet_peak">80dp</dimen> + + <!-- Reports datetime field --> + <dimen name="datetime_field">12sp</dimen> + <!-- User Information --> <dimen name="fields_spacing">8dp</dimen> <dimen name="fields_large_spacing">16dp</dimen> diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml index 865368e..c349cd8 100644 --- a/androidApp/src/main/res/values/strings.xml +++ b/androidApp/src/main/res/values/strings.xml @@ -20,6 +20,8 @@ <string name="shared_close">Close</string> <string name="shared_send">Send</string> <string name="shared_loading">Loading</string> + <string name="shared_slide_up">Slide up</string> + <string name="shared_slide_down">Slide down</string> <!-- LoginActivity --> <string name="login_username">Username</string> @@ -127,6 +129,10 @@ <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="period_separator">—</string> + <string name="period_from">Start</string> + <string name="period_to">End</string> <string name="table_event">Event</string> <string name="table_datetime">Datetime</string> diff --git a/androidApp/src/main/res/values/styles.xml b/androidApp/src/main/res/values/styles.xml index dc40450..982d4dc 100644 --- a/androidApp/src/main/res/values/styles.xml +++ b/androidApp/src/main/res/values/styles.xml @@ -12,4 +12,17 @@ <item name="android:textStyle">bold</item> </style> + <style name="BottomSheetCardStyle" parent="Widget.MaterialComponents.CardView"> + <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay_BottomSheetCard</item> + <item name="cardElevation">@dimen/card_elevation</item> + </style> + + <style name="ShapeAppearanceOverlay_BottomSheetCard"> + <item name="cornerFamily">rounded</item> + <item name="cornerSizeTopRight">@dimen/card_border_radius</item> + <item name="cornerSizeTopLeft">@dimen/card_border_radius</item> + <item name="cornerSizeBottomRight">0dp</item> + <item name="cornerSizeBottomLeft">0dp</item> + </style> + </resources>
\ No newline at end of file diff --git a/iosApp/iosApp.xcodeproj/project.pbxproj b/iosApp/iosApp.xcodeproj/project.pbxproj index 146f7c5..5aee1be 100644 --- a/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/iosApp/iosApp.xcodeproj/project.pbxproj @@ -46,6 +46,8 @@ E39ABC4327A4E88C00965D05 /* UnitsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E39ABC4227A4E88C00965D05 /* UnitsViewModel.swift */; }; E39ABC4627A4EBD500965D05 /* DevicesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E39ABC4527A4EBD500965D05 /* DevicesView.swift */; }; E39ABC4827A4EDEC00965D05 /* DeviceRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = E39ABC4727A4EDEC00965D05 /* DeviceRow.swift */; }; + E3B5740827F68F5F0018AFCF /* XlsxFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3B5740727F68F5F0018AFCF /* XlsxFile.swift */; }; + E3B5740A27F69F750018AFCF /* ShareViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3B5740927F69F750018AFCF /* ShareViewController.swift */; }; E3C651E727CB5426002F6F4C /* Tabler in Frameworks */ = {isa = PBXBuildFile; productRef = E3C651E627CB5426002F6F4C /* Tabler */; }; E3C651EA27CB61EE002F6F4C /* GEOSwift in Frameworks */ = {isa = PBXBuildFile; productRef = E3C651E927CB61EE002F6F4C /* GEOSwift */; }; E3E77EE6279E6CE400150070 /* FlowCollector.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3E77EE5279E6CE400150070 /* FlowCollector.swift */; }; @@ -63,6 +65,16 @@ name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; }; + E3B5741D27F6A3C70018AFCF /* Embed App Extensions */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 13; + files = ( + ); + name = "Embed App Extensions"; + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -106,6 +118,8 @@ E39ABC4227A4E88C00965D05 /* UnitsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnitsViewModel.swift; sourceTree = "<group>"; }; E39ABC4527A4EBD500965D05 /* DevicesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DevicesView.swift; sourceTree = "<group>"; }; E39ABC4727A4EDEC00965D05 /* DeviceRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceRow.swift; sourceTree = "<group>"; }; + E3B5740727F68F5F0018AFCF /* XlsxFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XlsxFile.swift; sourceTree = "<group>"; }; + E3B5740927F69F750018AFCF /* ShareViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareViewController.swift; sourceTree = "<group>"; }; E3E77EE5279E6CE400150070 /* FlowCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlowCollector.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ @@ -271,6 +285,8 @@ E33A237227A7581A00DD647F /* Utils.swift */, E34A2F4727A7878200AD8AEB /* HyperlinkText.swift */, E31D721427CF159900CDA320 /* SidewaysScroller.swift */, + E3B5740727F68F5F0018AFCF /* XlsxFile.swift */, + E3B5740927F69F750018AFCF /* ShareViewController.swift */, ); path = Shared; sourceTree = "<group>"; @@ -287,6 +303,7 @@ 7555FF78242A565900829871 /* Frameworks */, 7555FF79242A565900829871 /* Resources */, E33A236F27A7545500DD647F /* Embed Frameworks */, + E3B5741D27F6A3C70018AFCF /* Embed App Extensions */, ); buildRules = ( ); @@ -309,7 +326,7 @@ 7555FF73242A565900829871 /* Project object */ = { isa = PBXProject; attributes = { - LastSwiftUpdateCheck = 1130; + LastSwiftUpdateCheck = 1320; LastUpgradeCheck = 1130; ORGANIZATIONNAME = orgName; TargetAttributes = { @@ -396,6 +413,7 @@ E312E74F27B366060018C5DE /* DevicesViewModel.swift in Sources */, E33A236727A64E4500DD647F /* MarkerTransformations.swift in Sources */, E38F241527A242870069FC45 /* Inject.swift in Sources */, + E3B5740A27F69F750018AFCF /* ShareViewController.swift in Sources */, E39ABC4827A4EDEC00965D05 /* DeviceRow.swift in Sources */, E396282827AFBD72005D070E /* UnitCommandsView.swift in Sources */, E38F241C27A26DD70069FC45 /* RootViewModel.swift in Sources */, @@ -408,6 +426,7 @@ E39ABC4327A4E88C00965D05 /* UnitsViewModel.swift in Sources */, E360251B27BCA8A600958B21 /* AccountViewModel.swift in Sources */, E34A2F4827A7878200AD8AEB /* HyperlinkText.swift in Sources */, + E3B5740827F68F5F0018AFCF /* XlsxFile.swift in Sources */, 2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */, E39ABC4627A4EBD500965D05 /* DevicesView.swift in Sources */, E3141B8B27B9E91F00CE777C /* UnitReportsViewModel.swift in Sources */, @@ -552,6 +571,7 @@ 7555FFA6242A565B00829871 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = iosApp/iosApp.entitlements; @@ -589,6 +609,7 @@ 7555FFA7242A565B00829871 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = iosApp/iosApp.entitlements; diff --git a/iosApp/iosApp/AppDelegate.swift b/iosApp/iosApp/AppDelegate.swift index 72e3e92..22d4f75 100644 --- a/iosApp/iosApp/AppDelegate.swift +++ b/iosApp/iosApp/AppDelegate.swift @@ -32,6 +32,8 @@ class AppDelegate: NSObject, UIApplicationDelegate { application.registerForRemoteNotifications() + Utils.createReportTmpDirectory() + Utils.clearReportTmpDirectory() return true } diff --git a/iosApp/iosApp/Details/Reports/UnitReportsView.swift b/iosApp/iosApp/Details/Reports/UnitReportsView.swift index 2143b3b..7fd45a1 100644 --- a/iosApp/iosApp/Details/Reports/UnitReportsView.swift +++ b/iosApp/iosApp/Details/Reports/UnitReportsView.swift @@ -31,6 +31,9 @@ struct UnitReportsView: View { self.unit = unit self._unitReportsViewModel = StateObject( wrappedValue: UnitReportsViewModel(deviceId: unit.device.id)) + // Source: https://stackoverflow.com/a/63607411 + UITableView.appearance().contentInset.top = -35 + UITableView.appearance().contentInset.bottom = -25 } private var eventsConfig: TablerListConfig<EventInformation> { @@ -111,59 +114,76 @@ struct UnitReportsView: View { isReport: true) } - VStack { - HStack { - Text("report-period") - Spacer() - Picker(selection: $unitReportsViewModel.reportPeriod) { - Text("period-today").tag(ReportDates.ReportPeriod.today) - Text("period-last-24").tag(ReportDates.ReportPeriod.last24) - Text("period-yesterday").tag(ReportDates.ReportPeriod.yesterday) - Text("period-this-week").tag(ReportDates.ReportPeriod.thisWeek) - Text("period-last-7").tag(ReportDates.ReportPeriod.last7) - Text("period-this-month").tag(ReportDates.ReportPeriod.thisMonth) - Text("period-last-30").tag(ReportDates.ReportPeriod.last30) - } label: { - switch unitReportsViewModel.reportPeriod { - case .today: - Text("period-today") - case .last24: - Text("period-last-24") - case .yesterday: - Text("period-yesterday") - case .thisWeek: - Text("period-this-week") - case .last7: - Text("period-last-7") - case .thisMonth: - Text("period-this-month") - case .last30: - Text("period-last-30") - default: - Text("period-today") + // MARK: - Report type + Picker(selection: $unitReportsViewModel.reportType) { + Text("report-positions").tag(ReportController.ReportType.positions) + Text("report-events").tag(ReportController.ReportType.events) + Text("report-stops").tag(ReportController.ReportType.stops) + } label: { + EmptyView() + } + .pickerStyle(.segmented) + .padding() + + Form { + // MARK: - Report period + Section { + HStack { + Text("report-period") + Spacer() + Picker(selection: $unitReportsViewModel.periodType, label: Text("report-period")) { + Text("period-today").tag(ReportDates.PeriodTypes.today) + Text("period-last-24").tag(ReportDates.PeriodTypes.last24) + Text("period-yesterday").tag(ReportDates.PeriodTypes.yesterday) + Text("period-this-week").tag(ReportDates.PeriodTypes.thisWeek) + Text("period-last-7").tag(ReportDates.PeriodTypes.last7) + Text("period-this-month").tag(ReportDates.PeriodTypes.thisMonth) + Text("period-last-30").tag(ReportDates.PeriodTypes.last30) + Text("period-custom").tag(ReportDates.PeriodTypes.custom) } + .pickerStyle(.menu) } - }.padding() - Picker(selection: $unitReportsViewModel.reportType) { - Text("report-positions").tag(ReportController.ReportType.positions) - Text("report-events").tag(ReportController.ReportType.events) - Text("report-stops").tag(ReportController.ReportType.stops) - } label: { - EmptyView() - }.pickerStyle(SegmentedPickerStyle()) + + if unitReportsViewModel.periodType == .custom { + DatePicker(selection: $unitReportsViewModel.fromDate, in: ...Date()) { + Text("period-from") + } + + DatePicker(selection: $unitReportsViewModel.toDate, in: ...Date()) { + Text("period-to") + } + } + } - //HStack { - // Group { - // Button {} label: { - // Text("report-save") - // } - // - // Button {} label: { - // Text("report-share") - // } - // }.frame(maxWidth: .infinity) - //}.padding() - }.padding() + // MARK: - Report actions + Section { + Button { + unitReportsViewModel.saveXlsxReport() + } label: { + Text("report-save") + }.frame(maxWidth: .infinity) + + Button { + unitReportsViewModel.shareXlsxReport() + } label: { + Text("report-share") + }.frame(maxWidth: .infinity) + } + } + .frame(maxHeight: 150) + .sheet(isPresented: $unitReportsViewModel.showShareDialog) { + ShareView(activityItems: $unitReportsViewModel.activityItems) + } + .fileExporter(isPresented: $unitReportsViewModel.showExportDialog, + documents: [unitReportsViewModel.saveDocument], + contentType: .xlsx) { result in + switch result { + case .success (let url): + print ("Saved to \(url)") + case .failure (let error): + print (error.localizedDescription) + } + } }.onAppear { unitReportsViewModel.fetchReport() } diff --git a/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift b/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift index 4731a57..450bfa7 100644 --- a/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift +++ b/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift @@ -1,11 +1,20 @@ -// -// UnitReportsViewModel.swift -// iosApp -// -// Created by Iván on 13/02/22. -// Copyright © 2022 orgName. All rights reserved. -// - +/** + * 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/>. + */ import Foundation import Combine import shared @@ -14,13 +23,44 @@ class UnitReportsViewModel: ObservableObject { @Inject var reportController: ReportController @Inject var geofencesController: GeofencesController + enum XlsxAction { + case save + case share + } + @Published var deviceId: Int32? = nil + + // MARK: - Report settings @Published var reportType: ReportController.ReportType = .positions { didSet { fetchReport() } } - @Published var reportPeriod: ReportDates.ReportPeriod = .today { + @Published var periodType: ReportDates.PeriodTypes = .today { + didSet { + switch periodType { + case .today: + reportPeriod = ReportDates.ReportPeriodToday() + case .last24: + reportPeriod = ReportDates.ReportPeriodLast24() + case .yesterday: + reportPeriod = ReportDates.ReportPeriodYesterday() + case .thisWeek: + reportPeriod = ReportDates.ReportPeriodThisWeek() + case .last7: + reportPeriod = ReportDates.ReportPeriodLast7() + case .thisMonth: + reportPeriod = ReportDates.ReportPeriodThisMonth() + case .last30: + reportPeriod = ReportDates.ReportPeriodLast30() + case .custom: + reportPeriod = ReportDates.ReportPeriodCustom(from: nil, to: nil) + default: + reportPeriod = ReportDates.ReportPeriodToday() + } + } + } + @Published var reportPeriod: ReportDates.ReportPeriod = ReportDates.ReportPeriodToday() { didSet { fetchReport() } @@ -34,6 +74,16 @@ class UnitReportsViewModel: ObservableObject { markers = [] case let report as ReportController.ReportStopsReport: markers = report.stops.compactMap { s in Marker.companion.fromStop(stop: s) } + case let report as ReportController.ReportXlsxReport: + print ("Got XLSX report!") + saveDocument = XlsxFile(bytes: report.data) + if xlsxAction == .save { + showExportDialog = true + } else { + let url = saveDocument.data.dataToFile(fileName: "report.xlsx") + activityItems = [url!] + showShareDialog = true + } default: break } @@ -43,6 +93,25 @@ class UnitReportsViewModel: ObservableObject { @Published var markers = [Marker]() @Published var selectedMarker: Marker? = nil + @Published var fromDate: Date = Date() { + didSet { + if let custom = reportPeriod as? ReportDates.ReportPeriodCustom { + reportPeriod = custom.withFrom( + from: DateUtils.companion.iosDateToKotlin(date: fromDate)) + } + } + } + + @Published var toDate: Date = Calendar.current.startOfDay(for: Date()) { + didSet { + if let custom = reportPeriod as? ReportDates.ReportPeriodCustom { + reportPeriod = custom.withTo( + to: DateUtils.companion.iosDateToKotlin(date: toDate)) + } + } + } + + // MARK: - Geofences @Published var geofences: [Int: Geofence] = [:] { didSet { flatGeofences = Array(geofences.values) @@ -50,6 +119,13 @@ class UnitReportsViewModel: ObservableObject { } @Published var flatGeofences: [Geofence] = [] + // MARK: - Save and share + var xlsxAction: XlsxAction = .save + var saveDocument = XlsxFile() + @Published var showExportDialog: Bool = false + @Published var showShareDialog: Bool = false + @Published var activityItems: [Any] = [] + init(deviceId id: Int32?) { deviceId = id let collector = Collector<ReportController.Report>(callback: setReport) @@ -91,4 +167,14 @@ class UnitReportsViewModel: ObservableObject { ]) { _, _ in } } } + + func saveXlsxReport () { + xlsxAction = .save + fetchReport(xlsx: true) + } + + func shareXlsxReport () { + xlsxAction = .share + fetchReport(xlsx: true) + } } diff --git a/iosApp/iosApp/Map/MapViewController.swift b/iosApp/iosApp/Map/MapViewController.swift index 3c38b89..07b72a6 100644 --- a/iosApp/iosApp/Map/MapViewController.swift +++ b/iosApp/iosApp/Map/MapViewController.swift @@ -20,7 +20,7 @@ import WhirlyGlobe import shared import GEOSwift -typealias MarkerCallback = (Int32?) -> () +typealias MarkerCallback = (Int32?, Bool) -> () extension Geometry { func getCoordinates() -> [Point] { @@ -138,7 +138,7 @@ extension MapViewController: MaplyViewControllerDelegate { func maplyViewController(_ viewC: MaplyViewController, didTapAt coord: MaplyCoordinate) { - markerCallback?(nil) + markerCallback?(nil, true) } func maplyViewController(_ viewC: MaplyViewController, @@ -147,7 +147,7 @@ extension MapViewController: MaplyViewControllerDelegate { onScreen screenPt: CGPoint) { if let marker = selectedObj as? MaplyScreenMarker { if let id = marker.userObject as? Int32 { - markerCallback?(id) + markerCallback?(id, true) } } } diff --git a/iosApp/iosApp/Shared/ShareViewController.swift b/iosApp/iosApp/Shared/ShareViewController.swift new file mode 100644 index 0000000..ed26ab8 --- /dev/null +++ b/iosApp/iosApp/Shared/ShareViewController.swift @@ -0,0 +1,26 @@ +// +// ShareViewController.swift +// iosApp +// +// Created by Iván on 31/03/22. +// Copyright © 2022 orgName. All rights reserved. +// + +import Foundation +import SwiftUI +import UIKit + +struct ShareView: UIViewControllerRepresentable { + + @Binding var activityItems: [Any] + var excludedActivityTypes: [UIActivity.ActivityType]? = nil + + func makeUIViewController(context: UIViewControllerRepresentableContext<ShareView>) -> UIActivityViewController { + let controller = UIActivityViewController(activityItems: activityItems, + applicationActivities: nil) + controller.excludedActivityTypes = excludedActivityTypes + return controller + } + + func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ShareView>) {} +} diff --git a/iosApp/iosApp/Shared/Utils.swift b/iosApp/iosApp/Shared/Utils.swift index 61a8f90..4e04d1b 100644 --- a/iosApp/iosApp/Shared/Utils.swift +++ b/iosApp/iosApp/Shared/Utils.swift @@ -20,6 +20,25 @@ import CryptoKit import WhirlyGlobe import shared +// Source: https://stackoverflow.com/a/55092044 +extension Data { + func dataToFile(fileName: String) -> NSURL? { + let fileManager = FileManager.default + let data = self + let filePath = Utils.getReportTmpDirectory().appendingPathComponent(fileName) + do { + if(fileManager.fileExists(atPath: filePath)) { + try fileManager.removeItem(atPath: filePath) + } + fileManager.createFile(atPath: filePath, contents: data, attributes: nil) + return NSURL(fileURLWithPath: filePath) + } catch { + print("Error writing the file: \(error.localizedDescription)") + } + return nil + } +} + class Utils { static func MD5(string: String) -> String { let digest = Insecure.MD5.hash(data: string.data(using: .utf8) ?? Data()) @@ -45,4 +64,37 @@ class Utils { .replacingOccurrences(of: "{x}", with: "\(latitude)") .replacingOccurrences(of: "{y}", with: "\(longitude)")) } + + static func createReportTmpDirectory() { + let fileManager = FileManager.default + let tmpDir = getReportTmpDirectory() as String + do { + if (!fileManager.fileExists(atPath: tmpDir)) { + try fileManager.createDirectory(atPath: tmpDir, + withIntermediateDirectories: false, + attributes: nil) + } + } catch { + print("Could not create temp folder: \(error)") + } + } + + static func getReportTmpDirectory() -> NSString { + let tmpDir = NSTemporaryDirectory() + return (tmpDir as NSString).appendingPathComponent("reports") as NSString + } + + // Source: https://stackoverflow.com/a/33937110 + static func clearReportTmpDirectory() { + let fileManager = FileManager.default + let reportTmpDir = getReportTmpDirectory() + do { + let filePaths = try fileManager.contentsOfDirectory(atPath: reportTmpDir as String) + for filePath in filePaths { + try fileManager.removeItem(atPath: reportTmpDir.appendingPathComponent(filePath)) + } + } catch { + print("Could not clear temp folder: \(error)") + } + } } diff --git a/iosApp/iosApp/Shared/XlsxFile.swift b/iosApp/iosApp/Shared/XlsxFile.swift new file mode 100644 index 0000000..8015422 --- /dev/null +++ b/iosApp/iosApp/Shared/XlsxFile.swift @@ -0,0 +1,60 @@ +/** + * 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/>. + */ +import shared +import Foundation +import UniformTypeIdentifiers +import SwiftUI + +// Source: https://www.hackingwithswift.com/quick-start/swiftui/how-to-export-files-using-fileexporter + +extension UTType { + static var doc: Self { .init(filenameExtension: "doc")! } + static var docx: Self { .init(filenameExtension: "docx")! } + + static var xls: Self { .init(filenameExtension: "xls")! } + static var xlsx: Self { .init(filenameExtension: "xlsx")! } + + static var ppt: Self { .init(filenameExtension: "ppt")! } + static var pptx: Self { .init(filenameExtension: "pptx")! } +} + +struct XlsxFile: FileDocument { + static var readableContentTypes = [UTType.xlsx] + + var data: Data + + init() { + data = Data() + } + + init(bytes: KotlinByteArray) { + data = bytes.toData() + } + + init(configuration: ReadConfiguration) throws { + if let data = configuration.file.regularFileContents { + self.data = data + return + } + self.data = Data() + } + + func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { + return FileWrapper(regularFileWithContents: data) + } +} diff --git a/iosApp/iosApp/Units/UnitsViewModel.swift b/iosApp/iosApp/Units/UnitsViewModel.swift index 8ba6a4b..6a3ce19 100644 --- a/iosApp/iosApp/Units/UnitsViewModel.swift +++ b/iosApp/iosApp/Units/UnitsViewModel.swift @@ -49,12 +49,15 @@ class UnitsViewModel: ObservableObject { } } @Published var markers: [Marker] = [] + @Published var oldSelectedUnit: UnitInformation? = nil @Published var selectedUnit: UnitInformation? = nil { didSet { - if let unit = selectedUnit { - selectedMarker = Marker.companion.fromUnit(unit: unit) - } else { - selectedMarker = nil + if selectedUnit?.device.id != oldSelectedUnit?.device.id { + if let unit = selectedUnit { + selectedMarker = Marker.companion.fromUnit(unit: unit) + } else { + selectedMarker = nil + } } } } @@ -87,32 +90,65 @@ class UnitsViewModel: ObservableObject { private func setUnits(units: [UnitInformation]) { print("Positions") self.units = units + updateSelectedUnit() } private func setGeofences(geofences: [Int: Geofence]) { self.geofences = geofences } - func selectUnitWith(position id: Int32?) { + func selectUnit(unit: UnitInformation?, switchToMap: Bool = true) { + print ("Selecting unit \(unit?.device.name ?? "")") + oldSelectedUnit = selectedUnit + selectedUnit = unit + if unit != nil && switchToMap { + setDisplayMode(UnitsDisplayMode.map) + } + } + + func selectUnitWith(position id: Int32?, switchToMap: Bool = true) { if id == nil { print("Deselecting unit") - selectedUnit = nil + selectUnit(unit: nil, switchToMap: switchToMap) return } - print("Selecting unit with position id: \(id ?? 0)") - if let unit = units.first(where: { - Int32(truncating: $0.position?.id ?? 0) == id - }) { - selectedUnit = unit + print("Selecting unit with position id: \(id!)") + let unit = units.first { + Int32(truncating: $0.position?.id ?? 0) == id! } + selectUnit(unit: unit, switchToMap: switchToMap) + } + + private func selectUnitWith(id: Int32?, switchToMap: Bool = true) { + if id == nil { + print("Deselecting unit") + selectUnit(unit: nil, switchToMap: switchToMap) + return + } + print("Selecting unit with device id: \(id!)") + let unit = units.first { + $0.device.id == id! + } + selectUnit(unit: unit, switchToMap: switchToMap) + } + + private func updateSelectedUnit() { + if let selected = selectedUnit { + print("Updating selected unit with id: \(selected.device.id)") + selectUnitWith(id: selected.device.id, switchToMap: false) + } + } + + func setDisplayMode(_ displayMode: UnitsDisplayMode) { + unitsDisplayMode = displayMode } func toggleDisplayMode() { switch unitsDisplayMode { case .map: - unitsDisplayMode = .list + setDisplayMode(.list) case .list: - unitsDisplayMode = .map + setDisplayMode(.map) } } diff --git a/iosApp/iosApp/en.lproj/Localizable.strings b/iosApp/iosApp/en.lproj/Localizable.strings index ca8e5a1..0959a1f 100644 --- a/iosApp/iosApp/en.lproj/Localizable.strings +++ b/iosApp/iosApp/en.lproj/Localizable.strings @@ -68,6 +68,9 @@ "period-last-7" = "Last 7d"; "period-this-month" = "Month"; "period-last-30" = "Last 30d"; +"period-custom" = "Custom"; +"period-from" = "Start"; +"period-to" = "End"; "events-table-event" = "Event"; "events-table-datetime" = "Datetime"; diff --git a/iosApp/iosApp/es-419.lproj/Localizable.strings b/iosApp/iosApp/es-419.lproj/Localizable.strings index 9abd96a..52267cc 100644 --- a/iosApp/iosApp/es-419.lproj/Localizable.strings +++ b/iosApp/iosApp/es-419.lproj/Localizable.strings @@ -68,6 +68,9 @@ "period-last-7" = "Últimos 7d"; "period-this-month" = "Mes"; "period-last-30" = "Últimos 30d"; +"period-custom" = "Personalizado"; +"period-from" = "Inicio"; +"period-to" = "Fin"; "events-table-event" = "Evento"; "events-table-datetime" = "Fecha y hora"; 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 |