put player into seperate store module

This commit is contained in:
Jonas Heinrich 2021-01-16 13:12:44 +01:00
parent 9f0c480248
commit 855b5b2efe
6 changed files with 160 additions and 484 deletions

View File

@ -8,6 +8,8 @@
[235](https://git.project-insanity.org/onny/nextcloud-app-radio/-/issues/235) @onny
- Update license year to 2021
[234](https://git.project-insanity.org/onny/nextcloud-app-radio/-/issues/234) @onny
- Move menuState and volumeState into localStorage
[236](https://git.project-insanity.org/onny/nextcloud-app-radio/-/issues/236) @onny
## 1.0.1 - 2020-12
### Added

View File

@ -1,483 +0,0 @@
<!--
- @copyright Copyright (c) 2021 Jonas Heinrich
-
- @author Jonas Heinrich <onny@project-insanity.org>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<Content app-name="radio">
<Navigation
:station-data="tableData" />
<AppContent>
<Table
v-show="!pageLoading && tableData.length > 0"
v-resize="onResize"
:station-data="tableData"
:favorites="favorites"
@doPlay="doPlay"
@doFavor="doFavor"
@toggleSidebar="toggleSidebar" />
<EmptyContent
v-if="pageLoading"
icon="icon-loading" />
<EmptyContent
v-if="tableData.length === 0 && !pageLoading"
:icon="emptyContentIcon">
{{ emptyContentMessage }}
<template #desc>
{{ emptyContentDesc }}
</template>
</EmptyContent>
</AppContent>
<Sidebar
:show-sidebar="showSidebar"
:sidebar-station="sidebarStation"
@toggleSidebar="toggleSidebar" />
</Content>
</template>
<script>
import Content from '@nextcloud/vue/dist/Components/Content'
import AppContent from '@nextcloud/vue/dist/Components/AppContent'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import Navigation from './Navigation'
import Table from './Table'
import Sidebar from './Sidebar'
import { Howl, Howler } from 'howler'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
let audioPlayer = null
const requesttoken = axios.defaults.headers.requesttoken
export default {
name: 'Main',
components: {
Navigation,
Content,
AppContent,
Table,
EmptyContent,
Sidebar,
},
data: () => ({
tableData: [],
pageLoading: false,
blurHashes: require('../assets/blurHashes.json'),
favorites: [],
showSidebar: false,
sidebarStation: {},
queryParams: {},
}),
computed: {
player() {
return this.$store.state.player
},
emptyContentMessage() {
if (this.$route.name === 'FAVORITES') {
return t('radio', 'No favorites yet')
} else if (this.$route.name === 'RECENT') {
return t('radio', 'No recent stations yet')
} else if (this.$route.name === 'SEARCH') {
return t('radio', 'No search results')
}
return 'No stations here'
},
emptyContentIcon() {
if (this.$route.name === 'FAVORITES') {
return 'icon-star'
} else if (this.$route.name === 'RECENT') {
return 'icon-recent'
} else if (this.$route.name === 'SEARCH') {
return 'icon-search'
}
return 'icon-radio'
},
emptyContentDesc() {
if (this.$route.name === 'FAVORITES') {
return t('radio', 'Stations you mark as favorite will show up here')
} else if (this.$route.name === 'RECENT') {
return t('radio', 'Stations you recently played will show up here')
} else if (this.$route.name === 'SEARCH') {
return t('radio', 'No stations were found matching your search term')
}
return t('radio', 'No stations here')
},
},
watch: {
$route: 'onRoute',
'player.volume'(newVolume, oldVolume) {
if (audioPlayer !== null) {
audioPlayer.volume(newVolume)
}
},
'player.isPaused'(newState, oldState) {
if (newState === true && audioPlayer !== null) {
audioPlayer.pause()
} else if (newState === false && audioPlayer !== null) {
audioPlayer.play()
}
},
},
created() {
this.loadSettings()
this.loadFavorites()
},
mounted() {
this.onRoute()
this.scroll()
},
methods: {
onResize({ width, height }) {
const contentHeight = document.getElementById('app-content-vue').scrollHeight
const tableHeight = height
if (tableHeight < contentHeight) {
this.preFill()
}
},
preFill() {
const route = this.$route
this.loadStations(route.name)
},
async onRoute() {
this.tableData = []
this.pageLoading = true
const route = this.$route
this.loadStations(route.name)
},
/**
* Favor a new station by sending the information to the server
* @param {Object} station Station object
*/
async doFavor(station) {
if (this.favorites.flat().includes(station.stationuuid)) {
let stationid = null
try {
for (let i = 0, len = this.favorites.length; i < len; i++) {
if (station.stationuuid === this.favorites[i][1]) {
stationid = this.favorites[i][0]
}
}
axios.defaults.headers.requesttoken = requesttoken
await axios
.delete(generateUrl(`/apps/radio/api/favorites/${stationid}`))
.then(response => {
this.favorites = this.favorites.filter(item => item[1] !== station.stationuuid)
})
} catch (error) {
showError(t('radio', 'Could not remove station from favorites'))
}
} else {
try {
let stationSrc = ''
if (!station.url_resolved) {
stationSrc = station.urlresolved
} else {
stationSrc = station.url_resolved
}
const stationMap = {
id: -1,
name: station.name.toString(),
urlresolved: stationSrc.toString(),
favicon: station.favicon.toString(),
stationuuid: station.stationuuid.toString(),
bitrate: station.bitrate.toString(),
country: station.country.toString(),
language: station.language.toString(),
homepage: station.homepage.toString(),
codec: station.codec.toString(),
tags: station.tags.toString(),
}
axios.defaults.headers.requesttoken = requesttoken
await axios
.post(generateUrl('/apps/radio/api/favorites'), stationMap)
.then(response => {
this.favorites.push([response.data.id, station.stationuuid])
})
} catch (error) {
showError(t('radio', 'Could not favor station'))
}
}
},
/**
* Start playing a radio station and counting the playback
* @param {Object} station Station object
*/
async doPlay(station) {
const vm = this
vm.$store.dispatch('isBuffering', true)
if (audioPlayer !== null) {
audioPlayer.fade(vm.player.volume, 0, 500)
}
vm.$store.dispatch('setTitle', station.name)
let stationSrc = ''
if (!station.url_resolved) {
stationSrc = station.urlresolved
} else {
stationSrc = station.url_resolved
}
Howler.unload()
audioPlayer = new Howl({
src: stationSrc,
html5: true,
volume: vm.player.volume,
onplay() {
vm.$store.dispatch('isPlaying', true)
vm.$store.dispatch('isBuffering', false)
},
onpause() {
vm.$store.dispatch('isPlaying', false)
vm.$store.dispatch('isBuffering', false)
},
onend() {
showError(t('radio', 'Lost connection to radio station, retrying ...'))
vm.$store.dispatch('isPlaying', false)
vm.$store.dispatch('isBuffering', true)
},
})
audioPlayer.unload()
audioPlayer.play()
audioPlayer.fade(0, vm.player.volume, 500)
/* Count click */
try {
delete axios.defaults.headers.requesttoken
axios.get(this.$apiUrl + '/json/url/' + station.stationuuid)
} catch (error) {
showError(t('radio', 'Unable to count play on remote API'))
}
/* Put into recent stations */
try {
let stationSrc = ''
if (!station.url_resolved) {
stationSrc = station.urlresolved
} else {
stationSrc = station.url_resolved
}
const stationMap = {
id: -1,
name: station.name.toString(),
urlresolved: stationSrc.toString(),
favicon: station.favicon.toString(),
stationuuid: station.stationuuid.toString(),
bitrate: station.bitrate.toString(),
country: station.country.toString(),
language: station.language.toString(),
homepage: station.homepage.toString(),
codec: station.codec.toString(),
tags: station.tags.toString(),
}
axios.defaults.headers.requesttoken = requesttoken
await axios
.post(generateUrl('/apps/radio/api/recent'), stationMap)
} catch (error) {
showError(t('radio', 'Could not add station to recent list'))
}
},
async loadStations(menuState = 'TOP') {
const vm = this
const queryBase = this.$apiUrl + '/json/stations'
let queryURI = queryBase
let sortBy = 'clickcount'
if (vm.$route.name === 'CATEGORIES') {
if (vm.$route.path === '/categories') {
vm.tableData = [
{
name: t('radio', 'Countries'),
type: 'folder',
path: '/categories/countries',
},
{
name: t('radio', 'States'),
type: 'folder',
path: '/categories/states',
},
{
name: t('radio', 'Languages'),
type: 'folder',
path: '/categories/languages',
},
{
name: t('radio', 'Tags'),
type: 'folder',
path: '/categories/tags',
},
]
vm.pageLoading = false
return true
} else if (vm.$route.params.category === 'tags') {
if (vm.$route.params.query) {
queryURI = this.$apiUrl + '/json/stations/search?tag=' + vm.$route.params.query + '&tagExact=true'
} else {
queryURI = this.$apiUrl + '/json/tags'
}
} else if (vm.$route.params.category === 'countries') {
if (vm.$route.params.query) {
queryURI = this.$apiUrl + '/json/stations/search?country=' + vm.$route.params.query + '&countryExact=true'
} else {
queryURI = this.$apiUrl + '/json/countries'
}
} else if (vm.$route.params.category === 'states') {
if (vm.$route.params.query) {
queryURI = this.$apiUrl + '/json/stations/search?state=' + vm.$route.params.query + '&stateExact=true'
} else {
queryURI = this.$apiUrl + '/json/states'
}
} else if (vm.$route.params.category === 'languages') {
if (vm.$route.params.query) {
queryURI = this.$apiUrl + '/json/stations/search?language=' + vm.$route.params.query + '&languageExact=true'
} else {
queryURI = this.$apiUrl + '/json/languages'
}
}
}
// Skip loading more stations on certain sites
if (vm.tableData.length > 0
&& (vm.$route.name === 'FAVORITES'
|| vm.$route.name === 'RECENT'
|| vm.$route.name === 'CATEGORIES')) {
return true
}
if (menuState === 'TOP') {
sortBy = 'clickcount'
} else if (menuState === 'NEW') {
sortBy = 'lastchangetime'
} else if (menuState === 'SEARCH') {
const searchQuery = vm.$route.params.query
queryURI = queryBase + '/byname/' + searchQuery
} else if (menuState === 'FAVORITES') {
queryURI = generateUrl('/apps/radio/api/favorites')
} else if (menuState === 'RECENT') {
queryURI = generateUrl('/apps/radio/api/recent')
}
if (menuState !== 'CATEGORIES') {
vm.queryParams = {
limit: 20,
order: sortBy,
reverse: true,
offset: vm.tableData.length,
}
} else {
vm.queryParams = {}
}
try {
if (menuState === 'FAVORITES' || menuState === 'RECENT') {
axios.defaults.headers.requesttoken = requesttoken
} else {
delete axios.defaults.headers.requesttoken
}
await axios.get(queryURI, {
params: vm.queryParams,
})
.then(function(response) {
for (let i = 0; i < response.data.length; i++) {
const obj = response.data[i]
if (obj.stationuuid) {
let blurHash = vm.blurHashes[obj.stationuuid]
if (!blurHash) {
blurHash = 'L1TSUA?bj[?b~qfQfQj[ayfQfQfQ'
}
response.data[i].blurHash = blurHash
} else {
response.data[i].type = 'folder'
response.data[i].path = vm.$route.path + '/' + obj.name
}
}
vm.tableData = vm.tableData.concat(response.data)
vm.pageLoading = false
})
} catch (error) {
showError(t('radio', 'Could not fetch stations from remote API'))
}
},
/**
* On scroll event, load more stations if bottom reached
*/
scroll() {
window.onscroll = () => {
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
const route = this.$route
this.loadStations(route.name)
}
}
},
loadSettings() {
axios.defaults.headers.common = {
'User-Agent': 'Nextcloud Radio App/' + this.$version,
}
},
async loadFavorites() {
const vm = this
try {
axios.defaults.headers.requesttoken = requesttoken
await axios.get(generateUrl('/apps/radio/api/favorites'))
.then(function(response) {
const favorites = []
for (let i = 0, len = response.data.length; i < len; i++) {
favorites.push([response.data[i].id, response.data[i].stationuuid])
}
vm.favorites = favorites
})
} catch (error) {
showError(t('radio', 'Unable to load favorites'))
}
},
toggleSidebar(station = null) {
if (station) {
this.showSidebar = true
this.sidebarStation = station
} else {
this.showSidebar = false
}
},
},
}
</script>
<style>
@media only screen and (min-width: 1024px) {
.app-navigation-toggle {
display: none;
}
}
</style>

View File

@ -24,7 +24,7 @@ import Vue from 'vue'
import Router from 'vue-router'
import { generateUrl } from '@nextcloud/router'
import Main from './components/Main'
import Main from './views/Main'
import store from './store/main.js'
Vue.use(Router)

21
src/store/favorites.js Normal file
View File

@ -0,0 +1,21 @@
/*
* @copyright Copyright (c) 2021 Jonas Heinrich <onny@project-insanity.org>
*
* @author Jonas Heinrich <onny@project-insanity.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

49
src/store/main.js Normal file
View File

@ -0,0 +1,49 @@
/*
* @copyright Copyright (c) 2021 Jonas Heinrich <onny@project-insanity.org>
*
* @author Jonas Heinrich <onny@project-insanity.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Vue from 'vue'
import Vuex from 'vuex'
import player from './player'
import favorites from './favorites'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
player,
favorites,
},
state: {
menu: localStorage.getItem('radio.menu') || 'top',
},
mutations: {
setMenuState(state, menuState) {
localStorage.setItem('radio.menu', menuState)
state.menu = menuState
},
},
actions: {
setMenuState(context, menuState) {
context.commit('setMenuState', menuState)
},
},
})

87
src/store/player.js Normal file
View File

@ -0,0 +1,87 @@
/*
* @copyright Copyright (c) 2021 Jonas Heinrich <onny@project-insanity.org>
*
* @author Jonas Heinrich <onny@project-insanity.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
export default ({
state: {
isPlaying: false,
isBuffering: false,
isMute: false,
isPaused: false,
volume: localStorage.getItem('radio.volume') || 0.5,
oldVolume: localStorage.getItem('radio.volume') || 0.5,
title: '',
},
mutations: {
setPlaying(state, playerState) {
state.isPlaying = playerState
},
setBuffering(state, bufferingState) {
state.isBuffering = bufferingState
},
setVolume(state, volume) {
state.volume = volume
localStorage.setItem('radio.volume', volume)
},
toggleMute(state) {
if (state.isMute) {
state.volume = state.player.oldVolume
state.isMute = false
} else {
state.oldVolume = state.player.volume
state.volume = 0
state.isMute = true
}
},
togglePlay(state) {
if (state.isPlaying) {
state.isPlaying = false
state.isPaused = true
} else {
state.isPlaying = true
state.isPaused = false
}
},
setTitle(state, title) {
state.title = title
},
},
actions: {
setPlaying(context, playerState) {
context.commit('setPlaying', playerState)
},
setBuffering(context, bufferingState) {
context.commit('setBuffering', bufferingState)
},
setVolume(context, volume) {
context.commit('setVolume', volume)
},
toggleMute(context) {
context.commit('toggleMute')
},
togglePlay(context) {
context.commit('togglePlay')
},
setTitle(context, title) {
context.commit('setTitle', title)
},
},
})