Better spawn of the notification

This commit is contained in:
Lucas Verney 2018-09-21 15:11:12 +02:00
parent dca0f62394
commit 19cbb3165f

View File

@ -170,6 +170,7 @@ export default {
isReportDialogVisible: false, isReportDialogVisible: false,
noSleep: null, noSleep: null,
notification: null, notification: null,
notificationInitializationWatcher: null,
reportLatLng: null, reportLatLng: null,
}; };
}, },
@ -180,8 +181,18 @@ export default {
'Cycl\'Assist', 'Cycl\'Assist',
{ body: $t('notification.body'), icon: AppLogo, tag: 'CyclassistMap' }, { body: $t('notification.body'), icon: AppLogo, tag: 'CyclassistMap' },
); );
this.notification.addEventListener('click', this.showReportDialog); this.notification.addEventListener('click', () => {
this.showReportDialog();
// Recreate permanent notification in case the OS closed it on click.
this.createNotification();
});
this.notification.addEventListener('close', this.createNotification); this.notification.addEventListener('close', this.createNotification);
// Remove the watcher if still there, no further need to watch properties.
if (this.notificationInitializationWatcher) {
this.notificationInitializationWatcher();
this.notificationInitializationWatcher = null;
}
}, },
disableNoSleep() { disableNoSleep() {
if (this.noSleep) { if (this.noSleep) {
@ -249,12 +260,26 @@ export default {
return; return;
} }
const that = this;
// Only create the notification once the position is ready
const createNotificationWhenPositionIsReady = (newValue) => {
if (newValue) {
that.createNotification();
}
};
if (Notification.permission && Notification.permission === 'granted') { if (Notification.permission && Notification.permission === 'granted') {
this.createNotification(); this.notificationInitializationWatcher = this.$watch(
'currentLatLng',
createNotificationWhenPositionIsReady,
);
} else { } else {
Notification.requestPermission((permission) => { Notification.requestPermission((permission) => {
if (permission === 'granted') { if (permission === 'granted') {
this.createNotification(); this.notificationInitializationWatcher = this.$watch(
'currentLatLng',
createNotificationWhenPositionIsReady,
);
} }
}); });
} }
@ -269,6 +294,8 @@ export default {
}, },
}, },
mounted() { mounted() {
this.setupNotification();
if (this.$route.name !== 'SharedMap') { if (this.$route.name !== 'SharedMap') {
// Only enable NoSleep in normal map view (with position tracking). // Only enable NoSleep in normal map view (with position tracking).
this.setNoSleep(); this.setNoSleep();
@ -288,8 +315,6 @@ export default {
} }
} }
this.$store.dispatch('fetchReports').catch(() => {}); this.$store.dispatch('fetchReports').catch(() => {});
this.setupNotification();
}, },
}; };
</script> </script>