Skip to content

Commit

Permalink
fixed retrieving and storing values in localStorage (PokemonGoF#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chlodochar authored Jul 25, 2016
1 parent 6c01f06 commit 67341fc
Showing 1 changed file with 164 additions and 41 deletions.
205 changes: 164 additions & 41 deletions static/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,137 @@ var pokemon_sprites = {
}
};


//
// LocalStorage helpers
//

var StoreTypes = {
Boolean: {
parse: function(str) {
switch(str.toLowerCase()) {
case '1':
case 'true':
case 'yes':
return true;
default:
return false;
}
},
stringify: function(b) {
return b ? 'true' : 'false';
}
},
JSON: {
parse: function(str) {
return JSON.parse(str);
},
stringify: function(json) {
return JSON.stringify(json);
}
},
String: {
parse: function(str) {
return str;
},
stringify: function(str) {
return str;
}
},
Number: {
parse: function(str) {
return parseInt(str, 10);
},
stringify: function(number) {
return number.toString();
}
}
};

var StoreOptions = {
map_style: {
default: 'roadmap',
type: StoreTypes.String
},
remember_select_exclude: {
default: [],
type: StoreTypes.JSON
},
remember_select_notify: {
default: [],
type: StoreTypes.JSON
},
showGyms: {
default: false,
type: StoreTypes.Boolean
},
showPokemon: {
default: true,
type: StoreTypes.Boolean
},
showLuredPokemon: {
default: true,
type: StoreTypes.Boolean
},
showPokestops: {
default: true,
type: StoreTypes.Boolean
},
showScanned: {
default: false,
type: StoreTypes.Boolean
},
playSound: {
default: false,
type: StoreTypes.Boolean
},
geoLocate: {
default: false,
type: StoreTypes.Boolean
},
playSound: {
default: false,
type: StoreTypes.Boolean
},
pokemonIcons: {
default: 'highres',
type: StoreTypes.String
},
iconSizeModifier: {
default: 0,
type: StoreTypes.Number
}
};

var Store = {
getOption: function(key) {
var option = StoreOptions[key];
if (!option) {
throw "Store key was not defined " + key;
}
return option;
},
get: function(key) {
var option = this.getOption(key);
var optionType = option.type;
var rawValue = localStorage[key];
if (rawValue === null || rawValue === undefined) {
return option.default;
}
var value = optionType.parse(rawValue);
return value;
},
set: function(key, value) {
var option = this.getOption(key);
var optionType = option.type || StoreTypes.String;
var rawValue = optionType.stringify(value);
localStorage[key] = rawValue;
},
reset: function(key) {
localStorage.removeItem(key);
}
};

