summaryrefslogtreecommitdiff
path: root/Sora/Views/Settings/Section/SettingsSectionImportExportView.swift
blob: b74fe4b7dd22ba94630c28c2ae10f30f9e3be575 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import SwiftUI
import UniformTypeIdentifiers

struct SettingsSectionImportExportView: View {
  @EnvironmentObject private var settings: SettingsManager
  @State private var isFileExporterPresented = false
  @State private var isFileImporterPresented = false
  @State private var exportError: Error?
  @State private var importError: Error?
  private let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd_HH-mm-ss"
    return formatter
  }()

  var body: some View {
    Group {
      Button("Import Bookmarks") {
        isFileImporterPresented = true
      }

      Button("Export Bookmarks") {
        exportBookmarksToFile()
      }
    }
    #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

        case .failure(let error):
          exportError = error
        }
      }
    #endif
    .fileImporter(
      isPresented: $isFileImporterPresented,
      allowedContentTypes: [.json],
      allowsMultipleSelection: false
    ) { result in
      handleImportResult(result)
    }
    .alert(
      "Export Failed",
      isPresented: Binding(
        get: { exportError != nil },
        set: { if !$0 { exportError = nil } }
      )
    ) {
      Button("OK", role: .cancel) { () }
    } message: {
      Text(exportError?.localizedDescription ?? "An unknown error occurred while exporting.")
    }
    .alert(
      "Import Failed",
      isPresented: Binding(
        get: { importError != nil },
        set: { if !$0 { importError = nil } }
      )
    ) {
      Button("OK", role: .cancel) { () }
    } message: {
      Text(importError?.localizedDescription ?? "An unknown error occurred while importing.")
    }
  }

  private func exportBookmarksToFile() {
    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
    } catch {
      exportError = error
    }
  }

  private func handleImportResult(_ result: Result<[URL], Error>) {
    do {
      guard let selectedFile = try result.get().first else { return }
      guard selectedFile.startAccessingSecurityScopedResource() else {
        throw ImportError.accessDenied
      }

      defer { selectedFile.stopAccessingSecurityScopedResource() }

      let data = try Data(contentsOf: selectedFile)

      try settings.importBookmarks(from: data)
    } catch {
      importError = error
    }
  }

  private enum ImportError: Error {
    case accessDenied
  }
}