Skip to content

Commit

Permalink
MOBILE-3947 qr-scanner: Create qr scanner service
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonso-salces committed Dec 1, 2023
1 parent 7c643dc commit 0bf97b7
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/features/native/native.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { Keyboard } from '@awesome-cordova-plugins/keyboard/ngx';
import { LocalNotifications } from '@awesome-cordova-plugins/local-notifications/ngx';
import { MediaCapture } from '@awesome-cordova-plugins/media-capture/ngx';
import { Push } from '@features/native/plugins/push';
import { QRScanner } from '@ionic-native/qr-scanner/ngx';
import { QRScanner } from './plugins/qrscanner';
import { SplashScreen } from '@awesome-cordova-plugins/splash-screen/ngx';
import { SQLite } from '@awesome-cordova-plugins/sqlite/ngx';
import { StatusBar } from '@awesome-cordova-plugins/status-bar/ngx';
Expand Down
2 changes: 2 additions & 0 deletions src/core/features/native/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { makeSingleton } from '@singletons';
import { Chooser as ChooserService } from './chooser';
import { Push as PushService } from './push';
import { Zip as ZipService } from './zip';
import { QRScanner as QRScannerService } from './qrscanner';

export const Chooser = makeSingleton(ChooserService);
export const Push = makeSingleton(PushService);
export const Zip = makeSingleton(ZipService);
export const QRScanner = makeSingleton(QRScannerService);
182 changes: 182 additions & 0 deletions src/core/features/native/plugins/qrscanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { QRScannerError, QRScannerStatus } from '@moodlehq/cordova-plugin-qrscanner';

/**
* QR Scanner plugin wrapper
*/
@Injectable({ providedIn: 'root' })
export class QRScanner {

/**
* Destroy the scanner instance.
*
* @returns QR scanner status.
*/
destroy(): Promise<QRScannerStatus> {
return new Promise((resolve) => window.QRScanner.destroy(resolve));
}

/**
* Request permission to use QR scanner.
*
* @returns QR scanner status.
*/
prepare(): Promise<QRScannerStatus> {
return new Promise((resolve, reject) => {
window.QRScanner.prepare((error: QRScannerError, status: QRScannerStatus) => {
error ? reject(error) : resolve(status);
});
});
}

/**
* Configures the native webview to have a transparent background, then sets the background of the <body> and <html> DOM
* elements to transparent, allowing the webview to re-render with the transparent background.
*
* @returns QR scanner status.
*/
show(): Promise<QRScannerStatus> {
return new Promise(resolve => window.QRScanner.show((status) => resolve(status)));
}

/**
* Call this method to enable scanning. You must then call the `show` method to make the camera preview visible.
*
* @returns Observable that emits the scanned text. Unsubscribe from the observable to stop scanning.
*/
scan(): Observable<string> {
return new Observable(observer => {
window.QRScanner.scan((error: QRScannerError, text: string) => {
error ? observer.error(error) : observer.next(text);
});

return () => {
window.QRScanner.cancelScan();
};
});
}

/**
* Configures the native webview to be opaque with a white background, covering the video preview.
*
* @returns QR scanner status.
*/
hide(): Promise<QRScannerStatus> {
return new Promise((resolve) => window.QRScanner.hide(resolve));
}

/**
* Enable the device's light (for scanning in low-light environments).
*
* @returns QR scanner status.
*/
enableLight(): Promise<QRScannerStatus> {
return new Promise((resolve, reject) => {
window.QRScanner.enableLight((error: QRScannerError, status: QRScannerStatus) => {
error ? reject(error) : resolve(status);
});
});
}

/**
* Disable the device's light.
*
* @returns QR scanner status.
*/
disableLight(): Promise<QRScannerStatus> {
return new Promise((resolve, reject) => {
window.QRScanner.disableLight((error: QRScannerError, status: QRScannerStatus) => {
error ? reject(error) : resolve(status);
});
});
}

/**
* Use front camera.
*
* @returns QR scanner status.
*/
useFrontCamera(): Promise<QRScannerStatus> {
return new Promise((resolve, reject) => {
window.QRScanner.useFrontCamera((error: QRScannerError, status: QRScannerStatus) => {
error ? reject(error) : resolve(status);
});
});
}

/**
* Use back camera.
*
* @returns QR scanner status.
*/
useBackCamera(): Promise<QRScannerStatus> {
return new Promise((resolve, reject) => {
window.QRScanner.useBackCamera((error: QRScannerError, status: QRScannerStatus) => {
error ? reject(error) : resolve(status);
});
});
}

/**
* Disable the device's light.
*
* @param camera Provide `0` for back camera, and `1` for front camera.
* @returns QR scanner status.
*/
useCamera(camera: number): Promise<QRScannerStatus> {
return new Promise((resolve, reject) => {
window.QRScanner.useCamera(camera, (error: QRScannerError, status: QRScannerStatus) => {
error ? reject(error) : resolve(status);
});
});
}

/**
* Pauses the video preview on the current frame and pauses scanning.
*
* @returns QR scanner status.
*/
pausePreview(): Promise<QRScannerStatus> {
return new Promise((resolve) => window.QRScanner.pausePreview(resolve));
}

/**
* Resume the video preview and resumes scanning.
*
* @returns QR scanner status.
*/
resumePreview(): Promise<QRScannerStatus> {
return new Promise((resolve) => window.QRScanner.resumePreview(resolve));
}

/**
* Returns permission status.
*
* @returns QR scanner status.
*/
getStatus(): Promise<QRScannerStatus> {
return new Promise((resolve) => window.QRScanner.getStatus(resolve));
}

/**
* Opens settings to edit app permissions.
*/
openSettings(): void {
window.QRScanner.openSettings();
}

}
3 changes: 2 additions & 1 deletion src/core/services/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { CoreWS } from '@services/ws';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreMimetypeUtils } from '@services/utils/mimetype';
import { CoreTextUtils } from '@services/utils/text';
import { makeSingleton, Clipboard, InAppBrowser, FileOpener, WebIntent, QRScanner, Translate, NgZone } from '@singletons';
import { makeSingleton, Clipboard, InAppBrowser, FileOpener, WebIntent, Translate, NgZone } from '@singletons';
import { CoreLogger } from '@singletons/logger';
import { CoreViewerQRScannerComponent } from '@features/viewer/components/qr-scanner/qr-scanner';
import { CoreCanceledError } from '@classes/errors/cancelederror';
Expand All @@ -40,6 +40,7 @@ import { CoreSites } from '@services/sites';
import { CoreCancellablePromise } from '@classes/cancellable-promise';
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
import { CoreUrlUtils } from './url';
import { QRScanner } from '@features/native/plugins';

