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 /iosApp | |
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
Diffstat (limited to 'iosApp')
-rw-r--r-- | iosApp/iosApp.xcodeproj/project.pbxproj | 23 | ||||
-rw-r--r-- | iosApp/iosApp/AppDelegate.swift | 2 | ||||
-rw-r--r-- | iosApp/iosApp/Details/Reports/UnitReportsView.swift | 120 | ||||
-rw-r--r-- | iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift | 104 | ||||
-rw-r--r-- | iosApp/iosApp/Map/MapViewController.swift | 6 | ||||
-rw-r--r-- | iosApp/iosApp/Shared/ShareViewController.swift | 26 | ||||
-rw-r--r-- | iosApp/iosApp/Shared/Utils.swift | 52 | ||||
-rw-r--r-- | iosApp/iosApp/Shared/XlsxFile.swift | 60 | ||||
-rw-r--r-- | iosApp/iosApp/Units/UnitsViewModel.swift | 62 | ||||
-rw-r--r-- | iosApp/iosApp/en.lproj/Localizable.strings | 3 | ||||
-rw-r--r-- | iosApp/iosApp/es-419.lproj/Localizable.strings | 3 |
11 files changed, 385 insertions, 76 deletions
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"; |