Add export function

This commit is contained in:
Christoph Hagen
2025-01-31 13:06:11 +01:00
parent 740f776af6
commit 00e4da3f21
10 changed files with 189 additions and 13 deletions

View File

@@ -0,0 +1,81 @@
import SwiftUI
struct ExportSheet: View {
@Binding var isPresented: Bool
@State
private var isCreatingArchive = false
@State var url: URL?
@State var error: String?
@EnvironmentObject
private var storage: PersistentStorage
var body: some View {
VStack {
Text("Export")
.font(.title)
Text("Create a zip file of all stored data and share it.")
if isCreatingArchive {
ProgressView()
Text("Creating archive...")
}
if let error {
Text(error)
.foregroundStyle(.red)
}
Spacer()
Button("Create archive", action: createArchive)
.disabled(isCreatingArchive)
.padding()
if let url {
ShareLink(item: url) {
Label("Share archive", systemImage: "square.and.arrow.up")
}
.padding()
Button("Delete archive", action: deleteArchive)
.padding()
}
}
.padding()
}
private func createArchive() {
guard !isCreatingArchive else {
return
}
isCreatingArchive = true
DispatchQueue.main.async {
do {
let url = try storage.createZip()
DispatchQueue.main.async {
self.url = url
self.error = nil
self.isCreatingArchive = false
}
} catch {
DispatchQueue.main.async {
self.error = "Failed to create archive: \(error.localizedDescription)"
self.isCreatingArchive = false
}
}
}
}
private func deleteArchive() {
do {
try storage.removeZipArchive()
self.error = nil
self.url = nil
} catch {
self.error = "Failed to delete archive: \(error.localizedDescription)"
}
}
}
#Preview {
ExportSheet(isPresented: .constant(true))
}

View File

@@ -1,10 +1,14 @@
import SwiftUI
import SFSafeSymbols
struct HistoryList: View {
@EnvironmentObject
var storage: PersistentStorage
@State
private var showExportSheet = false
var body: some View {
NavigationView {
List(storage.dailyMeasurementCounts) { day in
@@ -25,6 +29,16 @@ struct HistoryList: View {
}
.navigationTitle("History")
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem {
Button(action: { showExportSheet = true }) {
Label("Export", systemSymbol: .arrowUpDoc)
}
}
}
.sheet(isPresented: $showExportSheet) {
ExportSheet(isPresented: $showExportSheet)
}
}
}