export type TreeNode<T> = T & { children: TreeNode<T>[] };

Expand Down
2 changes: 0 additions & 2 deletions src/core/singletons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import { WebView as WebViewService } from '@awesome-cordova-plugins/ionic-webvie
import { Keyboard as KeyboardService } from '@awesome-cordova-plugins/keyboard/ngx';
import { LocalNotifications as LocalNotificationsService } from '@awesome-cordova-plugins/local-notifications/ngx';
import { MediaCapture as MediaCaptureService } from '@awesome-cordova-plugins/media-capture/ngx';
import { QRScanner as QRScannerService } from '@ionic-native/qr-scanner/ngx';
import { StatusBar as StatusBarService } from '@awesome-cordova-plugins/status-bar/ngx';
import { SplashScreen as SplashScreenService } from '@awesome-cordova-plugins/splash-screen/ngx';
import { SQLite as SQLiteService } from '@awesome-cordova-plugins/sqlite/ngx';
Expand Down Expand Up @@ -183,7 +182,6 @@ export const Keyboard = makeSingleton(KeyboardService);
export const LocalNotifications = makeSingleton(LocalNotificationsService);
export const MediaCapture = makeSingleton(MediaCaptureService);
export const NativeHttp = makeSingleton(HTTP);
export const QRScanner = makeSingleton(QRScannerService);
export const StatusBar = makeSingleton(StatusBarService);
export const SplashScreen = makeSingleton(SplashScreenService);
export const SQLite = makeSingleton(SQLiteService);
Expand Down
68 changes: 68 additions & 0 deletions src/types/cordova-plugin-qrscanner.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Types for qr scanner plugin.
*
* @see https://github.com/moodlemobile/cordova-plugin-qrscanner
*/

type IQRScannerStatus = {
authorized: boolean;
denied: boolean;
restricted: boolean;
prepared: boolean;
scanning: boolean;
previewing: boolean;
showing: boolean;
lightEnabled: boolean;
canOpenSettings: boolean;
canEnableLight: boolean;
canChangeCamera: boolean;
currentCamera: number;
};

type IQRScannerError = {
name: string;
code: number;
_message: string; // eslint-disable-line @typescript-eslint/naming-convention
};

interface Window {

// eslint-disable-next-line @typescript-eslint/naming-convention
QRScanner: {
prepare(onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
destroy(onDone?: (status: IQRScannerStatus) => void): void;
scan(onDone: (error: IQRScannerError, text: string) => void): void;
cancelScan(onDone?: (status: IQRScannerStatus) => void): void;
show(onDone?: (status: IQRScannerStatus) => void): void;
hide(onDone?: (status: IQRScannerStatus) => void): void;
pausePreview(onDone?: (status: IQRScannerStatus) => void): void;
resumePreview(onDone?: (status: IQRScannerStatus) => void): void;
enableLight(onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
disableLight(onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
useCamera(camera: number, onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
useFrontCamera(onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
useBackCamera(onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
openSettings(onDone?: (error: IQRScannerError, status: IQRScannerStatus) => void): void;
getStatus(onDone: (status: IQRScannerStatus) => void): void;
};

}

declare module '@moodlehq/cordova-plugin-qrscanner' {
export type QRScannerStatus = IQRScannerStatus;
export type QRScannerError = IQRScannerError;
}

0 comments on commit 0bf97b7

Please sign in to comment.