diff --git a/app/app/Providers/ShareBaseUserWithViewsProvider.php b/app/app/Providers/ShareBaseUserWithViewsProvider.php index 600be979..4215a830 100644 --- a/app/app/Providers/ShareBaseUserWithViewsProvider.php +++ b/app/app/Providers/ShareBaseUserWithViewsProvider.php @@ -15,6 +15,7 @@ class ShareBaseUserWithViewsProvider extends ServiceProvider public function boot() { view()->share('base_user', new BaseUser()); + view()->share('google_map_api_key', config('app.google_map_api_key')); } /** diff --git a/app/public/js/home.js b/app/public/js/home.js index be48ec4f..c6a3afaf 100644 --- a/app/public/js/home.js +++ b/app/public/js/home.js @@ -55,8 +55,12 @@ function initMap() streetViewControl: false, clickableIcons: false }); - default_location = new google.maps.LatLng(default_location.latitude, default_location.longitude); - + try{ + default_location = new google.maps.LatLng(default_location.latitude, default_location.longitude); + } + catch(e){ + default_location= new google.maps.LatLng(function(){return 42.317503},function(){return -83.035474}); + } google.maps.event.addDomListener(window, "resize", function() { var center = map.getCenter(); google.maps.event.trigger(map, "resize"); diff --git a/app/public/js/service_worker_script.js b/app/public/js/service_worker_script.js new file mode 100644 index 00000000..ec97c9df --- /dev/null +++ b/app/public/js/service_worker_script.js @@ -0,0 +1,58 @@ +var url= window.location.href; +var pos = url.search('using_pwa'); + +function initServiceWorker(){ + if (pos+1 && url[pos+8] && 'serviceWorker' in navigator) { + navigator.serviceWorker + .register('service-worker.js') + .then(function(registration) { + if (!navigator.serviceWorker.controller){ + location.reload(); + } + sendNewLocation(); + }) + .catch(function(err) { + console.log("Service Worker Failed to Register", err); + }); + } +} + +function initListener(){ + if ('serviceWorker' in navigator){ + navigator.serviceWorker.addEventListener('message', function(e) { + if(e.data.type=='redirect') { + location.replace(e.data.url); + } else if(e.data.type='refresh') { + sendNewLocation(); + } + }); + } +} + + +initServiceWorker(); +initListener(); + +function sendNewLocation() { + getCurrentGeolocation().then( function(latlng){ + lng=latlng.lng(); + lat=latlng.lat(); + var urlGet = '/api/location/nearby/'+lng+'/'+lat; + var ajaxReq = $.ajax({ + type: 'get', + url: urlGet, + dataType: 'json', + success: function(data, status, xhr){ + navigator.serviceWorker.controller.postMessage(data); + }, + error: function(xhr, reason, ex){ + var sampleData={ + name: '404', + id:'0' + }; + navigator.serviceWorker.controller.postMessage(sampleData); + console.log("No nearby locations found at "+urlGet); + } + }); + }); +}; \ No newline at end of file diff --git a/app/public/service-worker.js b/app/public/service-worker.js new file mode 100644 index 00000000..0424dec4 --- /dev/null +++ b/app/public/service-worker.js @@ -0,0 +1,85 @@ +var reviewRedirect, title, options; +title = 'We Need Your Help!'; +options = { + body: '', + icon: '/images/logo-192x192.png', + sound: '/sounds/alert.mp3', + actions: [ + { + action: 'review', + title: 'Yes!' + } + ], + tag: 'help-notification', + vibrate: [500,110,500,110,450] + }; +function updateHelpNotificationData(name,id) { + if(name=='404') + options.body=''; + else{ + reviewRedirect='location/rating/'+id+'/5'; + options.body='Would you like to add a review to '+name+'?'; + } +}; + +function sendHelpNotification() { + sendMessage({type: 'refresh'}); + if(options.body!=='') + self.registration.showNotification(title,options); +} + +function sendMessageToClient(client, msg){ + return new Promise(function(resolve, reject){ + var msg_chan = new MessageChannel(); + + msg_chan.port1.onmessage = function(event){ + if(event.data.error){ + reject(event.data.error); + }else{ + resolve(event.data); + } + }; + + client.postMessage(msg, [msg_chan.port2]); + }); +} + +function sendMessage(content){ + clients.matchAll().then(clients => { + clients.forEach(client => { + sendMessageToClient(client, content); + }) + }) +} + +self.addEventListener('install', function(e) { + e.waitUntil(self.skipWaiting()); + console.log('Service Worker Installed') +}) + +self.addEventListener('activate', function(e) { + e.waitUntil(self.clients.claim()) + console.log('Service Worker Activated'); +}) + + +self.addEventListener('notificationclick', function(e) { + switch(e.action) { + case 'review': + sendMessage({ + type: 'redirect', + url: reviewRedirect + }); + break; + } + e.notification.close(); +}) + +self.addEventListener('message', function(e) { + updateHelpNotificationData(e.data.name,e.data.id); +}) + +//send first notification +setTimeout(sendHelpNotification,5000); +//Starting the push notification clock +setTimeout(setInterval(sendHelpNotification,3600000),5000); \ No newline at end of file diff --git a/app/public/sounds/alert.mp3 b/app/public/sounds/alert.mp3 new file mode 100644 index 00000000..b9e02f48 Binary files /dev/null and b/app/public/sounds/alert.mp3 differ diff --git a/app/resources/views/includes/footer.blade.php b/app/resources/views/includes/footer.blade.php index 53aa0239..5967b6a3 100644 --- a/app/resources/views/includes/footer.blade.php +++ b/app/resources/views/includes/footer.blade.php @@ -1 +1,7 @@ + + + + \ No newline at end of file diff --git a/app/resources/views/pages/home.blade.php b/app/resources/views/pages/home.blade.php index b8820924..6d8a7a83 100644 --- a/app/resources/views/pages/home.blade.php +++ b/app/resources/views/pages/home.blade.php @@ -1,5 +1,6 @@ @extends('layouts.default', ['body_class' => 'nav-home-page']) @section('footer-content') + @@ -10,9 +11,6 @@ 'longitude': {{ $default_location['longitude'] }} }; - @stop @section('content')