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