so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: b09280b64786666d9db9b25992de67fab42d42ad (12631 bytes)[doc][blame][raw]

1import {state, SWIPE_THRESHOLD} from '../main.js';

2import {http, $lang, $route} from '../utils.js';

3import VButton from '../element/button.js';

4import VIcon from '../element/icon.js';

5import VImage from '../element/image.js';

6

7export 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 state

21 };

22 },

23 components: {

24 VButton,

25 VIcon,

26 VImage

27 },

28 watch: {

29 async title(value) {

30 await this.$nextTick();

31 const outer = this.open ? this.$refs.songtitle : this.$refs.subsongtitle;

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 async open(value) {

37 await this.$nextTick();

38 const outer = value ? this.$refs.songtitle : this.$refs.subsongtitle;

39 const inner = outer.querySelector('span');

40 this.tooLong = outer.clientWidth - 12 < inner.clientWidth;

41 this.animationDuration = `${Math.max(Math.abs(outer.clientWidth - 12 - inner.clientWidth) * 70, 2500)}ms`;

42 }

43 },

44 methods: {

45 $lang,

46 $route,

47 play(station) {

48 if(this.station?.id === station?.id) {

49 return;

50 }

51 this.station = station;

52 this.playing = false;

53 this.error = false;

54 if(this.station !== null) {

55 this.loading = true;

56 this.title = null;

57 this.updateMediaSession();

58 }

59 },

60 setPlaying(playing) {

61 if(this.error || this.loading) {

62 return;

63 }

64 this.playing = playing;

65 this.$refs.player[this.playing ? 'play' : 'pause']();

66 if(this.playing) {

67 this.playing = false;

68 this.loading = true;

69 this.$refs.player.load();

70 }

71 },

72 setVolume(event) {

73 this.$refs.player.volume = event.target.value;

74 },

75 onPlay() {

76 if(this.resume) {

77 this.playing = false;

78 this.loading = true;

79 this.$refs.player.load();

80 }

81 },

82 onPause() {

83 this.playing = false;

84 this.resume = true;

85 },

86 onLoad() {

87 this.loading = false;

88 this.playing = true;

89 this.resume = false;

90 this.updateSong();

91 },

92 onError() {

93 this.error = true;

94 this.loading = false;

95 this.playing = false;

96 },

97 updateMediaSession() {

98 if('mediaSession' in navigator) {

99 navigator.mediaSession.metadata = new MediaMetadata({

100 title: this.title ?? $lang('player.no-song-title'),

101 artist: this.station.name,

102 artwork: [

103 { src: this.station.icon, sizes: '96x96', type: 'image/jpg' },

104 { src: this.station.icon, sizes: '128x128', type: 'image/jpg' },

105 { src: this.station.icon, sizes: '192x192', type: 'image/jpg' },

106 { src: this.station.icon, sizes: '256x256', type: 'image/jpg' },

107 { src: this.station.icon, sizes: '384x384', type: 'image/jpg' },

108 { src: this.station.icon, sizes: '512x512', type: 'image/jpg' }

109 ]

110 });

111 }

112 },

113 async fetchSong() {

114 try {

115 const song = await http`get::/api/song`(this.station);

116 this.title = song === '' ? null : song;

117 }

118 catch(err) {

119 console.error(err);

120 clearInterval(this.interval);

121 this.title = null;

122 }

123 this.updateMediaSession();

124 },

125 updateSong() {

126 this.fetchSong();

127 clearInterval(this.interval);

128 this.interval = setInterval(async () => {

129 if(!this.playing) {

130 clearInterval(this.interval);

131 return;

132 }

133 await this.fetchSong();

134 }, 10000);

135 },

136 async showDetail(station) {

137 $route('/station/' + this.station.id);

138 this.open = false;

139 },

140 onTouchStart(event) {

141 this.touch = event.changedTouches[0];

142 this.target = event.target;

143 },

144 onTouchEnd(event) {

145 const touch = event.changedTouches[0];

146 const diff = {

147 x: touch.pageX - this.touch.pageX,

148 y: touch.pageY - this.touch.pageY

149 };

150 const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);

151 const index = state.tabs.indexOf(state.tab);

152 if(this.open) {

153 if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {

154 if(diff.y > 0 && angle >= -30 && angle <= 30) {

155 this.open = false;

156 }

157 }

158 return;

159 }

160 if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {

161 if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {

162 if(!this.open && this.target === this.$refs.button?.$refs.image) {

163 if(diff.y < 0 && angle >= -30 && angle <= 30) {

164 this.open = true;

165 }

166 }

167 }

168 }

169 }

170 },

