mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-03 09:34:51 +02:00
Refactor storage model to work with native Realm
This commit is contained in:
parent
b0905d0270
commit
d83e04c47b
33 changed files with 1580 additions and 1305 deletions
73
ios/App/Shared/models/local/LocalFile.swift
Normal file
73
ios/App/Shared/models/local/LocalFile.swift
Normal file
|
@ -0,0 +1,73 @@
|
|||
//
|
||||
// LocalFile.swift
|
||||
// App
|
||||
//
|
||||
// Created by Ron Heft on 8/16/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RealmSwift
|
||||
|
||||
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
|
||||
|
||||
var contentUrl: String { AbsDownloader.itemDownloadFolder(path: _contentUrl)!.absoluteString }
|
||||
var contentPath: URL { AbsDownloader.itemDownloadFolder(path: _contentUrl)! }
|
||||
var basePath: String? { self.filename }
|
||||
|
||||
private enum CodingKeys : String, CodingKey {
|
||||
case id, filename, contentUrl, mimeType, size, basePath
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
try container.encode(basePath, forKey: .basePath)
|
||||
}
|
||||
}
|
||||
|
||||
extension LocalFile {
|
||||
convenience init(_ libraryItemId: String, _ filename: String, _ mimeType: String, _ localUrl: String, fileSize: Int) {
|
||||
self.init()
|
||||
|
||||
self.id = "\(libraryItemId)_\(filename.toBase64())"
|
||||
self.filename = filename
|
||||
self.mimeType = mimeType
|
||||
self._contentUrl = localUrl
|
||||
self.size = fileSize
|
||||
}
|
||||
|
||||
var absolutePath: String {
|
||||
return AbsDownloader.itemDownloadFolder(path: self._contentUrl)?.absoluteString ?? ""
|
||||
}
|
||||
|
||||
func isAudioFile() -> Bool {
|
||||
switch self.mimeType {
|
||||
case "application/octet-stream",
|
||||
"video/mp4":
|
||||
return true
|
||||
default:
|
||||
return self.mimeType?.starts(with: "audio") ?? false
|
||||
}
|
||||
}
|
||||
}
|
206
ios/App/Shared/models/local/LocalLibraryItem.swift
Normal file
206
ios/App/Shared/models/local/LocalLibraryItem.swift
Normal file
|
@ -0,0 +1,206 @@
|
|||
//
|
||||
// LocalLibraryItem.swift
|
||||
// App
|
||||
//
|
||||
// Created by Ron Heft on 8/16/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RealmSwift
|
||||
|
||||
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?
|
||||
|
||||
var contentUrl: String? {
|
||||
if let path = _contentUrl {
|
||||
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var contentDirectory: URL? {
|
||||
if let path = _contentUrl {
|
||||
return AbsDownloader.itemDownloadFolder(path: path)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var coverContentUrl: String? {
|
||||
if let path = self._coverContentUrl {
|
||||
return AbsDownloader.itemDownloadFolder(path: path)!.absoluteString
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var isBook: Bool { self.mediaType == "book" }
|
||||
var isPodcast: Bool { self.mediaType == "podcast" }
|
||||
|
||||
private enum CodingKeys : String, CodingKey {
|
||||
case id, basePath, contentUrl, isInvalid, mediaType, media, localFiles, coverContentUrl, isLocal, serverConnectionConfigId, serverAddress, serverUserId, libraryItemId
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
if let files = try? values.decode([LocalFile].self, forKey: .localFiles) {
|
||||
localFiles.append(objectsIn: files)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
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(Array(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)
|
||||
}
|
||||
}
|
||||
|
||||
extension LocalLibraryItem {
|
||||
convenience init(_ item: LibraryItem, localUrl: String, server: ServerConnectionConfig, files: [LocalFile], coverPath: String?) {
|
||||
self.init()
|
||||
|
||||
self._contentUrl = localUrl
|
||||
self.mediaType = item.mediaType
|
||||
self.localFiles.append(objectsIn: files)
|
||||
self._coverContentUrl = coverPath
|
||||
self.libraryItemId = item.id
|
||||
self.serverConnectionConfigId = server.id
|
||||
self.serverAddress = server.address
|
||||
self.serverUserId = server.userId
|
||||
|
||||
// Link the audio tracks and files
|
||||
linkLocalFiles(self.localFiles, fromMedia: item.media)
|
||||
}
|
||||
|
||||
func addFiles(_ files: [LocalFile], item: LibraryItem) throws {
|
||||
guard self.isPodcast else { throw LibraryItemDownloadError.podcastOnlySupported }
|
||||
self.localFiles.append(objectsIn: files.filter({ $0.isAudioFile() }))
|
||||
linkLocalFiles(self.localFiles, fromMedia: item.media)
|
||||
}
|
||||
|
||||
private func linkLocalFiles(_ files: List<LocalFile>, fromMedia: MediaType?) {
|
||||
guard let fromMedia = MediaType.detachCopy(of: fromMedia) else { return }
|
||||
let fileMap = files.map { ($0.filename ?? "", $0.id) }
|
||||
let fileIdByFilename = Dictionary(fileMap, uniquingKeysWith: { (_, last) in last })
|
||||
if ( self.isBook ) {
|
||||
for i in fromMedia.tracks.indices {
|
||||
_ = fromMedia.tracks[i].setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: i)
|
||||
}
|
||||
} else if ( self.isPodcast ) {
|
||||
let episodes = List<PodcastEpisode>()
|
||||
for episode in fromMedia.episodes {
|
||||
// Filter out episodes not downloaded
|
||||
let episodeIsDownloaded = episode.audioTrack?.setLocalInfo(filenameIdMap: fileIdByFilename, serverIndex: 0) ?? false
|
||||
if episodeIsDownloaded {
|
||||
episodes.append(episode)
|
||||
}
|
||||
}
|
||||
fromMedia.episodes = episodes
|
||||
}
|
||||
self.media = fromMedia
|
||||
}
|
||||
|
||||
func getDuration() -> Double {
|
||||
var total = 0.0
|
||||
self.media?.tracks.enumerated().forEach { _, track in total += track.duration }
|
||||
return total
|
||||
}
|
||||
|
||||
func getPodcastEpisode(episodeId: String?) -> PodcastEpisode? {
|
||||
guard self.isPodcast else { return nil }
|
||||
guard let episodes = self.media?.episodes else { return nil }
|
||||
return episodes.first(where: { $0.id == episodeId })
|
||||
}
|
||||
|
||||
func getPlaybackSession(episode: PodcastEpisode?) -> PlaybackSession {
|
||||
let localEpisodeId = episode?.id
|
||||
let sessionId = "play_local_\(UUID().uuidString)"
|
||||
|
||||
// Get current progress from local media
|
||||
let mediaProgressId = (localEpisodeId != nil) ? "\(self.id)-\(localEpisodeId!)" : self.id
|
||||
let mediaProgress = Database.shared.getLocalMediaProgress(localMediaProgressId: mediaProgressId)
|
||||
|
||||
let mediaMetadata = Metadata.detachCopy(of: self.media?.metadata)
|
||||
let chapters = List<Chapter>()
|
||||
self.media?.chapters.forEach { chapter in chapters.append(Chapter.detachCopy(of: chapter)!) }
|
||||
let authorName = mediaMetadata?.authorDisplayName
|
||||
|
||||
let audioTracks = List<AudioTrack>()
|
||||
if let episode = episode, let track = episode.audioTrack {
|
||||
audioTracks.append(AudioTrack.detachCopy(of: track)!)
|
||||
} else if let tracks = self.media?.tracks {
|
||||
tracks.forEach { t in audioTracks.append(AudioTrack.detachCopy(of: t)!) }
|
||||
}
|
||||
|
||||
let dateNow = Date().timeIntervalSince1970
|
||||
return PlaybackSession(
|
||||
id: sessionId,
|
||||
userId: self.serverUserId,
|
||||
libraryItemId: self.libraryItemId,
|
||||
episodeId: episode?.serverEpisodeId,
|
||||
mediaType: self.mediaType,
|
||||
chapters: chapters,
|
||||
displayTitle: mediaMetadata?.title,
|
||||
displayAuthor: authorName,
|
||||
coverPath: self.coverContentUrl,
|
||||
duration: self.getDuration(),
|
||||
playMethod: PlayMethod.local.rawValue,
|
||||
startedAt: dateNow,
|
||||
updatedAt: 0,
|
||||
timeListening: 0.0,
|
||||
audioTracks: audioTracks,
|
||||
currentTime: mediaProgress?.currentTime ?? 0.0,
|
||||
libraryItem: nil,
|
||||
localLibraryItem: self,
|
||||
serverConnectionConfigId: self.serverConnectionConfigId,
|
||||
serverAddress: self.serverAddress
|
||||
)
|
||||
}
|
||||
|
||||
func delete() {
|
||||
try! self.realm?.write {
|
||||
self.realm?.delete(self.localFiles)
|
||||
self.realm?.delete(self)
|
||||
}
|
||||
}
|
||||
}
|
168
ios/App/Shared/models/local/LocalMediaProgress.swift
Normal file
168
ios/App/Shared/models/local/LocalMediaProgress.swift
Normal file
|
@ -0,0 +1,168 @@
|
|||
//
|
||||
// LocalMediaProgress.swift
|
||||
// App
|
||||
//
|
||||
// Created by Ron Heft on 8/16/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RealmSwift
|
||||
|
||||
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
|
||||
@Persisted var serverConnectionConfigId: String?
|
||||
@Persisted var serverAddress: String?
|
||||
@Persisted var serverUserId: String?
|
||||
@Persisted(indexed: true) var libraryItemId: String?
|
||||
@Persisted(indexed: true) var episodeId: String?
|
||||
|
||||
var progressPercent: Int { Int(self.progress * 100) }
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
extension LocalMediaProgress {
|
||||
convenience init(localLibraryItem: LocalLibraryItem, episode: PodcastEpisode?) {
|
||||
self.init()
|
||||
|
||||
self.id = localLibraryItem.id
|
||||
self.localLibraryItemId = localLibraryItem.id
|
||||
self.libraryItemId = localLibraryItem.libraryItemId
|
||||
|
||||
self.serverAddress = localLibraryItem.serverAddress
|
||||
self.serverUserId = localLibraryItem.serverUserId
|
||||
self.serverConnectionConfigId = localLibraryItem.serverConnectionConfigId
|
||||
|
||||
self.duration = localLibraryItem.getDuration()
|
||||
self.progress = 0.0
|
||||
self.currentTime = 0.0
|
||||
self.isFinished = false
|
||||
self.lastUpdate = Int(Date().timeIntervalSince1970)
|
||||
self.startedAt = 0
|
||||
self.finishedAt = nil
|
||||
|
||||
if let episode = episode {
|
||||
self.id += "-\(episode.id)"
|
||||
self.episodeId = episode.id
|
||||
self.duration = episode.duration ?? 0.0
|
||||
}
|
||||
}
|
||||
|
||||
convenience init(localLibraryItem: LocalLibraryItem, episode: PodcastEpisode?, progress: MediaProgress) {
|
||||
self.init(localLibraryItem: localLibraryItem, episode: episode)
|
||||
self.duration = progress.duration
|
||||
self.progress = progress.progress
|
||||
self.currentTime = progress.currentTime
|
||||
self.isFinished = progress.isFinished
|
||||
self.lastUpdate = progress.lastUpdate
|
||||
self.startedAt = progress.startedAt
|
||||
self.finishedAt = progress.finishedAt
|
||||
}
|
||||
|
||||
func updateIsFinished(_ finished: Bool) {
|
||||
try! Realm().write {
|
||||
if self.isFinished != finished {
|
||||
self.progress = finished ? 1.0 : 0.0
|
||||
}
|
||||
|
||||
if self.startedAt == 0 && finished {
|
||||
self.startedAt = Int(Date().timeIntervalSince1970)
|
||||
}
|
||||
|
||||
self.isFinished = finished
|
||||
self.lastUpdate = Int(Date().timeIntervalSince1970)
|
||||
self.finishedAt = finished ? lastUpdate : nil
|
||||
}
|
||||
}
|
||||
|
||||
func updateFromPlaybackSession(_ playbackSession: PlaybackSession) {
|
||||
try! Realm().write {
|
||||
self.currentTime = playbackSession.currentTime
|
||||
self.progress = playbackSession.progress
|
||||
self.lastUpdate = Int(Date().timeIntervalSince1970)
|
||||
self.isFinished = playbackSession.progress >= 100.0
|
||||
self.finishedAt = self.isFinished ? self.lastUpdate : nil
|
||||
}
|
||||
}
|
||||
|
||||
func updateFromServerMediaProgress(_ serverMediaProgress: MediaProgress) {
|
||||
try! Realm().write {
|
||||
self.isFinished = serverMediaProgress.isFinished
|
||||
self.progress = serverMediaProgress.progress
|
||||
self.currentTime = serverMediaProgress.currentTime
|
||||
self.duration = serverMediaProgress.duration
|
||||
self.lastUpdate = serverMediaProgress.lastUpdate
|
||||
self.finishedAt = serverMediaProgress.finishedAt
|
||||
self.startedAt = serverMediaProgress.startedAt
|
||||
}
|
||||
}
|
||||
|
||||
static func fetchOrCreateLocalMediaProgress(localMediaProgressId: String?, localLibraryItemId: String?, localEpisodeId: String?) -> LocalMediaProgress? {
|
||||
if let localMediaProgressId = localMediaProgressId {
|
||||
return Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId)
|
||||
} else if let localLibraryItemId = localLibraryItemId {
|
||||
guard let localLibraryItem = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) else { return nil }
|
||||
let episode = localLibraryItem.getPodcastEpisode(episodeId: localEpisodeId)
|
||||
return LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
46
ios/App/Shared/models/local/LocalPodcastEpisode.swift
Normal file
46
ios/App/Shared/models/local/LocalPodcastEpisode.swift
Normal file
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// LocalPodcastEpisode.swift
|
||||
// App
|
||||
//
|
||||
// Created by Ron Heft on 8/16/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RealmSwift
|
||||
|
||||
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?
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue