advplyr.audiobookshelf-app/ios/App/Shared/models/LocalLibrary.swift

157 lines
5.3 KiB
Swift
Raw Normal View History

2022-07-06 10:09:17 -04:00
//
// LocalLibrary.swift
// App
//
// Created by benonymity on 6/15/22.
//
import Foundation
import Unrealm
2022-07-06 10:09:17 -04:00
struct LocalLibraryItem: Realmable, Codable {
var id: String = ""
var basePath: String = ""
2022-08-10 22:17:12 -04:00
var _contentUrl: String?
var isInvalid: Bool = false
var mediaType: String = ""
var media: MediaType?
var localFiles: [LocalFile] = []
2022-08-10 22:17:12 -04:00
var _coverContentUrl: String?
var isLocal: Bool = true
var serverConnectionConfigId: String?
var serverAddress: String?
var serverUserId: String?
var libraryItemId: String?
var contentUrl: String? {
2022-08-10 22:17:12 -04:00
if let path = _contentUrl {
2022-08-11 13:46:30 -04:00
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString
2022-08-10 22:17:12 -04:00
} else {
return nil
}
}
2022-08-11 14:12:45 -04:00
var contentDirectory: URL? {
if let path = _contentUrl {
return AbsDownloader.itemDownloadFolder(path: path)
} else {
return nil
}
}
var coverContentUrl: String? {
2022-08-10 22:17:12 -04:00
if let path = self._coverContentUrl {
2022-08-11 13:46:30 -04:00
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString
2022-08-10 22:17:12 -04:00
} else {
return nil
}
}
2022-08-11 12:32:58 -04:00
var isBook: Bool { self.mediaType == "book" }
var isPodcast: Bool { self.mediaType == "podcast" }
static func primaryKey() -> String? {
return "id"
}
private enum CodingKeys : String, CodingKey {
case id, basePath, contentUrl, isInvalid, mediaType, media, localFiles, coverContentUrl, isLocal, serverConnectionConfigId, serverAddress, serverUserId, libraryItemId
}
init() {}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decode(String.self, forKey: .id)
basePath = try values.decode(String.self, forKey: .basePath)
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)
isLocal = try values.decode(Bool.self, forKey: .isLocal)
2022-08-10 22:17:12 -04:00
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)
try container.encode(basePath, forKey: .basePath)
try container.encode(contentUrl, forKey: .contentUrl)
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(coverContentUrl, forKey: .coverContentUrl)
try container.encode(isLocal, forKey: .isLocal)
try container.encode(serverConnectionConfigId, forKey: .serverConnectionConfigId)
try container.encode(serverAddress, forKey: .serverAddress)
try container.encode(serverUserId, forKey: .serverUserId)
try container.encode(libraryItemId, forKey: .libraryItemId)
}
2022-07-06 10:09:17 -04:00
}
2022-07-09 15:18:34 -04:00
struct LocalFile: Realmable, Codable {
var id: String = UUID().uuidString
var filename: String?
2022-08-10 22:17:12 -04:00
var _contentUrl: String = ""
var mimeType: String?
var size: Int = 0
2022-08-10 22:17:12 -04:00
var contentUrl: String {
2022-08-11 13:46:30 -04:00
return AbsDownloader.itemDownloadFolder(path: _contentUrl)!.absoluteString
2022-08-10 22:17:12 -04:00
}
static func primaryKey() -> String? {
return "id"
}
private enum CodingKeys : String, CodingKey {
2022-08-10 22:17:12 -04:00
case id, filename, contentUrl, 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)
2022-08-10 22:17:12 -04:00
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)
try container.encode(filename, forKey: .filename)
try container.encode(contentUrl, forKey: .contentUrl)
try container.encode(mimeType, forKey: .mimeType)
try container.encode(size, forKey: .size)
}
2022-07-16 21:46:49 -04:00
}
struct LocalMediaProgress: Realmable, Codable {
2022-08-07 10:27:08 -04:00
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?
2022-07-06 10:09:17 -04:00
// 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?
static func primaryKey() -> String? {
return "id"
}
2022-07-06 10:09:17 -04:00
}