Skip to content

Commit

Permalink
MOBILE-3947 qr-scanner: Create qr sscanner service
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonso-salces committed Nov 24, 2023
1 parent ba50fb8 commit d2196b4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 27 deletions.
15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"@ionic-native/local-notifications": "^5.36.0",
"@ionic-native/media-capture": "^5.36.0",
"@ionic-native/network": "^5.36.0",
"@ionic-native/qr-scanner": "^5.36.0",
"@ionic-native/splash-screen": "^5.36.0",
"@ionic-native/sqlite": "^5.36.0",
"@ionic-native/status-bar": "^5.36.0",
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { CoreUpdateManagerProvider } from '@services/update-manager';
import { CoreUrlUtilsProvider } from '@services/utils/url';
import { CoreUtilsProvider } from '@services/utils/utils';
import { CoreWSProvider } from '@services/ws';
import { CoreQRScannerService } from '@services/qrscanner';

export const CORE_SERVICES: Type<unknown>[] = [
CoreAppProvider,
Expand Down Expand Up @@ -87,6 +88,7 @@ export const CORE_SERVICES: Type<unknown>[] = [
HammerModule,
],
providers: [
CoreQRScannerService,
{ provide: HTTP_INTERCEPTORS, useClass: CoreInterceptor, multi: true },
{ provide: HAMMER_GESTURE_CONFIG, useClass: CoreHammerGestureConfig },
{ provide: ApplicationInitStatus, useClass: CoreApplicationInitStatus, deps: [Injector] },
Expand Down
3 changes: 0 additions & 3 deletions src/core/features/native/native.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { Keyboard } from '@ionic-native/keyboard/ngx';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
import { MediaCapture } from '@ionic-native/media-capture/ngx';
import { Push } from '@moodlehq/ionic-native-push/ngx';
import { QRScanner } from '@ionic-native/qr-scanner/ngx';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { SQLite } from '@ionic-native/sqlite/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
Expand All @@ -55,7 +54,6 @@ export const CORE_NATIVE_SERVICES = [
LocalNotifications,
MediaCapture,
Push,
QRScanner,
SplashScreen,
StatusBar,
SQLite,
Expand All @@ -82,7 +80,6 @@ export const CORE_NATIVE_SERVICES = [
LocalNotifications,
MediaCapture,
Push,
QRScanner,
SplashScreen,
SQLite,
StatusBar,
Expand Down
68 changes: 68 additions & 0 deletions src/core/services/qrscanner.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.
import { makeSingleton } from '@singletons';
import { BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';

export class CoreQRScannerService {

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

constructor() {
this.window = window;
}

destroy(): Promise<null> {
return new Promise((resolve) => this.window.QRScanner.destroy(resolve));
}

prepare(): Promise<CoreQRScannerStatus> {
return new Promise(resolve =>
this.window.QRScanner.prepare((foo: unknown, status: CoreQRScannerStatus) => (resolve(status))));
}

show(): Promise<CoreQRScannerStatus> {
return new Promise(resolve => this.window.QRScanner.show((status: CoreQRScannerStatus) => resolve(status)));
}

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(): Promise<null> {
return new Promise((resolve) => this.window.QRScanner.hide(resolve));
}

}

export const CoreQRScanner = makeSingleton(CoreQRScannerService);

export type CoreQRScannerStatus = {
authorized: boolean;
denied: boolean;
restricted: boolean;
prepared: boolean;
scanning: boolean;
previewing: boolean;
showing: boolean;
lightEnabled: boolean;
canOpenSettings: boolean;
canEnableLight: boolean;
canChangeCamera: boolean;
currentCamera: number;
};
13 changes: 7 additions & 6 deletions 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 { CoreQRScanner } from '@services/qrscanner';

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

Expand Down Expand Up @@ -1707,7 +1708,7 @@ export class CoreUtilsProvider {
// Ask the user for permission to use the camera.
// The scan method also does this, but since it returns an Observable we wouldn't be able to detect if the user denied.
try {
const status = await QRScanner.prepare();
const status = await CoreQRScanner.prepare();

if (!status.authorized) {
// No access to the camera, reject. In android this shouldn't happen, denying access passes through catch.
Expand All @@ -1724,12 +1725,12 @@ export class CoreUtilsProvider {
deferred: new CorePromisedValue(),

// When text is received, stop scanning and return the text.
observable: QRScanner.scan().subscribe(text => this.stopScanQR(text, false)),
observable: CoreQRScanner.scan().subscribe(text => this.stopScanQR(text, false)),
};

// Show the camera.
try {
await QRScanner.show();
await CoreQRScanner.show();

document.body.classList.add('core-scanning-qr');

Expand Down Expand Up @@ -1772,8 +1773,8 @@ export class CoreUtilsProvider {
// Set color-scheme to the initial value.
document.querySelector('meta[name="color-scheme"]')?.setAttribute('content', this.initialColorSchemeContent);

QRScanner.hide();
QRScanner.destroy();
CoreQRScanner.hide();
CoreQRScanner.destroy();

this.qrScanData.observable.unsubscribe(); // Stop scanning.

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 @@ -54,7 +54,6 @@ import { Keyboard as KeyboardService } from '@ionic-native/keyboard/ngx';
import { LocalNotifications as LocalNotificationsService } from '@ionic-native/local-notifications/ngx';
import { MediaCapture as MediaCaptureService } from '@ionic-native/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 '@ionic-native/status-bar/ngx';
import { SplashScreen as SplashScreenService } from '@ionic-native/splash-screen/ngx';
import { SQLite as SQLiteService } from '@ionic-native/sqlite/ngx';
Expand Down Expand Up @@ -188,7 +187,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 d2196b4

Please sign in to comment.