so://phie

verbal

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

blob: a3b7d8c8e5149549eb54d637a7a604e433e48993 (9385 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 };

19 },

20 components: {

21 VButton,

22 VIcon,

23 VImage

24 },

25 watch: {

26 station(value) {

27 this.playing = false;

28 this.error = false;

29 if(value !== null) {

30 this.loading = true;

31 this.title = null;

32 this.updateMediaSession();

33 }

34 },

35 open(value) {

36 state.open = value;

37 }

38 },

39 methods: {

40 $lang,

41 setPlaying(playing) {

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

43 return;

44 }

45 this.playing = playing;

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

47 if(this.playing) {

48 this.playing = false;

49 this.loading = true;

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

51 }

52 },

53 setVolume(event) {

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

55 },

56 onPlay() {

57 if(this.resume) {

58 this.playing = false;

59 this.loading = true;

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

61 }

62 },

63 onPause() {

64 this.playing = false;

65 this.resume = true;

66 },

67 onLoad() {

68 this.loading = false;

69 this.playing = true;

70 this.resume = false;

71 this.updateSong();

72 },

73 onError() {

74 this.error = true;

75 this.loading = false;

76 this.playing = false;

77 },

78 updateMediaSession() {

79 if('mediaSession' in navigator) {

80 navigator.mediaSession.metadata = new MediaMetadata({

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

82 artist: this.station.name,

83 artwork: [

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

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

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

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

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

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

90 ]

91 });

92 }

93 },

94 async fetchSong() {

95 try {

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

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

98 }

99 catch(err) {

100 console.error(err);

101 clearInterval(this.interval);

102 this.title = null;

103 }

104 this.updateMediaSession();

105 },

106 updateSong() {

107 this.fetchSong();

108 clearInterval(this.interval);

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

110 if(!this.playing) {

111 clearInterval(this.interval);

112 return;

113 }

114 await this.fetchSong();

115 }, 10000);

116 },

117 onTouchStart(event) {

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

119 this.target = event.target;

120 },

121 onTouchEnd(event) {

122 const touch = event.changedTouches[0];

123 const diff = {

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

125 y: touch.pageY - this.touch.pageY

126 };

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

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

129 if(this.open) {

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

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

132 this.open = false;

133 }

134 }

135 return;

136 }

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

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

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

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

141 this.open = true;

142 }

143 }

144 }

145 }

146 }

147 },

148 created() {

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

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

151 },

152 onUnmounted() {

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

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

155 },

156 computed: {

157 opacity() {

158 return {

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

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

161 };

162 }

163 },

164 template: `

165 <div class="z-30">

166 <div

167 @click="open = false"

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

169 :style="opacity"

170 >

171 </div>

172 <div

173 ref="modal"

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

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

176 >

177 <audio

178 v-if="station"

179 ref="player"

180 controls autoplay

181 :src="station.url"

182 @loadeddata="onLoad"

183 @play="onPlay"

184 @pause="onPause"

185 @error="onError"

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

187 >

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

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

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

191 </audio>

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

193 <v-button

194 icon="chevron-down"

195 @click="open = false"

196 ></v-button>

197 <v-button

198 icon="cast"

199 ></v-button>

200 </div>

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

202 <v-image

203 :station="station"

204 ref="button"

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

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

207 @click="open = true"

208 >

209 </v-image>

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

211 <div

212 v-if="station"

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

214 :style="opacity"

215 >

216 <p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center">

217 {{station.name}}

218 </p>

219 <p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center">

220 {{title ?? $lang('player.no-song-title')}}

221 </p>

222 </div>

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

224 <v-button

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

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

227 size="28px"

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

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

230 @click="setPlaying(!playing)"

231 ></v-button>

232 <input

233 type="range"

234 min="0"

235 max="1"

236 step="0.05"

237 @input="setVolume"

238 class="w-[200px]"

239 >

240 </div>

241 </div>

242 </div>

243 </div>

244 `

245}