so://phie

verbal

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

blob: 4974097b9740e0844e36af997960f26bb1cdcbd5 (10111 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 max-w-[1200px] 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 <audio

188 v-if="station"

189 ref="player"

190 controls autoplay

191 :src="station.url"

192 @loadeddata="onLoad"

193 @play="onPlay"

194 @pause="onPause"

195 @error="onError"

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

197 >

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

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

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

201 </audio>

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

203 <v-button

204 icon="chevron-down"

205 @click="open = false"

206 ></v-button>

207 <v-button

208 icon="cast"

209 ></v-button>

210 </div>

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

212 <v-image

213 :station="station"

214 ref="button"

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

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

217 @click="open = true"

218 >

219 </v-image>

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

221 <div

222 v-if="station"

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

224 :style="opacity"

225 >

226 <p

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

228 @click="openDetail(station)"

229 >

230 {{station.name}}

231 </p>

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

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

234 </p>

235 </div>

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

237 <v-button

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

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

240 size="28px"

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

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

243 @click="setPlaying(!playing)"

244 ></v-button>

245 <input

246 type="range"

247 min="0"

248 max="1"

249 step="0.05"

250 @input="setVolume"

251 class="w-[200px]"

252 >

253 </div>

254 </div>

255 </div>

256 </div>

257 `

258}