diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-03-31 22:00:12 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-03-31 22:00:12 -0600 |
commit | d95742dd9f80506a0d773cbba7bc6a4508de1964 (patch) | |
tree | 86c6f8b50d3221d0b825e786aa36753ee0386646 /iosApp/iosApp | |
parent | 620cd5b1e2782adfd57660017878013f2fb896bd (diff) | |
download | etbsa-trackermap-mobile-d95742dd9f80506a0d773cbba7bc6a4508de1964.tar.gz etbsa-trackermap-mobile-d95742dd9f80506a0d773cbba7bc6a4508de1964.tar.bz2 etbsa-trackermap-mobile-d95742dd9f80506a0d773cbba7bc6a4508de1964.zip |
Added support for sharing and saving reports
Diffstat (limited to 'iosApp/iosApp')
-rw-r--r-- | iosApp/iosApp/AppDelegate.swift | 2 | ||||
-rw-r--r-- | iosApp/iosApp/Details/Reports/UnitReportsView.swift | 39 | ||||
-rw-r--r-- | iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift | 58 | ||||
-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 |
6 files changed, 218 insertions, 19 deletions
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..e189835 100644 --- a/iosApp/iosApp/Details/Reports/UnitReportsView.swift +++ b/iosApp/iosApp/Details/Reports/UnitReportsView.swift @@ -152,17 +152,34 @@ struct UnitReportsView: View { EmptyView() }.pickerStyle(SegmentedPickerStyle()) - //HStack { - // Group { - // Button {} label: { - // Text("report-save") - // } - // - // Button {} label: { - // Text("report-share") - // } - // }.frame(maxWidth: .infinity) - //}.padding() + HStack { + Group { + Button { + unitReportsViewModel.saveXlsxReport() + } label: { + Text("report-save") + } + + Button { + unitReportsViewModel.shareXlsxReport() + } label: { + Text("report-share") + } + }.frame(maxWidth: .infinity) + }.padding() + .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) + } + } }.padding() }.onAppear { unitReportsViewModel.fetchReport() diff --git a/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift b/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift index 4731a57..e5b3e4b 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,6 +23,11 @@ class UnitReportsViewModel: ObservableObject { @Inject var reportController: ReportController @Inject var geofencesController: GeofencesController + enum XlsxAction { + case save + case share + } + @Published var deviceId: Int32? = nil @Published var reportType: ReportController.ReportType = .positions { didSet { @@ -34,6 +48,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 +67,7 @@ class UnitReportsViewModel: ObservableObject { @Published var markers = [Marker]() @Published var selectedMarker: Marker? = nil + // Geofences @Published var geofences: [Int: Geofence] = [:] { didSet { flatGeofences = Array(geofences.values) @@ -50,6 +75,13 @@ class UnitReportsViewModel: ObservableObject { } @Published var flatGeofences: [Geofence] = [] + // 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 +123,14 @@ class UnitReportsViewModel: ObservableObject { ]) { _, _ in } } } + + func saveXlsxReport () { + xlsxAction = .save + fetchReport(xlsx: true) + } + + func shareXlsxReport () { + xlsxAction = .share + fetchReport(xlsx: 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) + } +} |