From 164d3829d23fe3aac336bfbeb44043389011b2e8 Mon Sep 17 00:00:00 2001 From: Alfonso Salces Date: Fri, 24 Nov 2023 13:04:50 +0100 Subject: [PATCH] MOBILE-3947 qr-scanner: Create qr scanner service --- src/core/features/native/native.module.ts | 3 +- src/core/features/native/plugins/index.ts | 2 + src/core/features/native/plugins/qrscanner.ts | 95 +++++++++++++++++++ src/core/services/utils/utils.ts | 3 +- src/core/singletons/index.ts | 2 - 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 src/core/features/native/plugins/qrscanner.ts diff --git a/src/core/features/native/native.module.ts b/src/core/features/native/native.module.ts index b1bd71648d4..8b8466ef4eb 100644 --- a/src/core/features/native/native.module.ts +++ b/src/core/features/native/native.module.ts @@ -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, @@ -81,7 +81,6 @@ export const CORE_NATIVE_SERVICES = [ LocalNotifications, MediaCapture, Push, - QRScanner, SplashScreen, SQLite, StatusBar, diff --git a/src/core/features/native/plugins/index.ts b/src/core/features/native/plugins/index.ts index 8efe862c2bd..1ed23c34515 100644 --- a/src/core/features/native/plugins/index.ts +++ b/src/core/features/native/plugins/index.ts @@ -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); diff --git a/src/core/features/native/plugins/qrscanner.ts b/src/core/features/native/plugins/qrscanner.ts new file mode 100644 index 00000000000..ae7218c583b --- /dev/null +++ b/src/core/features/native/plugins/qrscanner.ts @@ -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 { + return new Promise((resolve) => this.window.QRScanner.destroy(resolve)); + } + + /** + * Prepare QR scanner instance. + * + * @returns qr scanner status. + */ + prepare(): Promise { + return new Promise(resolve => + this.window.QRScanner.prepare((foo: unknown, status: QRScannerStatus) => (resolve(status)))); + } + + /** + * Show QR Scanner. + * + * @returns Qr scanner status. + */ + show(): Promise { + return new Promise(resolve => this.window.QRScanner.show((status: QRScannerStatus) => resolve(status))); + } + + /** + * Return QR content scanned. + * + * @returns Content scanned. + */ + scan(): Observable { + const subject = new BehaviorSubject(''); + 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 { + 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; +}; diff --git a/src/core/services/utils/utils.ts b/src/core/services/utils/utils.ts index 2e92b29732f..db08c512ea9 100644 --- a/src/core/services/utils/utils.ts +++ b/src/core/services/utils/utils.ts @@ -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'; @@ -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 & { children: TreeNode[] }; diff --git a/src/core/singletons/index.ts b/src/core/singletons/index.ts index 3cc93fbb25c..245c50f3972 100644 --- a/src/core/singletons/index.ts +++ b/src/core/singletons/index.ts @@ -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'; @@ -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);