A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: ace70e2266adaa52254d28920227bc08e746c0ca (7639 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: false16
};17
},18
components: {19
VButton,20
VIcon21
},22
watch: {23
station: function(station) {24
this.playing = false;25
this.error = false;26
if(station !== null) {27
this.loading = true;28
this.title = '- No song title available -';29
this.updateMediaSession();30
}31
}32
},33
methods: {34
$lang,35
async setLike(is_favorite) {36
try {37
await http`${is_favorite ? 'post' : 'delete'}::/api/favorite`(this.station.id);38
this.station.is_favorite = is_favorite;39
state.app.$refs.favorites.doFetch();40
}41
catch(error) {42
console.log(error);43
}44
},45
setPlaying(playing) {46
if(this.error || this.loading) {47
return;48
}49
this.playing = playing;50
this.$refs.player[this.playing ? 'play' : 'pause']();51
if(this.playing) {52
this.playing = false;53
this.loading = true;54
this.$refs.player.load();55
}56
},57
onPlay() {58
if(this.resume) {59
this.playing = false;60
this.loading = true;61
this.$refs.player.load();62
}63
},64
onPause() {65
this.playing = false;66
this.resume = true;67
},68
onLoad() {69
this.loading = false;70
this.playing = true;71
this.resume = false;72
this.updateSong();73
},74
onError(event) {75
this.error = true;76
this.loading = false;77
this.playing = false;78
},79
updateMediaSession() {80
if('mediaSession' in navigator) {81
navigator.mediaSession.metadata = new MediaMetadata({82
title: this.title,83
artist: this.station.name,84
artwork: [85
{ src: this.station.icon, sizes: '96x96', type: 'image/jpg' },86
{ src: this.station.icon, sizes: '128x128', type: 'image/jpg' },87
{ src: this.station.icon, sizes: '192x192', type: 'image/jpg' },88
{ src: this.station.icon, sizes: '256x256', type: 'image/jpg' },89
{ src: this.station.icon, sizes: '384x384', type: 'image/jpg' },90
{ src: this.station.icon, sizes: '512x512', type: 'image/jpg' }91
]92
});93
}94
},95
async fetchSong() {96
try {97
const song = await http`get::/api/song`(this.station);98
this.title = song === '' ? '- No song title available -' : song;99
}100
catch(err) {101
console.error(err);102
this.title = '- No song title available -';103
}104
this.updateMediaSession();105
},106
updateSong() {107
this.fetchSong();108
clearInterval(this.interval);109
this.interval = setInterval(async () => {110
if(!this.playing) {111
clearInterval(this.interval);112
return;113
}114
await this.fetchSong();115
}, 10000);116
}117
},118
template: `119
<div class="sticky top-[98px] z-30">120
<div121
v-if="station"122
class="flex flex-col bg-black pb-6"123
>124
<audio125
ref="player"126
controls autoplay127
:src="station.url"128
@loadeddata="onLoad"129
@play="onPlay"130
@pause="onPause"131
@error="onError"132
class="select-none pointer-events-none w-0 h-0 opacity-0"133
>134
<source :src="station.url" type="audio/mpeg">135
<source :src="station.url" type="audio/ogg">136
<source :src="station.url" type="audio/aac">137
</audio>138
<div class="p-5 bg-zinc-800 rounded-xl flex gap-5 items-center relative overflow-hidden shadow-2xl z-30">139
<img140
v-if="station.icon"141
:src="station.icon"142
:alt="station.name"143
:class="{ 'animate-pulse': playing }"144
class="w-full h-full object-cover absolute top-0 left-0 right-0 bottom-0 blur-2xl opacity-30 z-0 pointer-events-none select-none"145
>146
<div class="w-[64px] h-[64px] min-w-[64px] bg-zinc-900 rounded-lg overflow-hidden z-10">147
<img148
v-if="station.icon"149
:src="station.icon"150
:alt="station.name"151
class="w-full h-full object-contain select-none"152
>153
<v-icon154
v-else155
id="access-point"156
size="38px"157
class="text-gray-400"158
></v-icon>159
</div>160
<div161
class="flex flex-col flex-1 z-10 min-w-0 cursor-pointer"162
>163
<p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none">164
{{station.name}}165
</p>166
<p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none">167
{{title ?? $lang('player.loading')}}168
</p>169
</div>170
<div class="inline-flex gap-4 z-10 pr-2">171
<v-button172
:icon="[ station.is_favorite ? 'heart-off' : 'heart' ]"173
:title="[ $lang(station.is_favorite ? 'global.unlike' : 'global.like') ]"174
:active="station.is_favorite"175
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"176
@click="setLike(!station.is_favorite)"177
></v-button>178
<v-button179
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"180
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"181
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"182
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"183
@click="setPlaying(!playing)"184
></v-button>185
</div>186
</div>187
</div>188
<div class="bg-gradient-to-b from-black h-2 sm:h-4"></div>189
</div>190
`191
}