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-05-03 12:55:13 +02:00
|
|
|
public static var shared = {
|
2022-08-07 17:46:13 -04:00
|
|
|
return Database()
|
2022-05-03 12:55:13 +02:00
|
|
|
}()
|
|
|
|
|
2022-08-07 17:46:13 -04:00
|
|
|
private init() {}
|
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-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
let existing: ServerConnectionConfig? = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
|
|
|
|
|
|
|
if config.index == 0 {
|
|
|
|
let lastConfig: ServerConnectionConfig? = realm.objects(ServerConnectionConfig.self).last
|
2022-04-14 11:54:53 +02:00
|
|
|
|
2022-08-07 17:46:13 -04:00
|
|
|
if lastConfig != nil {
|
|
|
|
config.index = lastConfig!.index + 1
|
|
|
|
} else {
|
|
|
|
config.index = 1
|
2022-04-15 10:16:11 +02:00
|
|
|
}
|
2022-08-07 17:46:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
try realm.write {
|
|
|
|
if existing != nil {
|
|
|
|
realm.delete(existing!)
|
2022-04-14 11:54:53 +02:00
|
|
|
}
|
2022-08-07 17:46:13 -04:00
|
|
|
realm.add(config)
|
2022-04-11 18:31:14 +02:00
|
|
|
}
|
2022-08-07 17:46:13 -04:00
|
|
|
} catch(let exception) {
|
|
|
|
NSLog("failed to save server config")
|
|
|
|
debugPrint(exception)
|
2022-04-11 16:29:19 +02:00
|
|
|
}
|
2022-08-07 17:46:13 -04: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) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
let config = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: id)
|
|
|
|
|
|
|
|
do {
|
|
|
|
try realm.write {
|
|
|
|
if config != nil {
|
|
|
|
realm.delete(config!)
|
2022-04-15 13:01:58 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-07 17:46:13 -04:00
|
|
|
} catch(let exception) {
|
|
|
|
NSLog("failed to delete server config")
|
|
|
|
debugPrint(exception)
|
2022-04-15 13:01:58 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 10:09:17 -04:00
|
|
|
|
2022-05-03 12:55:13 +02:00
|
|
|
public func getServerConnectionConfigs() -> [ServerConnectionConfig] {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return Array(realm.objects(ServerConnectionConfig.self))
|
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() {
|
2022-08-07 17:46:13 -04:00
|
|
|
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-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
2022-04-15 10:16:11 +02:00
|
|
|
do {
|
2022-08-07 17:46:13 -04:00
|
|
|
try realm.write {
|
|
|
|
var existing = realm.objects(ServerConnectionConfigActiveIndex.self).last ?? ServerConnectionConfigActiveIndex(index: index)
|
2022-08-02 17:10:45 -04:00
|
|
|
existing.index = index
|
2022-08-07 17:46:13 -04:00
|
|
|
realm.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? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil
|
2022-04-15 10:16:11 +02:00
|
|
|
}
|
2022-07-09 15:18:45 -04:00
|
|
|
|
2022-07-02 18:29:41 -05:00
|
|
|
public func setDeviceSettings(deviceSettings: DeviceSettings) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
let existing = realm.objects(DeviceSettings.self)
|
2022-07-02 18:29:41 -05:00
|
|
|
|
2022-08-07 17:46:13 -04:00
|
|
|
do {
|
|
|
|
try realm.write {
|
|
|
|
realm.delete(existing)
|
|
|
|
realm.add(deviceSettings)
|
2022-07-09 15:18:45 -04:00
|
|
|
}
|
2022-08-07 17:46:13 -04:00
|
|
|
} catch {
|
|
|
|
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] {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return Array(realm.objects(LocalLibraryItem.self))
|
2022-07-06 10:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public func getLocalLibraryItemByLLId(libraryItem: String) -> LocalLibraryItem? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.objects(LocalLibraryItem.self).first(where: { $0.libraryItemId == libraryItem })
|
2022-07-06 10:09:17 -04:00
|
|
|
}
|
|
|
|
|
2022-07-30 18:25:20 -04:00
|
|
|
public func getLocalLibraryItem(localLibraryItem: String) -> LocalLibraryItem? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.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) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
2022-08-07 21:02:25 -04:00
|
|
|
try! realm.write { realm.add(localLibraryItem, update: .modified) }
|
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? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.object(ofType: DownloadItem.self, forPrimaryKey: downloadItemId)
|
2022-08-06 10:11:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public func getDownloadItem(libraryItemId: String) -> DownloadItem? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.objects(DownloadItem.self).filter("libraryItemId == %@", libraryItemId).first
|
2022-08-06 10:11:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public func getDownloadItem(downloadItemPartId: String) -> DownloadItem? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.objects(DownloadItem.self).filter("SUBQUERY(downloadItemParts, $part, $part.id == %@) .@count > 0", downloadItemPartId).first
|
2022-08-06 10:11:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public func saveDownloadItem(_ downloadItem: DownloadItem) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
2022-08-07 21:02:25 -04:00
|
|
|
return try! realm.write { realm.add(downloadItem, update: .modified) }
|
2022-08-06 10:11:46 -04:00
|
|
|
}
|
|
|
|
|
2022-08-06 18:31:59 -04:00
|
|
|
public func updateDownloadItemPart(_ part: DownloadItemPart) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return try! realm.write { realm.add(part, update: .modified) }
|
2022-08-06 18:21:11 -04:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:27:08 -04:00
|
|
|
public func removeDownloadItem(_ downloadItem: DownloadItem) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return try! realm.write { realm.delete(downloadItem) }
|
2022-08-07 10:27:08 -04:00
|
|
|
}
|
|
|
|
|
2022-07-02 18:29:41 -05:00
|
|
|
public func getDeviceSettings() -> DeviceSettings {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
|
2022-07-02 18:29:41 -05:00
|
|
|
}
|
2022-07-06 10:09:17 -04:00
|
|
|
|
|
|
|
public func removeLocalLibraryItem(localLibraryItemId: String) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
try! realm.write {
|
|
|
|
let item = getLocalLibraryItemByLLId(libraryItem: localLibraryItemId)
|
|
|
|
realm.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) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
try! realm.write { realm.add(mediaProgress) }
|
2022-07-31 13:33:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}"
|
|
|
|
public func getLocalMediaProgress(localMediaProgressId: String) -> LocalMediaProgress? {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
return realm.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
|
2022-07-31 13:33:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public func removeLocalMediaProgress(localMediaProgressId: String) {
|
2022-08-07 17:46:13 -04:00
|
|
|
let realm = try! Realm()
|
|
|
|
try! realm.write {
|
|
|
|
let progress = realm.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
|
|
|
|
realm.delete(progress!)
|
2022-07-31 13:33:36 -04:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 16:29:19 +02:00
|
|
|
}
|