diff --git a/ios/App/App/plugins/AbsDatabase.swift b/ios/App/App/plugins/AbsDatabase.swift index a78619df..4a36a1d8 100644 --- a/ios/App/App/plugins/AbsDatabase.swift +++ b/ios/App/App/plugins/AbsDatabase.swift @@ -176,10 +176,10 @@ public class AbsDatabase: CAPPlugin { call.reject("Local media progress not found or created") return } - localMediaProgress.updateFromServerMediaProgress(serverMediaProgress) NSLog("syncServerMediaProgressWithLocalMediaProgress: Saving local media progress") - Database.shared.saveLocalMediaProgress(localMediaProgress) + localMediaProgress.updateFromServerMediaProgress(serverMediaProgress) + call.resolve(try localMediaProgress.asDictionary()) } catch { call.reject("Failed to sync media progress") @@ -203,7 +203,6 @@ public class AbsDatabase: CAPPlugin { // Update finished status localMediaProgress.updateIsFinished(isFinished) - Database.shared.saveLocalMediaProgress(localMediaProgress) // Build API response let progressDictionary = try? localMediaProgress.asDictionary() diff --git a/ios/App/Shared/models/local/LocalMediaProgress.swift b/ios/App/Shared/models/local/LocalMediaProgress.swift index 001e07ff..c15c38a9 100644 --- a/ios/App/Shared/models/local/LocalMediaProgress.swift +++ b/ios/App/Shared/models/local/LocalMediaProgress.swift @@ -158,19 +158,24 @@ extension LocalMediaProgress { } static func fetchOrCreateLocalMediaProgress(localMediaProgressId: String?, localLibraryItemId: String?, localEpisodeId: String?) -> LocalMediaProgress? { - if let localMediaProgressId = localMediaProgressId { - // Check if it existing in the database, if not, we need to create it - if let progress = Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId) { - return progress + let realm = try! Realm() + return try! realm.write { () -> LocalMediaProgress? in + if let localMediaProgressId = localMediaProgressId { + // Check if it existing in the database, if not, we need to create it + if let progress = Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId) { + return progress + } + } + + if let localLibraryItemId = localLibraryItemId { + guard let localLibraryItem = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) else { return nil } + let episode = localLibraryItem.getPodcastEpisode(episodeId: localEpisodeId) + let progress = LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode) + realm.add(progress) + return progress + } else { + return nil } - } - - 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 } } } diff --git a/ios/App/Shared/player/PlayerProgress.swift b/ios/App/Shared/player/PlayerProgress.swift index 231ad102..afe03c86 100644 --- a/ios/App/Shared/player/PlayerProgress.swift +++ b/ios/App/Shared/player/PlayerProgress.swift @@ -84,7 +84,6 @@ class PlayerProgress { } localMediaProgress.updateFromPlaybackSession(session) - Database.shared.saveLocalMediaProgress(localMediaProgress) NSLog("Local progress saved to the database")