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 Nov 29, 2023
1 parent fbc19ee commit 164d382
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/core/features/native/native.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ 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 '@moodlehq/ionic-native-push/ngx';
import { QRScanner } from '@ionic-native/qr-scanner/ngx';
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';
import { WebIntent } from '@awesome-cordova-plugins/web-intent/ngx';
import { Zip } from '@awesome-cordova-plugins/zip/ngx';
import { QRScanner } from './plugins';

export const CORE_NATIVE_SERVICES = [
Badge,
Expand Down Expand Up @@ -81,7 +81,6 @@ export const CORE_NATIVE_SERVICES = [
LocalNotifications,
MediaCapture,
Push,
QRScanner,
SplashScreen,
SQLite,
StatusBar,
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 @@ -14,5 +14,7 @@

import { makeSingleton } from '@singletons';
import { Chooser as ChooserService } from './chooser';
import { QRScannerService } from './qrscanner';

export const Chooser = makeSingleton(ChooserService);
export const QRScanner = makeSingleton(QRScannerService);
95 changes: 95 additions & 0 deletions src/core/features/native/plugins/qrscanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// (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 { BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';

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

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private window: any;

constructor() {
this.window = window;
}

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

/**
* Prepare QR scanner instance.
*
* @returns qr scanner status.
*/
prepare(): Promise<QRScannerStatus> {
return new Promise(resolve =>
this.window.QRScanner.prepare((foo: unknown, status: QRScannerStatus) => (resolve(status))));
}

/**
* Show QR Scanner.
*
* @returns Qr scanner status.
*/
show(): Promise<QRScannerStatus> {
return new Promise(resolve => this.window.QRScanner.show((status: QRScannerStatus) => resolve(status)));
}

/**
* Return QR content scanned.
*
* @returns Content scanned.
*/
scan(): Observable<string> {
const subject = new BehaviorSubject<string>('');
this.window.QRScanner.scan((foo: unknown, text: string) => subject.next(text));

return subject.asObservable().pipe(filter(text => !!text));
}

/**
* Hide QR Scanner.
*
* @returns null.
*/
hide(): Promise<null> {
return new Promise((resolve) => this.window.QRScanner.hide(resolve));
}

}

export type QRScannerStatus = {
authorized: boolean;
denied: boolean;
restricted: boolean;
prepared: boolean;
scanning: boolean;
previewing: boolean;
showing: boolean;
lightEnabled: boolean;
canOpenSettings: boolean;
canEnableLight: boolean;
canChangeCamera: boolean;
currentCamera: number;
};
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 @@ -53,7 +53,6 @@ import { Keyboard as KeyboardService } from '@awesome-cordova-plugins/keyboard/n
import { LocalNotifications as LocalNotificationsService } from '@awesome-cordova-plugins/local-notifications/ngx';
import { MediaCapture as MediaCaptureService } from '@awesome-cordova-plugins/media-capture/ngx';
import { Push as PushService } from '@moodlehq/ionic-native-push/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 @@ -186,7 +185,6 @@ export const LocalNotifications = makeSingleton(LocalNotificationsService);
export const MediaCapture = makeSingleton(MediaCaptureService);
export const NativeHttp = makeSingleton(HTTP);
export const Push = makeSingleton(PushService);
export const QRScanner = makeSingleton(QRScannerService);
export const StatusBar = makeSingleton(StatusBarService);
export const SplashScreen = makeSingleton(SplashScreenService);
export const SQLite = makeSingleton(SQLiteService);
Expand Down

0 comments on commit 164d382

Please sign in to comment.