A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: f605c43d158abfb99a408fd3508e6f2ae5c8c2f6 (9866 bytes)[doc][blame][raw]
1
import {state, SWIPE_THRESHOLD} 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: null,14
interval: null,15
resume: false,16
open: false17
};18
},19
components: {20
VButton,21
VIcon22
},23
watch: {24
station(value) {25
this.playing = false;26
this.error = false;27
if(value !== null) {28
this.loading = true;29
this.title = null;30
this.updateMediaSession();31
this.open = true;32
}33
},34
open(value) {35
state.open = value;36
}37
},38
methods: {39
$lang,40
setPlaying(playing) {41
if(this.error || this.loading) {42
return;43
}44
this.playing = playing;45
this.$refs.player[this.playing ? 'play' : 'pause']();46
if(this.playing) {47
this.playing = false;48
this.loading = true;49
this.$refs.player.load();50
}51
},52
setVolume(event) {53
this.$refs.player.volume = event.target.value;54
},55
onPlay() {56
if(this.resume) {57
this.playing = false;58
this.loading = true;59
this.$refs.player.load();60
}61
},62
onPause() {63
this.playing = false;64
this.resume = true;65
},66
onLoad() {67
this.loading = false;68
this.playing = true;69
this.resume = false;70
this.updateSong();71
},72
onError() {73
this.error = true;74
this.loading = false;75
this.playing = false;76
},77
updateMediaSession() {78
if('mediaSession' in navigator) {79
navigator.mediaSession.metadata = new MediaMetadata({80
title: this.title ?? $lang('player.no-song-title'),81
artist: this.station.name,82
artwork: [83
{ src: this.station.icon, sizes: '96x96', type: 'image/jpg' },84
{ src: this.station.icon, sizes: '128x128', type: 'image/jpg' },85
{ src: this.station.icon, sizes: '192x192', type: 'image/jpg' },86
{ src: this.station.icon, sizes: '256x256', type: 'image/jpg' },87
{ src: this.station.icon, sizes: '384x384', type: 'image/jpg' },88
{ src: this.station.icon, sizes: '512x512', type: 'image/jpg' }89
]90
});91
}92
},93
async fetchSong() {94
try {95
const song = await http`get::/api/song`(this.station);96
this.title = song === '' ? null : song;97
}98
catch(err) {99
console.error(err);100
clearInterval(this.interval);101
this.title = null;102
}103
this.updateMediaSession();104
},105
updateSong() {106
this.fetchSong();107
clearInterval(this.interval);108
this.interval = setInterval(async () => {109
if(!this.playing) {110
clearInterval(this.interval);111
return;112
}113
await this.fetchSong();114
}, 10000);115
},116
onTouchStart(event) {117
this.touch = event.changedTouches[0];118
this.target = event.target;119
},120
onTouchEnd(event) {121
const touch = event.changedTouches[0];122
const diff = {123
x: touch.pageX - this.touch.pageX,124
y: touch.pageY - this.touch.pageY125
};126
const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);127
const index = state.tabs.indexOf(state.tab);128
if(this.open) {129
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {130
if(diff.y > 0 && angle >= -30 && angle <= 30) {131
this.open = false;132
}133
}134
return;135
}136
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {137
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {138
if(!this.open && this.target === this.$refs.button) {139
if(diff.y < 0 && angle >= -30 && angle <= 30) {140
this.open = true;141
}142
}143
}144
}145
}146
},147
created() {148
document.body.addEventListener('touchstart', this.onTouchStart);149
document.body.addEventListener('touchend', this.onTouchEnd);150
},151
onUnmounted() {152
document.body.removeEventListener('touchstart', this.onTouchStart);153
document.body.removeEventListener('touchend', this.onTouchEnd);154
},155
computed: {156
opacity() {157
return {158
'opacity': `${this.open === true ? 1 : 0}`,159
'pointer-events': `${this.open === true ? 'all' : 'none'}`160
};161
}162
},163
template: `164
<div class="z-30">165
<div166
@click="open = false"167
class="fixed top-0 bottom-0 left-0 right-0 bg-black/60 transition-modal"168
:style="opacity"169
>170
</div>171
<div172
ref="modal"173
class="w-full max-w-[1200px] mx-auto flex flex-col bg-black fixed left-0 right-0 top-[100vh] h-[100vh] px-4 sm:px-10 transition-modal"174
:class="{ 'modal-active': open }"175
>176
<audio177
v-if="station"178
ref="player"179
controls autoplay180
:src="station.url"181
@loadeddata="onLoad"182
@play="onPlay"183
@pause="onPause"184
@error="onError"185
class="select-none pointer-events-none w-0 h-0 opacity-0"186
>187
<source :src="station.url" type="audio/mpeg">188
<source :src="station.url" type="audio/ogg">189
<source :src="station.url" type="audio/aac">190
</audio>191
<div class="w-full h-[98px] py-6 flex gap-4 justify-between">192
<v-button193
icon="chevron-down"194
@click="open = false"195
></v-button>196
<v-button197
icon="cast"198
></v-button>199
</div>200
<div class="flex gap-8 items-center flex-col">201
<div202
ref="button"203
class="h-[75vw] max-h-[400px] aspect-square bg-zinc-900 rounded-lg overflow-hidden z-10 fixed right-[50%] translate-x-[50%] translate-y-[100%] bottom-[calc(100vh-98px)] transition-modal"204
:class="{ 'slide-active': !open, 'slide-hidden': !station }"205
@click="open = true"206
>207
<img208
v-if="station?.icon"209
:src="station.icon"210
:alt="station.name"211
class="w-full h-full object-contain select-none pointer-events-none"212
>213
<v-icon214
v-else215
id="access-point"216
size="38px"217
class="text-gray-400 pointer-events-none"218
></v-icon>219
</div>220
<div class="h-[75vw] max-h-[400px]"></div>221
<div222
v-if="station"223
class="flex flex-col flex-1 z-10 min-w-0 w-[75vw] max-w-[400px] transition-modal"224
:style="opacity"225
>226
<p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center">227
{{station.name}}228
</p>229
<p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center">230
{{title ?? $lang('player.no-song-title')}}231
</p>232
</div>233
<div class="inline-flex flex-col gap-6 z-10 pr-2 items-center">234
<v-button235
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"236
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"237
size="28px"238
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"239
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10 !min-w-[64px] !min-h-[64px]"240
@click="setPlaying(!playing)"241
></v-button>242
<input243
type="range"244
min="0"245
max="1"246
step="0.05"247
@change="setVolume"248
class="w-[200px]"249
>250
</div>251
</div>252
</div>253
</div>254
`255
}