mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-02-10 15:29:57 +01:00
Update:Remove call to server for user settings, user settings stored locally
This commit is contained in:
parent
415dda37a4
commit
7852804a9c
@ -367,11 +367,11 @@ export default {
|
|||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.init()
|
this.init()
|
||||||
this.$store.commit('user/addSettingsListener', { id: 'bookshelftoolbar', meth: this.settingsUpdated })
|
this.$eventBus.$on('user-settings', this.settingsUpdated)
|
||||||
this.$eventBus.$on('bookshelf-total-entities', this.setBookshelfTotalEntities)
|
this.$eventBus.$on('bookshelf-total-entities', this.setBookshelfTotalEntities)
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
this.$store.commit('user/removeSettingsListener', 'bookshelftoolbar')
|
this.$eventBus.$off('user-settings', this.settingsUpdated)
|
||||||
this.$eventBus.$off('bookshelf-total-entities', this.setBookshelfTotalEntities)
|
this.$eventBus.$off('bookshelf-total-entities', this.setBookshelfTotalEntities)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -670,8 +670,7 @@ export default {
|
|||||||
this.$eventBus.$on('series-sort-updated', this.seriesSortUpdated)
|
this.$eventBus.$on('series-sort-updated', this.seriesSortUpdated)
|
||||||
this.$eventBus.$on('bookshelf_clear_selection', this.clearSelectedEntities)
|
this.$eventBus.$on('bookshelf_clear_selection', this.clearSelectedEntities)
|
||||||
this.$eventBus.$on('socket_init', this.socketInit)
|
this.$eventBus.$on('socket_init', this.socketInit)
|
||||||
|
this.$eventBus.$on('user-settings', this.settingsUpdated)
|
||||||
this.$store.commit('user/addSettingsListener', { id: 'lazy-bookshelf', meth: this.settingsUpdated })
|
|
||||||
|
|
||||||
if (this.$root.socket) {
|
if (this.$root.socket) {
|
||||||
this.$root.socket.on('item_updated', this.libraryItemUpdated)
|
this.$root.socket.on('item_updated', this.libraryItemUpdated)
|
||||||
@ -699,8 +698,7 @@ export default {
|
|||||||
this.$eventBus.$off('series-sort-updated', this.seriesSortUpdated)
|
this.$eventBus.$off('series-sort-updated', this.seriesSortUpdated)
|
||||||
this.$eventBus.$off('bookshelf_clear_selection', this.clearSelectedEntities)
|
this.$eventBus.$off('bookshelf_clear_selection', this.clearSelectedEntities)
|
||||||
this.$eventBus.$off('socket_init', this.socketInit)
|
this.$eventBus.$off('socket_init', this.socketInit)
|
||||||
|
this.$eventBus.$off('user-settings', this.settingsUpdated)
|
||||||
this.$store.commit('user/removeSettingsListener', 'lazy-bookshelf')
|
|
||||||
|
|
||||||
if (this.$root.socket) {
|
if (this.$root.socket) {
|
||||||
this.$root.socket.off('item_updated', this.libraryItemUpdated)
|
this.$root.socket.off('item_updated', this.libraryItemUpdated)
|
||||||
|
@ -234,13 +234,10 @@ export default {
|
|||||||
this.showChaptersModal = false
|
this.showChaptersModal = false
|
||||||
},
|
},
|
||||||
setUseChapterTrack() {
|
setUseChapterTrack() {
|
||||||
var useChapterTrack = !this.useChapterTrack
|
this.useChapterTrack = !this.useChapterTrack
|
||||||
this.useChapterTrack = useChapterTrack
|
if (this.$refs.trackbar) this.$refs.trackbar.setUseChapterTrack(this.useChapterTrack)
|
||||||
if (this.$refs.trackbar) this.$refs.trackbar.setUseChapterTrack(useChapterTrack)
|
|
||||||
|
|
||||||
this.$store.dispatch('user/updateUserSettings', { useChapterTrack }).catch((err) => {
|
this.$store.dispatch('user/updateUserSettings', { useChapterTrack: this.useChapterTrack })
|
||||||
console.error('Failed to update settings', err)
|
|
||||||
})
|
|
||||||
this.updateTimestamp()
|
this.updateTimestamp()
|
||||||
},
|
},
|
||||||
checkUpdateChapterTrack() {
|
checkUpdateChapterTrack() {
|
||||||
@ -311,7 +308,7 @@ export default {
|
|||||||
init() {
|
init() {
|
||||||
this.playbackRate = this.$store.getters['user/getUserSetting']('playbackRate') || 1
|
this.playbackRate = this.$store.getters['user/getUserSetting']('playbackRate') || 1
|
||||||
|
|
||||||
var _useChapterTrack = this.$store.getters['user/getUserSetting']('useChapterTrack') || false
|
const _useChapterTrack = this.$store.getters['user/getUserSetting']('useChapterTrack') || false
|
||||||
this.useChapterTrack = this.chapters.length ? _useChapterTrack : false
|
this.useChapterTrack = this.chapters.length ? _useChapterTrack : false
|
||||||
|
|
||||||
if (this.$refs.trackbar) this.$refs.trackbar.setUseChapterTrack(this.useChapterTrack)
|
if (this.$refs.trackbar) this.$refs.trackbar.setUseChapterTrack(this.useChapterTrack)
|
||||||
@ -345,13 +342,14 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.$store.commit('user/addSettingsListener', { id: 'audioplayer', meth: this.settingsUpdated })
|
|
||||||
this.init()
|
|
||||||
this.$eventBus.$on('player-hotkey', this.hotkey)
|
this.$eventBus.$on('player-hotkey', this.hotkey)
|
||||||
|
this.$eventBus.$on('user-settings', this.settingsUpdated)
|
||||||
|
|
||||||
|
this.init()
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
this.$store.commit('user/removeSettingsListener', 'audioplayer')
|
|
||||||
this.$eventBus.$off('player-hotkey', this.hotkey)
|
this.$eventBus.$off('player-hotkey', this.hotkey)
|
||||||
|
this.$eventBus.$off('user-settings', this.settingsUpdated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -280,7 +280,6 @@ export default {
|
|||||||
userUpdated(user) {
|
userUpdated(user) {
|
||||||
if (this.$store.state.user.user.id === user.id) {
|
if (this.$store.state.user.user.id === user.id) {
|
||||||
this.$store.commit('user/setUser', user)
|
this.$store.commit('user/setUser', user)
|
||||||
this.$store.commit('user/setSettings', user.settings)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
userOnline(user) {
|
userOnline(user) {
|
||||||
|
@ -137,6 +137,8 @@ export default {
|
|||||||
|
|
||||||
this.$store.commit('libraries/setCurrentLibrary', userDefaultLibraryId)
|
this.$store.commit('libraries/setCurrentLibrary', userDefaultLibraryId)
|
||||||
this.$store.commit('user/setUser', user)
|
this.$store.commit('user/setUser', user)
|
||||||
|
|
||||||
|
this.$store.dispatch('user/loadUserSettings')
|
||||||
},
|
},
|
||||||
async submitForm() {
|
async submitForm() {
|
||||||
this.error = null
|
this.error = null
|
||||||
|
@ -5,8 +5,6 @@ import { formatDistance, format, addDays, isDate } from 'date-fns'
|
|||||||
|
|
||||||
Vue.directive('click-outside', vClickOutside.directive)
|
Vue.directive('click-outside', vClickOutside.directive)
|
||||||
|
|
||||||
Vue.prototype.$eventBus = new Vue()
|
|
||||||
|
|
||||||
Vue.prototype.$dateDistanceFromNow = (unixms) => {
|
Vue.prototype.$dateDistanceFromNow = (unixms) => {
|
||||||
if (!unixms) return ''
|
if (!unixms) return ''
|
||||||
return formatDistance(unixms, Date.now(), { addSuffix: true })
|
return formatDistance(unixms, Date.now(), { addSuffix: true })
|
||||||
@ -159,6 +157,7 @@ export {
|
|||||||
export default ({ app, store }, inject) => {
|
export default ({ app, store }, inject) => {
|
||||||
app.$decode = decode
|
app.$decode = decode
|
||||||
app.$encode = encode
|
app.$encode = encode
|
||||||
|
inject('eventBus', new Vue())
|
||||||
inject('isDev', process.env.NODE_ENV !== 'production')
|
inject('isDev', process.env.NODE_ENV !== 'production')
|
||||||
|
|
||||||
store.commit('setRouterBasePath', app.$config.routerBasePath)
|
store.commit('setRouterBasePath', app.$config.routerBasePath)
|
||||||
|
@ -7,9 +7,9 @@ export const state = () => ({
|
|||||||
playbackRate: 1,
|
playbackRate: 1,
|
||||||
bookshelfCoverSize: 120,
|
bookshelfCoverSize: 120,
|
||||||
collapseSeries: false,
|
collapseSeries: false,
|
||||||
collapseBookSeries: false
|
collapseBookSeries: false,
|
||||||
},
|
useChapterTrack: false
|
||||||
settingsListeners: []
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export const getters = {
|
export const getters = {
|
||||||
@ -66,7 +66,7 @@ export const getters = {
|
|||||||
export const actions = {
|
export const actions = {
|
||||||
// When changing libraries make sure sort and filter is still valid
|
// When changing libraries make sure sort and filter is still valid
|
||||||
checkUpdateLibrarySortFilter({ state, dispatch, commit }, mediaType) {
|
checkUpdateLibrarySortFilter({ state, dispatch, commit }, mediaType) {
|
||||||
var settingsUpdate = {}
|
const settingsUpdate = {}
|
||||||
if (mediaType == 'podcast') {
|
if (mediaType == 'podcast') {
|
||||||
if (state.settings.orderBy == 'media.metadata.authorName' || state.settings.orderBy == 'media.metadata.authorNameLF') {
|
if (state.settings.orderBy == 'media.metadata.authorName' || state.settings.orderBy == 'media.metadata.authorNameLF') {
|
||||||
settingsUpdate.orderBy = 'media.metadata.author'
|
settingsUpdate.orderBy = 'media.metadata.author'
|
||||||
@ -77,8 +77,8 @@ export const actions = {
|
|||||||
if (state.settings.orderBy == 'media.metadata.publishedYear') {
|
if (state.settings.orderBy == 'media.metadata.publishedYear') {
|
||||||
settingsUpdate.orderBy = 'media.metadata.title'
|
settingsUpdate.orderBy = 'media.metadata.title'
|
||||||
}
|
}
|
||||||
var invalidFilters = ['series', 'authors', 'narrators', 'languages', 'progress', 'issues']
|
const invalidFilters = ['series', 'authors', 'narrators', 'languages', 'progress', 'issues']
|
||||||
var filterByFirstPart = (state.settings.filterBy || '').split('.').shift()
|
const filterByFirstPart = (state.settings.filterBy || '').split('.').shift()
|
||||||
if (invalidFilters.includes(filterByFirstPart)) {
|
if (invalidFilters.includes(filterByFirstPart)) {
|
||||||
settingsUpdate.filterBy = 'all'
|
settingsUpdate.filterBy = 'all'
|
||||||
}
|
}
|
||||||
@ -94,30 +94,47 @@ export const actions = {
|
|||||||
dispatch('updateUserSettings', settingsUpdate)
|
dispatch('updateUserSettings', settingsUpdate)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
updateUserSettings({ commit }, payload) {
|
updateUserSettings({ state, commit }, payload) {
|
||||||
var updatePayload = {
|
if (!payload) return false
|
||||||
...payload
|
|
||||||
|
let hasChanges = false
|
||||||
|
const existingSettings = { ...state.settings }
|
||||||
|
for (const key in existingSettings) {
|
||||||
|
if (payload[key] !== undefined && existingSettings[key] !== payload[key]) {
|
||||||
|
hasChanges = true
|
||||||
|
existingSettings[key] = payload[key]
|
||||||
}
|
}
|
||||||
// Immediately update
|
|
||||||
commit('setSettings', updatePayload)
|
|
||||||
return this.$axios.$patch('/api/me/settings', updatePayload).then((result) => {
|
|
||||||
if (result.success) {
|
|
||||||
commit('setSettings', result.settings)
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}).catch((error) => {
|
if (hasChanges) {
|
||||||
console.error('Failed to update settings', error)
|
localStorage.setItem('userSettings', JSON.stringify(existingSettings))
|
||||||
return false
|
commit('setSettings', existingSettings)
|
||||||
})
|
this.$eventBus.$emit('user-settings', state.settings)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
loadUserSettings({ state, commit }) {
|
||||||
|
// Load settings from local storage
|
||||||
|
try {
|
||||||
|
let userSettingsFromLocal = localStorage.getItem('userSettings')
|
||||||
|
if (userSettingsFromLocal) {
|
||||||
|
userSettingsFromLocal = JSON.parse(userSettingsFromLocal)
|
||||||
|
const userSettings = { ...state.settings }
|
||||||
|
for (const key in userSettings) {
|
||||||
|
if (userSettingsFromLocal[key] !== undefined) {
|
||||||
|
userSettings[key] = userSettingsFromLocal[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commit('setSettings', userSettings)
|
||||||
|
this.$eventBus.$emit('user-settings', state.settings)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load userSettings from local storage', error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mutations = {
|
export const mutations = {
|
||||||
setUser(state, user) {
|
setUser(state, user) {
|
||||||
state.user = user
|
state.user = user
|
||||||
state.settings = user.settings
|
|
||||||
if (user) {
|
if (user) {
|
||||||
if (user.token) localStorage.setItem('token', user.token)
|
if (user.token) localStorage.setItem('token', user.token)
|
||||||
} else {
|
} else {
|
||||||
@ -143,25 +160,6 @@ export const mutations = {
|
|||||||
},
|
},
|
||||||
setSettings(state, settings) {
|
setSettings(state, settings) {
|
||||||
if (!settings) return
|
if (!settings) return
|
||||||
var hasChanges = false
|
state.settings = settings
|
||||||
for (const key in settings) {
|
|
||||||
if (state.settings[key] !== settings[key]) {
|
|
||||||
hasChanges = true
|
|
||||||
state.settings[key] = settings[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (hasChanges) {
|
|
||||||
state.settingsListeners.forEach((listener) => {
|
|
||||||
listener.meth(state.settings)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
addSettingsListener(state, listener) {
|
|
||||||
var index = state.settingsListeners.findIndex(l => l.id === listener.id)
|
|
||||||
if (index >= 0) state.settingsListeners.splice(index, 1, listener)
|
|
||||||
else state.settingsListeners.push(listener)
|
|
||||||
},
|
|
||||||
removeSettingsListener(state, listenerId) {
|
|
||||||
state.settingsListeners = state.settingsListeners.filter(l => l.id !== listenerId)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -63,13 +63,7 @@ class User {
|
|||||||
return {
|
return {
|
||||||
mobileOrderBy: 'recent',
|
mobileOrderBy: 'recent',
|
||||||
mobileOrderDesc: true,
|
mobileOrderDesc: true,
|
||||||
mobileFilterBy: 'all',
|
mobileFilterBy: 'all'
|
||||||
orderBy: 'media.metadata.title',
|
|
||||||
orderDesc: false,
|
|
||||||
filterBy: 'all',
|
|
||||||
playbackRate: 1,
|
|
||||||
bookshelfCoverSize: 120,
|
|
||||||
collapseSeries: false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user