From c1e296ce80d0333498e644204a8e48ea2906fc7c Mon Sep 17 00:00:00 2001 From: cranci1 <100066266+cranci1@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:48:49 +0100 Subject: [PATCH] added data management (kinda) --- Sora.xcodeproj/project.pbxproj | 4 + .../SettingsSubViews/SettingsViewData.swift | 99 +++++++++++++++++++ Sora/Views/SettingsView/SettingsView.swift | 5 +- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift diff --git a/Sora.xcodeproj/project.pbxproj b/Sora.xcodeproj/project.pbxproj index c01272c..605a0e1 100644 --- a/Sora.xcodeproj/project.pbxproj +++ b/Sora.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 130C6BFA2D53AB1F00DC1432 /* SettingsViewData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 130C6BF92D53AB1F00DC1432 /* SettingsViewData.swift */; }; 131845F92D47C62D00CA7A54 /* SettingsViewGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */; }; 133D7C6E2D2BE2500075467E /* SoraApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133D7C6D2D2BE2500075467E /* SoraApp.swift */; }; 133D7C702D2BE2500075467E /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133D7C6F2D2BE2500075467E /* ContentView.swift */; }; @@ -44,6 +45,7 @@ /* Begin PBXFileReference section */ 130C6BF82D53A4C200DC1432 /* Sora.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Sora.entitlements; sourceTree = ""; }; + 130C6BF92D53AB1F00DC1432 /* SettingsViewData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewData.swift; sourceTree = ""; }; 131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewGeneral.swift; sourceTree = ""; }; 133D7C6A2D2BE2500075467E /* Sora.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sora.app; sourceTree = BUILT_PRODUCTS_DIR; }; 133D7C6D2D2BE2500075467E /* SoraApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoraApp.swift; sourceTree = ""; }; @@ -161,6 +163,7 @@ 133D7C842D2BE2630075467E /* SettingsViewModule.swift */, 131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */, 135CCBE12D4D1138008B9C0E /* SettingsViewPlayer.swift */, + 130C6BF92D53AB1F00DC1432 /* SettingsViewData.swift */, ); path = SettingsSubViews; sourceTree = ""; @@ -373,6 +376,7 @@ 1399FAD62D3AB3DB00E97C31 /* Logger.swift in Sources */, 139935662D468C450065CEFF /* ModuleManager.swift in Sources */, 133D7C902D2BE2640075467E /* SettingsView.swift in Sources */, + 130C6BFA2D53AB1F00DC1432 /* SettingsViewData.swift in Sources */, 13EA2BD72D32D97400C1EBD7 /* MusicProgressSlider.swift in Sources */, 1E9FF1D32D403E49008AC100 /* SettingsViewLoggerFilter.swift in Sources */, 13EA2BD92D32D98400C1EBD7 /* NormalPlayer.swift in Sources */, diff --git a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift new file mode 100644 index 0000000..9fb75a0 --- /dev/null +++ b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift @@ -0,0 +1,99 @@ +// +// SettingsViewData.swift +// Sora +// +// Created by Francesco on 05/02/25. +// + +import SwiftUI + +struct SettingsViewData: View { + @State private var showEraseAppDataAlert = false + @State private var showRemoveDocumentsAlert = false + @State private var showSizeAlert = false + + var body: some View { + Form { + Section(header: Text("App storage")) { + Button(action: clearCache) { + Text("Clear Cache") + } + + Button(action: { + showEraseAppDataAlert = true + }) { + Text("Erase all App Data") + } + .alert(isPresented: $showEraseAppDataAlert) { + Alert( + title: Text("Confirm Erase App Data"), + message: Text("Are you sure you want to erase all app data? This action cannot be undone. (The app will then restart)"), + primaryButton: .destructive(Text("Erase")) { + eraseAppData() + }, + secondaryButton: .cancel() + ) + } + + Button(action: { + showRemoveDocumentsAlert = true + }) { + Text("Remove All Files in Documents") + } + .alert(isPresented: $showRemoveDocumentsAlert) { + Alert( + title: Text("Confirm Remove All Files"), + message: Text("Are you sure you want to remove all files in the documents folder? This will also remove all modules and you will lose the favorite items. This action cannot be undone. (The app will then restart)"), + primaryButton: .destructive(Text("Remove")) { + removeAllFilesInDocuments() + }, + secondaryButton: .cancel() + ) + } + } + .navigationTitle("App Data") + } + .navigationViewStyle(StackNavigationViewStyle()) + } + + func eraseAppData() { + if let domain = Bundle.main.bundleIdentifier { + UserDefaults.standard.removePersistentDomain(forName: domain) + UserDefaults.standard.synchronize() + Logger.shared.log("Cleared app data!", type: "General") + exit(0) + } + } + + func clearCache() { + let cacheURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first + + do { + if let cacheURL = cacheURL { + let filePaths = try FileManager.default.contentsOfDirectory(at: cacheURL, includingPropertiesForKeys: nil, options: []) + for filePath in filePaths { + try FileManager.default.removeItem(at: filePath) + } + Logger.shared.log("Cache cleared successfully!", type: "General") + } + } catch { + Logger.shared.log("Failed to clear cache.", type: "Error") + } + } + + func removeAllFilesInDocuments() { + let fileManager = FileManager.default + if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first { + do { + let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil) + for fileURL in fileURLs { + try fileManager.removeItem(at: fileURL) + } + Logger.shared.log("All files in documents folder removed", type: "General") + exit(0) + } catch { + Logger.shared.log("Error removing files in documents folder: \(error)", type: "Error") + } + } + } +} diff --git a/Sora/Views/SettingsView/SettingsView.swift b/Sora/Views/SettingsView/SettingsView.swift index 17b5f75..7ae1ba7 100644 --- a/Sora/Views/SettingsView/SettingsView.swift +++ b/Sora/Views/SettingsView/SettingsView.swift @@ -23,7 +23,10 @@ struct SettingsView: View { } } - Section(header: Text("Debug")) { + Section(header: Text("Info")) { + NavigationLink(destination: SettingsViewData()) { + Text("Data") + } NavigationLink(destination: SettingsViewLogger()) { Text("Logs") }