//
// Functions
//
Expand Down Expand Up @@ -128,14 +259,10 @@ function initMap() {
map.mapTypes.set('style_pgo_nl', style_pgo_nl);

map.addListener('maptypeid_changed', function(s) {
localStorage['map_style'] = this.mapTypeId;
Store.set('map_style', this.mapTypeId);
});

if (!localStorage['map_style'] || localStorage['map_style'] === 'undefined') {
localStorage['map_style'] = 'roadmap';
}

map.setMapTypeId(localStorage['map_style']);
map.setMapTypeId(Store.get('map_style'));
google.maps.event.addListener(map, 'idle', updateMap);

marker = createSearchMarker();
Expand Down Expand Up @@ -185,13 +312,13 @@ function createSearchMarker() {
}

function initSidebar() {
$('#gyms-switch').prop('checked', localStorage.showGyms === 'true');
$('#pokemon-switch').prop('checked', localStorage.showPokemon === 'true');
$('#lured-pokemon-switch').prop('checked', localStorage.showLuredPokemon === 'true');
$('#pokestops-switch').prop('checked', localStorage.showPokestops === 'true');
$('#geoloc-switch').prop('checked', localStorage.geoLocate === 'true');
$('#scanned-switch').prop('checked', localStorage.showScanned === 'true');
$('#sound-switch').prop('checked', localStorage.playSound === 'true');
$('#gyms-switch').prop('checked', Store.get('showGyms'));
$('#pokemon-switch').prop('checked', Store.get('showPokemon'));
$('#lured-pokemon-switch').prop('checked', Store.get('showLuredPokemon'));
$('#pokestops-switch').prop('checked', Store.get('showPokestops'));
$('#geoloc-switch').prop('checked', Store.get('geoLocate'));
$('#scanned-switch').prop('checked', Store.get('showScanned'));
$('#sound-switch').prop('checked', Store.get('playSound'));

var searchBox = new google.maps.places.SearchBox(document.getElementById('next-location'));

Expand All @@ -210,8 +337,8 @@ function initSidebar() {
$.each(pokemon_sprites, function(key, value) {
icons.append($('<option></option>').attr("value", key).text(value.name));
});
icons.val((pokemon_sprites[localStorage.pokemonIcons]) ? localStorage.pokemonIcons : 'highres');
$('#pokemon-icon-size').val(localStorage.iconModifierSize || 0);
icons.val((pokemon_sprites[Store.get('pokemonIcons')]) ? Store.get('pokemonIcons') : 'highres');
$('#pokemon-icon-size').val(Store.get('iconSizeModifier'));
}

function pad(number) { return number <= 99 ? ("0" + number).slice(-2) : number; }
Expand Down Expand Up @@ -374,9 +501,9 @@ function getGoogleSprite(index, sprite, display_height) {
function setupPokemonMarker(item) {

// Scale icon size up with the map exponentially
var icon_size = 2 + (map.getZoom()-3) * (map.getZoom()-3) * .2 + parseInt(localStorage.iconSizeModifier || 0);
var icon_size = 2 + (map.getZoom()-3) * (map.getZoom()-3) * .2 + Store.get('iconSizeModifier');
var pokemon_index = item.pokemon_id - 1;
var sprite = pokemon_sprites[localStorage.pokemonIcons] || pokemon_sprites['highres']
var sprite = pokemon_sprites[Store.get('pokemonIcons')] || pokemon_sprites['highres']
var icon = getGoogleSprite(pokemon_index, sprite, icon_size);

var marker = new google.maps.Marker({
Expand All @@ -396,7 +523,7 @@ function setupPokemonMarker(item) {
});

if (notifiedPokemon.indexOf(item.pokemon_id) > -1) {
if (localStorage.playSound === 'true') {
if (Store.get('playSound')) {
audio.play();
}

Expand Down Expand Up @@ -564,10 +691,10 @@ function clearOutOfBoundsMarkers(markers) {
}

function loadRawData() {
var loadPokemon = localStorage.showPokemon || true;
var loadGyms = localStorage.showGyms || true;
var loadPokestops = localStorage.showPokestops || localStorage.showLuredPokemon || false; //lured mons need pokestop data
var loadScanned = localStorage.showScanned || false;
var loadPokemon = Store.get('showPokemon');
var loadGyms = Store.get('showGyms');
var loadPokestops = Store.get('showPokestops') || Store.get('showLuredPokemon');
var loadScanned = Store.get('showScanned');

var bounds = map.getBounds();
var swPoint = bounds.getSouthWest();
Expand Down Expand Up @@ -605,7 +732,7 @@ function loadRawData() {
}

function processPokemons(i, item) {
if (!(localStorage.showPokemon === 'true')) {
if (!Store.get('showPokemon')) {
return false; // in case the checkbox was unchecked in the meantime.
}
if (!(item.encounter_id in map_data.pokemons) &&
Expand All @@ -618,7 +745,7 @@ function processPokemons(i, item) {
}

function processPokestops(i, item) {
if (!(localStorage.showPokestops === 'true')) {
if (!Store.get('showPokestops')) {
return false;
}
if (map_data.pokestops[item.pokestop_id] == null) { // add marker to map and item to dict
Expand All @@ -638,7 +765,7 @@ function processPokestops(i, item) {
}

function processLuredPokemon(i, item) {
if (!(localStorage.showLuredPokemon === 'true')) {
if (!Store.get('showLuredPokemon')) {
return false;
}

Expand Down Expand Up @@ -671,7 +798,7 @@ function processLuredPokemon(i, item) {
}

function processGyms(i, item) {
if (!(localStorage.showGyms === 'true')) {
if (!Store.get('showGyms')) {
return false; // in case the checkbox was unchecked in the meantime.
}

Expand All @@ -697,7 +824,7 @@ function processGyms(i, item) {


function processScanned(i, item) {
if (!localStorage.showScanned) {
if (!Store.get('showScanned')) {
return false;
}

Expand Down Expand Up @@ -939,27 +1066,23 @@ $(function () {
$selectExclude.on("change", function (e) {
excludedPokemon = $selectExclude.val().map(Number);
clearStaleMarkers();
localStorage.remember_select_exclude = JSON.stringify(excludedPokemon);
Store.set('remember_select_exclude', excludedPokemon);
});
$selectNotify.on("change", function (e) {
notifiedPokemon = $selectNotify.val().map(Number);
localStorage.remember_select_notify = JSON.stringify(notifiedPokemon);
Store.set('remember_select_notify', notifiedPokemon);
});

// recall saved lists
if (localStorage['remember_select_exclude']) {
$selectExclude.val(JSON.parse(localStorage.remember_select_exclude)).trigger("change");
}
if (localStorage['remember_select_notify']) {
$selectNotify.val(JSON.parse(localStorage.remember_select_notify)).trigger("change");
}
$selectExclude.val(Store.get('remember_select_exclude')).trigger("change");
$selectNotify.val(Store.get('remember_select_notify')).trigger("change");
});

// run interval timers to regularly update map and timediffs
window.setInterval(updateLabelDiffTime, 1000);
window.setInterval(updateMap, 5000);
window.setInterval(function() {
if(navigator.geolocation && localStorage.geoLocate === 'true') {
if(navigator.geolocation && Store.get('geoLocate')) {
navigator.geolocation.getCurrentPosition(function (position){
var baseURL = location.protocol + "//" + location.hostname + (location.port ? ":"+location.port: "");
lat = position.coords.latitude;
Expand All @@ -982,7 +1105,7 @@ $(function () {

function buildSwitchChangeListener(data, data_type, storageKey) {
return function () {
localStorage[storageKey] = this.checked;
Store.set(storageKey, this.checked);
if (this.checked) {
updateMap();
} else {
Expand All @@ -1002,17 +1125,17 @@ $(function () {
$('#scanned-switch').change(buildSwitchChangeListener(map_data, "scanned", "showScanned"));

$('#sound-switch').change(function() {
localStorage["playSound"] = this.checked;
Store.set("playSound", this.checked);
});

$('#pokemon-icons').change(function() {
localStorage["pokemonIcons"] = this.value;
Store.set('pokemonIcons', this.value);
redrawPokemon(map_data.pokemons);
redrawPokemon(map_data.lure_pokemons);
});

$('#pokemon-icon-size').change(function() {
localStorage["iconSizeModifier"] = this.value;
Store.set('iconSizeModifier', this.value);
redrawPokemon(map_data.pokemons);
redrawPokemon(map_data.lure_pokemons);
});
Expand All @@ -1021,7 +1144,7 @@ $(function () {
if(!navigator.geolocation)
this.checked = false;
else
localStorage["geoLocate"] = this.checked;
Store.set('geoLocate', this.checked);
});

});

0 comments on commit 67341fc

Please sign in to comment.