feat: iOS download groundwork

This commit is contained in:
benonymity 2022-07-06 10:09:17 -04:00
parent e07e7f70d6
commit 2ca9ce797d
6 changed files with 368 additions and 120 deletions

View file

@ -64,6 +64,7 @@ class Database {
setLastActiveConfigIndex(index: config.index)
}
}
public func deleteServerConnectionConfig(id: String) {
Database.realmQueue.sync {
let config = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: id)
@ -80,6 +81,7 @@ class Database {
}
}
}
public func getServerConnectionConfigs() -> [ServerConnectionConfig] {
var refrences: [ThreadSafeReference<ServerConnectionConfig>] = []
@ -108,6 +110,7 @@ class Database {
setLastActiveConfigIndex(index: nil)
}
}
public func setLastActiveConfigIndex(index: Int?) {
let existing = instance.objects(ServerConnectionConfigActiveIndex.self)
let obj = ServerConnectionConfigActiveIndex()
@ -123,6 +126,7 @@ class Database {
debugPrint(exception)
}
}
public func getLastActiveConfigIndex() -> Int? {
return Database.realmQueue.sync {
return instance.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil
@ -139,6 +143,49 @@ class Database {
}
} catch(let exception) {
NSLog("failed to save device settings")
public func getLocalLibraryItems(mediaType: MediaType? = nil) -> [LocalLibraryItem] {
var localLibraryItems: [ThreadSafeReference<LocalLibraryItem>] = []
Database.realmQueue.sync {
let items = instance.objects(LocalLibraryItem.self)
localLibraryItems = items.map { item in
return ThreadSafeReference(to: item)
}
}
do {
let realm = try Realm()
return localLibraryItems.map { item in
return realm.resolve(item)!
}
} catch(let exception) {
NSLog("error while readling local library items")
debugPrint(exception)
return []
}
}
public func getLocalLibraryItemByLLId(libraryItem: String) -> LocalLibraryItem? {
let items = getLocalLibraryItems()
for item in items {
if (item.id == libraryItem) {
return item
}
}
NSLog("Local library item with id \(libraryItem) not found")
return nil
}
public func saveLocalLibraryItem(localLibraryItem: LocalLibraryItem) {
Database.realmQueue.sync {
do {
try instance.write {
instance.add(localLibraryItem);
}
} catch(let exception) {
NSLog("Unable to save local library item")
debugPrint(exception)
}
}
@ -148,4 +195,22 @@ class Database {
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
}
}
public func removeLocalLibraryItem(localLibraryItemId: String) {
let item = getLocalLibraryItemByLLId(libraryItem: localLibraryItemId)
Database.realmQueue.sync {
do {
try instance.write {
if item != nil {
instance.delete(item!)
} else {
NSLog("Unable to find local library item to delete")
}
}
} catch(let exception) {
NSLog("Unable to delete local library item")
debugPrint(exception)
}
}
}
}