diff options
Diffstat (limited to 'Sora/Views/Settings/Section/SettingsSectionImportExportView.swift')
| -rw-r--r-- | Sora/Views/Settings/Section/SettingsSectionImportExportView.swift | 72 |
1 files changed, 31 insertions, 41 deletions
diff --git a/Sora/Views/Settings/Section/SettingsSectionImportExportView.swift b/Sora/Views/Settings/Section/SettingsSectionImportExportView.swift index b74fe4b..846e9e5 100644 --- a/Sora/Views/Settings/Section/SettingsSectionImportExportView.swift +++ b/Sora/Views/Settings/Section/SettingsSectionImportExportView.swift @@ -2,9 +2,12 @@ import SwiftUI import UniformTypeIdentifiers struct SettingsSectionImportExportView: View { - @EnvironmentObject private var settings: SettingsManager + @Environment(SettingsManager.self) + private var settings @State private var isFileExporterPresented = false @State private var isFileImporterPresented = false + @State private var bookmarksExportDocument: JSONFileDocument? + @State private var bookmarksExportFilename = "sora_bookmarks.json" @State private var exportError: Error? @State private var importError: Error? private let dateFormatter: DateFormatter = { @@ -20,26 +23,30 @@ struct SettingsSectionImportExportView: View { } Button("Export Bookmarks") { - exportBookmarksToFile() + prepareBookmarksExport() } } #if os(macOS) .trailingFrame() - .fileExporter( - isPresented: $isFileExporterPresented, - document: try? JSONFileDocument(settings.exportBookmarks()), - contentType: .json, - defaultFilename: "sora_bookmarks.json" - ) { result in - switch result { - case .success: - break + #endif + .fileExporter( + isPresented: $isFileExporterPresented, + document: bookmarksExportDocument, + contentType: .json, + defaultFilename: bookmarksExportFilename + ) { result in + bookmarksExportDocument = nil - case .failure(let error): + switch result { + case .success: + break + + case .failure(let error): + if !isUserCancelled(error) { exportError = error } } - #endif + } .fileImporter( isPresented: $isFileImporterPresented, allowedContentTypes: [.json], @@ -71,38 +78,14 @@ struct SettingsSectionImportExportView: View { } } - private func exportBookmarksToFile() { + private func prepareBookmarksExport() { do { let data = try settings.exportBookmarks() let timestamp = dateFormatter.string(from: Date()) - #if os(macOS) - _ = data - isFileExporterPresented = true - #elseif os(iOS) - let temporaryURL = FileManager.default.temporaryDirectory - .appendingPathComponent("sora_bookmarks_\(timestamp).json") - - try data.write(to: temporaryURL) - - let activityController = UIActivityViewController( - activityItems: [temporaryURL], - applicationActivities: nil - ) - - if let windowScene = UIApplication.shared.connectedScenes - .first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene, - let rootViewController = windowScene.windows.first?.rootViewController - { - activityController.popoverPresentationController?.sourceView = rootViewController.view - - rootViewController.present(activityController, animated: true) - } - - activityController.completionWithItemsHandler = { _, _, _, _ in - try? FileManager.default.removeItem(at: temporaryURL) - } - #endif + bookmarksExportDocument = JSONFileDocument(data) + bookmarksExportFilename = "sora_bookmarks_\(timestamp).json" + isFileExporterPresented = true } catch { exportError = error } @@ -125,6 +108,13 @@ struct SettingsSectionImportExportView: View { } } + private func isUserCancelled(_ error: Error) -> Bool { + let error = error as NSError + + return error.domain == NSCocoaErrorDomain + && error.code == CocoaError.userCancelled.rawValue + } + private enum ImportError: Error { case accessDenied } |