Add export function
This commit is contained in:
81
TempTrack/Views/ExportSheet.swift
Normal file
81
TempTrack/Views/ExportSheet.swift
Normal 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))
|
||||
}
|
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user