From 81b11bac933ccd4ce28bca62037b280b0a826127 Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 13 Nov 2023 16:33:19 -0600 Subject: [PATCH] Fix:iOS downloading not saving progress for local copy #825 --- ios/App/App/AppDelegate.swift | 2 +- ios/App/Shared/models/server/MediaProgress.swift | 7 +++++-- ios/App/Shared/util/ApiClient.swift | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ios/App/App/AppDelegate.swift b/ios/App/App/AppDelegate.swift index 8b95944c..cf760e00 100644 --- a/ios/App/App/AppDelegate.swift +++ b/ios/App/App/AppDelegate.swift @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Override point for customization after application launch. let configuration = Realm.Configuration( - schemaVersion: 13, + schemaVersion: 14, migrationBlock: { [weak self] migration, oldSchemaVersion in if (oldSchemaVersion < 1) { self?.logger.log("Realm schema version was \(oldSchemaVersion)") diff --git a/ios/App/Shared/models/server/MediaProgress.swift b/ios/App/Shared/models/server/MediaProgress.swift index 6512d4ae..c5890258 100644 --- a/ios/App/Shared/models/server/MediaProgress.swift +++ b/ios/App/Shared/models/server/MediaProgress.swift @@ -10,6 +10,7 @@ import RealmSwift class MediaProgress: EmbeddedObject, Codable { @Persisted var id: String = "" + @Persisted var userId: String = "" @Persisted var libraryItemId: String = "" @Persisted var episodeId: String? @Persisted var duration: Double = 0 @@ -23,7 +24,7 @@ class MediaProgress: EmbeddedObject, Codable { @Persisted var finishedAt: Double? private enum CodingKeys : String, CodingKey { - case id, libraryItemId, episodeId, duration, progress, currentTime, isFinished, ebookLocation, ebookProgress, lastUpdate, startedAt, finishedAt + case id, userId, libraryItemId, episodeId, duration, progress, currentTime, isFinished, ebookLocation, ebookProgress, lastUpdate, startedAt, finishedAt } override init() { @@ -33,6 +34,7 @@ class MediaProgress: EmbeddedObject, Codable { required init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) + userId = try values.decode(String.self, forKey: .userId) libraryItemId = try values.decode(String.self, forKey: .libraryItemId) episodeId = try? values.decode(String.self, forKey: .episodeId) duration = try values.doubleOrStringDecoder(key: .duration) @@ -40,7 +42,7 @@ class MediaProgress: EmbeddedObject, Codable { currentTime = try values.doubleOrStringDecoder(key: .currentTime) isFinished = try values.decode(Bool.self, forKey: .isFinished) ebookLocation = try values.decodeIfPresent(String.self, forKey: .ebookLocation) - ebookProgress = try values.doubleOrStringDecoder(key: .ebookProgress) + ebookProgress = try? values.doubleOrStringDecoder(key: .ebookProgress) lastUpdate = try values.doubleOrStringDecoder(key: .lastUpdate) startedAt = try values.doubleOrStringDecoder(key: .startedAt) finishedAt = try? values.doubleOrStringDecoder(key: .finishedAt) @@ -49,6 +51,7 @@ class MediaProgress: EmbeddedObject, Codable { func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) + try container.encode(userId, forKey: .userId) try container.encode(libraryItemId, forKey: .libraryItemId) try container.encode(episodeId, forKey: .episodeId) try container.encode(duration, forKey: .duration) diff --git a/ios/App/Shared/util/ApiClient.swift b/ios/App/Shared/util/ApiClient.swift index 44e0ddcf..bb18d3fa 100644 --- a/ios/App/Shared/util/ApiClient.swift +++ b/ios/App/Shared/util/ApiClient.swift @@ -219,7 +219,9 @@ class ApiClient { } if (localMediaProgress != nil && mediaProgress.lastUpdate > localMediaProgress!.lastUpdate) { logger.log("syncLocalSessionsWithServer: Updating local media progress \(localMediaProgress!.id) with server media progress") - try localMediaProgress?.updateFromServerMediaProgress(mediaProgress) + if let localMediaProgress = localMediaProgress?.thaw() { + try localMediaProgress.updateFromServerMediaProgress(mediaProgress) + } } else if (localMediaProgress != nil) { logger.log("syncLocalSessionsWithServer: Local progress for \(localMediaProgress!.id) is more recent then server progress") } @@ -276,9 +278,10 @@ class ApiClient { } ApiClient.getResource(endpoint: endpoint, decodable: LibraryItem.self) { obj in - callback(obj) + callback(obj) } } + public static func pingServer() async -> Bool { var status = true AF.request("\(Store.serverConfig!.address)/ping", method: .get).responseDecodable(of: PingResponsePayload.self) { response in