Skip to content

Commit

Permalink
Move native code from file load to inside ionicPlatform.ready
Browse files Browse the repository at this point in the history
I guess this has been working on production due to timing issues but it is not
a long-term fix. All native code should really be inside an ionicPlatform.ready
block and not in random places during file load. This includes all callbacks
  • Loading branch information
shankari committed Sep 25, 2018
1 parent f3918fc commit 0ecfdae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion www/js/common/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ angular.module('emission.main.common.services', [])
commonGraph.data = {};
commonGraph.UPDATE_DONE = "COMMON_GRAPH_UPDATE_DONE";

var db = window.cordova.plugins.BEMUserCache;
var selKey = "common-trips";

commonGraph.createEmpty = function() {
Expand All @@ -18,6 +17,7 @@ angular.module('emission.main.common.services', [])
};

commonGraph.updateCurrent = function() {
var db = window.cordova.plugins.BEMUserCache;
db.getDocument(selKey, false).then(function(entryList) {
try{
var cmGraph = entryList;
Expand Down
33 changes: 21 additions & 12 deletions www/js/diary/current.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
.controller('CurrMapCtrl', function($scope, Config, $state, $timeout, $ionicActionSheet,leafletData,
Logger, $window, PostTripManualMarker, CommHelper, $http, storage, $ionicPlatform) {

Logger.log("controller CurrMapCtrl called from current.js");
console.log("controller CurrMapCtrl called from current.js");
var _map;
var _localIncidentMarkers = [];
var _serverIncidentMarkers = [];
var db = window.cordova.plugins.BEMUserCache;
var db = function() {
return window.cordova.plugins.BEMUserCache;
}
MANUAL_INCIDENT = "manual/incident";
BACKGROUND_LOCATION = "background/location";
INCIDENT_CONFIG = 'incident_config';
Expand All @@ -32,7 +34,7 @@
});

angular.extend($scope.mapCtrl.defaults, Config.getMapTiles());
Logger.log("mapCtrl", $scope.mapCtrl);
console.log("mapCtrl", $scope.mapCtrl);

$scope.verticalSlider = {
options: {
Expand Down Expand Up @@ -106,7 +108,7 @@
});

var refreshTrip = function() {
db.getAllSensorData(BACKGROUND_LOCATION, true).then(function(result) {
db().getAllSensorData(BACKGROUND_LOCATION, true).then(function(result) {
$scope.$apply(function() {
Logger.log("current location data" + JSON.stringify(result[0].data));
var coordinates = result.map(function(locWrapper, index, locList) {
Expand Down Expand Up @@ -175,8 +177,10 @@
};

$scope.$on('leafletDirectiveMap.current.resize', function(event, data) {
Logger.log("current/map received resize event, invalidating map size");
data.leafletObject.invalidateSize();
$ionicPlatform.ready().then(function() {
Logger.log("current/map received resize event, invalidating map size");
data.leafletObject.invalidateSize();
});
});

var addIncidentLayer = function(stress, marker, map) {
Expand Down Expand Up @@ -217,7 +221,7 @@

var getLocalIncidents = function() {
// No metadata, to make it consistent with the server incidents
db.getAllMessages(MANUAL_INCIDENT, false).then(function(incidents) {
db().getAllMessages(MANUAL_INCIDENT, false).then(function(incidents) {
Logger.log("Incidents stored locally" + JSON.stringify(incidents));
if(incidents.length > 0) {
addIncidents(incidents, _map, _localIncidentMarkers);
Expand All @@ -244,7 +248,7 @@

var marker;
$scope.showIncidentSheet = function() {
db.getAllSensorData(BACKGROUND_LOCATION, true).then(function(result) {
db().getAllSensorData(BACKGROUND_LOCATION, true).then(function(result) {
both = [result[0].data.latitude, result[0].data.longitude];
var ts = result[0].data.ts;
var latlng = L.latLng(both);
Expand Down Expand Up @@ -281,9 +285,11 @@
};

$scope.$on('$ionicView.enter', function() {
Logger.log("entered current screen, starting incident refresh");
refreshTripLoop();
getIncidentsLoop();
$ionicPlatform.ready().then(function() {
Logger.log("entered current screen, starting incident refresh");
refreshTripLoop();
getIncidentsLoop();
});
});

$scope.$on('$ionicView.leave', function() {
Expand All @@ -303,6 +309,9 @@
clearTimeout(mapRunning);
clearTimeout(gettingIncidents);
});
getLocalIncidents();

$ionicPlatform.ready().then(function() {
Logger.log("ionicPlatform.ready in current screen, getting local incidents");
getLocalIncidents();
});
});

0 comments on commit 0ecfdae

Please sign in to comment.