Handle deleting library items

This commit is contained in:
ronaldheft 2022-08-11 14:12:45 -04:00
parent 8edeefc1cd
commit 60cb5f398c
7 changed files with 40 additions and 22 deletions

View file

@ -44,7 +44,7 @@ public class AbsAudioPlayer: CAPPlugin {
let isLocalItem = libraryItemId?.starts(with: "local_") ?? false
if (isLocalItem) {
let item = Database.shared.getLocalLibraryItem(localLibraryItem: libraryItemId!)
let item = Database.shared.getLocalLibraryItem(localLibraryItemId: libraryItemId!)
// TODO: Logic required for podcasts here
let playbackSession = item?.getPlaybackSession(episode: nil)
PlayerHandler.startPlayback(session: playbackSession!, playWhenReady: playWhenReady, playbackRate: playbackRate)

View file

@ -81,7 +81,7 @@ public class AbsDatabase: CAPPlugin {
@objc func getLocalLibraryItem(_ call: CAPPluginCall) {
do {
let item = Database.shared.getLocalLibraryItem(localLibraryItem: call.getString("id") ?? "")
let item = Database.shared.getLocalLibraryItem(localLibraryItemId: call.getString("id") ?? "")
switch item {
case .some(let foundItem):
call.resolve(try foundItem.asDictionary())
@ -97,7 +97,7 @@ public class AbsDatabase: CAPPlugin {
@objc func getLocalLibraryItemByLId(_ call: CAPPluginCall) {
do {
let item = Database.shared.getLocalLibraryItemByLLId(libraryItem: call.getString("libraryItemId") ?? "")
let item = Database.shared.getLocalLibraryItem(byServerLibraryItemId: call.getString("libraryItemId") ?? "")
switch item {
case .some(let foundItem):
call.resolve(try foundItem.asDictionary())

View file

@ -157,7 +157,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
}
return LocalFile(libraryItem.id, part.filename!, part.mimeType()!, part.destinationUri!, fileSize: Int(part.destinationURL!.fileSize))
}
var localLibraryItem = Database.shared.getLocalLibraryItemByLLId(libraryItem: libraryItem.id)
var localLibraryItem = Database.shared.getLocalLibraryItem(byServerLibraryItemId: libraryItem.id)
if (localLibraryItem != nil && localLibraryItem!.isPodcast) {
try! localLibraryItem?.addFiles(files, item: libraryItem)
} else {

View file

@ -67,13 +67,23 @@ public class AbsFileSystem: CAPPlugin {
}
@objc func deleteItem(_ call: CAPPluginCall) {
let localLibraryItemId = call.getString("localLibraryItemId")
let localLibraryItemId = call.getString("id")
let contentUrl = call.getString("contentUrl")
// TODO: Implement
NSLog("deleteItem \(localLibraryItemId ?? "UNSET") url \(contentUrl ?? "UNSET")")
call.resolve()
var success = false
do {
if let localLibraryItemId = localLibraryItemId, let item = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) {
try FileManager.default.removeItem(at: item.contentDirectory!)
Database.shared.removeLocalLibraryItem(localLibraryItemId: localLibraryItemId)
success = true
}
} catch {
NSLog("Failed to delete \(error)")
}
call.resolve(["success": success])
}
@objc func deleteTrackFromItem(_ call: CAPPluginCall) {

View file

@ -31,6 +31,14 @@ struct LocalLibraryItem: Realmable, Codable {
}
}
var contentDirectory: URL? {
if let path = _contentUrl {
return AbsDownloader.itemDownloadFolder(path: path)
} else {
return nil
}
}
var coverContentUrl: String? {
if let path = self._coverContentUrl {
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString

View file

@ -108,14 +108,14 @@ class Database {
return Array(realm.objects(LocalLibraryItem.self))
}
public func getLocalLibraryItemByLLId(libraryItem: String) -> LocalLibraryItem? {
public func getLocalLibraryItem(byServerLibraryItemId: String) -> LocalLibraryItem? {
let realm = try! Realm()
return realm.objects(LocalLibraryItem.self).first(where: { $0.libraryItemId == libraryItem })
return realm.objects(LocalLibraryItem.self).first(where: { $0.libraryItemId == byServerLibraryItemId })
}
public func getLocalLibraryItem(localLibraryItem: String) -> LocalLibraryItem? {
public func getLocalLibraryItem(localLibraryItemId: String) -> LocalLibraryItem? {
let realm = try! Realm()
return realm.object(ofType: LocalLibraryItem.self, forPrimaryKey: localLibraryItem)
return realm.object(ofType: LocalLibraryItem.self, forPrimaryKey: localLibraryItemId)
}
public func saveLocalLibraryItem(localLibraryItem: LocalLibraryItem) {
@ -123,6 +123,14 @@ class Database {
try! realm.write { realm.add(localLibraryItem, update: .modified) }
}
public func removeLocalLibraryItem(localLibraryItemId: String) {
let realm = try! Realm()
try! realm.write {
let item = getLocalLibraryItem(localLibraryItemId: localLibraryItemId)
realm.delete(item!)
}
}
public func getDownloadItem(downloadItemId: String) -> DownloadItem? {
let realm = try! Realm()
return realm.object(ofType: DownloadItem.self, forPrimaryKey: downloadItemId)
@ -158,14 +166,6 @@ class Database {
return realm.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
}
public func removeLocalLibraryItem(localLibraryItemId: String) {
let realm = try! Realm()
try! realm.write {
let item = getLocalLibraryItemByLLId(libraryItem: localLibraryItemId)
realm.delete(item!)
}
}
public func saveLocalMediaProgress(_ mediaProgress: LocalMediaProgress) {
let realm = try! Realm()
try! realm.write { realm.add(mediaProgress, update: .modified) }

View file

@ -322,13 +322,13 @@ export default {
async deleteItem() {
const { value } = await Dialog.confirm({
title: 'Confirm',
message: `Warning! This will delete the folder "${this.basePath}" and all contents. Are you sure?`
message: `Warning! This will delete "${this.media.metadata.title}" and all associated local files. Are you sure?`
})
if (value) {
var res = await AbsFileSystem.deleteItem(this.localLibraryItem)
if (res && res.success) {
this.$toast.success('Deleted Successfully')
this.$router.replace(`/localMedia/folders/${this.folderId}`)
this.$router.replace(this.isIos ? '/bookshelf' : `/localMedia/folders/${this.folderId}`)
} else this.$toast.error('Failed to delete')
}
},