Refactor capacitor plugins, clean up and organize android plugin classes

This commit is contained in:
advplyr
2022-04-04 19:08:27 -05:00
parent cc744bb975
commit 77ef0c119b
26 changed files with 376 additions and 385 deletions
+14 -14
View File
@@ -91,7 +91,7 @@
<script>
import { Capacitor } from '@capacitor/core'
import MyNativeAudio from '@/plugins/my-native-audio'
import { AbsAudioPlayer } from '@/plugins/capacitor'
export default {
props: {
@@ -266,7 +266,7 @@ export default {
},
castClick() {
console.log('Cast Btn Click')
MyNativeAudio.requestSession()
AbsAudioPlayer.requestSession()
},
clickContainer() {
this.showFullscreen = true
@@ -308,18 +308,18 @@ export default {
setPlaybackSpeed(speed) {
console.log(`[AudioPlayer] Set Playback Rate: ${speed}`)
this.currentPlaybackRate = speed
MyNativeAudio.setPlaybackSpeed({ speed: speed })
AbsAudioPlayer.setPlaybackSpeed({ speed: speed })
},
restart() {
this.seek(0)
},
backward10() {
if (this.isLoading) return
MyNativeAudio.seekBackward({ amount: '10000' })
AbsAudioPlayer.seekBackward({ amount: '10000' })
},
forward10() {
if (this.isLoading) return
MyNativeAudio.seekForward({ amount: '10000' })
AbsAudioPlayer.seekForward({ amount: '10000' })
},
setStreamReady() {
this.readyTrackWidth = this.trackWidth
@@ -421,7 +421,7 @@ export default {
this.seekedTime = time
this.seekLoading = true
MyNativeAudio.seekPlayer({ timeMs: String(Math.floor(time * 1000)) })
AbsAudioPlayer.seekPlayer({ timeMs: String(Math.floor(time * 1000)) })
if (this.$refs.playedTrack) {
var perc = time / this.totalDuration
@@ -466,19 +466,19 @@ export default {
}
},
play() {
MyNativeAudio.playPlayer()
AbsAudioPlayer.playPlayer()
this.startPlayInterval()
this.isPlaying = true
},
pause() {
MyNativeAudio.pausePlayer()
AbsAudioPlayer.pausePlayer()
this.stopPlayInterval()
this.isPlaying = false
},
startPlayInterval() {
clearInterval(this.playInterval)
this.playInterval = setInterval(async () => {
var data = await MyNativeAudio.getCurrentTime()
var data = await AbsAudioPlayer.getCurrentTime()
this.currentTime = Number((data.value / 1000).toFixed(2))
this.bufferedTime = Number((data.bufferedTime / 1000).toFixed(2))
console.log('[AudioPlayer] Got Current Time', this.currentTime)
@@ -494,7 +494,7 @@ export default {
},
terminateStream() {
if (!this.playbackSession) return
MyNativeAudio.terminateStream()
AbsAudioPlayer.terminateStream()
},
onPlayingUpdate(data) {
console.log('onPlayingUpdate', JSON.stringify(data))
@@ -542,10 +542,10 @@ export default {
async init() {
this.useChapterTrack = await this.$localStore.getUseChapterTrack()
this.onPlaybackSessionListener = MyNativeAudio.addListener('onPlaybackSession', this.onPlaybackSession)
this.onPlaybackClosedListener = MyNativeAudio.addListener('onPlaybackClosed', this.onPlaybackClosed)
this.onPlayingUpdateListener = MyNativeAudio.addListener('onPlayingUpdate', this.onPlayingUpdate)
this.onMetadataListener = MyNativeAudio.addListener('onMetadata', this.onMetadata)
this.onPlaybackSessionListener = AbsAudioPlayer.addListener('onPlaybackSession', this.onPlaybackSession)
this.onPlaybackClosedListener = AbsAudioPlayer.addListener('onPlaybackClosed', this.onPlaybackClosed)
this.onPlayingUpdateListener = AbsAudioPlayer.addListener('onPlayingUpdate', this.onPlayingUpdate)
this.onMetadataListener = AbsAudioPlayer.addListener('onMetadata', this.onMetadata)
},
handleGesture() {
var touchDistance = this.touchEndY - this.touchStartY
+8 -8
View File
@@ -11,7 +11,7 @@
</template>
<script>
import MyNativeAudio from '@/plugins/my-native-audio'
import { AbsAudioPlayer } from '@/plugins/capacitor'
export default {
data() {
@@ -92,21 +92,21 @@ export default {
},
async selectSleepTimeout({ time, isChapterTime }) {
console.log('Setting sleep timer', time, isChapterTime)
var res = await MyNativeAudio.setSleepTimer({ time: String(time), isChapterTime })
var res = await AbsAudioPlayer.setSleepTimer({ time: String(time), isChapterTime })
if (!res.success) {
return this.$toast.error('Sleep timer did not set, invalid time')
}
},
increaseSleepTimer() {
// Default time to increase = 5 min
MyNativeAudio.increaseSleepTime({ time: '300000' })
AbsAudioPlayer.increaseSleepTime({ time: '300000' })
},
decreaseSleepTimer() {
MyNativeAudio.decreaseSleepTime({ time: '300000' })
AbsAudioPlayer.decreaseSleepTime({ time: '300000' })
},
async cancelSleepTimer() {
console.log('Canceling sleep timer')
await MyNativeAudio.cancelSleepTimer()
await AbsAudioPlayer.cancelSleepTimer()
},
streamClosed() {
console.log('Stream Closed')
@@ -168,7 +168,7 @@ export default {
}
},
async playLibraryItem(libraryItemId) {
MyNativeAudio.prepareLibraryItem({ libraryItemId, playWhenReady: true })
AbsAudioPlayer.prepareLibraryItem({ libraryItemId, playWhenReady: true })
.then((data) => {
console.log('TEST library item play response', JSON.stringify(data))
})
@@ -178,8 +178,8 @@ export default {
}
},
mounted() {
this.onSleepTimerEndedListener = MyNativeAudio.addListener('onSleepTimerEnded', this.onSleepTimerEnded)
this.onSleepTimerSetListener = MyNativeAudio.addListener('onSleepTimerSet', this.onSleepTimerSet)
this.onSleepTimerEndedListener = AbsAudioPlayer.addListener('onSleepTimerEnded', this.onSleepTimerEnded)
this.onSleepTimerSetListener = AbsAudioPlayer.addListener('onSleepTimerSet', this.onSleepTimerSet)
this.playbackSpeed = this.$store.getters['user/getUserSetting']('playbackRate')
console.log(`[AudioPlayerContainer] Init Playback Speed: ${this.playbackSpeed}`)
+27 -84
View File
@@ -46,8 +46,8 @@ export default {
}
},
computed: {
isSocketConnected() {
return this.$store.state.socketConnected
user() {
return this.$store.state.user.user
},
isBookEntity() {
return this.entityName === 'books' || this.entityName === 'series-books'
@@ -62,9 +62,6 @@ export default {
hasFilter() {
return this.filterBy !== 'all'
},
books() {
return this.$store.getters['downloads/getAudiobooks']
},
orderBy() {
return this.$store.getters['user/getUserSetting']('mobileOrderBy')
},
@@ -110,12 +107,6 @@ export default {
totalEntityCardWidth() {
// Includes margin
return this.entityWidth + 24
},
downloads() {
return this.$store.getters['downloads/getDownloads']
},
downloadedBooks() {
return []
}
},
methods: {
@@ -132,19 +123,6 @@ export default {
if (!this.initialized) {
this.currentSFQueryString = this.buildSearchParams()
}
// var entityPath = this.entityName === 'books' ? `books/all` : this.entityName
// var sfQueryString = this.currentSFQueryString ? this.currentSFQueryString + '&' : ''
// var queryString = `?${sfQueryString}&limit=${this.booksPerFetch}&page=${page}`
// if (this.entityName === 'series-books') {
// entityPath = `series/${this.seriesId}`
// queryString = ''
// }
// var payload = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/${entityPath}${queryString}`).catch((error) => {
// console.error('failed to fetch books', error)
// return null
// })
var entityPath = this.entityName === 'books' || this.entityName === 'series-books' ? `items` : this.entityName
var sfQueryString = this.currentSFQueryString ? this.currentSFQueryString + '&' : ''
@@ -174,12 +152,12 @@ export default {
for (let i = 0; i < payload.results.length; i++) {
if (this.entityName === 'books' || this.entityName === 'series-books') {
// Check if has download and append download obj
var download = this.downloads.find((dl) => dl.id === payload.results[i].id)
if (download) {
var dl = { ...download }
delete dl.audiobook
payload.results[i].download = dl
}
// var download = this.downloads.find((dl) => dl.id === payload.results[i].id)
// if (download) {
// var dl = { ...download }
// delete dl.audiobook
// payload.results[i].download = dl
// }
}
var index = i + startIndex
@@ -191,6 +169,10 @@ export default {
}
},
async loadPage(page) {
if (!this.currentLibraryId) {
console.error('[LazyBookshelf] loadPage current library id not set')
return
}
this.pagesLoaded[page] = true
await this.fetchEntities(page)
},
@@ -241,7 +223,7 @@ export default {
},
setDownloads() {
if (this.entityName === 'books') {
this.entities = this.downloadedBooks
this.entities = []
// TOOD: Sort and filter here
this.totalEntities = this.entities.length
this.totalShelves = Math.ceil(this.totalEntities / this.entitiesPerShelf)
@@ -269,14 +251,12 @@ export default {
this.initialized = false
this.initSizeData()
if (this.isSocketConnected) {
if (this.user) {
await this.loadPage(0)
var lastBookIndex = Math.min(this.totalEntities, this.shelvesPerPage * this.entitiesPerShelf)
this.mountEntites(0, lastBookIndex)
} else {
this.setDownloads()
this.mountEntites(0, this.totalEntities - 1)
// Local only
}
},
remountEntities() {
@@ -321,28 +301,12 @@ export default {
var lastBookIndex = Math.min(this.totalEntities, this.shelvesPerPage * this.entitiesPerShelf)
this.mountEntites(0, lastBookIndex)
},
initDownloads() {
this.initSizeData()
this.setDownloads()
this.$nextTick(() => {
console.log('Mounting downloads', this.totalEntities, 'total shelves', this.totalShelves)
this.mountEntites(0, this.totalEntities)
})
},
scroll(e) {
if (!e || !e.target) return
if (!this.isSocketConnected) return // Offline books are all mounted at once
if (!this.user) return
var { scrollTop } = e.target
this.handleScroll(scrollTop)
},
socketInit(isConnected) {
if (isConnected) {
this.init()
} else {
this.isFirstInit = false
this.resetEntities()
}
},
buildSearchParams() {
let searchParams = new URLSearchParams()
if (this.filterBy && this.filterBy !== 'all') {
@@ -383,11 +347,6 @@ export default {
this.resetEntities()
}
},
downloadsLoaded() {
if (!this.isSocketConnected) {
this.resetEntities()
}
},
libraryItemAdded(libraryItem) {
console.log('libraryItem added', libraryItem)
// TODO: Check if item would be on this shelf
@@ -433,18 +392,13 @@ export default {
}
this.$eventBus.$on('library-changed', this.libraryChanged)
this.$eventBus.$on('downloads-loaded', this.downloadsLoaded)
this.$store.commit('user/addSettingsListener', { id: 'lazy-bookshelf', meth: this.settingsUpdated })
// if (this.$server.socket) {
// this.$server.socket.on('item_updated', this.libraryItemUpdated)
// this.$server.socket.on('item_added', this.libraryItemAdded)
// this.$server.socket.on('item_removed', this.libraryItemRemoved)
// this.$server.socket.on('items_updated', this.libraryItemsUpdated)
// this.$server.socket.on('items_added', this.libraryItemsAdded)
// } else {
// console.error('Bookshelf - Socket not initialized')
// }
this.$socket.$on('item_updated', this.libraryItemUpdated)
this.$socket.$on('item_added', this.libraryItemAdded)
this.$socket.$on('item_removed', this.libraryItemRemoved)
this.$socket.$on('items_updated', this.libraryItemsUpdated)
this.$socket.$on('items_added', this.libraryItemsAdded)
},
removeListeners() {
var bookshelf = document.getElementById('bookshelf-wrapper')
@@ -453,31 +407,20 @@ export default {
}
this.$eventBus.$off('library-changed', this.libraryChanged)
this.$eventBus.$off('downloads-loaded', this.downloadsLoaded)
this.$store.commit('user/removeSettingsListener', 'lazy-bookshelf')
// if (this.$server.socket) {
// this.$server.socket.off('item_updated', this.libraryItemUpdated)
// this.$server.socket.off('item_added', this.libraryItemAdded)
// this.$server.socket.off('item_removed', this.libraryItemRemoved)
// this.$server.socket.off('items_updated', this.libraryItemsUpdated)
// this.$server.socket.off('items_added', this.libraryItemsAdded)
// } else {
// console.error('Bookshelf - Socket not initialized')
// }
this.$socket.$off('item_updated', this.libraryItemUpdated)
this.$socket.$off('item_added', this.libraryItemAdded)
this.$socket.$off('item_removed', this.libraryItemRemoved)
this.$socket.$off('items_updated', this.libraryItemsUpdated)
this.$socket.$off('items_added', this.libraryItemsAdded)
}
},
mounted() {
// if (this.$server.initialized) {
// this.init()
// } else {
// this.initDownloads()
// }
this.$socket.on('initialized', this.socketInit)
this.init()
this.initListeners()
},
beforeDestroy() {
this.$socket.off('initialized', this.socketInit)
this.removeListeners()
}
}