so://phie

verbal

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

blob: ab3d3b7f3263dde1c75d1f1b34f03e9f1b604da0 (10612 bytes)[doc][blame][raw]

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

2import {http, $lang} 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.$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 play(station) {

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

41 return;

42 }

43 this.station = station;

44 this.playing = false;

45 this.error = false;

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

47 this.loading = true;

48 this.title = null;

49 this.updateMediaSession();

50 }

51 },

52 setPlaying(playing) {

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

54 return;

55 }

56 this.playing = playing;

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

58 if(this.playing) {

59 this.playing = false;

60 this.loading = true;

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

62 }

63 },

64 setVolume(event) {

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

66 },

67 onPlay() {

68 if(this.resume) {

69 this.playing = false;

70 this.loading = true;

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

72 }

73 },

74 onPause() {

75 this.playing = false;

76 this.resume = true;

77 },

78 onLoad() {

79 this.loading = false;

80 this.playing = true;

81 this.resume = false;

82 this.updateSong();

83 },

84 onError() {

85 this.error = true;

86 this.loading = false;

87 this.playing = false;

88 },

89 updateMediaSession() {

90 if('mediaSession' in navigator) {

91 navigator.mediaSession.metadata = new MediaMetadata({

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

93 artist: this.station.name,

94 artwork: [

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

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

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

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

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

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

101 ]

102 });

103 }

104 },

105 async fetchSong() {

106 try {

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

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

109 }

110 catch(err) {

111 console.error(err);

112 clearInterval(this.interval);

113 this.title = null;

114 }

115 this.updateMediaSession();

116 },

117 updateSong() {

118 this.fetchSong();

119 clearInterval(this.interval);

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

121 if(!this.playing) {

122 clearInterval(this.interval);

123 return;

124 }

125 await this.fetchSong();

126 }, 10000);

127 },

128 async showDetail(station) {

129 await state.app.$refs.detail.show(station);

130 this.open = false;

131 },

132 onTouchStart(event) {

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

134 this.target = event.target;

135 },

136 onTouchEnd(event) {

137 const touch = event.changedTouches[0];

138 const diff = {

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

140 y: touch.pageY - this.touch.pageY

141 };

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

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

144 if(this.open) {

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

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

147 this.open = false;

148 }

149 }

150 return;

151 }

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

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

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

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

156 this.open = true;

157 }

158 }

159 }

160 }

161 }

162 },

163 created() {

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

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

166 },

167 onUnmounted() {

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

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

170 },

171 computed: {

172 opacity() {

173 return {

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

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

176 };

177 }

178 },

179 template: `

180 <div class="z-50">

181 <div

182 @click="open = false"

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

184 :style="opacity"

185 >

186 </div>

187 <div

188 ref="modal"

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

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

191 >

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

193 <audio

194 v-if="station"

195 ref="player"

196 controls autoplay

197 :src="station.url"

198 @loadeddata="onLoad"

199 @play="onPlay"

200 @pause="onPause"

201 @error="onError"

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

203 >

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

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

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

207 </audio>

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

209 <v-button

210 icon="chevron-down"

211 @click="open = false"

212 ></v-button>

213 <v-button

214 icon="cast"

215 ></v-button>

216 </div>

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

218 <v-image

219 :station="station"

220 ref="button"

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

222 :class="{ 'slide-active': !open, 'slide-hidden': !station }"

223 @click="open = true"

224 >

225 </v-image>

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

227 <div

228 v-if="station"

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

230 :style="opacity"

231 >

232 <p

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

234 @click="showDetail(station)"

235 >

236 {{station.name}}

237 </p>

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

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

240 </p>

241 </div>

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

243 <v-button

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

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

246 size="28px"

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

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

249 @click="setPlaying(!playing)"

250 ></v-button>

251 <input

252 type="range"

253 min="0"

254 max="1"

255 step="0.05"

256 @input="setVolume"

257 class="w-[200px]"

258 >

259 </div>

260 </div>

261 </div>

262 </div>

263 </div>

264 `

265}