A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 77f8d7df5e44133b2a471805eaa931d0b9bb8c97 (6682 bytes)[doc][blame][raw]
1
import {http, $lang} from './utils.js';2
import VMenu from './component/menu.js';3
import VPlayer from './component/player.js';4
import VPopup from './component/popup.js';5
import VSearch from './page/search.js';6
import VFavorites from './page/favorites.js';7
import VAccount from './page/account.js';9
const VERSION = 'dev-{{version}}';10
const SWIPE_THRESHOLD = 70;12
export 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: VERSION21
};23
(async () => {24
state.locale = await http`get::/asset/json/locale.json`();26
new Vue({27
el: '#app',28
data: {29
state,30
touch: null,31
target: null32
},33
components: {34
VMenu,35
VSearch,36
VFavorites,37
VAccount,38
VPlayer,39
VPopup40
},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
// ignore53
}54
if(state.account === null) {55
// TODO: create account or authenticate56
return;57
}58
state.authenticated = true;59
document.addEventListener('scroll', this.onScroll);60
document.addEventListener('wheel', this.onScroll);61
if('serviceWorker' in navigator) {62
navigator.serviceWorker.register('/worker.js');63
navigator.serviceWorker.addEventListener('controllerchange', this.updated);64
}65
},66
showError(error) {67
this.$refs.popup.open({68
title: $lang('global.error'),69
description: $lang('global.error.message', error),70
actions: [71
{72
title: $lang('global.report'),73
icon: 'message-report',74
handle: () => window.open('https://github.com/typable/verbal/issues')75
},76
{77
title: $lang('global.continue'),78
icon: 'arrow-right',79
handle: () => this.$refs.popup.close()80
}81
]82
});83
},84
updated() {85
this.$refs.popup.open({86
title: $lang('global.update'),87
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.',88
actions: [89
{90
title: $lang('global.continue'),91
icon: 'check',92
handle: () => window.location.reload()93
}94
]95
});96
},97
onScroll() {98
if(state.tab === 'search') {99
if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {100
if(this.$refs.search.searching) {101
this.$refs.search.doSearchMore();102
}103
}104
}105
},106
onTouchStart(event) {107
this.touch = event.changedTouches[0];108
this.target = event.target;109
},110
onTouchEnd(event) {111
const touch = event.changedTouches[0];112
const diff = {113
x: touch.pageX - this.touch.pageX,114
y: touch.pageY - this.touch.pageY115
};116
const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);117
const index = state.tabs.indexOf(state.tab);118
if(state.open) {119
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {120
if(diff.y > 0 && angle >= -30 && angle <= 30) {121
state.open = false;122
}123
}124
return;125
}126
if(Math.abs(diff.x) >= SWIPE_THRESHOLD) {127
if(diff.x > 0 && index > 0 && (angle <= -60 || angle >= 60)) {128
state.tab = state.tabs[index - 1];129
}130
if(diff.x < 0 && index < state.tabs.length - 1 && (angle <= -60 || angle >= 60)) {131
state.tab = state.tabs[index + 1];132
}133
}134
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {135
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {136
if(!state.open && this.target === this.$refs.player.$refs.button) {137
if(diff.y < 0 && angle >= -30 && angle <= 30) {138
state.open = true;139
}140
}141
}142
}143
}144
},145
computed: {146
styles() {147
const index = state.tabs.indexOf(state.tab);148
const offset = window.innerWidth < 1200149
? `${index * -100}vw`150
: `${index * -1200}px`;151
return {152
'transform': `translate(${offset}, 0)`153
};154
}155
},156
template: `157
<div158
@touchstart="onTouchStart"159
@touchend="onTouchEnd"160
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"161
>162
<v-menu :state="state"></v-menu>163
<div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">164
<v-search v-if="state.authenticated" ref="search" :state="state"></v-search>165
<v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>166
<v-account v-if="state.authenticated" :state="state"></v-account>167
</div>168
<v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>169
<v-popup ref="popup"></v-popup>170
</div>171
`172
});173
})();