Skip to content

Commit

Permalink
chore: prepped ios
Browse files Browse the repository at this point in the history
  • Loading branch information
Slicit committed Apr 23, 2021
1 parent e680895 commit 758ad37
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ionic/platforms/ios/www/dist/services.bundle.min.js

Large diffs are not rendered by default.

170 changes: 170 additions & 0 deletions ionic/platforms/ios/www/js/services/picture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* Picture
*
* @author Xtraball SAS
*/
angular
.module('starter')
.service('Picture', function ($cordovaCamera, $ionicActionSheet, $q, $rootScope, $translate, Dialog, SB) {
var service = {
isOpen: false,
sheetResolver: null,
stack: []
};

/**
* @param width
* @param height
* @param quality
*/
service.takePicture = function (width, height, quality) {
if (service.isOpen) {
return $q.reject();
}

if (Camera === undefined) {
Dialog.alert("Error", "Camera is not available.", "OK", -1)
.then(function () {
return $q.reject();
});
return $q.reject();
}

service.isOpen = true;

var deferred = $q.defer();

var localWidth = (width === undefined) ? 1200 : width;
var localHeight = (height === undefined) ? 1200 : height;
var localQuality = (quality === undefined) ? 90 : quality;

var sourceType = Camera.PictureSourceType.CAMERA;

var _buttons = [
{
text: $translate.instant("Import from Library")
}
];

if (DEVICE_TYPE !== SB.DEVICE.TYPE_BROWSER) {
_buttons.unshift({
text: $translate.instant('Take a picture')
});
}

service.sheetResolver = $ionicActionSheet.show({
buttons: _buttons,
cancelText: $translate.instant('Cancel'),
cancel: function () {
service.sheetResolver();
deferred.reject({
message: $translate.instant('Cancelled')
});
service.isOpen = false;
},
buttonClicked: function (index) {
if (index === 0) {
sourceType = Camera.PictureSourceType.CAMERA;
}

if (index === 1) {
sourceType = Camera.PictureSourceType.PHOTOLIBRARY;
}

var options = {
quality: localQuality,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: sourceType,
encodingType: Camera.EncodingType.JPEG,
targetWidth: localWidth,
targetHeight: localHeight,
correctOrientation: true,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};

if (DEVICE_TYPE === SB.DEVICE.TYPE_BROWSER) {
var input = angular.element('<input type="file" accept="image/*">');
var selectedFile = function (selectEvent) {
var file = selectEvent.currentTarget.files[0];
var reader = new FileReader();
reader.onload = function (onloadEvent) {
input.off('change', selectedFile);

if (onloadEvent.target.result.length > 0) {
service.sheetResolver();
deferred.resolve({
image: onloadEvent.target.result
});
service.isOpen = false;
} else {
service.sheetResolver();
service.isOpen = false;
}
};

reader.onerror = function (onerrorEvent) {
service.sheetResolver();
Dialog.alert('Error', 'An error occurred while loading the picture.', 'OK', -1)
.then(function () {
service.isOpen = false;
});
};

reader.readAsDataURL(file);
};

document.body._onfocus = document.body.onfocus;
document.body.onfocus = function (filePickerClosed) {
service.isOpen = false;

// Revert back
document.body.onfocus = document.body._onfocus;
};

input.on('change', selectedFile);
input[0].click();
} else {
$cordovaCamera.getPicture(options)
.then(function (imageData) {
service.sheetResolver();

deferred.resolve({
image: 'data:image/jpeg;base64,' + imageData
});

service.isOpen = false;
}, function (error) {
service.sheetResolver();

Dialog.alert('Error', 'An error occurred while taking a picture.', 'OK', -1)
.then(function () {
service.isOpen = false;
});

deferred.reject({
message: error
});
}).catch(function (error) {
service.sheetResolver();

Dialog.alert('Error', 'An error occurred while taking a picture.', 'OK', -1)
.then(function () {
service.isOpen = false;
});

deferred.reject({
message: error
});
});
}

return true;
}
});

return deferred.promise;
};

return service;
});

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 758ad37

Please sign in to comment.