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