-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters