Skip to content

Commit

Permalink
Images: Check for Cordova when getting or saving images
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicagood committed Nov 26, 2018
1 parent 5faaab8 commit 6c09a2f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
29 changes: 20 additions & 9 deletions www/app/project/manage/manage-project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
.module('app')
.controller('ManageProjectController', ManageProjectController);

ManageProjectController.$inject = ['$document', '$ionicModal', '$ionicLoading', '$ionicPopover', '$ionicPopup', '$log', '$scope', '$state', '$q', 'FormFactory', 'ImageFactory', 'LiveDBFactory', 'LocalStorageFactory', 'OtherMapsFactory',
'ProjectFactory', 'RemoteServerFactory', 'SpotFactory', 'UserFactory', 'IS_WEB'];

function ManageProjectController($document, $ionicModal, $ionicLoading, $ionicPopover, $ionicPopup, $log, $scope, $state, $q, FormFactory, ImageFactory, LiveDBFactory, LocalStorageFactory, OtherMapsFactory, ProjectFactory, RemoteServerFactory,SpotFactory, UserFactory, IS_WEB) {
ManageProjectController.$inject = ['$document', '$ionicModal', '$ionicLoading', '$ionicPopover', '$ionicPopup',
'$log', '$scope', '$state', '$q', '$window', 'FormFactory', 'ImageFactory', 'LiveDBFactory', 'LocalStorageFactory',
'OtherMapsFactory', 'ProjectFactory', 'RemoteServerFactory', 'SpotFactory', 'UserFactory', 'IS_WEB'];

function ManageProjectController($document, $ionicModal, $ionicLoading, $ionicPopover, $ionicPopup, $log, $scope,
$state, $q, $window, FormFactory, ImageFactory, LiveDBFactory, LocalStorageFactory,
OtherMapsFactory, ProjectFactory, RemoteServerFactory,SpotFactory, UserFactory,
IS_WEB) {
var vm = this;

var downloadErrors = false;
Expand Down Expand Up @@ -76,7 +80,8 @@

ProjectFactory.setUser(user);
FormFactory.setForm('project');
LocalStorageFactory.checkImagesDir();
if (!IS_WEB && $window.cordova) LocalStorageFactory.checkImagesDir();
if (!IS_WEB && !$window.cordova) $log.warn('Not Web but unable get image directory. Running for development?');

if (_.isEmpty(vm.project)) {
vm.showExitProjectModal = false;
Expand Down Expand Up @@ -203,7 +208,7 @@
}

function downloadImages(neededImagesIds) {
if (!IS_WEB) {
if (!IS_WEB && $window.cordova) {
var promises = [];
var imagesDownloadedCount = 0;
var imagesFailedCount = 0;
Expand All @@ -225,6 +230,9 @@
outputMessage(
'NEW Images Downloaded: ' + imagesDownloadedCount + ' of ' + neededImagesIds.length +
'<br>NEW Images Saved: ' + savedImagesCount + ' of ' + neededImagesIds.length);
}, function () {
$log.error('Unable to save image locally', neededImageId);
imagesFailedCount++;
});
//});
}
Expand All @@ -244,7 +252,10 @@
outputMessage('Image Downloads Failed: ' + imagesFailedCount);
}
});

}
else if (!IS_WEB && !$window.cordova) {
$log.warn('Not Web but no Cordova so unable to get local image sources. Running for development?');
outputMessage('Unable to Download Images: ' + neededImagesIds.length);
}
}

Expand Down Expand Up @@ -449,7 +460,7 @@
setSpotsDataset();
}, function (err) {
notifyMessages.splice(0, 1);
outputMessage('<br>Error Updating Dataset! Error:' + err);
outputMessage('<br>Error Updating Dataset! Error: ' + err);
}).finally(function () {
$ionicLoading.show({
scope: $scope, template: notifyMessages.join('<br>') + '<br><br>' +
Expand Down Expand Up @@ -1078,7 +1089,7 @@
initializeProject();
}, function (err) {
notifyMessages.splice(0, 1);
outputMessage('<br>Error Updating Project! Error:' + err);
outputMessage('<br>Error Updating Project! Error: ' + err);
}).finally(function () {
$ionicLoading.show({
scope: $scope, template: notifyMessages.join('<br>') + '<br><br>' +
Expand Down
37 changes: 22 additions & 15 deletions www/app/shared/localstorage.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
.module('app')
.factory('LocalStorageFactory', LocalStorageFactory);

LocalStorageFactory.$inject = ['$cordovaDevice', '$cordovaFile', '$log', '$q', '$window', 'HelpersFactory'];
LocalStorageFactory.$inject = ['$cordovaDevice', '$cordovaFile', '$log', '$q', '$window', 'HelpersFactory', 'IS_WEB'];

function LocalStorageFactory($cordovaDevice, $cordovaFile, $log, $q, $window, HelpersFactory) {
function LocalStorageFactory($cordovaDevice, $cordovaFile, $log, $q, $window, HelpersFactory, IS_WEB) {
var dbs = {};
dbs.configDb = {}; // global LocalForage for configuration and user data
dbs.imagesDb = {}; // global LocalForage for images
Expand Down Expand Up @@ -134,7 +134,8 @@
}
}
catch (err) {
$log.log(err);
//$log.log(err);
$log.error('Error getting device path');
}
return devicePath;
}
Expand Down Expand Up @@ -242,7 +243,7 @@
deferred.resolve(fullPath + fileName);
},
function (writeError) {
$log.log('Error writing file!', writeError);
$log.error('Error writing file!');
deferred.reject(fullPath + fileName)
}
);
Expand Down Expand Up @@ -383,19 +384,25 @@
function getImageById(imageId) { //read file from file system
var deferred = $q.defer(); // init promise

var devicePath = getDevicePath();
var filePath = devicePath + imagesDirectory;
var fileName = imageId + '.jpg';
$log.log('Looking for file: ',filePath,fileName);
$cordovaFile.checkFile(filePath + '/', fileName).then(function (checkDirSuccess) {
//$cordovaFile.readAsText(filePath + '/', fileName).then(function(result){
$cordovaFile.readAsDataURL(filePath + '/', fileName).then(function(result){
deferred.resolve(result);
if ($window.cordova) {
var devicePath = getDevicePath();
var filePath = devicePath + imagesDirectory;
var fileName = imageId + '.jpg';
$log.log('Looking for file: ', filePath, fileName);
$cordovaFile.checkFile(filePath + '/', fileName).then(function (checkDirSuccess) {
//$cordovaFile.readAsText(filePath + '/', fileName).then(function(result){
$cordovaFile.readAsDataURL(filePath + '/', fileName).then(function (result) {
deferred.resolve(result);
});
}, function (checkDirFailed) {
$log.log('Check file not found.', checkDirFailed);
deferred.resolve('img/image-not-found.png');
});
}, function (checkDirFailed) {
$log.log('Check file not found.',checkDirFailed)
}
else {
if (!IS_WEB) $log.warn('Not Web but no Cordova so unable to get local image source. Running for development?');
deferred.resolve('img/image-not-found.png');
});
}

return deferred.promise;
}
Expand Down

0 comments on commit 6c09a2f

Please sign in to comment.