Initial work

This commit is contained in:
Marcos Carvalho 2024-05-23 22:33:06 +01:00
parent f047f6a23b
commit d788623509
No known key found for this signature in database
9 changed files with 214 additions and 44 deletions

View file

@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Override point for customization after application launch.
let configuration = Realm.Configuration(
schemaVersion: 17,
schemaVersion: 18,
migrationBlock: { [weak self] migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
self?.logger.log("Realm schema version was \(oldSchemaVersion)")
@ -54,6 +54,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
newObject?["chapterTrack"] = false
}
}
if (oldSchemaVersion < 17) {
self?.logger.log("Realm schema version was \(oldSchemaVersion)... Adding downloadUsingCellular and streamingUsingCellular settings")
migration.enumerateObjects(ofType: PlayerSettings.className()) { oldObject, newObject in
newObject?["downloadUsingCellular"] = "ALWAYS"
newObject?["streamingUsingCellular"] = "ALWAYS"
}
}
)

View file

@ -29,20 +29,20 @@ extension String {
@objc(AbsDatabase)
public class AbsDatabase: CAPPlugin {
private let logger = AppLogger(category: "AbsDatabase")
@objc func setCurrentServerConnectionConfig(_ call: CAPPluginCall) {
var id = call.getString("id")
let address = call.getString("address", "")
let userId = call.getString("userId", "")
let username = call.getString("username", "")
let token = call.getString("token", "")
let name = "\(address) (\(username))"
if id == nil {
id = "\(address)@\(username)".toBase64()
}
let config = ServerConnectionConfig()
config.id = id ?? ""
config.index = 0
@ -51,7 +51,7 @@ public class AbsDatabase: CAPPlugin {
config.userId = userId
config.username = username
config.token = token
Store.serverConfig = config
let savedConfig = Store.serverConfig // Fetch the latest value
call.resolve(convertServerConnectionConfigToJSON(config: savedConfig!))
@ -59,26 +59,26 @@ public class AbsDatabase: CAPPlugin {
@objc func removeServerConnectionConfig(_ call: CAPPluginCall) {
let id = call.getString("serverConnectionConfigId", "")
Database.shared.deleteServerConnectionConfig(id: id)
call.resolve()
}
@objc func logout(_ call: CAPPluginCall) {
Store.serverConfig = nil
call.resolve()
}
@objc func getDeviceData(_ call: CAPPluginCall) {
let configs = Database.shared.getServerConnectionConfigs()
let index = Database.shared.getLastActiveConfigIndex()
let settings = Database.shared.getDeviceSettings()
call.resolve([
"serverConnectionConfigs": configs.map { config in convertServerConnectionConfigToJSON(config: config) },
"lastServerConnectionConfigId": configs.first { config in config.index == index }?.id as Any,
"deviceSettings": deviceSettingsToJSON(settings: settings)
])
}
@objc func getLocalLibraryItems(_ call: CAPPluginCall) {
do {
let items = Database.shared.getLocalLibraryItems()
@ -89,7 +89,7 @@ public class AbsDatabase: CAPPlugin {
call.resolve()
}
}
@objc func getLocalLibraryItem(_ call: CAPPluginCall) {
do {
let item = Database.shared.getLocalLibraryItem(localLibraryItemId: call.getString("id") ?? "")
@ -105,7 +105,7 @@ public class AbsDatabase: CAPPlugin {
call.resolve()
}
}
@objc func getLocalLibraryItemByLId(_ call: CAPPluginCall) {
do {
let item = Database.shared.getLocalLibraryItem(byServerLibraryItemId: call.getString("libraryItemId") ?? "")
@ -121,11 +121,11 @@ public class AbsDatabase: CAPPlugin {
call.resolve()
}
}
@objc func getLocalLibraryItemsInFolder(_ call: CAPPluginCall) {
call.resolve([ "value": [] ])
}
@objc func getAllLocalMediaProgress(_ call: CAPPluginCall) {
do {
call.resolve([ "value": try Database.shared.getAllLocalMediaProgress().asDictionaryArray() ])
@ -135,7 +135,7 @@ public class AbsDatabase: CAPPlugin {
call.resolve(["value": []])
}
}
@objc func removeLocalMediaProgress(_ call: CAPPluginCall) {
let localMediaProgressId = call.getString("localMediaProgressId")
guard let localMediaProgressId = localMediaProgressId else {
@ -145,7 +145,7 @@ public class AbsDatabase: CAPPlugin {
try? Database.shared.removeLocalMediaProgress(localMediaProgressId: localMediaProgressId)
call.resolve()
}
@objc func syncLocalSessionsWithServer(_ call: CAPPluginCall) {
let isFirstSync = call.getBool("isFirstSync", false)
logger.log("syncLocalSessionsWithServer: Starting (First sync: \(isFirstSync))")
@ -153,19 +153,19 @@ public class AbsDatabase: CAPPlugin {
call.reject("syncLocalSessionsWithServer not connected to server")
return call.resolve()
}
Task {
await ApiClient.syncLocalSessionsWithServer(isFirstSync: isFirstSync)
call.resolve()
}
}
@objc func syncServerMediaProgressWithLocalMediaProgress(_ call: CAPPluginCall) {
let serverMediaProgress = call.getJson("mediaProgress", type: MediaProgress.self)
let localLibraryItemId = call.getString("localLibraryItemId")
let localEpisodeId = call.getString("localEpisodeId")
let localMediaProgressId = call.getString("localMediaProgressId")
do {
guard let serverMediaProgress = serverMediaProgress else {
return call.reject("serverMediaProgress not specified")
@ -173,35 +173,35 @@ public class AbsDatabase: CAPPlugin {
guard localLibraryItemId != nil || localMediaProgressId != nil else {
return call.reject("localLibraryItemId or localMediaProgressId must be specified")
}
let localMediaProgress = try LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localMediaProgressId, localLibraryItemId: localLibraryItemId, localEpisodeId: localEpisodeId)
guard let localMediaProgress = localMediaProgress else {
call.reject("Local media progress not found or created")
return
}
logger.log("syncServerMediaProgressWithLocalMediaProgress: Saving local media progress")
try localMediaProgress.updateFromServerMediaProgress(serverMediaProgress)
call.resolve(try localMediaProgress.asDictionary())
} catch {
call.reject("Failed to sync media progress")
debugPrint(error)
}
}
@objc func updateLocalMediaProgressFinished(_ call: CAPPluginCall) {
let localLibraryItemId = call.getString("localLibraryItemId")
let localEpisodeId = call.getString("localEpisodeId")
let isFinished = call.getBool("isFinished", false)
var localMediaProgressId = localLibraryItemId ?? ""
if localEpisodeId != nil {
localMediaProgressId += "-\(localEpisodeId ?? "")"
}
logger.log("updateLocalMediaProgressFinished \(localMediaProgressId) | Is Finished: \(isFinished)")
do {
let localMediaProgress = try LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localMediaProgressId, localLibraryItemId: localLibraryItemId, localEpisodeId: localEpisodeId)
guard let localMediaProgress = localMediaProgress else {
@ -211,11 +211,11 @@ public class AbsDatabase: CAPPlugin {
// Update finished status
try localMediaProgress.updateIsFinished(isFinished)
// Build API response
let progressDictionary = try? localMediaProgress.asDictionary()
var response: [String: Any] = ["local": true, "server": false, "localMediaProgress": progressDictionary ?? ""]
// Send update to the server if logged in
let hasLinkedServer = localMediaProgress.serverConnectionConfigId != nil
let loggedIntoServer = Store.serverConfig?.id == localMediaProgress.serverConnectionConfigId
@ -234,7 +234,7 @@ public class AbsDatabase: CAPPlugin {
return
}
}
@objc func updateDeviceSettings(_ call: CAPPluginCall) {
let disableAutoRewind = call.getBool("disableAutoRewind") ?? false
let enableAltView = call.getBool("enableAltView") ?? false
@ -244,6 +244,8 @@ public class AbsDatabase: CAPPlugin {
let lockOrientation = call.getString("lockOrientation") ?? "NONE"
let hapticFeedback = call.getString("hapticFeedback") ?? "LIGHT"
let languageCode = call.getString("languageCode") ?? "en-us"
let downloadUsingCellular = call.getString("downloadUsingCellular") ?? "ALWAYS"
let streamingUsingCellular = call.getString("downloadUsingCellular") ?? "ALWAYS"
let settings = DeviceSettings()
settings.disableAutoRewind = disableAutoRewind
settings.enableAltView = enableAltView
@ -253,22 +255,24 @@ public class AbsDatabase: CAPPlugin {
settings.lockOrientation = lockOrientation
settings.hapticFeedback = hapticFeedback
settings.languageCode = languageCode
settings.downloadUsingCellular = downloadUsingCellular
settings.streamingUsingCellular = streamingUsingCellular
Database.shared.setDeviceSettings(deviceSettings: settings)
// Updates the media notification controls (for allowSeekingOnMediaControls setting)
PlayerHandler.updateRemoteTransportControls()
getDeviceData(call)
}
@objc func updateLocalEbookProgress(_ call: CAPPluginCall) {
let localLibraryItemId = call.getString("localLibraryItemId")
let ebookLocation = call.getString("ebookLocation", "")
let ebookProgress = call.getDouble("ebookProgress", 0.0)
logger.log("updateLocalEbookProgress \(localLibraryItemId ?? "Unknown") | ebookLocation: \(ebookLocation) | ebookProgress: \(ebookProgress)")
do {
let localMediaProgress = try LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localLibraryItemId, localLibraryItemId: localLibraryItemId, localEpisodeId: nil)
guard let localMediaProgress = localMediaProgress else {
@ -278,7 +282,7 @@ public class AbsDatabase: CAPPlugin {
// Update finished status
try localMediaProgress.updateEbookProgress(ebookLocation: ebookLocation, ebookProgress: ebookProgress)
// Build API response
let progressDictionary = try? localMediaProgress.asDictionary()
let response: [String: Any] = ["localMediaProgress": progressDictionary ?? ""]

View file

@ -17,6 +17,8 @@ class DeviceSettings: Object {
@Persisted var lockOrientation: String = "NONE"
@Persisted var hapticFeedback: String = "LIGHT"
@Persisted var languageCode: String = "en-us"
@Persisted var downloadUsingCellular: String = "ALWAYS"
@Persisted var streamingUsingCellular: String = "ALWAYS"
}
func getDefaultDeviceSettings() -> DeviceSettings {
@ -32,6 +34,8 @@ func deviceSettingsToJSON(settings: DeviceSettings) -> Dictionary<String, Any> {
"jumpForwardTime": settings.jumpForwardTime,
"lockOrientation": settings.lockOrientation,
"hapticFeedback": settings.hapticFeedback,
"languageCode": settings.languageCode
"languageCode": settings.languageCode,
"downloadUsingCellular": settings.downloadUsingCellular,
"streamingUsingCellular": settings.streamingUsingCellular
]
}