so://phie

verbal

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

blob: c616c2f11a38512808efd305dfbacbd2e6b25d92 (12430 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() {

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/' + 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 if(this.open) {

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

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

154 this.open = false;

155 }

156 }

157 return;

158 }

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

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

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

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

163 this.open = true;

164 }

165 }

166 }

167 }

168 }

169 },

170 created() {

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

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

173 },

174 onUnmounted() {

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

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

177 },

178 computed: {

179 opacity() {

180 return {

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

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

183 };

184 }

185 },

186 template: `

187 <div class="z-50">

188 <div

189 @click="open = false"

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

191 :style="opacity"

192 >

193 </div>

194 <div

195 ref="modal"

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

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

198 >

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

200 <audio

201 v-if="station"

202 ref="player"

203 controls autoplay

204 :src="station.url"

205 @loadeddata="onLoad"

206 @play="onPlay"

207 @pause="onPause"

208 @error="onError"

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

210 >

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

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

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

214 </audio>

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

216 <v-button

217 icon="chevron-down"

218 @click="open = false"

219 ></v-button>

220 <v-button

221 icon="cast"

222 ></v-button>

223 </div>

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

225 <div

226 ref="button"

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

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

229 @click="open = true"

230 >

231 <v-image

232 :station="station"

233 class="h-full"

234 >

235 </v-image>

236 <div

237 v-if="station"

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

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

240 >

241 <p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap cursor-pointer">

242 {{station.name}}

243 </p>

244 <p

245 ref="subsongtitle"

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

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

248 >

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

250 </p>

251 </div>

252 </div>

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

254 <div

255 v-if="station"

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

257 :style="opacity"

258 >

259 <p

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

261 @click="showDetail(station)"

262 >

263 {{station.name}}

264 </p>

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

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

267 </p>

268 </div>

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

270 <v-button

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

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

273 size="28px"

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

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

276 @click="setPlaying(!playing)"

277 ></v-button>

278 <input

279 type="range"

280 min="0"

281 max="1"

282 step="0.05"

283 @input="setVolume"

284 class="w-[200px]"

285 >

286 </div>

287 </div>

288 </div>

289 </div>

290 </div>

291 `

292}