mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-29 22:29:29 +02:00
Fix:Refreshing server media progress after local sync
This commit is contained in:
parent
8bab4ae383
commit
860c7aac80
5 changed files with 35 additions and 16 deletions
|
@ -32,8 +32,8 @@ class ApiHandler(var ctx:Context) {
|
||||||
|
|
||||||
data class LocalMediaProgressSyncPayload(val localMediaProgress:List<LocalMediaProgress>)
|
data class LocalMediaProgressSyncPayload(val localMediaProgress:List<LocalMediaProgress>)
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
data class MediaProgressSyncResponsePayload(val numServerProgressUpdates:Int, val localProgressUpdates:List<LocalMediaProgress>)
|
data class MediaProgressSyncResponsePayload(val numServerProgressUpdates:Int, val localProgressUpdates:List<LocalMediaProgress>, val serverProgressUpdates:List<MediaProgress>)
|
||||||
data class LocalMediaProgressSyncResultsPayload(var numLocalMediaProgressForServer:Int, var numServerProgressUpdates:Int, var numLocalProgressUpdates:Int)
|
data class LocalMediaProgressSyncResultsPayload(var numLocalMediaProgressForServer:Int, var numServerProgressUpdates:Int, var numLocalProgressUpdates:Int, var serverProgressUpdates:List<MediaProgress>)
|
||||||
|
|
||||||
fun getRequest(endpoint:String, httpClient:OkHttpClient?, config:ServerConnectionConfig?, cb: (JSObject) -> Unit) {
|
fun getRequest(endpoint:String, httpClient:OkHttpClient?, config:ServerConnectionConfig?, cb: (JSObject) -> Unit) {
|
||||||
val address = config?.address ?: DeviceManager.serverAddress
|
val address = config?.address ?: DeviceManager.serverAddress
|
||||||
|
@ -254,7 +254,7 @@ class ApiHandler(var ctx:Context) {
|
||||||
fun syncMediaProgress(cb: (LocalMediaProgressSyncResultsPayload) -> Unit) {
|
fun syncMediaProgress(cb: (LocalMediaProgressSyncResultsPayload) -> Unit) {
|
||||||
if (!isOnline()) {
|
if (!isOnline()) {
|
||||||
Log.d(tag, "Error not online")
|
Log.d(tag, "Error not online")
|
||||||
cb(LocalMediaProgressSyncResultsPayload(0,0,0))
|
cb(LocalMediaProgressSyncResultsPayload(0,0,0, mutableListOf()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ class ApiHandler(var ctx:Context) {
|
||||||
it.serverConnectionConfigId == DeviceManager.serverConnectionConfig?.id
|
it.serverConnectionConfigId == DeviceManager.serverConnectionConfig?.id
|
||||||
}
|
}
|
||||||
|
|
||||||
val localSyncResultsPayload = LocalMediaProgressSyncResultsPayload(localMediaProgress.size,0, 0)
|
val localSyncResultsPayload = LocalMediaProgressSyncResultsPayload(localMediaProgress.size,0, 0, mutableListOf())
|
||||||
|
|
||||||
if (localMediaProgress.isNotEmpty()) {
|
if (localMediaProgress.isNotEmpty()) {
|
||||||
Log.d(tag, "Sending sync local progress request with ${localMediaProgress.size} progress items")
|
Log.d(tag, "Sending sync local progress request with ${localMediaProgress.size} progress items")
|
||||||
|
@ -279,16 +279,21 @@ class ApiHandler(var ctx:Context) {
|
||||||
val progressSyncResponsePayload = jacksonMapper.readValue<MediaProgressSyncResponsePayload>(it.toString())
|
val progressSyncResponsePayload = jacksonMapper.readValue<MediaProgressSyncResponsePayload>(it.toString())
|
||||||
|
|
||||||
localSyncResultsPayload.numLocalProgressUpdates = progressSyncResponsePayload.localProgressUpdates.size
|
localSyncResultsPayload.numLocalProgressUpdates = progressSyncResponsePayload.localProgressUpdates.size
|
||||||
|
localSyncResultsPayload.serverProgressUpdates = progressSyncResponsePayload.serverProgressUpdates
|
||||||
localSyncResultsPayload.numServerProgressUpdates = progressSyncResponsePayload.numServerProgressUpdates
|
localSyncResultsPayload.numServerProgressUpdates = progressSyncResponsePayload.numServerProgressUpdates
|
||||||
Log.d(tag, "Media Progress Sync | Local Updates: $localSyncResultsPayload")
|
Log.d(tag, "Media Progress Sync | Local Updates: $localSyncResultsPayload")
|
||||||
if (progressSyncResponsePayload.localProgressUpdates.isNotEmpty()) {
|
if (progressSyncResponsePayload.localProgressUpdates.isNotEmpty()) {
|
||||||
// Update all local media progress
|
// Update all local media progress
|
||||||
progressSyncResponsePayload.localProgressUpdates.forEach { localMediaProgress ->
|
progressSyncResponsePayload.localProgressUpdates.forEach { localMediaProgress ->
|
||||||
MediaEventManager.syncEvent(localMediaProgress, "Received from server sync local API request")
|
MediaEventManager.syncEvent(localMediaProgress, "Local progress updated. Received from server sync local API request")
|
||||||
|
|
||||||
DeviceManager.dbManager.saveLocalMediaProgress(localMediaProgress)
|
DeviceManager.dbManager.saveLocalMediaProgress(localMediaProgress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
progressSyncResponsePayload.serverProgressUpdates.forEach { localMediaProgress ->
|
||||||
|
MediaEventManager.syncEvent(localMediaProgress, "Server progress updated. Received from server sync local API request")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cb(localSyncResultsPayload)
|
cb(localSyncResultsPayload)
|
||||||
|
|
|
@ -183,12 +183,19 @@ export default {
|
||||||
this.$store.commit('setLastLocalMediaSyncResults', null)
|
this.$store.commit('setLastLocalMediaSyncResults', null)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const { numLocalMediaProgressForServer, numServerProgressUpdates, numLocalProgressUpdates } = response
|
const { numLocalMediaProgressForServer, numServerProgressUpdates, numLocalProgressUpdates, serverProgressUpdates } = response
|
||||||
if (numLocalMediaProgressForServer > 0) {
|
if (numLocalMediaProgressForServer > 0) {
|
||||||
response.syncedAt = Date.now()
|
response.syncedAt = Date.now()
|
||||||
response.serverConfigName = this.$store.getters['user/getServerConfigName']
|
response.serverConfigName = this.$store.getters['user/getServerConfigName']
|
||||||
this.$store.commit('setLastLocalMediaSyncResults', response)
|
this.$store.commit('setLastLocalMediaSyncResults', response)
|
||||||
|
|
||||||
|
if (serverProgressUpdates && serverProgressUpdates.length) {
|
||||||
|
serverProgressUpdates.forEach((progress) => {
|
||||||
|
console.log(`[default] Server progress was updated ${progress.id}`)
|
||||||
|
this.$store.commit('user/updateUserMediaProgress', progress)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (numServerProgressUpdates > 0 || numLocalProgressUpdates > 0) {
|
if (numServerProgressUpdates > 0 || numLocalProgressUpdates > 0) {
|
||||||
console.log(`[default] ${numServerProgressUpdates} Server progress updates | ${numLocalProgressUpdates} Local progress updates`)
|
console.log(`[default] ${numServerProgressUpdates} Server progress updates | ${numLocalProgressUpdates} Local progress updates`)
|
||||||
} else {
|
} else {
|
||||||
|
@ -199,7 +206,8 @@ export default {
|
||||||
this.$store.commit('setLastLocalMediaSyncResults', null)
|
this.$store.commit('setLastLocalMediaSyncResults', null)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async userUpdated(user) {
|
userUpdated(user) {
|
||||||
|
console.log('[default] userUpdated:', JSON.stringify(user))
|
||||||
if (this.user && this.user.id == user.id) {
|
if (this.user && this.user.id == user.id) {
|
||||||
this.$store.commit('user/setUser', user)
|
this.$store.commit('user/setUser', user)
|
||||||
}
|
}
|
||||||
|
@ -210,7 +218,7 @@ export default {
|
||||||
// Update local media progress if exists
|
// Update local media progress if exists
|
||||||
const localProg = await this.$db.getLocalMediaProgressForServerItem({ libraryItemId: prog.libraryItemId, episodeId: prog.episodeId })
|
const localProg = await this.$db.getLocalMediaProgressForServerItem({ libraryItemId: prog.libraryItemId, episodeId: prog.episodeId })
|
||||||
|
|
||||||
var newLocalMediaProgress = null
|
let newLocalMediaProgress = null
|
||||||
if (localProg && localProg.lastUpdate < prog.lastUpdate) {
|
if (localProg && localProg.lastUpdate < prog.lastUpdate) {
|
||||||
if (localProg.currentTime == prog.currentTime && localProg.isFinished == prog.isFinished) {
|
if (localProg.currentTime == prog.currentTime && localProg.isFinished == prog.isFinished) {
|
||||||
console.log('[default] syncing progress server lastUpdate > local lastUpdate but currentTime and isFinished is equal')
|
console.log('[default] syncing progress server lastUpdate > local lastUpdate but currentTime and isFinished is equal')
|
||||||
|
@ -229,12 +237,12 @@ export default {
|
||||||
} else if (!localProg) {
|
} else if (!localProg) {
|
||||||
// Check if local library item exists
|
// Check if local library item exists
|
||||||
// local media progress may not exist yet if it hasn't been played
|
// local media progress may not exist yet if it hasn't been played
|
||||||
var localLibraryItem = await this.$db.getLocalLibraryItemByLId(prog.libraryItemId)
|
const localLibraryItem = await this.$db.getLocalLibraryItemByLId(prog.libraryItemId)
|
||||||
if (localLibraryItem) {
|
if (localLibraryItem) {
|
||||||
if (prog.episodeId) {
|
if (prog.episodeId) {
|
||||||
// If episode check if local episode exists
|
// If episode check if local episode exists
|
||||||
var lliEpisodes = localLibraryItem.media.episodes || []
|
const lliEpisodes = localLibraryItem.media.episodes || []
|
||||||
var localEpisode = lliEpisodes.find((ep) => ep.serverEpisodeId === prog.episodeId)
|
const localEpisode = lliEpisodes.find((ep) => ep.serverEpisodeId === prog.episodeId)
|
||||||
if (localEpisode) {
|
if (localEpisode) {
|
||||||
// Add new local media progress
|
// Add new local media progress
|
||||||
const payload = {
|
const payload = {
|
||||||
|
|
|
@ -45,7 +45,14 @@ export default {
|
||||||
networkConnected(newVal) {
|
networkConnected(newVal) {
|
||||||
// Update shelves when network connect status changes
|
// Update shelves when network connect status changes
|
||||||
console.log(`Network changed to ${newVal} - fetch categories`)
|
console.log(`Network changed to ${newVal} - fetch categories`)
|
||||||
|
|
||||||
|
if (newVal) {
|
||||||
|
setTimeout(() => {
|
||||||
this.fetchCategories()
|
this.fetchCategories()
|
||||||
|
}, 4000)
|
||||||
|
} else {
|
||||||
|
this.fetchCategories()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
@ -123,7 +130,7 @@ export default {
|
||||||
this.localLibraryItems = await this.$db.getLocalLibraryItems()
|
this.localLibraryItems = await this.$db.getLocalLibraryItems()
|
||||||
const localCategories = await this.getLocalMediaItemCategories()
|
const localCategories = await this.getLocalMediaItemCategories()
|
||||||
|
|
||||||
if (this.user && this.currentLibraryId) {
|
if (this.user && this.currentLibraryId && this.networkConnected) {
|
||||||
const categories = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/personalized?minified=1`).catch((error) => {
|
const categories = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/personalized?minified=1`).catch((error) => {
|
||||||
console.error('Failed to fetch categories', error)
|
console.error('Failed to fetch categories', error)
|
||||||
return []
|
return []
|
||||||
|
|
|
@ -27,7 +27,6 @@ export default function ({ $axios, store }) {
|
||||||
})
|
})
|
||||||
|
|
||||||
$axios.onError(error => {
|
$axios.onError(error => {
|
||||||
const code = parseInt(error.response && error.response.status)
|
console.error('Axios error code', error)
|
||||||
console.error('Axios error code', code)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -124,7 +124,7 @@ export const mutations = {
|
||||||
},
|
},
|
||||||
updateUserMediaProgress(state, data) {
|
updateUserMediaProgress(state, data) {
|
||||||
if (!data || !state.user) return
|
if (!data || !state.user) return
|
||||||
var mediaProgressIndex = state.user.mediaProgress.findIndex(mp => mp.id === data.id)
|
const mediaProgressIndex = state.user.mediaProgress.findIndex(mp => mp.id === data.id)
|
||||||
if (mediaProgressIndex >= 0) {
|
if (mediaProgressIndex >= 0) {
|
||||||
state.user.mediaProgress.splice(mediaProgressIndex, 1, data)
|
state.user.mediaProgress.splice(mediaProgressIndex, 1, data)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue