so://phie

verbal

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

blob: 7508b3a48d867df5c2b095e4abab38d2dcc1e6d3 (10465 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 };

21 },

22 components: {

23 VButton,

24 VIcon,

25 VImage

26 },

27 watch: {

28 station(value) {

29 this.playing = false;

30 this.error = false;

31 if(value !== null) {

32 this.loading = true;

33 this.title = null;

34 this.updateMediaSession();

35 }

36 },

37 async title(value) {

38 await this.$nextTick();

39 const outer = this.$refs.songtitle;

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

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

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

43 }

44 },

45 methods: {

46 $lang,

47 async openDetail(station) {

48 await state.app.$refs.detail.load(station.id);

49 this.open = false;

50 },

51 setPlaying(playing) {

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

53 return;

54 }

55 this.playing = playing;

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

57 if(this.playing) {

58 this.playing = false;

59 this.loading = true;

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

61 }

62 },

63 setVolume(event) {

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

65 },

66 onPlay() {

67 if(this.resume) {

68 this.playing = false;

69 this.loading = true;

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

71 }

72 },

73 onPause() {

74 this.playing = false;

75 this.resume = true;

76 },

77 onLoad() {

78 this.loading = false;

79 this.playing = true;

80 this.resume = false;

81 this.updateSong();

82 },

83 onError() {

84 this.error = true;

85 this.loading = false;

86 this.playing = false;

87 },

88 updateMediaSession() {

89 if('mediaSession' in navigator) {

90 navigator.mediaSession.metadata = new MediaMetadata({

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

92 artist: this.station.name,

93 artwork: [

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

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

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

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

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

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

100 ]

101 });

102 }

103 },

104 async fetchSong() {

105 try {

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

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

108 }

109 catch(err) {

110 console.error(err);

111 clearInterval(this.interval);

112 this.title = null;

113 }

114 this.updateMediaSession();

115 },

116 updateSong() {

117 this.fetchSong();

118 clearInterval(this.interval);

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

120 if(!this.playing) {

121 clearInterval(this.interval);

122 return;

123 }

124 await this.fetchSong();

125 }, 10000);

126 },

127 onTouchStart(event) {

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

129 this.target = event.target;

130 },

131 onTouchEnd(event) {

132 const touch = event.changedTouches[0];

133 const diff = {

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

135 y: touch.pageY - this.touch.pageY

136 };

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

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

139 if(this.open) {

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

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

142 this.open = false;

143 }

144 }

145 return;

146 }

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

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

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

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

151 this.open = true;

152 }

153 }

154 }

155 }

156 }

157 },

158 created() {

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

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

161 },

162 onUnmounted() {

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

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

165 },

166 computed: {

167 opacity() {

168 return {

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

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

171 };

172 }

173 },

174 template: `

175 <div class="z-50">

176 <div

177 @click="open = false"

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

179 :style="opacity"

180 >

181 </div>

182 <div

183 ref="modal"

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

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

186 >

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

188 <audio

189 v-if="station"

190 ref="player"

191 controls autoplay

192 :src="station.url"

193 @loadeddata="onLoad"

194 @play="onPlay"

195 @pause="onPause"

196 @error="onError"

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

198 >

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

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

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

202 </audio>

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

204 <v-button

205 icon="chevron-down"

206 @click="open = false"

207 ></v-button>

208 <v-button

209 icon="cast"

210 ></v-button>

211 </div>

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

213 <v-image

214 :station="station"

215 ref="button"

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

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

218 @click="open = true"

219 >

220 </v-image>

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

222 <div

223 v-if="station"

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

225 :style="opacity"

226 >

227 <p

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

229 @click="openDetail(station)"

230 >

231 {{station.name}}

232 </p>

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

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

235 </p>

236 </div>

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

238 <v-button

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

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

241 size="28px"

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

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

244 @click="setPlaying(!playing)"

245 ></v-button>

246 <input

247 type="range"

248 min="0"

249 max="1"

250 step="0.05"

251 @input="setVolume"

252 class="w-[200px]"

253 >

254 </div>

255 </div>

256 </div>

257 </div>

258 </div>

259 `

260}