Skip to content

Commit

Permalink
added data management (kinda)
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed Feb 5, 2025
1 parent f13a43f commit c1e296c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sora.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -44,6 +45,7 @@

/* Begin PBXFileReference section */
130C6BF82D53A4C200DC1432 /* Sora.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Sora.entitlements; sourceTree = "<group>"; };
130C6BF92D53AB1F00DC1432 /* SettingsViewData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewData.swift; sourceTree = "<group>"; };
131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewGeneral.swift; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -161,6 +163,7 @@
133D7C842D2BE2630075467E /* SettingsViewModule.swift */,
131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */,
135CCBE12D4D1138008B9C0E /* SettingsViewPlayer.swift */,
130C6BF92D53AB1F00DC1432 /* SettingsViewData.swift */,
);
path = SettingsSubViews;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
99 changes: 99 additions & 0 deletions Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
}
5 changes: 4 additions & 1 deletion Sora/Views/SettingsView/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit c1e296c

Please sign in to comment.