Improved error handling

This commit is contained in:
ronaldheft
2022-08-25 15:42:37 -04:00
parent 01678f2c91
commit 8c87b31e56
14 changed files with 220 additions and 141 deletions
+20 -15
View File
@@ -70,7 +70,7 @@ public class AbsFileSystem: CAPPlugin {
do {
if let localLibraryItemId = localLibraryItemId, let item = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) {
try FileManager.default.removeItem(at: item.contentDirectory!)
item.delete()
try item.delete()
success = true
}
} catch {
@@ -89,24 +89,29 @@ public class AbsFileSystem: CAPPlugin {
var success = false
if let localLibraryItemId = localLibraryItemId, let trackLocalFileId = trackLocalFileId, let item = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) {
item.update {
do {
if let fileIndex = item.localFiles.firstIndex(where: { $0.id == trackLocalFileId }) {
try FileManager.default.removeItem(at: item.localFiles[fileIndex].contentPath)
item.realm?.delete(item.localFiles[fileIndex])
if item.isPodcast, let media = item.media {
if let episodeIndex = media.episodes.firstIndex(where: { $0.audioTrack?.localFileId == trackLocalFileId }) {
media.episodes.remove(at: episodeIndex)
do {
try item.update {
do {
if let fileIndex = item.localFiles.firstIndex(where: { $0.id == trackLocalFileId }) {
try FileManager.default.removeItem(at: item.localFiles[fileIndex].contentPath)
item.realm?.delete(item.localFiles[fileIndex])
if item.isPodcast, let media = item.media {
if let episodeIndex = media.episodes.firstIndex(where: { $0.audioTrack?.localFileId == trackLocalFileId }) {
media.episodes.remove(at: episodeIndex)
}
item.media = media
}
item.media = media
call.resolve(try item.asDictionary())
success = true
}
call.resolve(try item.asDictionary())
success = true
} catch {
NSLog("Failed to delete \(error)")
success = false
}
} catch {
NSLog("Failed to delete \(error)")
success = false
}
} catch {
NSLog("Failed to delete \(error)")
success = false
}
}