Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/app/Providers/ShareBaseUserWithViewsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down
8 changes: 6 additions & 2 deletions app/public/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When was this needed? What error did you run into? Was default_location.latitude undefined at some point?

}
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");
Expand Down
50 changes: 50 additions & 0 deletions app/public/js/service_worker_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var url= window.location.href;
var pos = url.search('using_pwa');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this check to the server. HomeController.php has a method that would be called for route "/". Store the true/false value of whether the request was using_pwa into the server's session so it is available as the user navigates around the site.

This value on the server can be used to prevent including these service worker scripts when not in a PWA.


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);
});
}

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();
}
});
}

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);
}
});
});
};
90 changes: 90 additions & 0 deletions app/public/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var reviewRedirect, title, options;
title = 'We Need Your Help!';
options = {
body: '',
icon: '/images/logo-192x192.png',
sound: '/sounds/alert.wav',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forget to commit this public/sounds/alert.wav file?

Also, is it big? mp3 may be better compressed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must've lost it when I was fixing rebase issues, I'll add it back and change it to an mp3

actions: [
{
action: 'review',
title: 'Yes!'
}
],
tag: 'help-notification',
vibrate: [500,110,500,110,450]
};
function helpNotification(name,id) {
if(name=='404')
options.body='';
else{
reviewRedirect='location/rating/'+id+'/5';
options.body='Would you like to add a review to '+name+'?';
}
//self.registration.showNotification(title, options);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does helpNotification do anything after the showNotification was commented out?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it will be used to update the data based on the new user locations. I'll rename the methods to clear up the confusion since they've changed from what I originally intended when making them

};

function sendNotification() {
//check to send here
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('fetch',function(e) {
//console.log('fetching '+e);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this code that doesn't do anything. The fetch here doesn't do anything. I guess you used it for troubleshooting but it doesn't need to get deployed.

})

self.addEventListener('notificationclick', function(e) {
switch(e.action) {
case 'review':
sendMessage({
type: 'redirect',
url: reviewRedirect
});
break;
}
e.notification.close();
})

self.addEventListener('message', function(e) {
helpNotification(e.data.name,e.data.id);
})

//send first notification
setTimeout(sendNotification,5000);
//Starting the push notification clock
setTimeout(setInterval(sendNotification,3600000),5000);
6 changes: 6 additions & 0 deletions app/resources/views/includes/footer.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<div id="copyright"></div>
<script src="/js/jquery-3.1.1.js" type="text/javascript"></script>
<script src="/js/utils.js" type="text/javascript"></script>
<script type="text/javascript" async defer
src="//maps.googleapis.com/maps/api/js?key={{ $google_map_api_key }}&amp;callback=initMap">
</script>
<script src="/js/service_worker_script.js" type="text/javascript"></script>
4 changes: 1 addition & 3 deletions app/resources/views/pages/home.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@extends('layouts.default', ['body_class' => 'nav-home-page'])
@section('footer-content')
<script src="/js/service_worker_script.js" type="text/javascript"></script>
<script src="/js/jquery-3.1.1.js" type="text/javascript"></script>
<script src="/js/utils.js" type="text/javascript"></script>
<script src="/js/home.js" type="text/javascript"></script>
Expand All @@ -10,9 +11,6 @@
'longitude': {{ $default_location['longitude'] }}
};
</script>
<script type="text/javascript" async defer
src="//maps.googleapis.com/maps/api/js?key={{ $google_map_api_key }}&amp;callback=initMap">
</script>
@stop
@section('content')
<div class="home-page">
Expand Down