|
| 1 | +// |
| 2 | +// AppDataScreen.swift |
| 3 | +// SwiftUI-Days |
| 4 | +// |
| 5 | +// Created by Oleg991 on 06.04.2025. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import SwiftData |
| 10 | + |
| 11 | +struct AppDataScreen: View { |
| 12 | + @Environment(\.modelContext) private var modelContext |
| 13 | + @Query private var items: [Item] |
| 14 | + @State private var showDeleteDataConfirmation = false |
| 15 | + @State private var isCreatingBackup = false |
| 16 | + @State private var isRestoringFromBackup = false |
| 17 | + @State private var showResult = false |
| 18 | + @State private var operationResult: OperationResult? |
| 19 | + |
| 20 | + var body: some View { |
| 21 | + VStack(spacing: 16) { |
| 22 | + Group { |
| 23 | + backupDataButton |
| 24 | + restoreDataButton |
| 25 | + if !items.isEmpty { |
| 26 | + removeAllDataButton |
| 27 | + } |
| 28 | + } |
| 29 | + .buttonStyle(.borderedProminent) |
| 30 | + .foregroundStyle(.buttonTint) |
| 31 | + } |
| 32 | + .animation(.default, value: items.isEmpty) |
| 33 | + .alert( |
| 34 | + operationResult?.title ?? "", |
| 35 | + isPresented: $showResult, |
| 36 | + presenting: operationResult, |
| 37 | + actions: { _ in |
| 38 | + Button("Ok") {} |
| 39 | + }, |
| 40 | + message: { result in |
| 41 | + Text(result.message) |
| 42 | + } |
| 43 | + ) |
| 44 | + .navigationTitle("App data") |
| 45 | + } |
| 46 | + |
| 47 | + private var backupDataButton: some View { |
| 48 | + Button("Create a backup") { |
| 49 | + isCreatingBackup.toggle() |
| 50 | + } |
| 51 | + .accessibilityIdentifier("backupDataButton") |
| 52 | + .fileExporter( |
| 53 | + isPresented: $isCreatingBackup, |
| 54 | + document: BackupFileDocument(items: items.map(BackupFileDocument.makeBackupItem)), |
| 55 | + contentType: .json, |
| 56 | + defaultFilename: "Days backup" |
| 57 | + ) { result in |
| 58 | + switch result { |
| 59 | + case .success: |
| 60 | + operationResult = .backupSuccess |
| 61 | + case let .failure(error): |
| 62 | + operationResult = .error(error.localizedDescription) |
| 63 | + } |
| 64 | + showResult = true |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private var restoreDataButton: some View { |
| 69 | + Button("Restore from backup") { |
| 70 | + isRestoringFromBackup.toggle() |
| 71 | + } |
| 72 | + .accessibilityIdentifier("restoreDataButton") |
| 73 | + .fileImporter( |
| 74 | + isPresented: $isRestoringFromBackup, |
| 75 | + allowedContentTypes: [.json], |
| 76 | + allowsMultipleSelection: false |
| 77 | + ) { result in |
| 78 | + switch result { |
| 79 | + case let .success(urls): |
| 80 | + if let url = urls.first, |
| 81 | + url.startAccessingSecurityScopedResource(), |
| 82 | + let data = try? Data(contentsOf: url), |
| 83 | + let importedItems = try? JSONDecoder().decode([BackupFileDocument.BackupItem].self, from: data) { |
| 84 | + defer { url.stopAccessingSecurityScopedResource() } |
| 85 | + let mappedRealItems = importedItems.map(\.realItem) |
| 86 | + mappedRealItems.forEach { modelContext.insert($0) } |
| 87 | + operationResult = .restoreSuccess |
| 88 | + } else { |
| 89 | + operationResult = .failedToRestore |
| 90 | + } |
| 91 | + case let .failure(error): |
| 92 | + operationResult = .error(error.localizedDescription) |
| 93 | + } |
| 94 | + showResult = true |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private var removeAllDataButton: some View { |
| 99 | + Button("Delete all data", role: .destructive) { |
| 100 | + showDeleteDataConfirmation.toggle() |
| 101 | + } |
| 102 | + .accessibilityIdentifier("removeAllDataButton") |
| 103 | + .transition(.slide.combined(with: .scale).combined(with: .opacity)) |
| 104 | + .confirmationDialog( |
| 105 | + "Do you want to delete all data permanently?", |
| 106 | + isPresented: $showDeleteDataConfirmation, |
| 107 | + titleVisibility: .visible |
| 108 | + ) { |
| 109 | + Button("Delete", role: .destructive) { |
| 110 | + do { |
| 111 | + try modelContext.delete(model: Item.self) |
| 112 | + } catch { |
| 113 | + assertionFailure(error.localizedDescription) |
| 114 | + operationResult = .error(error.localizedDescription) |
| 115 | + showResult = true |
| 116 | + } |
| 117 | + } |
| 118 | + .accessibilityIdentifier("confirmRemoveAllDataButton") |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +extension AppDataScreen { |
| 124 | + private enum OperationResult: Equatable { |
| 125 | + case backupSuccess |
| 126 | + case restoreSuccess |
| 127 | + case failedToRestore |
| 128 | + case error(String) |
| 129 | + |
| 130 | + var title: LocalizedStringKey { |
| 131 | + switch self { |
| 132 | + case .backupSuccess, .restoreSuccess: "Done" |
| 133 | + case .failedToRestore, .error: "Error" |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + var message: LocalizedStringKey { |
| 138 | + switch self { |
| 139 | + case .backupSuccess: "Backup data saved" |
| 140 | + case .restoreSuccess: "Data restored from backup" |
| 141 | + case .failedToRestore: "Unable to recover data from the selected file" |
| 142 | + case let .error(message): .init(message) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +#if DEBUG |
| 149 | +#Preview { |
| 150 | + NavigationStack { |
| 151 | + AppDataScreen() |
| 152 | + .environment(AppSettings()) |
| 153 | + .modelContainer(PreviewModelContainer.make(with: Item.makeList())) |
| 154 | + } |
| 155 | +} |
| 156 | +#endif |
0 commit comments