diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/LocalMediaProgress.kt b/android/app/src/main/java/com/audiobookshelf/app/data/LocalMediaProgress.kt index fa242e3a..75ac9a15 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/LocalMediaProgress.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/LocalMediaProgress.kt @@ -42,4 +42,15 @@ data class LocalMediaProgress( isFinished = playbackSession.progress >= 0.99 finishedAt = if (isFinished) lastUpdate else null } + + @JsonIgnore + fun updateFromServerMediaProgress(serverMediaProgress:MediaProgress) { + isFinished = serverMediaProgress.isFinished + progress = serverMediaProgress.progress + currentTime = serverMediaProgress.currentTime + duration = serverMediaProgress.duration + lastUpdate = serverMediaProgress.lastUpdate + finishedAt = serverMediaProgress.finishedAt + startedAt = serverMediaProgress.startedAt + } } diff --git a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDatabase.kt b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDatabase.kt index 53ea45ee..183070b4 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDatabase.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDatabase.kt @@ -6,6 +6,7 @@ import com.audiobookshelf.app.device.DeviceManager import com.audiobookshelf.app.server.ApiHandler import com.fasterxml.jackson.core.json.JsonReadFeature import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue import com.getcapacitor.* import com.getcapacitor.annotation.CapacitorPlugin import kotlinx.coroutines.Dispatchers @@ -188,7 +189,7 @@ class AbsDatabase : Plugin() { @PluginMethod fun removeLocalMediaProgress(call:PluginCall) { - var localMediaProgressId = call.getString("localMediaProgressId", "").toString() + val localMediaProgressId = call.getString("localMediaProgressId", "").toString() DeviceManager.dbManager.removeLocalMediaProgress(localMediaProgressId) call.resolve() } @@ -204,6 +205,63 @@ class AbsDatabase : Plugin() { } } + // Updates received via web socket + // This function doesn't need to sync with the server also because this data is coming from the server + // If sending the localMediaProgressId then update existing media progress + // If sending localLibraryItemId then save new local media progress + @PluginMethod + fun syncServerMediaProgressWithLocalMediaProgress(call:PluginCall) { + val serverMediaProgress = call.getObject("mediaProgress").toString() + val localLibraryItemId = call.getString("localLibraryItemId", "").toString() + var localEpisodeId:String? = call.getString("localEpisodeId", "").toString() + if (localEpisodeId.isNullOrEmpty()) localEpisodeId = null + var localMediaProgressId = call.getString("localMediaProgressId") ?: "" + + val mediaProgress = jacksonMapper.readValue(serverMediaProgress) + + if (localMediaProgressId == "") { + val localLibraryItem = DeviceManager.dbManager.getLocalLibraryItem(localLibraryItemId) + if (localLibraryItem != null) { + localMediaProgressId = if (localEpisodeId.isNullOrEmpty()) localLibraryItemId else "$localLibraryItemId-$localEpisodeId" + + val localMediaProgress = LocalMediaProgress( + id = localMediaProgressId, + localLibraryItemId = localLibraryItemId, + localEpisodeId = localEpisodeId, + duration = mediaProgress.duration, + progress = mediaProgress.progress, + currentTime = mediaProgress.currentTime, + isFinished = mediaProgress.isFinished, + lastUpdate = mediaProgress.lastUpdate, + startedAt = mediaProgress.startedAt, + finishedAt = mediaProgress.finishedAt, + serverConnectionConfigId = localLibraryItem.serverConnectionConfigId, + serverAddress = localLibraryItem.serverAddress, + serverUserId = localLibraryItem.serverUserId, + libraryItemId = localLibraryItem.libraryItemId, + episodeId = mediaProgress.episodeId) + + Log.d(tag, "syncServerMediaProgressWithLocalMediaProgress: Saving new local media progress $localMediaProgress") + DeviceManager.dbManager.saveLocalMediaProgress(localMediaProgress) + call.resolve(JSObject(jacksonMapper.writeValueAsString(localMediaProgress))) + } else { + Log.e(tag, "syncServerMediaProgressWithLocalMediaProgress: Local library item not found") + } + } else { + Log.d(tag, "syncServerMediaProgressWithLocalMediaProgress $localMediaProgressId") + val localMediaProgress = DeviceManager.dbManager.getLocalMediaProgress(localMediaProgressId) + + if (localMediaProgress == null) { + Log.w(tag, "syncServerMediaProgressWithLocalMediaProgress Local media progress not found $localMediaProgressId") + call.resolve() + } else { + localMediaProgress.updateFromServerMediaProgress(mediaProgress) + DeviceManager.dbManager.saveLocalMediaProgress(localMediaProgress) + call.resolve(JSObject(jacksonMapper.writeValueAsString(localMediaProgress))) + } + } + } + @PluginMethod fun updateLocalMediaProgressFinished(call:PluginCall) { val localLibraryItemId = call.getString("localLibraryItemId", "").toString() diff --git a/components/modals/Dialog.vue b/components/modals/Dialog.vue index 1b3e7255..f1c37bf4 100644 --- a/components/modals/Dialog.vue +++ b/components/modals/Dialog.vue @@ -10,7 +10,7 @@