2021-09-01 20:07:11 -05:00
|
|
|
<template>
|
2021-12-31 16:57:53 -06:00
|
|
|
<div class="w-full layout-wrapper bg-bg text-white">
|
2021-09-01 20:07:11 -05:00
|
|
|
<app-appbar />
|
2021-12-31 16:57:53 -06:00
|
|
|
<div id="content" class="overflow-hidden relative" :class="playerIsOpen ? 'playerOpen' : ''">
|
2021-09-01 20:07:11 -05:00
|
|
|
<Nuxt />
|
|
|
|
</div>
|
2021-11-01 21:06:51 -05:00
|
|
|
<app-audio-player-container ref="streamContainer" />
|
2021-10-06 07:24:15 -05:00
|
|
|
<modals-libraries-modal />
|
2021-11-14 19:59:34 -06:00
|
|
|
<app-side-drawer />
|
2021-10-17 20:20:00 -05:00
|
|
|
<readers-reader />
|
2021-09-01 20:07:11 -05:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-09-04 12:31:00 -05:00
|
|
|
import { AppUpdate } from '@robingenz/capacitor-app-update'
|
2022-04-04 19:08:27 -05:00
|
|
|
import { AbsFileSystem, AbsDownloader } from '@/plugins/capacitor'
|
2021-09-04 12:31:00 -05:00
|
|
|
|
2021-09-01 20:07:11 -05:00
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {}
|
|
|
|
},
|
2021-11-14 19:59:34 -06:00
|
|
|
watch: {
|
|
|
|
networkConnected: {
|
|
|
|
handler(newVal) {
|
|
|
|
if (newVal) {
|
|
|
|
this.attemptConnection()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-09-01 20:07:11 -05:00
|
|
|
computed: {
|
2021-09-12 18:37:08 -05:00
|
|
|
playerIsOpen() {
|
|
|
|
return this.$store.getters['playerIsOpen']
|
2021-09-01 20:07:11 -05:00
|
|
|
},
|
|
|
|
routeName() {
|
|
|
|
return this.$route.name
|
2021-11-14 19:59:34 -06:00
|
|
|
},
|
|
|
|
networkConnected() {
|
|
|
|
return this.$store.state.networkConnected
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
user() {
|
|
|
|
return this.$store.state.user.user
|
|
|
|
},
|
2021-12-04 19:56:29 -06:00
|
|
|
currentLibraryId() {
|
|
|
|
return this.$store.state.libraries.currentLibraryId
|
|
|
|
},
|
|
|
|
isSocketConnected() {
|
|
|
|
return this.$store.state.socketConnected
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
initialStream(stream) {
|
|
|
|
if (this.$refs.streamContainer && this.$refs.streamContainer.audioPlayerReady) {
|
|
|
|
this.$refs.streamContainer.streamOpen(stream)
|
|
|
|
}
|
2021-09-02 12:19:26 -05:00
|
|
|
},
|
2021-09-04 12:31:00 -05:00
|
|
|
async clickUpdateToast() {
|
|
|
|
var immediateUpdateAllowed = this.$store.state.appUpdateInfo.immediateUpdateAllowed
|
|
|
|
if (immediateUpdateAllowed) {
|
|
|
|
await AppUpdate.performImmediateUpdate()
|
2021-09-02 12:19:26 -05:00
|
|
|
} else {
|
2021-09-04 12:31:00 -05:00
|
|
|
await AppUpdate.openAppStore()
|
2021-09-02 12:19:26 -05:00
|
|
|
}
|
|
|
|
},
|
2021-09-04 12:31:00 -05:00
|
|
|
async checkForUpdate() {
|
2022-04-03 14:37:44 -05:00
|
|
|
if (this.$platform == 'web') return
|
2021-10-16 10:08:13 -05:00
|
|
|
console.log('Checking for app update')
|
2021-09-04 12:31:00 -05:00
|
|
|
const result = await AppUpdate.getAppUpdateInfo()
|
|
|
|
if (!result) {
|
|
|
|
console.error('Invalid version check')
|
2021-09-02 12:19:26 -05:00
|
|
|
return
|
|
|
|
}
|
2021-10-16 10:08:13 -05:00
|
|
|
console.log('App Update Info', JSON.stringify(result))
|
2021-09-04 12:31:00 -05:00
|
|
|
this.$store.commit('setAppUpdateInfo', result)
|
2021-09-12 18:37:08 -05:00
|
|
|
if (result.updateAvailability === 2) {
|
|
|
|
setTimeout(() => {
|
2021-09-19 18:44:10 -05:00
|
|
|
this.$toast.info(`Update is available! Click to update.`, {
|
2021-09-12 18:37:08 -05:00
|
|
|
draggable: false,
|
|
|
|
hideProgressBar: false,
|
2021-09-19 18:44:10 -05:00
|
|
|
timeout: 20000,
|
|
|
|
closeButton: true,
|
|
|
|
onClick: this.clickUpdateToast
|
2021-09-12 18:37:08 -05:00
|
|
|
})
|
|
|
|
}, 5000)
|
|
|
|
}
|
|
|
|
},
|
2021-10-28 09:37:31 -05:00
|
|
|
async searchFolder(downloadFolder) {
|
|
|
|
try {
|
2022-04-04 19:08:27 -05:00
|
|
|
var response = await AbsFileSystem.searchFolder({ folderUrl: downloadFolder.uri })
|
2021-10-28 09:37:31 -05:00
|
|
|
var searchResults = response
|
|
|
|
searchResults.folders = JSON.parse(searchResults.folders)
|
|
|
|
searchResults.files = JSON.parse(searchResults.files)
|
|
|
|
|
|
|
|
console.log('Search folders results length', searchResults.folders.length)
|
|
|
|
searchResults.folders = searchResults.folders.map((sr) => {
|
|
|
|
if (sr.files) {
|
|
|
|
sr.files = JSON.parse(sr.files)
|
|
|
|
}
|
|
|
|
return sr
|
|
|
|
})
|
|
|
|
|
|
|
|
return searchResults
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed', error)
|
|
|
|
this.$toast.error('Failed to search downloads folder')
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
// async syncDownloads(downloads, downloadFolder) {
|
|
|
|
// console.log('Syncing downloads ' + downloads.length)
|
|
|
|
// var mediaScanResults = await this.searchFolder(downloadFolder)
|
2021-10-28 09:37:31 -05:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
// this.$store.commit('downloads/setMediaScanResults', mediaScanResults)
|
2021-10-28 09:37:31 -05:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
// // Filter out media folders without any audio files
|
|
|
|
// var mediaFolders = mediaScanResults.folders.filter((sr) => {
|
|
|
|
// if (!sr.files) return false
|
|
|
|
// var audioFiles = sr.files.filter((mf) => !!mf.isAudio)
|
|
|
|
// return audioFiles.length
|
|
|
|
// })
|
2021-09-12 18:37:08 -05:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
// downloads.forEach((download) => {
|
|
|
|
// var mediaFolder = mediaFolders.find((mf) => mf.name === download.folderName)
|
|
|
|
// if (mediaFolder) {
|
|
|
|
// console.log('Found download ' + download.folderName)
|
|
|
|
// if (download.isMissing) {
|
|
|
|
// download.isMissing = false
|
|
|
|
// this.$store.commit('downloads/addUpdateDownload', download)
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// console.error('Download not found ' + download.folderName)
|
|
|
|
// if (!download.isMissing) {
|
|
|
|
// download.isMissing = true
|
|
|
|
// this.$store.commit('downloads/addUpdateDownload', download)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// },
|
2022-04-02 19:43:43 -05:00
|
|
|
onItemDownloadUpdate(data) {
|
|
|
|
console.log('ON ITEM DOWNLOAD UPDATE', JSON.stringify(data))
|
|
|
|
},
|
|
|
|
onItemDownloadComplete(data) {
|
|
|
|
console.log('ON ITEM DOWNLOAD COMPLETE', JSON.stringify(data))
|
|
|
|
},
|
2021-09-19 18:44:10 -05:00
|
|
|
async initMediaStore() {
|
2021-09-12 18:37:08 -05:00
|
|
|
// Request and setup listeners for media files on native
|
2022-04-04 19:08:27 -05:00
|
|
|
AbsDownloader.addListener('onItemDownloadUpdate', (data) => {
|
2022-04-02 19:43:43 -05:00
|
|
|
this.onItemDownloadUpdate(data)
|
2021-09-19 18:44:10 -05:00
|
|
|
})
|
2022-04-04 19:08:27 -05:00
|
|
|
AbsDownloader.addListener('onItemDownloadComplete', (data) => {
|
2022-04-02 19:43:43 -05:00
|
|
|
this.onItemDownloadComplete(data)
|
2021-09-12 18:37:08 -05:00
|
|
|
})
|
|
|
|
},
|
2021-12-05 18:31:47 -06:00
|
|
|
async loadSavedSettings() {
|
|
|
|
var userSavedServerSettings = await this.$localStore.getServerSettings()
|
|
|
|
if (userSavedServerSettings) {
|
|
|
|
this.$store.commit('setServerSettings', userSavedServerSettings)
|
|
|
|
}
|
|
|
|
|
|
|
|
var userSavedSettings = await this.$localStore.getUserSettings()
|
|
|
|
if (userSavedSettings) {
|
|
|
|
this.$store.commit('user/setSettings', userSavedSettings)
|
|
|
|
}
|
|
|
|
},
|
2021-11-14 19:59:34 -06:00
|
|
|
async attemptConnection() {
|
|
|
|
if (!this.networkConnected) {
|
|
|
|
console.warn('No network connection')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
var deviceData = await this.$db.getDeviceData()
|
|
|
|
var serverConfig = null
|
|
|
|
if (deviceData && deviceData.lastServerConnectionConfigId && deviceData.serverConnectionConfigs.length) {
|
|
|
|
serverConfig = deviceData.serverConnectionConfigs.find((scc) => scc.id == deviceData.lastServerConnectionConfigId)
|
2021-11-14 19:59:34 -06:00
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
if (!serverConfig) {
|
|
|
|
// No last server config set
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var authRes = await this.$axios.$post(`${serverConfig.address}/api/authorize`, null, { headers: { Authorization: `Bearer ${serverConfig.token}` } }).catch((error) => {
|
|
|
|
console.error('[Server] Server auth failed', error)
|
|
|
|
var errorMsg = error.response ? error.response.data || 'Unknown Error' : 'Unknown Error'
|
|
|
|
this.error = errorMsg
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if (!authRes) return
|
|
|
|
|
|
|
|
const { user, userDefaultLibraryId } = authRes
|
|
|
|
if (userDefaultLibraryId) {
|
|
|
|
this.$store.commit('libraries/setCurrentLibrary', userDefaultLibraryId)
|
|
|
|
}
|
|
|
|
var serverConnectionConfig = await this.$db.setServerConnectionConfig(serverConfig)
|
|
|
|
|
|
|
|
this.$store.commit('user/setUser', user)
|
|
|
|
this.$store.commit('user/setServerConnectionConfig', serverConnectionConfig)
|
|
|
|
|
|
|
|
this.$socket.connect(serverConnectionConfig.address, serverConnectionConfig.token)
|
|
|
|
|
|
|
|
console.log('Successful connection on last saved connection config', JSON.stringify(serverConnectionConfig))
|
|
|
|
await this.initLibraries()
|
2021-12-05 18:31:47 -06:00
|
|
|
},
|
2022-03-23 17:59:14 -05:00
|
|
|
itemRemoved(libraryItem) {
|
|
|
|
if (this.$route.name.startsWith('item')) {
|
|
|
|
if (this.$route.params.id === libraryItem.id) {
|
2021-12-05 18:31:47 -06:00
|
|
|
this.$router.replace(`/bookshelf`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
userLoggedOut() {
|
|
|
|
// Only cancels stream if streamining not playing downloaded
|
2022-04-02 12:12:00 -05:00
|
|
|
this.$eventBus.$emit('close-stream')
|
2021-12-05 18:31:47 -06:00
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
socketConnectionUpdate(isConnected) {
|
|
|
|
console.log('Socket connection update', isConnected)
|
2021-12-05 18:31:47 -06:00
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
socketConnectionFailed(err) {
|
|
|
|
this.$toast.error('Socket connection error: ' + err.message)
|
|
|
|
},
|
|
|
|
socketInit(data) {},
|
|
|
|
async initLibraries() {
|
|
|
|
await this.$store.dispatch('libraries/load')
|
|
|
|
this.$eventBus.$emit('library-changed')
|
|
|
|
this.$store.dispatch('libraries/fetch', this.currentLibraryId)
|
2021-09-12 18:37:08 -05:00
|
|
|
}
|
2021-09-01 20:07:11 -05:00
|
|
|
},
|
2021-11-14 19:59:34 -06:00
|
|
|
async mounted() {
|
2022-04-03 14:24:17 -05:00
|
|
|
// this.$server.on('logout', this.userLoggedOut)
|
|
|
|
// this.$server.on('connected', this.connected)
|
|
|
|
// this.$server.on('connectionFailed', this.socketConnectionFailed)
|
|
|
|
// this.$server.on('initialStream', this.initialStream)
|
|
|
|
// this.$server.on('show_error_toast', this.showErrorToast)
|
|
|
|
// this.$server.on('show_success_toast', this.showSuccessToast)
|
2021-09-01 20:07:11 -05:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
this.$socket.on('connection-update', this.socketConnectionUpdate)
|
|
|
|
this.$socket.on('initialized', this.socketInit)
|
2021-09-01 20:07:11 -05:00
|
|
|
|
2021-09-19 18:44:10 -05:00
|
|
|
if (this.$store.state.isFirstLoad) {
|
|
|
|
this.$store.commit('setIsFirstLoad', false)
|
2021-11-21 06:54:10 -06:00
|
|
|
await this.$store.dispatch('setupNetworkListener')
|
2022-04-03 14:24:17 -05:00
|
|
|
|
|
|
|
if (this.$store.state.user.serverConnectionConfig) {
|
|
|
|
await this.initLibraries()
|
|
|
|
} else {
|
|
|
|
await this.attemptConnection()
|
|
|
|
}
|
|
|
|
|
2021-09-19 18:44:10 -05:00
|
|
|
this.checkForUpdate()
|
2021-12-05 18:31:47 -06:00
|
|
|
this.loadSavedSettings()
|
2021-09-19 18:44:10 -05:00
|
|
|
this.initMediaStore()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
2022-04-03 14:24:17 -05:00
|
|
|
this.$socket.off('connection-update', this.socketConnectionUpdate)
|
|
|
|
this.$socket.off('initialized', this.socketInit)
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|