|
@@ -1,15 +1,14 @@
|
|
|
import { Component } from '@angular/core';
|
|
|
-import { NavController, NavParams, ActionSheetController, ToastController, LoadingController } from 'ionic-angular';
|
|
|
-import { Shake, BarcodeScanner } from 'ionic-native';
|
|
|
+import { NavController, NavParams, ActionSheetController, ToastController, LoadingController, AlertController } from 'ionic-angular';
|
|
|
import { Product } from '../../models/product';
|
|
|
import { ProductListPage } from '../product-list/product-list';
|
|
|
import { DataProvider } from '../../providers/data-provider'
|
|
|
-import { CameraProvider } from '../../providers/camera-provider';
|
|
|
+import { PhoneProvider } from '../../providers/phone-provider';
|
|
|
import { PreferencesProvider } from '../../providers/preferences-provider';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'page-product-details',
|
|
|
- providers: [ Product, CameraProvider],
|
|
|
+ providers: [ Product, PhoneProvider ],
|
|
|
templateUrl: 'product-details.html'
|
|
|
})
|
|
|
export class ProductDetailsPage {
|
|
@@ -28,7 +27,8 @@ export class ProductDetailsPage {
|
|
|
public toastCtrl: ToastController,
|
|
|
public actionSheetCtrl: ActionSheetController,
|
|
|
public loadingCtrl: LoadingController,
|
|
|
- public cameraProvider: CameraProvider,
|
|
|
+ public alertCtrl: AlertController,
|
|
|
+ public phone: PhoneProvider,
|
|
|
public preferencesPreferences: PreferencesProvider
|
|
|
) {
|
|
|
if (!(this.params.data instanceof ProductListPage)) {
|
|
@@ -41,25 +41,50 @@ export class ProductDetailsPage {
|
|
|
*
|
|
|
*/
|
|
|
ionViewDidLoad() {
|
|
|
- this.watcher = Shake.startWatch(40).subscribe(() => {
|
|
|
- BarcodeScanner.scan().then(bar => {
|
|
|
- this.product.ean13 = bar.cancelled ? null : bar.text;
|
|
|
- }).catch(e => {
|
|
|
- this.toastCtrl.create({
|
|
|
- message: 'No se ha podido leer el código',
|
|
|
- duration: 3000
|
|
|
- }).present();
|
|
|
- });
|
|
|
- });
|
|
|
+ this.phone.enableShakeWatcher().subscribe(() => this.askIfScanBarcode(), e => console.log(e));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
ionViewDidLeave() {
|
|
|
- if (this.watcher) {
|
|
|
- this.watcher.unsubscribe();
|
|
|
- }
|
|
|
+ this.phone.disableShakeWatcher();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ askIfScanBarcode(): void {
|
|
|
+ this.alertCtrl.create({
|
|
|
+ title: "Confirmar",
|
|
|
+ message: "Desea escanear código de barras?",
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: "Cancelar",
|
|
|
+ handler: () => console.log("Canceled")
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: "Aceptar",
|
|
|
+ handler: () => this.scanBarcode()
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }).present();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ scanBarcode(): void {
|
|
|
+ this.phone.scanBarcode().subscribe(barcode => {
|
|
|
+ this.product.ean13 = barcode.cancelled ? null : barcode.text;
|
|
|
+ }, e => {
|
|
|
+ console.log(e);
|
|
|
+
|
|
|
+ this.toastCtrl.create({
|
|
|
+ message: 'No se ha podido leer el código de barras',
|
|
|
+ duration: 3000
|
|
|
+ }).present();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -83,16 +108,12 @@ export class ProductDetailsPage {
|
|
|
{
|
|
|
text: "Desde la cámara",
|
|
|
icon: "camera",
|
|
|
- handler: () => {
|
|
|
- this.takePicture("camera");
|
|
|
- }
|
|
|
+ handler: () => this.takePicture("camera")
|
|
|
},
|
|
|
{
|
|
|
text: "Desde la galería",
|
|
|
icon: "images",
|
|
|
- handler: () => {
|
|
|
- this.takePicture("album");
|
|
|
- }
|
|
|
+ handler: () => this.takePicture("album")
|
|
|
},
|
|
|
{
|
|
|
text: "Cancelar",
|
|
@@ -106,11 +127,16 @@ export class ProductDetailsPage {
|
|
|
*
|
|
|
*/
|
|
|
takePicture(source: string): void {
|
|
|
- this.cameraProvider.getPicture(source).then(i => {
|
|
|
- this.product.image_medium = i;
|
|
|
- this.product.image_small = i;
|
|
|
- }).catch(e => {
|
|
|
- console.log(e);
|
|
|
+ this.phone.openCamera(source).subscribe(image => {
|
|
|
+ this.product.image_medium = image;
|
|
|
+ this.product.image_small = image;
|
|
|
+ }, e => {
|
|
|
+ console.log(e);
|
|
|
+
|
|
|
+ this.toastCtrl.create({
|
|
|
+ message: 'No se ha podido tomar la foto',
|
|
|
+ duration: 3000
|
|
|
+ }).present();
|
|
|
});
|
|
|
}
|
|
|
|