support multiple servers

This commit is contained in:
Rasmus Krämer
2022-04-15 10:16:11 +02:00
parent 9a7b2e8172
commit 29fd29728a
8 changed files with 68 additions and 38 deletions
@@ -17,6 +17,11 @@ class ServerConnectionConfig: Object {
@Persisted var username: String
@Persisted var token: String
}
class ServerConnectionConfigActiveIndex: Object {
// This could overflow, but you really would have to try
@Persisted var index: Int?
}
func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
return Database.realmQueue.sync {
+2 -2
View File
@@ -164,11 +164,11 @@ class AudioPlayer: NSObject {
// MARK: - Private
private func createAsset() -> AVAsset {
let headers: [String: String] = [
"Authorization": "Bearer \(Store.serverConfig.token)"
"Authorization": "Bearer \(Store.serverConfig!.token)"
]
debugPrint(activeAudioTrack)
return AVURLAsset(url: URL(string: "\(Store.serverConfig.address)\(activeAudioTrack.contentUrl)")!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
return AVURLAsset(url: URL(string: "\(Store.serverConfig!.address)\(activeAudioTrack.contentUrl)")!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
}
private func initAudioSession() {
do {
+4 -4
View File
@@ -24,10 +24,10 @@ class ApiClient {
public static func postResource<T: Decodable>(endpoint: String, parameters: [String: String], decodable: T.Type = T.self, callback: ((_ param: T) -> Void)?) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(Store.serverConfig.token)"
"Authorization": "Bearer \(Store.serverConfig!.token)"
]
AF.request("\(Store.serverConfig.address)/\(endpoint)", method: .post, parameters: parameters, encoder: JSONParameterEncoder.default, headers: headers).responseDecodable(of: decodable) { response in
AF.request("\(Store.serverConfig!.address)/\(endpoint)", method: .post, parameters: parameters, encoder: JSONParameterEncoder.default, headers: headers).responseDecodable(of: decodable) { response in
switch response.result {
case .success(let obj):
callback?(obj)
@@ -50,8 +50,8 @@ class ApiClient {
], decodable: PlaybackSession.self) { obj in
var session = obj
session.serverConnectionConfigId = Store.serverConfig.id
session.serverAddress = Store.serverConfig.address
session.serverConnectionConfigId = Store.serverConfig!.id
session.serverAddress = Store.serverConfig!.address
callback(session)
}
+39 -12
View File
@@ -9,23 +9,12 @@ import Foundation
import RealmSwift
class Database {
public static let realmQueue = DispatchQueue(label: "realm-queue")
// All DB releated actions must be executed on "realm-queue"
public static let realmQueue = DispatchQueue(label: "realm-queue")
private static var instance: Realm = {
// TODO: handle thread errors
try! Realm(queue: realmQueue)
}()
public static func getActiveServerConfigIndex() -> Int {
return realmQueue.sync {
guard let config = instance.objects(ServerConnectionConfig.self).first else {
return -1
}
return config.index
}
}
public static func setServerConnectionConfig(config: ServerConnectionConfig) {
var refrence: ThreadSafeReference<ServerConnectionConfig>?
if config.realm != nil {
@@ -35,6 +24,16 @@ class Database {
realmQueue.sync {
let existing: ServerConnectionConfig? = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
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 {
@@ -54,6 +53,8 @@ class Database {
NSLog("failed to save server config")
debugPrint(exception)
}
setLastActiveConfigIndex(index: config.index)
}
}
public static func getServerConnectionConfigs() -> [ServerConnectionConfig] {
@@ -78,4 +79,30 @@ class Database {
return []
}
}
public static func setLastActiveConfigIndexToNil() {
realmQueue.sync {
setLastActiveConfigIndex(index: nil)
}
}
public static func setLastActiveConfigIndex(index: Int?) {
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)
}
}
public static func getLastActiveConfigIndex() -> Int? {
return realmQueue.sync {
return instance.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil
}
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ class NowPlayingInfo {
}
*/
guard let url = URL(string: "\(Store.serverConfig.address)/api/items/\(metadata.itemId)/cover?token=\(Store.serverConfig.token)") else {
guard let url = URL(string: "\(Store.serverConfig!.address)/api/items/\(metadata.itemId)/cover?token=\(Store.serverConfig!.token)") else {
return
}
+8 -11
View File
@@ -10,20 +10,17 @@ import RealmSwift
class Store {
@ThreadSafe private static var _serverConfig: ServerConnectionConfig?
public static var serverConfig: ServerConnectionConfig {
public static var serverConfig: ServerConnectionConfig? {
get {
if _serverConfig == nil {
let index = Database.getActiveServerConfigIndex()
// TODO: change this when multiple configs are possible
_serverConfig = Database.getServerConnectionConfigs().first { config in
return config.index == index
}
}
return _serverConfig ?? ServerConnectionConfig()
return _serverConfig
}
set(updated) {
Database.setServerConnectionConfig(config: updated)
if updated != nil {
Database.setServerConnectionConfig(config: updated!)
} else {
Database.setLastActiveConfigIndexToNil()
}
_serverConfig = nil
}
}