Browse Source

act more like a client-side app

* stores location prefs client side
* retrieves options immediately if location checkbox is unchecked
* if location fails, loads buttons w/o location context
* waits for location to succeed to load options
* if no location is provided, uses location of last entry when retrieving buttons
pull/10/head
Aaron Parecki 5 years ago
parent
commit
82a67a4334
5 changed files with 2578 additions and 57 deletions
  1. +21
    -8
      lib/helpers.php
  2. +2497
    -0
      public/js/localforage.js
  3. +1
    -1
      views/appcache.php
  4. +1
    -0
      views/layout.php
  5. +58
    -48
      views/new-post.php

+ 21
- 8
lib/helpers.php View File

@@ -277,7 +277,7 @@ function query_user_nearby_options($type, $user_id, $latitude, $longitude) {
WHERE num > 2 /* only include things that have been used more than 2 times in this bucket */
GROUP BY content /* group by name again */
ORDER BY SUM(num) DESC /* order by overall frequency */
LIMIT 4
LIMIT 6
', ['user_id'=>$user_id, 'type'=>$type, 'latitude'=>$latitude, 'longitude'=>$longitude, 'published'=>$published])->find_many();
foreach($optionsQ as $o) {
$options[] = [
@@ -300,7 +300,7 @@ function query_user_frequent_options($type, $user_id) {
AND published > :published
GROUP BY content
ORDER BY COUNT(1) DESC
LIMIT 4
LIMIT 6
', ['user_id'=>$user_id, 'type'=>$type, 'published'=>$published])->find_many();
foreach($optionsQ as $o) {
$options[] = [
@@ -351,7 +351,7 @@ function query_last_eaten($user_id, $type, $content) {
function get_entry_options($user_id, $latitude=null, $longitude=null) {
/*
Sections:
* Recent 2 posts (food + drink combined)
* Recent posts (food + drink combined)
* Drinks (based on location)
* custom box below
* Food (based on location)
@@ -373,8 +373,14 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) {
ORDER BY published DESC) AS tmp
GROUP BY content
ORDER BY MAX(published) DESC
LIMIT 4', ['user_id'=>$user_id])->find_many();
LIMIT 6', ['user_id'=>$user_id])->find_many();
$last_latitude = false;
$last_longitude = false;
foreach($recentQ as $r) {
if($last_latitude == false && $r->latitude) {
$last_latitude = $r->latitude;
$last_longitude = $r->longitude;
}
$recent[] = [
'title' => $r->content,
'subtitle' => query_last_eaten($user_id, $r->type, $r->content),
@@ -382,6 +388,12 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) {
];
}

// If no location was provided, but there is a location in the most recent entry, use that
if($latitude == null && $last_latitude) {
$latitude = $last_latitude;
$longitude = $last_longitude;
}

if($latitude) {
$drinks = query_user_nearby_options('drink', $user_id, $latitude, $longitude);
}
@@ -390,9 +402,10 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) {
$drinks = query_user_frequent_options('drink', $user_id);
}
// If there's less than 4 options available, fill the list with the default options
if(count($drinks) < 4) {
$num_options = 6;
if(count($drinks) < 6) {
$default = default_drink_options();
while(count($drinks) < 4) {
while(count($drinks) < 6) {
$next = array_shift($default);
if(!in_array(['title'=>$next,'type'=>'drink'], $drinks)) {
$drinks[] = [
@@ -410,9 +423,9 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) {
if(count($food) == 0) {
$food = query_user_frequent_options('eat', $user_id);
}
if(count($food) < 4) {
if(count($food) < $num_options) {
$default = default_food_options();
while(count($food) < 4) {
while(count($food) < $num_options) {
$next = array_shift($default);
if(!in_array(['title'=>$next,'type'=>'eat'], $food)) {
$food[] = [


+ 2497
- 0
public/js/localforage.js
File diff suppressed because it is too large
View File


+ 1
- 1
views/appcache.php View File

@@ -12,4 +12,4 @@ CACHE MANIFEST
NETWORK:
*

# v9
# v11

+ 1
- 0
views/layout.php View File

@@ -29,6 +29,7 @@
<link rel="icon" href="/images/teacup-16px.png" type="image/png">

<script src="/js/jquery-1.7.1.min.js"></script>
<script src="/js/localforage.js"></script>
</head>

<body role="document">


+ 58
- 48
views/new-post.php View File

@@ -12,7 +12,6 @@
</div>

<div id="entry-buttons">
<?= partial('partials/entry-buttons', ['options'=>$this->default_options]) ?>
</div>

<div class="form-group">
@@ -84,12 +83,6 @@ $(function(){
return num;
}

// Set the date from JS
var d = new Date();
$("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate()));
$("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
$("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1));

function bind_keyboard_shortcuts() {
$(".text-custom-eat").keydown(function(e){
if(e.keyCode == 13) {
@@ -105,11 +98,9 @@ $(function(){
});
}

bind_keyboard_shortcuts();

function location_error(msg) {
$("#note_location_msg").val(msg);
$("#note_location_chk").removeAttr("checked");
// $("#note_location_chk").removeAttr("checked");
$("#note_location_loading").hide();
$("#note_location_img").hide();
$("#note_location_msg").removeClass("img-visible");
@@ -123,34 +114,7 @@ $(function(){

navigator.geolocation.getCurrentPosition(function(position){

$.getJSON('/options.json', {
latitude: position.coords.latitude,
longitude: position.coords.longitude
}, function(response) {
// save and restore the value entered in the custom fields
var custom_eat = $('#custom_eat').val();
var custom_drink = $('#custom_drink').val();

var selected = false;
if($("#custom_drink:focus").length == 1) {
selected = '#custom_drink';
}
if($("#custom_eat:focus").length == 1) {
selected = '#custom_eat';
}

$("#entry-buttons").html(response.buttons);

// restore the custom values entered
$('#custom_eat').val(custom_eat);
$('#custom_drink').val(custom_drink);

if(selected) {
$(selected).focus();
}

bind_keyboard_shortcuts();
});
load_entry_buttons(position.coords);

$("#note_location_loading").hide();
var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
@@ -169,17 +133,32 @@ $(function(){
} else if(err.code == 3) {
location_error("Timed out getting location");
}
// Load the entry buttons with no location context
load_entry_buttons();
});
}

function set_location_enabled(enabled) {
localforage.setItem('location-enabled', {enabled: enabled});
}
function get_location_enabled(callback) {
localforage.getItem('location-enabled', function(err,val){
if(val) {
callback(val.enabled);
} else {
callback(false);
}
});
}

$("#note_location_chk").click(function(){
if($(this).attr("checked") == "checked") {
if(navigator.geolocation) {
$.post("/prefs", {
enabled: 1
});
fetch_location();
set_location_enabled(true);
fetch_location(); // will load the entry buttons even if location fails
} else {
set_location_enabled(false);
location_error("Browser location is not supported");
}
} else {
@@ -188,17 +167,48 @@ $(function(){
$("#note_location_msg").val('');
$("#note_location").val('');

$.post("/prefs", {
enabled: 0
});
set_location_enabled(false);
}
});

if($("#location_enabled").val() == 1) {
$("#note_location_chk").attr("checked","checked");
fetch_location();
// This loads the buttons with or without location
function load_entry_buttons(coords) {
var latitude = coords ? coords.latitude : '';
var longitude = coords ? coords.longitude : '';

$.getJSON('/options.json', {
latitude: latitude,
longitude: longitude
}, function(response) {
$("#entry-buttons").html(response.buttons);
bind_keyboard_shortcuts();
});
}

///////////////////////////////////////////////////////////////
// App Start

// Set the date from JS
var d = new Date();
$("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate()));
$("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
$("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1));

// Check if location is enabled in the localstorage prefs
get_location_enabled(function(enabled){
if(enabled) {
// If location is enabled, fetch location and load the entry buttons
fetch_location(); // will load the buttons even if location fails
$("#note_location_chk").attr("checked","checked");

} else {
// If location is not enabled, fetch prefs immediately
$("#note_location_chk").removeAttr("checked");
load_entry_buttons();

}
});

});

</script>


Loading…
Cancel
Save