Skip to content

Instantly share code, notes, and snippets.

@lucianomlima
Created May 13, 2016 15:57
Show Gist options
  • Save lucianomlima/7ad5d582f110896416d5da2591372496 to your computer and use it in GitHub Desktop.
Save lucianomlima/7ad5d582f110896416d5da2591372496 to your computer and use it in GitHub Desktop.
Pequena parte de código do controller de Gastronomia de um aplicativo, referente a Geolocalização e cálculo de distância.
/**
* @class App.controller.Eat
* @extends Ext.app.Controller
* Controller da parte de Gastronomia
*/
Ext.define('App.controller.Eat', {
extend: 'Ext.app.Controller',
config: {
// Some code here...
},
// Hub Button Handlers
restaurantButtonTapHandler: function () {
App.util.Analytics.trackEvent({
category: 'Gastronomia',
action: 'tap',
label: 'botão',
value: 'Restaurantes'
});
var store = App.app.getStore('restaurant', 'Food', App.util.cache.Food),
avoidConnectionCheck = (store && store.isLoaded()),
callback = this.getLocationCallback();
if (!avoidConnectionCheck) {
avoidConnectionCheck = App.util.SQLite.validCache('restaurant');
}
if (App.app.hasListener('positionaquired')) {
Ext.Viewport.setMasked(false);
App.app.un('positionaquired', callback, this);
}
this.setLocationCallback(false);
App.util.Config.redirectTo('eat/restaurants', avoidConnectionCheck);
},
barButtonTapHandler: function () {
App.util.Analytics.trackEvent({
category: 'Gastronomia',
action: 'tap',
label: 'botão',
value: 'Bares'
});
var store = App.app.getStore('bar', 'Food', App.util.cache.Food),
avoidConnectionCheck = (store && store.isLoaded()),
callback = this.getLocationCallback();
if (!avoidConnectionCheck) {
avoidConnectionCheck = App.util.SQLite.validCache('bar');
}
if (App.app.hasListener('positionaquired')) {
Ext.Viewport.setMasked(false);
App.app.un('positionaquired', callback, this);
}
this.setLocationCallback(false);
App.util.Config.redirectTo('eat/bars', avoidConnectionCheck);
},
pastryButtonTapHandler: function () {
App.util.Analytics.trackEvent({
category: 'Gastronomia',
action: 'tap',
label: 'botão',
value: 'Doces e Salgados'
});
var store = App.app.getStore('pastry', 'Food', App.util.cache.Food),
avoidConnectionCheck = (store && store.isLoaded()),
callback = this.getLocationCallback();
if (!avoidConnectionCheck) {
avoidConnectionCheck = App.util.SQLite.validCache('pastry');
}
if (App.app.hasListener('positionaquired')) {
Ext.Viewport.setMasked(false);
App.app.un('positionaquired', callback, this);
}
this.setLocationCallback(false);
App.util.Config.redirectTo('eat/pastries', avoidConnectionCheck);
},
// Location Helper Functions
locationEnabledSuccess: function(enabled) {
var controller = this,
callback = controller.getLocationCallback();
if (!enabled) {
if (Ext.os.is('android')) {
Ext.Msg.confirm('GPS desligado', 'Gostaria de habilitar o GPS agora?', function(response) {
if (response == 'yes') {
cordova.plugins.diagnostic.switchToLocationSettings();
var handler = Ext.bind(this.onLocationChange, this);
this.setResumeHandler(handler);
document.addEventListener('resume', handler, false);
return;
}
if (callback) {
callback();
}
}, controller);
} else {
var handler = Ext.bind(controller.locationAuthorizedSuccess, controller);
cordova.plugins.diagnostic.isLocationAuthorized(handler, controller.locationError);
}
return;
}
if (callback) {
App.app.on('positionaquired', callback, controller);
}
App.util.GeoPoints.getUserPosition();
},
locationError: function(message) {
var callback = this.getLocationCallback();
if (callback) {
callback();
}
},
onLocationChange: function() {
var controller = this,
callback = this.getLocationCallback();
cordova.plugins.diagnostic.getLocationMode(function(mode) {
if (mode == 'location_off') {
if (callback) {
callback();
}
} else {
// Get user position
if (callback) {
App.app.on('positionaquired', callback, controller);
}
App.util.GeoPoints.getUserPosition();
}
}, function() {
if (callback) {
callback();
}
});
if (this.getResumeHandler()) {
document.removeEventListener('resume', this.getResumeHandler(), false);
this.setResumeHandler(false);
}
},
locationAuthorizedSuccess: function(allowed) {
var controller = this,
callback = controller.getLocationCallback();
if (!allowed) {
var handler = Ext.bind(controller.locationStatusCheck, controller);
cordova.plugins.diagnostic.requestLocationAuthorization(handler, controller.locationError, 'when_in_use');
return;
}
if (callback) {
App.app.on('positionaquired', callback, controller);
}
App.util.GeoPoints.getUserPosition();
},
locationStatusCheck: function (status) {
var denied = false,
controller = this,
callback = controller.getLocationCallback();
if (Ext.os.is('ios')) {
denied = (status == 'denied');
} else {
denied = (status != cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED);
}
if (denied) {
var msg = [
'Você não autorizou ao aplicativo obter sualocalização.',
'Não será possível determinar qual o estabelecimento mais próximo de você.'
];
return Ext.Msg.alert('', msg.join(' '), function () {
if (callback) {
callback();
}
});
}
if (callback) {
App.app.on('positionaquired', callback, controller);
}
App.util.GeoPoints.getUserPosition();
}
// More code here...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment