mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-26 05:14:09 +02:00
Convert objects to realm-native
This commit is contained in:
@@ -6,76 +6,52 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Unrealm
|
||||
import RealmSwift
|
||||
|
||||
struct LocalLibraryItem: Realmable, Codable {
|
||||
var id: String = "local_\(UUID().uuidString)"
|
||||
var basePath: String = ""
|
||||
dynamic var _contentUrl: String?
|
||||
var isInvalid: Bool = false
|
||||
var mediaType: String = ""
|
||||
var media: MediaType?
|
||||
var localFiles: [LocalFile] = []
|
||||
dynamic var _coverContentUrl: String?
|
||||
var isLocal: Bool = true
|
||||
var serverConnectionConfigId: String?
|
||||
var serverAddress: String?
|
||||
var serverUserId: String?
|
||||
var libraryItemId: String?
|
||||
|
||||
var contentUrl: String? {
|
||||
set(url) {
|
||||
_contentUrl = url
|
||||
}
|
||||
get {
|
||||
if let path = _contentUrl {
|
||||
return AbsDownloader.downloadsDirectory.appendingPathComponent(path).absoluteString
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var coverContentUrl: String? {
|
||||
set(url) {
|
||||
_coverContentUrl = url
|
||||
}
|
||||
get {
|
||||
if let path = self._coverContentUrl {
|
||||
return AbsDownloader.downloadsDirectory.appendingPathComponent(path).absoluteString
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static func primaryKey() -> String? {
|
||||
return "id"
|
||||
}
|
||||
class LocalLibraryItem: Object, Codable {
|
||||
@Persisted(primaryKey: true) var id: String = "local_\(UUID().uuidString)"
|
||||
@Persisted var basePath: String = ""
|
||||
@Persisted var _contentUrl: String?
|
||||
@Persisted var isInvalid: Bool = false
|
||||
@Persisted var mediaType: String = ""
|
||||
@Persisted var media: MediaType?
|
||||
@Persisted var localFiles = List<LocalFile>()
|
||||
@Persisted var _coverContentUrl: String?
|
||||
@Persisted var isLocal: Bool = true
|
||||
@Persisted var serverConnectionConfigId: String?
|
||||
@Persisted var serverAddress: String?
|
||||
@Persisted var serverUserId: String?
|
||||
@Persisted(indexed: true) var libraryItemId: String?
|
||||
|
||||
private enum CodingKeys : String, CodingKey {
|
||||
case id, basePath, contentUrl, isInvalid, mediaType, media, localFiles, coverContentUrl, isLocal, serverConnectionConfigId, serverAddress, serverUserId, libraryItemId
|
||||
}
|
||||
|
||||
init() {}
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
required init(from decoder: Decoder) throws {
|
||||
super.init()
|
||||
|
||||
let values = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try values.decode(String.self, forKey: .id)
|
||||
basePath = try values.decode(String.self, forKey: .basePath)
|
||||
contentUrl = try values.decode(String.self, forKey: .contentUrl)
|
||||
isInvalid = try values.decode(Bool.self, forKey: .isInvalid)
|
||||
mediaType = try values.decode(String.self, forKey: .mediaType)
|
||||
media = try values.decode(MediaType.self, forKey: .media)
|
||||
localFiles = try values.decode([LocalFile].self, forKey: .localFiles)
|
||||
coverContentUrl = try values.decode(String.self, forKey: .coverContentUrl)
|
||||
media = try? values.decode(MediaType.self, forKey: .media)
|
||||
if let files = try? values.decode([LocalFile].self, forKey: .localFiles) {
|
||||
localFiles.append(objectsIn: files)
|
||||
}
|
||||
_coverContentUrl = try values.decode(String.self, forKey: .coverContentUrl)
|
||||
isLocal = try values.decode(Bool.self, forKey: .isLocal)
|
||||
serverConnectionConfigId = try values.decode(String.self, forKey: .serverConnectionConfigId)
|
||||
serverAddress = try values.decode(String.self, forKey: .serverAddress)
|
||||
serverUserId = try values.decode(String.self, forKey: .serverUserId)
|
||||
libraryItemId = try values.decode(String.self, forKey: .libraryItemId)
|
||||
serverConnectionConfigId = try? values.decode(String.self, forKey: .serverConnectionConfigId)
|
||||
serverAddress = try? values.decode(String.self, forKey: .serverAddress)
|
||||
serverUserId = try? values.decode(String.self, forKey: .serverUserId)
|
||||
libraryItemId = try? values.decode(String.self, forKey: .libraryItemId)
|
||||
}
|
||||
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(id, forKey: .id)
|
||||
@@ -84,7 +60,7 @@ struct LocalLibraryItem: Realmable, Codable {
|
||||
try container.encode(isInvalid, forKey: .isInvalid)
|
||||
try container.encode(mediaType, forKey: .mediaType)
|
||||
try container.encode(media, forKey: .media)
|
||||
try container.encode(localFiles, forKey: .localFiles)
|
||||
try container.encode(Array(localFiles), forKey: .localFiles)
|
||||
try container.encode(coverContentUrl, forKey: .coverContentUrl)
|
||||
try container.encode(isLocal, forKey: .isLocal)
|
||||
try container.encode(serverConnectionConfigId, forKey: .serverConnectionConfigId)
|
||||
@@ -94,54 +70,65 @@ struct LocalLibraryItem: Realmable, Codable {
|
||||
}
|
||||
}
|
||||
|
||||
struct LocalPodcastEpisode: Realmable, Codable {
|
||||
var id: String = UUID().uuidString
|
||||
var index: Int = 0
|
||||
var episode: String?
|
||||
var episodeType: String?
|
||||
var title: String = "Unknown"
|
||||
var subtitle: String?
|
||||
var desc: String?
|
||||
var audioFile: AudioFile?
|
||||
var audioTrack: AudioTrack?
|
||||
var duration: Double = 0
|
||||
var size: Int = 0
|
||||
var serverEpisodeId: String?
|
||||
class LocalPodcastEpisode: Object, Codable {
|
||||
@Persisted(primaryKey: true) var id: String = UUID().uuidString
|
||||
@Persisted var index: Int = 0
|
||||
@Persisted var episode: String?
|
||||
@Persisted var episodeType: String?
|
||||
@Persisted var title: String = "Unknown"
|
||||
@Persisted var subtitle: String?
|
||||
@Persisted var desc: String?
|
||||
@Persisted var audioFile: AudioFile?
|
||||
@Persisted var audioTrack: AudioTrack?
|
||||
@Persisted var duration: Double = 0
|
||||
@Persisted var size: Int = 0
|
||||
@Persisted(indexed: true) var serverEpisodeId: String?
|
||||
|
||||
static func primaryKey() -> String? {
|
||||
return "id"
|
||||
private enum CodingKeys : String, CodingKey {
|
||||
case id
|
||||
}
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
required init(from decoder: Decoder) throws {
|
||||
super.init()
|
||||
|
||||
let values = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try values.decode(String.self, forKey: .id)
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(id, forKey: .id)
|
||||
}
|
||||
}
|
||||
|
||||
struct LocalFile: Realmable, Codable {
|
||||
var id: String = UUID().uuidString
|
||||
var filename: String?
|
||||
var contentUrl: String = ""
|
||||
var absolutePath: String {
|
||||
return AbsDownloader.downloadsDirectory.appendingPathComponent(self.contentUrl).absoluteString
|
||||
}
|
||||
var mimeType: String?
|
||||
var size: Int = 0
|
||||
|
||||
static func primaryKey() -> String? {
|
||||
return "id"
|
||||
}
|
||||
class LocalFile: Object, Codable {
|
||||
@Persisted(primaryKey: true) var id: String = UUID().uuidString
|
||||
@Persisted var filename: String?
|
||||
@Persisted var contentUrl: String = ""
|
||||
@Persisted var mimeType: String?
|
||||
@Persisted var size: Int = 0
|
||||
|
||||
private enum CodingKeys : String, CodingKey {
|
||||
case id, filename, contentUrl, absolutePath, mimeType, size
|
||||
}
|
||||
|
||||
init() {}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let values = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try values.decode(String.self, forKey: .id)
|
||||
filename = try values.decode(String.self, forKey: .filename)
|
||||
contentUrl = try values.decode(String.self, forKey: .contentUrl)
|
||||
mimeType = try values.decode(String.self, forKey: .mimeType)
|
||||
size = try values.decode(Int.self, forKey: .size)
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
required init(from decoder: Decoder) throws {
|
||||
let values = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try values.decode(String.self, forKey: .id)
|
||||
filename = try? values.decode(String.self, forKey: .filename)
|
||||
contentUrl = try values.decode(String.self, forKey: .contentUrl)
|
||||
mimeType = try? values.decode(String.self, forKey: .mimeType)
|
||||
size = try values.decode(Int.self, forKey: .size)
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(id, forKey: .id)
|
||||
@@ -153,25 +140,69 @@ struct LocalFile: Realmable, Codable {
|
||||
}
|
||||
}
|
||||
|
||||
struct LocalMediaProgress: Realmable, Codable {
|
||||
var id: String = ""
|
||||
var localLibraryItemId: String = ""
|
||||
var localEpisodeId: String?
|
||||
var duration: Double = 0
|
||||
var progress: Double = 0
|
||||
var currentTime: Double = 0
|
||||
var isFinished: Bool = false
|
||||
var lastUpdate: Int = 0
|
||||
var startedAt: Int = 0
|
||||
var finishedAt: Int?
|
||||
class LocalMediaProgress: Object, Codable {
|
||||
@Persisted(primaryKey: true) var id: String = ""
|
||||
@Persisted(indexed: true) var localLibraryItemId: String = ""
|
||||
@Persisted(indexed: true) var localEpisodeId: String?
|
||||
@Persisted var duration: Double = 0
|
||||
@Persisted var progress: Double = 0
|
||||
@Persisted var currentTime: Double = 0
|
||||
@Persisted var isFinished: Bool = false
|
||||
@Persisted var lastUpdate: Int = 0
|
||||
@Persisted var startedAt: Int = 0
|
||||
@Persisted var finishedAt: Int?
|
||||
// For local lib items from server to support server sync
|
||||
var serverConnectionConfigId: String?
|
||||
var serverAddress: String?
|
||||
var serverUserId: String?
|
||||
var libraryItemId: String?
|
||||
var episodeId: String?
|
||||
@Persisted var serverConnectionConfigId: String?
|
||||
@Persisted var serverAddress: String?
|
||||
@Persisted var serverUserId: String?
|
||||
@Persisted(indexed: true) var libraryItemId: String?
|
||||
@Persisted(indexed: true) var episodeId: String?
|
||||
|
||||
static func primaryKey() -> String? {
|
||||
return "id"
|
||||
private enum CodingKeys : String, CodingKey {
|
||||
case id, localLibraryItemId, localEpisodeId, duration, progress, currentTime, isFinished, lastUpdate, startedAt, finishedAt, serverConnectionConfigId, serverAddress, serverUserId, libraryItemId, episodeId
|
||||
}
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
required init(from decoder: Decoder) throws {
|
||||
super.init()
|
||||
|
||||
let values = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try values.decode(String.self, forKey: .id)
|
||||
localLibraryItemId = try values.decode(String.self, forKey: .localLibraryItemId)
|
||||
localEpisodeId = try? values.decode(String.self, forKey: .localEpisodeId)
|
||||
duration = try values.decode(Double.self, forKey: .duration)
|
||||
progress = try values.decode(Double.self, forKey: .progress)
|
||||
currentTime = try values.decode(Double.self, forKey: .currentTime)
|
||||
isFinished = try values.decode(Bool.self, forKey: .isFinished)
|
||||
lastUpdate = try values.decode(Int.self, forKey: .lastUpdate)
|
||||
startedAt = try values.decode(Int.self, forKey: .startedAt)
|
||||
finishedAt = try? values.decode(Int.self, forKey: .finishedAt)
|
||||
serverConnectionConfigId = try? values.decode(String.self, forKey: .serverConnectionConfigId)
|
||||
serverAddress = try? values.decode(String.self, forKey: .serverAddress)
|
||||
serverUserId = try? values.decode(String.self, forKey: .serverUserId)
|
||||
libraryItemId = try? values.decode(String.self, forKey: .libraryItemId)
|
||||
episodeId = try? values.decode(String.self, forKey: .episodeId)
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(id, forKey: .id)
|
||||
try container.encode(localLibraryItemId, forKey: .localLibraryItemId)
|
||||
try container.encode(localEpisodeId, forKey: .localEpisodeId)
|
||||
try container.encode(duration, forKey: .duration)
|
||||
try container.encode(progress, forKey: .progress)
|
||||
try container.encode(currentTime, forKey: .currentTime)
|
||||
try container.encode(isFinished, forKey: .isFinished)
|
||||
try container.encode(lastUpdate, forKey: .lastUpdate)
|
||||
try container.encode(startedAt, forKey: .startedAt)
|
||||
try container.encode(finishedAt, forKey: .finishedAt)
|
||||
try container.encode(serverConnectionConfigId, forKey: .serverConnectionConfigId)
|
||||
try container.encode(serverAddress, forKey: .serverAddress)
|
||||
try container.encode(serverUserId, forKey: .serverUserId)
|
||||
try container.encode(libraryItemId, forKey: .libraryItemId)
|
||||
try container.encode(episodeId, forKey: .episodeId)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user