so://phie

verbal

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

blob: 4d9992e59b8ce7297f5d7b495a10500a585fb02b (9914 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 open(value) {

38 state.open = value;

39 },

40 async title(value) {

41 await this.$nextTick();

42 const outer = this.$refs.songtitle;

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

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

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

46 }

47 },

48 methods: {

49 $lang,

50 setPlaying(playing) {

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

52 return;

53 }

54 this.playing = playing;

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

56 if(this.playing) {

57 this.playing = false;

58 this.loading = true;

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

60 }

61 },

62 setVolume(event) {

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

64 },

65 onPlay() {

66 if(this.resume) {

67 this.playing = false;

68 this.loading = true;

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

70 }

71 },

72 onPause() {

73 this.playing = false;

74 this.resume = true;

75 },

76 onLoad() {

77 this.loading = false;

78 this.playing = true;

79 this.resume = false;

80 this.updateSong();

81 },

82 onError() {

83 this.error = true;

84 this.loading = false;

85 this.playing = false;

86 },

87 updateMediaSession() {

88 if('mediaSession' in navigator) {

89 navigator.mediaSession.metadata = new MediaMetadata({

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

91 artist: this.station.name,

92 artwork: [

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

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

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

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

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

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

99 ]

100 });

101 }

102 },

103 async fetchSong() {

104 try {

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

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

107 }

108 catch(err) {

109 console.error(err);

110 clearInterval(this.interval);

111 this.title = null;

112 }

113 this.updateMediaSession();

114 },

115 updateSong() {

116 this.fetchSong();

117 clearInterval(this.interval);

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

119 if(!this.playing) {

120 clearInterval(this.interval);

121 return;

122 }

123 await this.fetchSong();

124 }, 10000);

125 },

126 onTouchStart(event) {

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

128 this.target = event.target;

129 },

130 onTouchEnd(event) {

131 const touch = event.changedTouches[0];

132 const diff = {

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

134 y: touch.pageY - this.touch.pageY

135 };

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

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

138 if(this.open) {

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

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

141 this.open = false;

142 }

143 }

144 return;

145 }

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

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

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

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

150 this.open = true;

151 }

152 }

153 }

154 }

155 }

156 },

157 created() {

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

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

160 },

161 onUnmounted() {

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

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

164 },

165 computed: {

166 opacity() {

167 return {

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

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

170 };

171 }

172 },

173 template: `

174 <div class="z-50">

175 <div

176 @click="open = false"

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

178 :style="opacity"

179 >

180 </div>

181 <div

182 ref="modal"

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

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

185 >

186 <audio

187 v-if="station"

188 ref="player"

189 controls autoplay

190 :src="station.url"

191 @loadeddata="onLoad"

192 @play="onPlay"

193 @pause="onPause"

194 @error="onError"

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

196 >

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

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

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

200 </audio>

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

202 <v-button

203 icon="chevron-down"

204 @click="open = false"

205 ></v-button>

206 <v-button

207 icon="cast"

208 ></v-button>

209 </div>

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

211 <v-image

212 :station="station"

213 ref="button"

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

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

216 @click="open = true"

217 >

218 </v-image>

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

220 <div

221 v-if="station"

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

223 :style="opacity"

224 >

225 <p class="text-xl font-semibold text-white pb-2 overflow-hidden text-ellipsis whitespace-nowrap text-center">

226 {{station.name}}

227 </p>

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

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

230 </p>

231 </div>

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

233 <v-button

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

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

236 size="28px"

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

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

239 @click="setPlaying(!playing)"

240 ></v-button>

241 <input

242 type="range"

243 min="0"

244 max="1"

245 step="0.05"

246 @input="setVolume"

247 class="w-[200px]"

248 >

249 </div>

250 </div>

251 </div>

252 </div>

253 `

254}