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) {
|
2022-08-01 09:40:28 -04:00
|
|
|
var config = config
|
2022-05-03 12:55:13 +02:00
|
|
|
Database.realmQueue.sync {
|
2022-08-01 09:40:28 -04:00
|
|
|
var existing: ServerConnectionConfig? = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
2022-04-14 11:54:53 +02:00
|
|
|
|
2022-04-15 10:16:11 +02:00
|
|
|
if config.index == 0 {
|
2022-08-01 09:40:28 -04:00
|
|
|
var lastConfig: ServerConnectionConfig? = instance.objects(ServerConnectionConfig.self).last
|
2022-04-15 10:16:11 +02:00
|
|
|
|
|
|
|
if lastConfig != nil {
|
|
|
|
config.index = lastConfig!.index + 1
|
|
|
|
} else {
|
|
|
|
config.index = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:54:53 +02:00
|
|
|
do {
|
|
|
|
try instance.write {
|
|
|
|
if existing != nil {
|
|
|
|
instance.delete(existing!)
|
|
|
|
}
|
2022-08-01 09:40:28 -04:00
|
|
|
instance.add(config)
|
2022-04-14 11:54:53 +02:00
|
|
|
}
|
|
|
|
} 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 {
|
2022-04-15 13:01:58 +02:00
|
|
|
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] {
|
|
|
|
Database.realmQueue.sync {
|
2022-08-01 09:40:28 -04:00
|
|
|
return Array(instance.objects(ServerConnectionConfig.self))
|
2022-04-11 18:31:14 +02:00
|
|
|
}
|
2022-04-11 16:29:19 +02:00
|
|
|
}
|
2022-04-15 10:16:11 +02:00
|
|
|
|
2022-08-02 17:32:40 -04:00
|
|
|
public func setLastActiveConfigIndexToNil() {
|
|
|
|
Database.realmQueue.sync {
|
|
|
|
setLastActiveConfigIndex(index: nil)
|
|
|
|
}
|
2022-04-15 10:16:11 +02:00
|
|
|
}
|
2022-07-06 10:09:17 -04:00
|
|
|
|
2022-08-02 17:10:45 -04:00
|
|
|
private func setLastActiveConfigIndex(index: Int?) {
|
2022-04-15 10:16:11 +02:00
|
|
|
do {
|
|
|
|
try instance.write {
|
2022-08-02 17:10:45 -04:00
|
|
|
var existing = instance.objects(ServerConnectionConfigActiveIndex.self).last ?? ServerConnectionConfigActiveIndex(index: index)
|
|
|
|
existing.index = index
|
|
|
|
instance.add(existing, update: .modified)
|
2022-04-15 10:16:11 +02:00
|
|
|
}
|
|
|
|
} 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
|
|
|
|
2022-07-02 18:29:41 -05: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] {
|
|
|
|
Database.realmQueue.sync {
|
2022-08-01 09:40:28 -04:00
|
|
|
Array(instance.objects(LocalLibraryItem.self))
|
2022-07-06 10:09:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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? {
|
2022-08-01 09:40:28 -04:00
|
|
|
Database.realmQueue.sync {
|
|
|
|
instance.object(ofType: LocalLibraryItem.self, forPrimaryKey: localLibraryItem)
|
2022-07-30 18:25:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 10:09:17 -04:00
|
|
|
public func saveLocalLibraryItem(localLibraryItem: LocalLibraryItem) {
|
|
|
|
Database.realmQueue.sync {
|
2022-08-01 09:40:28 -04:00
|
|
|
try! instance.write { instance.add(localLibraryItem) }
|
2022-07-02 18:29:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-09 15:18:45 -04:00
|
|
|
|
2022-08-06 10:11:46 -04:00
|
|
|
public func getDownloadItem(downloadItemId: String) -> DownloadItem? {
|
|
|
|
Database.realmQueue.sync {
|
|
|
|
instance.object(ofType: DownloadItem.self, forPrimaryKey: downloadItemId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func getDownloadItem(libraryItemId: String) -> DownloadItem? {
|
|
|
|
Database.realmQueue.sync {
|
|
|
|
instance.objects(DownloadItem.self).filter("libraryItemId == %@", libraryItemId).first
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func getDownloadItem(downloadItemPartId: String) -> DownloadItem? {
|
|
|
|
Database.realmQueue.sync {
|
|
|
|
instance.objects(DownloadItem.self).filter("SUBQUERY(downloadItemParts, $part, $part.id == %@) .@count > 0", downloadItemPartId).first
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func saveDownloadItem(_ downloadItem: DownloadItem) {
|
|
|
|
Database.realmQueue.sync {
|
|
|
|
try! instance.write { instance.add(downloadItem) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:31:59 -04:00
|
|
|
public func updateDownloadItemPart(_ part: DownloadItemPart) {
|
2022-08-06 18:21:11 -04:00
|
|
|
Database.realmQueue.sync {
|
|
|
|
try! instance.write {
|
|
|
|
instance.add(part, update: .modified)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:27:08 -04:00
|
|
|
public func removeDownloadItem(_ downloadItem: DownloadItem) {
|
|
|
|
Database.realmQueue.sync {
|
|
|
|
try! instance.write { instance.delete(downloadItem) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 18:29:41 -05: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) {
|
|
|
|
Database.realmQueue.sync {
|
2022-08-01 09:40:28 -04:00
|
|
|
try! instance.write {
|
|
|
|
let item = getLocalLibraryItemByLLId(libraryItem: localLibraryItemId)
|
|
|
|
instance.delete(item!)
|
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
|
|
|
}
|