so://phie

verbal

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

blob: de19e95295df1b87bdb24aac40e712f5debef8fa (7331 bytes)[doc][blame][raw]

1import {http, $lang} from './utils.js';

2import VMenu from './component/menu.js';

3import VPlayer from './component/player.js';

4import VPopup from './component/popup.js';

5import VSearch from './page/search.js';

6import VFavorites from './page/favorites.js';

7import VAccount from './page/account.js';

8

9const VERSION = 'dev-{{version}}';

10const SWIPE_THRESHOLD = 70;

11

12export const state = {

13 app: null,

14 tab: 'search',

15 tabs: ['search', 'favorites', 'account'],

16 account: null,

17 station: null,

18 authenticated: false,

19 open: false,

20 version: VERSION

21};

22

23(async () => {

24 state.locale = await http`get::/asset/json/locale.json`();

25

26 new Vue({

27 el: '#app',

28 data: {

29 state,

30 touch: null,

31 target: null

32 },

33 components: {

34 VMenu,

35 VSearch,

36 VFavorites,

37 VAccount,

38 VPlayer,

39 VPopup

40 },

41 mounted() {

42 document.body.style.display = '';

43 state.app = this;

44 this.init();

45 },

46 methods: {

47 async init() {

48 try {

49 state.account = await http`get::/api/account`();

50 }

51 catch(error) {

52 // ignore

53 }

54 if(state.account === null) {

55 this.$refs.popup.open({

56 title: $lang('global.welcome'),

57 description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.',

58 actions: [

59 {

60 icon: 'check',

61 title: $lang('global.continue'),

62 handle: () => {

63 localStorage.setItem('verbal-token', 'test-token');

64 window.location.reload();

65 }

66 }

67 ]

68 });

69 return;

70 }

71 state.authenticated = true;

72 document.addEventListener('scroll', this.onScroll);

73 document.addEventListener('wheel', this.onScroll);

74 if('serviceWorker' in navigator) {

75 navigator.serviceWorker.register('/worker.js');

76 navigator.serviceWorker.addEventListener('controllerchange', this.updated);

77 }

78 },

79 showError(error) {

80 this.$refs.popup.open({

81 title: $lang('global.error'),

82 description: $lang('global.error.message', error),

83 actions: [

84 {

85 title: $lang('global.report'),

86 icon: 'message-report',

87 handle: () => window.open('https://github.com/typable/verbal/issues')

88 },

89 {

90 title: $lang('global.continue'),

91 icon: 'arrow-right',

92 handle: () => this.$refs.popup.close()

93 }

94 ]

95 });

96 },

97 updated() {

98 this.$refs.popup.open({

99 title: $lang('global.update'),

100 description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.',

101 actions: [

102 {

103 title: $lang('global.continue'),

104 icon: 'check',

105 handle: () => window.location.reload()

106 }

107 ]

108 });

109 },

110 onScroll() {

111 if(state.tab === 'search') {

112 if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {

113 if(this.$refs.search.searching) {

114 this.$refs.search.doSearchMore();

115 }

116 }

117 }

118 },

119 onTouchStart(event) {

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

121 this.target = event.target;

122 },

123 onTouchEnd(event) {

124 const touch = event.changedTouches[0];

125 const diff = {

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

127 y: touch.pageY - this.touch.pageY

128 };

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

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

131 if(state.open) {

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

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

134 state.open = false;

135 }

136 }

137 return;

138 }

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

140 if(diff.x > 0 && index > 0 && (angle <= -60 || angle >= 60)) {

141 state.tab = state.tabs[index - 1];

142 }

143 if(diff.x < 0 && index < state.tabs.length - 1 && (angle <= -60 || angle >= 60)) {

144 state.tab = state.tabs[index + 1];

145 }

146 }

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

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

149 if(!state.open && this.target === this.$refs.player.$refs.button) {

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

151 state.open = true;

152 }

153 }

154 }

155 }

156 }

157 },

158 computed: {

159 styles() {

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

161 const offset = window.innerWidth < 1200

162 ? `${index * -100}vw`

163 : `${index * -1200}px`;

164 return {

165 'transform': `translate(${offset}, 0)`

166 };

167 }

168 },

169 template: `

170 <div

171 @touchstart="onTouchStart"

172 @touchend="onTouchEnd"

173 class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"

174 >

175 <v-menu :state="state"></v-menu>

176 <div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">

177 <v-search v-if="state.authenticated" ref="search" :state="state"></v-search>

178 <v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>

179 <v-account v-if="state.authenticated" :state="state"></v-account>

180 </div>

181 <v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>

182 <v-popup ref="popup"></v-popup>

183 </div>

184 `

185 });

186})();

187