171 created() {

172 document.body.addEventListener('touchstart', this.onTouchStart);

173 document.body.addEventListener('touchend', this.onTouchEnd);

174 },

175 onUnmounted() {

176 document.body.removeEventListener('touchstart', this.onTouchStart);

177 document.body.removeEventListener('touchend', this.onTouchEnd);

178 },

179 computed: {

180 opacity() {

181 return {

182 'opacity': `${this.open === true ? 1 : 0}`,

183 'pointer-events': `${this.open === true ? 'all' : 'none'}`

184 };

185 }

186 },

187 template: `

188 <div class="z-50">

189 <div

190 @click="open = false"

191 class="fixed top-0 bottom-0 left-0 right-0 bg-black/60 transition-modal"

192 :style="opacity"

193 >

194 </div>

195 <div

196 ref="modal"

197 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"

198 :class="{ 'modal-active': open }"

199 >

200 <div class="w-full flex flex-col max-w-[1200px] mx-auto">

201 <audio

202 v-if="station"

203 ref="player"

204 controls autoplay

205 :src="station.url"

206 @loadeddata="onLoad"

207 @play="onPlay"

208 @pause="onPause"

209 @error="onError"

210 class="select-none pointer-events-none w-0 h-0 opacity-0"

211 >

212 <source :src="station.url" type="audio/mpeg">

213 <source :src="station.url" type="audio/ogg">

214 <source :src="station.url" type="audio/aac">

215 </audio>

216 <div class="w-full h-[98px] py-6 flex gap-4 justify-between">

217 <v-button

218 icon="chevron-down"

219 @click="open = false"

220 ></v-button>

221 <v-button

222 icon="cast"

223 ></v-button>

224 </div>

225 <div class="flex gap-8 items-center flex-col">

226 <div

227 ref="button"

228 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 flex"

229 :class="{ 'slide-active bg-zinc-900 items-center gap-4': !open, 'slide-hidden': !station }"

230 @click="open = true"

231 >

232 <v-image

233 :station="station"

234 class="h-full"

235 >

236 </v-image>

237 <div

238 v-if="station"

239 class="hidden md:flex flex-col flex-1 min-w-0 w-[400px] max-w-[400px] mr-4 transition-modal"

240 :class="{ 'max-w-0 mr-0 opacity-0': open }"

241 >

242 <p

243 class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap cursor-pointer"

244 @click="showDetail(station)"

245 >

246 {{station.name}}

247 </p>

248 <p

249 ref="subsongtitle"

250 class="subsongtitle text-md text-gray-400 overflow-hidden whitespace-nowrap relative px-2 -ml-2 before:bg-gradient-to-r before:from-zinc-900 before:to-transparent after:bg-gradient-to-l after:from-zinc-900 after:to-transparent"

251 :class="{'text-animate': tooLong}"

252 >

253 <span class="inline-block" :style="{'animation-duration': animationDuration}">{{title ?? $lang('player.no-song-title')}}</span>

254 </p>

255 </div>

256 </div>

257 <div class="h-[75vw] max-h-[400px] mt-[32px]"></div>

258 <div

259 v-if="station"

260 class="flex flex-col flex-1 z-10 min-w-0 w-[75vw] max-w-[400px] transition-modal"

261 :style="opacity"

262 >

263 <p

264 class="text-xl font-semibold text-white pb-2 overflow-hidden text-ellipsis whitespace-nowrap text-center cursor-pointer"

265 @click="showDetail(station)"

266 >

267 {{station.name}}

268 </p>

269 <p ref="songtitle" class="songtitle text-md text-gray-400 overflow-hidden whitespace-nowrap relative px-2" :class="{'text-center': !tooLong, 'text-animate': tooLong}">

270 <span class="inline-block" :style="{'animation-duration': animationDuration}">{{title ?? $lang('player.no-song-title')}}</span>

271 </p>

272 </div>

273 <div class="inline-flex flex-col gap-6 z-10 pr-2 items-center">

274 <v-button

275 :icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"

276 :title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"

277 size="28px"

278 :animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"

279 class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10 !min-w-[64px] !min-h-[64px]"

280 @click="setPlaying(!playing)"

281 ></v-button>

282 <input

283 type="range"

284 min="0"

285 max="1"

286 step="0.05"

287 @input="setVolume"

288 class="w-[200px]"

289 >

290 </div>

291 </div>

292 </div>

293 </div>

294 </div>

295 `

296}