advplyr.audiobookshelf-app/ios/App/App/plugins/AbsDatabase.swift

133 lines
4.5 KiB
Swift
Raw Normal View History

2022-04-11 16:29:19 +02:00
//
// AbsDatabase.swift
// App
//
// Created by Rasmus Krämer on 11.04.22.
//
import Foundation
import Capacitor
import RealmSwift
extension String {
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else {
return nil
}
return String(data: data, encoding: .utf8)
}
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
}
@objc(AbsDatabase)
public class AbsDatabase: CAPPlugin {
@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()
2022-04-11 16:29:19 +02:00
}
let config = ServerConnectionConfig(id: id!, index: 0, name: name, address: address, userId: userId, username: username, token: token)
Store.serverConfig = config
call.resolve(convertServerConnectionConfigToJSON(config: config))
2022-04-11 16:29:19 +02:00
}
@objc func removeServerConnectionConfig(_ call: CAPPluginCall) {
let id = call.getString("serverConnectionConfigId", "")
2022-05-03 12:55:13 +02:00
Database.shared.deleteServerConnectionConfig(id: id)
call.resolve()
}
2022-04-15 10:16:11 +02:00
@objc func logout(_ call: CAPPluginCall) {
Store.serverConfig = nil
call.resolve()
}
2022-04-11 16:29:19 +02:00
@objc func getDeviceData(_ call: CAPPluginCall) {
2022-05-03 12:55:13 +02:00
let configs = Database.shared.getServerConnectionConfigs()
let index = Database.shared.getLastActiveConfigIndex()
let settings = Database.shared.getDeviceSettings()
call.resolve([
2022-04-15 10:16:11 +02:00
"serverConnectionConfigs": configs.map { config in convertServerConnectionConfigToJSON(config: config) },
2022-05-03 12:55:13 +02:00
"lastServerConnectionConfigId": configs.first { config in config.index == index }?.id as Any,
"deviceSettings": deviceSettingsToJSON(settings: settings)
])
2022-04-11 16:29:19 +02:00
}
2022-04-11 18:31:14 +02:00
2022-04-11 18:50:25 +02:00
@objc func getLocalLibraryItems(_ call: CAPPluginCall) {
2022-07-16 20:25:04 -04:00
do {
let items = Database.shared.getLocalLibraryItems()
call.resolve([ "value": try items.asDictionaryArray()])
2022-07-16 20:25:04 -04:00
} catch(let exception) {
NSLog("error while readling local library items")
debugPrint(exception)
2022-07-30 18:25:20 -04:00
call.resolve()
2022-07-16 20:25:04 -04:00
}
2022-04-11 18:50:25 +02:00
}
2022-07-30 18:25:20 -04:00
2022-04-11 18:50:25 +02:00
@objc func getLocalLibraryItem(_ call: CAPPluginCall) {
2022-07-30 18:25:20 -04:00
do {
2022-07-30 18:40:30 -04:00
let item = Database.shared.getLocalLibraryItem(localLibraryItem: call.getString("id") ?? "")
2022-07-30 18:25:20 -04:00
switch item {
case .some(let foundItem):
call.resolve(try foundItem.asDictionary())
default:
call.resolve()
}
} catch(let exception) {
NSLog("error while readling local library items")
debugPrint(exception)
call.resolve()
}
2022-04-11 18:50:25 +02:00
}
2022-07-30 18:25:20 -04:00
2022-07-13 16:44:02 -05:00
@objc func getLocalLibraryItemByLId(_ call: CAPPluginCall) {
2022-07-30 18:25:20 -04:00
do {
let item = Database.shared.getLocalLibraryItemByLLId(libraryItem: call.getString("libraryItemId") ?? "")
switch item {
case .some(let foundItem):
call.resolve(try foundItem.asDictionary())
default:
call.resolve()
}
} catch(let exception) {
NSLog("error while readling local library items")
debugPrint(exception)
call.resolve()
}
2022-04-11 18:50:25 +02:00
}
2022-07-30 18:25:20 -04:00
2022-04-11 18:50:25 +02:00
@objc func getLocalLibraryItemsInFolder(_ call: CAPPluginCall) {
call.resolve([ "value": [] ])
}
@objc func getAllLocalMediaProgress(_ call: CAPPluginCall) {
call.resolve([ "value": [] ])
}
@objc func updateDeviceSettings(_ call: CAPPluginCall) {
let disableAutoRewind = call.getBool("disableAutoRewind") ?? false
let enableAltView = call.getBool("enableAltView") ?? false
let jumpBackwardsTime = call.getInt("jumpBackwardsTime") ?? 10
let jumpForwardTime = call.getInt("jumpForwardTime") ?? 10
let settings = DeviceSettings(disableAutoRewind: disableAutoRewind, enableAltView: enableAltView, jumpBackwardsTime: jumpBackwardsTime, jumpForwardTime: jumpForwardTime)
Database.shared.setDeviceSettings(deviceSettings: settings)
// call.resolve([ "value": [] ])
getDeviceData(call)
}
2022-04-11 16:29:19 +02:00
}