advplyr.audiobookshelf-app/ios/App/Shared/util/Database.swift

255 lines
8.2 KiB
Swift
Raw Normal View History

2022-04-11 16:29:19 +02:00
//
// Database.swift
// App
//
// Created by Rasmus Krämer on 11.04.22.
//
import Foundation
import RealmSwift
class Database {
2022-04-11 18:50:25 +02:00
// All DB releated actions must be executed on "realm-queue"
2022-05-03 12:55:13 +02:00
public static let realmQueue: DispatchQueue = DispatchQueue(label: "realm-queue")
public static var shared = {
realmQueue.sync {
return Database()
}
}()
private var instance: Realm
private init() {
self.instance = try! Realm(queue: Database.realmQueue)
}
2022-04-11 18:50:25 +02:00
2022-05-03 12:55:13 +02:00
public func setServerConnectionConfig(config: ServerConnectionConfig) {
var refrence: ThreadSafeReference<ServerConnectionConfig>?
if config.realm != nil {
refrence = ThreadSafeReference(to: config)
}
2022-04-11 16:29:19 +02:00
2022-05-03 12:55:13 +02:00
Database.realmQueue.sync {
let existing: ServerConnectionConfig? = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
2022-04-15 10:16:11 +02:00
if config.index == 0 {
let lastConfig: ServerConnectionConfig? = instance.objects(ServerConnectionConfig.self).last
if lastConfig != nil {
config.index = lastConfig!.index + 1
} else {
config.index = 1
}
}
do {
try instance.write {
if existing != nil {
instance.delete(existing!)
}
if refrence == nil {
instance.add(config)
} else {
guard let resolved = instance.resolve(refrence!) else {
throw "unable to resolve refrence"
}
instance.add(resolved);
}
}
} catch(let exception) {
NSLog("failed to save server config")
debugPrint(exception)
2022-04-11 18:31:14 +02:00
}
2022-04-15 10:16:11 +02:00
setLastActiveConfigIndex(index: config.index)
2022-04-11 16:29:19 +02:00
}
}
2022-07-06 10:09:17 -04:00
2022-05-03 12:55:13 +02:00
public func deleteServerConnectionConfig(id: String) {
Database.realmQueue.sync {
let config = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: id)
do {
try instance.write {
if config != nil {
instance.delete(config!)
}
}
} catch(let exception) {
NSLog("failed to delete server config")
debugPrint(exception)
}
}
}
2022-07-06 10:09:17 -04:00
2022-05-03 12:55:13 +02:00
public func getServerConnectionConfigs() -> [ServerConnectionConfig] {
var refrences: [ThreadSafeReference<ServerConnectionConfig>] = []
2022-04-11 16:29:19 +02:00
2022-05-03 12:55:13 +02:00
Database.realmQueue.sync {
let configs = instance.objects(ServerConnectionConfig.self)
refrences = configs.map { config in
return ThreadSafeReference(to: config)
}
}
do {
let realm = try Realm()
return refrences.map { refrence in
return realm.resolve(refrence)!
}
} catch(let exception) {
NSLog("error while readling configs")
debugPrint(exception)
2022-04-11 18:31:14 +02:00
return []
}
2022-04-11 16:29:19 +02:00
}
2022-04-15 10:16:11 +02:00
2022-05-03 12:55:13 +02:00
public func setLastActiveConfigIndexToNil() {
Database.realmQueue.sync {
2022-04-15 10:16:11 +02:00
setLastActiveConfigIndex(index: nil)
}
}
2022-07-06 10:09:17 -04:00
2022-05-03 12:55:13 +02:00
public func setLastActiveConfigIndex(index: Int?) {
2022-04-15 10:16:11 +02:00
let existing = instance.objects(ServerConnectionConfigActiveIndex.self)
let obj = ServerConnectionConfigActiveIndex()
obj.index = index
do {
try instance.write {
instance.delete(existing)
instance.add(obj)
}
} catch(let exception) {
NSLog("failed to save server config active index")
debugPrint(exception)
}
}
2022-07-06 10:09:17 -04:00
2022-05-03 12:55:13 +02:00
public func getLastActiveConfigIndex() -> Int? {
return Database.realmQueue.sync {
2022-04-15 10:16:11 +02:00
return instance.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil
}
}
2022-07-09 15:18:45 -04:00
public func setDeviceSettings(deviceSettings: DeviceSettings) {
Database.realmQueue.sync {
let existing = instance.objects(DeviceSettings.self)
do {
try instance.write {
instance.delete(existing)
instance.add(deviceSettings)
}
} catch(let exception) {
NSLog("failed to save device settings")
2022-07-09 15:18:45 -04:00
}
}
}
2022-07-06 10:09:17 -04:00
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 {
2022-07-30 18:25:20 -04:00
if (item.libraryItemId == libraryItem) {
2022-07-06 10:09:17 -04:00
return item
}
}
NSLog("Local library item with id \(libraryItem) not found")
return nil
}
2022-07-30 18:25:20 -04:00
public func getLocalLibraryItem(localLibraryItem: String) -> LocalLibraryItem? {
let items = getLocalLibraryItems()
for item in items {
if (item.id == localLibraryItem) {
return item
}
}
NSLog("Local library item with id \(localLibraryItem) not found")
return nil
}
2022-07-06 10:09:17 -04:00
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)
}
}
}
2022-07-09 15:18:45 -04:00
public func getDeviceSettings() -> DeviceSettings {
return Database.realmQueue.sync {
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
}
}
2022-07-06 10:09:17 -04:00
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")
}
}
2022-07-09 15:18:45 -04:00
} catch (let exception) {
NSLog("Unable to delete local library item")
debugPrint(exception)
2022-07-06 10:09:17 -04:00
}
}
2022-07-09 15:18:45 -04:00
}
2022-07-31 13:33:36 -04:00
public func saveLocalMediaProgress(_ mediaProgress: LocalMediaProgress) {
Database.realmQueue.sync {
try! instance.write { instance.add(mediaProgress) }
}
}
// For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}"
public func getLocalMediaProgress(localMediaProgressId: String) -> LocalMediaProgress? {
Database.realmQueue.sync {
instance.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
}
}
public func removeLocalMediaProgress(localMediaProgressId: String) {
Database.realmQueue.sync {
try! instance.write {
let progress = instance.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
instance.delete(progress!)
}
}
}
2022-04-11 16:29:19 +02:00
}