A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 9a2cf6f0579a3452ce30253faa102d9a2b0cf553 (8213 bytes)[doc][blame][raw]
1
import {state} from '../main.js';2
import {http, $lang} from '../utils.js';3
import VButton from '../element/button.js';4
import VIcon from '../element/icon.js';6
export default {7
props: ['station'],8
data() {9
return {10
playing: false,11
loading: false,12
error: false,13
title: '- No song title available -',14
interval: null,15
resume: false,16
state17
};18
},19
components: {20
VButton,21
VIcon22
},23
watch: {24
station: function(station) {25
this.playing = false;26
this.error = false;27
if(station !== null) {28
this.loading = true;29
this.title = '- No song title available -';30
this.updateMediaSession();31
state.open = true;32
}33
}34
},35
methods: {36
$lang,37
async setLike(is_favorite) {38
try {39
await http`${is_favorite ? 'post' : 'delete'}::/api/favorite`(this.station.id);40
this.station.is_favorite = is_favorite;41
state.app.$refs.favorites.doFetch();42
}43
catch(error) {44
console.log(error);45
}46
},47
setPlaying(playing) {48
if(this.error || this.loading) {49
return;50
}51
this.playing = playing;52
this.$refs.player[this.playing ? 'play' : 'pause']();53
if(this.playing) {54
this.playing = false;55
this.loading = true;56
this.$refs.player.load();57
}58
},59
setVolume(event) {60
this.$refs.player.volume = event.target.value;61
},62
onPlay() {63
if(this.resume) {64
this.playing = false;65
this.loading = true;66
this.$refs.player.load();67
}68
},69
onPause() {70
this.playing = false;71
this.resume = true;72
},73
onLoad() {74
this.loading = false;75
this.playing = true;76
this.resume = false;77
this.updateSong();78
},79
onError() {80
this.error = true;81
this.loading = false;82
this.playing = false;83
},84
updateMediaSession() {85
if('mediaSession' in navigator) {86
navigator.mediaSession.metadata = new MediaMetadata({87
title: this.title,88
artist: this.station.name,89
artwork: [90
{ src: this.station.icon, sizes: '96x96', type: 'image/jpg' },91
{ src: this.station.icon, sizes: '128x128', type: 'image/jpg' },92
{ src: this.station.icon, sizes: '192x192', type: 'image/jpg' },93
{ src: this.station.icon, sizes: '256x256', type: 'image/jpg' },94
{ src: this.station.icon, sizes: '384x384', type: 'image/jpg' },95
{ src: this.station.icon, sizes: '512x512', type: 'image/jpg' }96
]97
});98
}99
},100
async fetchSong() {101
try {102
const song = await http`get::/api/song`(this.station);103
this.title = song === '' ? '- No song title available -' : song;104
}105
catch(err) {106
console.error(err);107
this.title = '- No song title available -';108
}109
this.updateMediaSession();110
},111
updateSong() {112
this.fetchSong();113
clearInterval(this.interval);114
this.interval = setInterval(async () => {115
if(!this.playing) {116
clearInterval(this.interval);117
return;118
}119
await this.fetchSong();120
}, 10000);121
}122
},123
computed: {124
transform() {125
return {126
'transform': `translate(0, ${state.open ? 0 : 100}vh)`127
};128
},129
opacity() {130
return {131
'opacity': `${state.open ? 1 : 0}`,132
'pointer-events': `${state.open ? 'all' : 'none'}`133
};134
}135
},136
template: `137
<div class="z-30">138
<div139
@click="state.open = false"140
class="fixed top-0 bottom-0 left-0 right-0 bg-black/60 transition-opacity duration-300 ease-in-out"141
:style="opacity"142
>143
</div>144
<div145
v-if="station"146
ref="button"147
class="fixed w-[64px] h-[64px] bg-white rounded-full bottom-4 right-4 z-40 cursor-pointer shadow-xl"148
@click="state.open = !state.open"149
>150
<v-icon151
id="playlist"152
class="pointer-events-none"153
>154
</v-icon>155
</div>156
<div157
class="w-full flex flex-col bg-zinc-800 pb-6 rounded-t-2xl fixed left-0 right-0 bottom-0 px-4 pt-4 h-[70vh] transition-transform duration-300 ease-in-out"158
:style="transform"159
>160
<div class="w-[40px] h-[8px] bg-zinc-500 rounded-[4px] mx-auto mb-12"></div>161
<audio162
v-if="station"163
ref="player"164
controls autoplay165
:src="station.url"166
@loadeddata="onLoad"167
@play="onPlay"168
@pause="onPause"169
@error="onError"170
class="select-none pointer-events-none w-0 h-0 opacity-0"171
>172
<source :src="station.url" type="audio/mpeg">173
<source :src="station.url" type="audio/ogg">174
<source :src="station.url" type="audio/aac">175
</audio>176
<div v-if="station" class="flex gap-5 items-center relative flex-col sm:flex-row">177
<div class="w-[140px] h-[140px] min-w-[140px] bg-zinc-900 rounded-lg overflow-hidden z-10">178
<img179
v-if="station.icon"180
:src="station.icon"181
:alt="station.name"182
class="w-full h-full object-contain select-none"183
>184
<v-icon185
v-else186
id="access-point"187
size="38px"188
class="text-gray-400"189
></v-icon>190
</div>191
<div192
class="flex flex-col flex-1 z-10 min-w-0 max-w-full"193
>194
<p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center sm:text-left">195
{{station.name}}196
</p>197
<p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center sm:text-left">198
{{title ?? $lang('player.loading')}}199
</p>200
</div>201
<div class="inline-flex gap-4 z-10 pr-2">202
<input type="range" min="0" max="1" step="0.05" @change="setVolume">203
<v-button204
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"205
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"206
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"207
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"208
@click="setPlaying(!playing)"209
></v-button>210
</div>211
</div>212
</div>213
</div>214
`215
}