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 a6c26c0a..53ea45ee 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 @@ -206,45 +206,93 @@ class AbsDatabase : Plugin() { @PluginMethod fun updateLocalMediaProgressFinished(call:PluginCall) { - var localMediaProgressId = call.getString("localMediaProgressId", "").toString() - var isFinished = call.getBoolean("isFinished", false) == true + val localLibraryItemId = call.getString("localLibraryItemId", "").toString() + var localEpisodeId:String? = call.getString("localEpisodeId", "").toString() + if (localEpisodeId.isNullOrEmpty()) localEpisodeId = null + + val localMediaProgressId = if (localEpisodeId.isNullOrEmpty()) localLibraryItemId else "$localLibraryItemId-$localEpisodeId" + val isFinished = call.getBoolean("isFinished", false) == true + Log.d(tag, "updateLocalMediaProgressFinished $localMediaProgressId | Is Finished:$isFinished") var localMediaProgress = DeviceManager.dbManager.getLocalMediaProgress(localMediaProgressId) - if (localMediaProgress == null) { - Log.e(tag, "updateLocalMediaProgressFinished Local Media Progress not found $localMediaProgressId") - call.resolve(JSObject("{\"error\":\"Progress not found\"}")) + + if (localMediaProgress == null) { // Create new local media progress if does not exist + Log.d(tag, "updateLocalMediaProgressFinished Local Media Progress not found $localMediaProgressId - Creating new") + val localLibraryItem = DeviceManager.dbManager.getLocalLibraryItem(localLibraryItemId) + + if (localLibraryItem == null) { + return call.resolve(JSObject("{\"error\":\"Library Item not found\"}")) + } + if (localLibraryItem.mediaType != "podcast" && !localEpisodeId.isNullOrEmpty()) { + return call.resolve(JSObject("{\"error\":\"Invalid library item not a podcast\"}")) + } + + var duration = 0.0 + var podcastEpisode:PodcastEpisode? = null + if (!localEpisodeId.isNullOrEmpty()) { + val podcast = localLibraryItem.media as Podcast + podcastEpisode = podcast.episodes?.find { episode -> + episode.id == localEpisodeId + } + if (podcastEpisode == null) { + return call.resolve(JSObject("{\"error\":\"Podcast episode not found\"}")) + } + duration = podcastEpisode.duration ?: 0.0 + } else { + val book = localLibraryItem.media as Book + duration = book.duration ?: 0.0 + } + + val currentTime = System.currentTimeMillis() + localMediaProgress = LocalMediaProgress( + id = localMediaProgressId, + localLibraryItemId = localLibraryItemId, + localEpisodeId = localEpisodeId, + duration = duration, + progress = if (isFinished) 1.0 else 0.0, + currentTime = 0.0, + isFinished = isFinished, + lastUpdate = currentTime, + startedAt = if (isFinished) currentTime else 0L, + finishedAt = if (isFinished) currentTime else null, + serverConnectionConfigId = localLibraryItem.serverConnectionConfigId, + serverAddress = localLibraryItem.serverAddress, + serverUserId = localLibraryItem.serverUserId, + libraryItemId = localLibraryItem.libraryItemId, + episodeId = podcastEpisode?.serverEpisodeId) } else { localMediaProgress.updateIsFinished(isFinished) + } - var lmpstring = jacksonMapper.writeValueAsString(localMediaProgress) - Log.d(tag, "updateLocalMediaProgressFinished: Local Media Progress String $lmpstring") + // Save local media progress locally + DeviceManager.dbManager.saveLocalMediaProgress(localMediaProgress) - // Send update to server media progress is linked to a server and user is logged into that server - localMediaProgress.serverConnectionConfigId?.let { configId -> - if (DeviceManager.serverConnectionConfigId == configId) { - var libraryItemId = localMediaProgress.libraryItemId ?: "" - var episodeId = localMediaProgress.episodeId ?: "" - var updatePayload = JSObject() - updatePayload.put("isFinished", isFinished) - apiHandler.updateMediaProgress(libraryItemId,episodeId,updatePayload) { - Log.d(tag, "updateLocalMediaProgressFinished: Updated media progress isFinished on server") - var jsobj = JSObject() - jsobj.put("local", true) - jsobj.put("server", true) - jsobj.put("localMediaProgress", JSObject(lmpstring)) - call.resolve(jsobj) -// call.resolve(JSObject("{\"local\":true,\"server\":true,\"localMediaProgress\":$lmpstring}")) - } + val lmpstring = jacksonMapper.writeValueAsString(localMediaProgress) + Log.d(tag, "updateLocalMediaProgressFinished: Local Media Progress String $lmpstring") + + // Send update to server media progress is linked to a server and user is logged into that server + localMediaProgress.serverConnectionConfigId?.let { configId -> + if (DeviceManager.serverConnectionConfigId == configId) { + var libraryItemId = localMediaProgress.libraryItemId ?: "" + var episodeId = localMediaProgress.episodeId ?: "" + var updatePayload = JSObject() + updatePayload.put("isFinished", isFinished) + apiHandler.updateMediaProgress(libraryItemId,episodeId,updatePayload) { + Log.d(tag, "updateLocalMediaProgressFinished: Updated media progress isFinished on server") + var jsobj = JSObject() + jsobj.put("local", true) + jsobj.put("server", true) + jsobj.put("localMediaProgress", JSObject(lmpstring)) + call.resolve(jsobj) } } - if (localMediaProgress.serverConnectionConfigId == null || DeviceManager.serverConnectionConfigId != localMediaProgress.serverConnectionConfigId) { -// call.resolve(JSObject("{\"local\":true,\"localMediaProgress\":$lmpstring}}")) - var jsobj = JSObject() - jsobj.put("local", true) - jsobj.put("server", false) - jsobj.put("localMediaProgress", JSObject(lmpstring)) - call.resolve(jsobj) - } + } + if (localMediaProgress.serverConnectionConfigId == null || DeviceManager.serverConnectionConfigId != localMediaProgress.serverConnectionConfigId) { + var jsobj = JSObject() + jsobj.put("local", true) + jsobj.put("server", false) + jsobj.put("localMediaProgress", JSObject(lmpstring)) + call.resolve(jsobj) } } diff --git a/components/tables/podcast/EpisodeRow.vue b/components/tables/podcast/EpisodeRow.vue index 714f3eeb..4551dcad 100644 --- a/components/tables/podcast/EpisodeRow.vue +++ b/components/tables/podcast/EpisodeRow.vue @@ -209,9 +209,7 @@ export default { var isFinished = !this.userIsFinished var localLibraryItemId = this.isLocal ? this.libraryItemId : this.localLibraryItemId var localEpisodeId = this.isLocal ? this.episode.id : this.localEpisode.id - var localMediaProgressId = `${localLibraryItemId}-${localEpisodeId}` - console.log('toggleFinished local media progress id', localMediaProgressId, isFinished) - var payload = await this.$db.updateLocalMediaProgressFinished({ localMediaProgressId, isFinished }) + var payload = await this.$db.updateLocalMediaProgressFinished({ localLibraryItemId, localEpisodeId, isFinished }) console.log('toggleFinished payload', JSON.stringify(payload)) if (!payload || payload.error) { var errorMsg = payload ? payload.error : 'Unknown error' diff --git a/ios/App/Podfile b/ios/App/Podfile index 584acebb..44119188 100644 --- a/ios/App/Podfile +++ b/ios/App/Podfile @@ -9,13 +9,13 @@ install! 'cocoapods', :disable_input_output_paths => true def capacitor_pods pod 'Capacitor', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' - pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app' - pod 'CapacitorDialog', :path => '../../node_modules/@capacitor/dialog' - pod 'CapacitorHaptics', :path => '../../node_modules/@capacitor/haptics' - pod 'CapacitorNetwork', :path => '../../node_modules/@capacitor/network' - pod 'CapacitorStatusBar', :path => '../../node_modules/@capacitor/status-bar' - pod 'CapacitorStorage', :path => '../../node_modules/@capacitor/storage' - pod 'RobingenzCapacitorAppUpdate', :path => '../../node_modules/@robingenz/capacitor-app-update' + pod 'CapacitorApp', :path => '..\..\node_modules\@capacitor\app' + pod 'CapacitorDialog', :path => '..\..\node_modules\@capacitor\dialog' + pod 'CapacitorHaptics', :path => '..\..\node_modules\@capacitor\haptics' + pod 'CapacitorNetwork', :path => '..\..\node_modules\@capacitor\network' + pod 'CapacitorStatusBar', :path => '..\..\node_modules\@capacitor\status-bar' + pod 'CapacitorStorage', :path => '..\..\node_modules\@capacitor\storage' + pod 'RobingenzCapacitorAppUpdate', :path => '..\..\node_modules\@robingenz\capacitor-app-update' end target 'App' do diff --git a/pages/item/_id.vue b/pages/item/_id.vue index b3d61325..1c17d9c5 100644 --- a/pages/item/_id.vue +++ b/pages/item/_id.vue @@ -20,6 +20,7 @@

Your Progress: {{ Math.round(progressPercent * 100) }}%

{{ $elapsedPretty(userTimeRemaining) }} remaining

+

Finished {{ $formatDate(userProgressFinishedAt) }}

close
@@ -360,9 +361,7 @@ export default { this.isProcessingReadUpdate = true if (this.isLocal) { var isFinished = !this.userIsFinished - var localMediaProgressId = this.localLibraryItemId - console.log('toggleFinished local media progress id', localMediaProgressId, isFinished) - var payload = await this.$db.updateLocalMediaProgressFinished({ localMediaProgressId, isFinished }) + var payload = await this.$db.updateLocalMediaProgressFinished({ localLibraryItemId: this.localLibraryItemId, isFinished }) console.log('toggleFinished payload', JSON.stringify(payload)) if (!payload || payload.error) { var errorMsg = payload ? payload.error : 'Unknown error' diff --git a/plugins/capacitor/AbsDatabase.js b/plugins/capacitor/AbsDatabase.js index 1b46aaed..7ad26378 100644 --- a/plugins/capacitor/AbsDatabase.js +++ b/plugins/capacitor/AbsDatabase.js @@ -199,7 +199,8 @@ class AbsDatabaseWeb extends WebPlugin { return [] } - async updateLocalMediaProgressFinished({ localMediaProgressId, isFinished }) { + async updateLocalMediaProgressFinished(payload) { + // { localLibraryItemId, localEpisodeId, isFinished } return null } }