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

193 lines
6.5 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-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-10 17:09:49 -04:00
let 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-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-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-08-07 17:46:13 -04:00
} 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] {
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
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 {
2022-08-10 17:09:49 -04:00
let existing = realm.objects(ServerConnectionConfigActiveIndex.self).last
if ( existing?.index != index ) {
if let existing = existing {
realm.delete(existing)
}
let activeConfig = ServerConnectionConfigActiveIndex()
activeConfig.index = index
realm.add(activeConfig)
}
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
public func setDeviceSettings(deviceSettings: DeviceSettings) {
2022-08-07 17:46:13 -04:00
let realm = try! Realm()
let existing = realm.objects(DeviceSettings.self)
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
}
2022-08-11 14:12:45 -04:00
public func getLocalLibraryItem(byServerLibraryItemId: String) -> LocalLibraryItem? {
2022-08-07 17:46:13 -04:00
let realm = try! Realm()
2022-08-11 14:12:45 -04:00
return realm.objects(LocalLibraryItem.self).first(where: { $0.libraryItemId == byServerLibraryItemId })
2022-07-06 10:09:17 -04:00
}
2022-08-11 14:12:45 -04:00
public func getLocalLibraryItem(localLibraryItemId: String) -> LocalLibraryItem? {
2022-08-07 17:46:13 -04:00
let realm = try! Realm()
2022-08-11 14:12:45 -04:00
return realm.object(ofType: LocalLibraryItem.self, forPrimaryKey: localLibraryItemId)
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()
try! realm.write { realm.add(localLibraryItem, update: .modified) }
}
2022-07-09 15:18:45 -04:00
2022-08-11 18:29:55 -04:00
public func getLocalFile(localFileId: String) -> LocalFile? {
let realm = try! Realm()
return realm.object(ofType: LocalFile.self, forPrimaryKey: localFileId)
}
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)
}
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
}
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
}
public func saveDownloadItem(_ downloadItem: DownloadItem) {
2022-08-07 17:46:13 -04:00
let realm = try! Realm()
return try! realm.write { realm.add(downloadItem, update: .modified) }
}
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-06 10:09:17 -04:00
2022-08-11 16:26:32 -04:00
public func getAllLocalMediaProgress() -> [LocalMediaProgress] {
2022-08-07 17:46:13 -04:00
let realm = try! Realm()
2022-08-11 16:26:32 -04:00
return Array(realm.objects(LocalMediaProgress.self))
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, update: .modified) }
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-08-16 12:32:22 -04:00
public func getPlaybackSession(id: String) -> PlaybackSession? {
let realm = try! Realm()
return realm.object(ofType: PlaybackSession.self, forPrimaryKey: id)
}
2022-04-11 16:29:19 +02:00
}