Revert "Refactor ScanditOverlayComponent to use dependency injection for DataCaptureContext and DataCaptureView, and enhance module setup with async context initialization"

This reverts commit 6e1c434edf.
This commit is contained in:
Lorenz Hilpert
2024-11-22 18:31:05 +01:00
parent 6e1c434edf
commit 05eb3cc756
3 changed files with 21 additions and 38 deletions

View File

@@ -1,18 +1,17 @@
import { Component, ChangeDetectionStrategy, ElementRef, ViewChild, NgZone, AfterViewInit, OnDestroy, OnInit, inject } from '@angular/core';
import { Component, ChangeDetectionStrategy, ElementRef, ViewChild, NgZone, AfterViewInit, OnDestroy, OnInit } from '@angular/core';
import { UiModalService } from '@ui/modal';
import { BarcodeCapture, BarcodeCaptureSettings, Symbology } from 'scandit-web-datacapture-barcode';
import { Camera, FrameSourceState } from 'scandit-web-datacapture-core';
import { DATA_CAPTURE_CONTEXT_ASYNC, DATA_CAPTURE_VIEW } from './scandit-scan-adapter.module';
import { Camera, DataCaptureContext, DataCaptureView, FrameSourceState } from 'scandit-web-datacapture-core';
@Component({
selector: 'app-scandit-overlay',
templateUrl: 'scandit-overlay.component.html',
styleUrls: ['scandit-overlay.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
})
export class ScanditOverlayComponent implements OnInit, AfterViewInit, OnDestroy {
private readonly dataCaptureContextAsync = inject(DATA_CAPTURE_CONTEXT_ASYNC);
private readonly dataCaptureView = inject(DATA_CAPTURE_VIEW);
private dataCaptureContext: DataCaptureContext;
private dataCaptureView: DataCaptureView;
private barcodeCapture: BarcodeCapture;
private camera: Camera;
@@ -22,21 +21,25 @@ export class ScanditOverlayComponent implements OnInit, AfterViewInit, OnDestroy
@ViewChild('scanContainer', { read: ElementRef, static: true }) scanContainer: ElementRef;
constructor(private _zone: NgZone) {}
constructor(
private _zone: NgZone,
private _modal: UiModalService,
) {}
ngOnInit(): void {
this.dataCaptureView = new DataCaptureView();
this.dataCaptureView.connectToElement(this.scanContainer.nativeElement);
this.dataCaptureView.setProgressBarMessage('Kamera wird gestartet...');
this.dataCaptureView.showProgressBar();
}
async ngAfterViewInit() {
const dataCaptureContext = await this.dataCaptureContextAsync();
this.dataCaptureContext = await DataCaptureContext.create();
this.dataCaptureView.setContext(dataCaptureContext);
this.dataCaptureView.setContext(this.dataCaptureContext);
this.barcodeCapture = await BarcodeCapture.forContext(dataCaptureContext, this.getScanSettings());
this.barcodeCapture = await BarcodeCapture.forContext(this.dataCaptureContext, this.getScanSettings());
this.barcodeCapture.addListener({
didScan: (_, session, __) => {
@@ -52,7 +55,7 @@ export class ScanditOverlayComponent implements OnInit, AfterViewInit, OnDestroy
this.camera = Camera.default;
dataCaptureContext.setFrameSource(this.camera);
this.dataCaptureContext.setFrameSource(this.camera);
await this.camera.switchToDesiredState(FrameSourceState.On);
this.dataCaptureView.hideProgressBar();

View File

@@ -1,40 +1,20 @@
import { InjectionToken, NgModule } from '@angular/core';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ScanditOverlayComponent } from './scandit-overlay.component';
import { ScanditScanAdapter } from './scandit.scan-adapter';
import { SCAN_ADAPTER } from '../tokens';
import { DataCaptureContext, DataCaptureView } from 'scandit-web-datacapture-core';
export const DATA_CAPTURE_VIEW = new InjectionToken<DataCaptureView>('Adapter.Scan.Scandit.DataCaptureView');
export const DATA_CAPTURE_CONTEXT_ASYNC = new InjectionToken<() => Promise<DataCaptureContext>>('Adapter.Scan.Scandit.DataCaptureContext');
@NgModule({
imports: [CommonModule],
exports: [ScanditOverlayComponent],
declarations: [ScanditOverlayComponent],
})
export class ScanditScanAdapterModule {
static forRoot() {
return {
ngModule: ScanditScanAdapterModule,
providers: [
{ provide: SCAN_ADAPTER, useClass: ScanditScanAdapter, multi: true },
{ provide: DATA_CAPTURE_VIEW, useFactory: () => new DataCaptureView() },
{
provide: DATA_CAPTURE_CONTEXT_ASYNC,
useFactory: (dataCaptureView: DataCaptureView) => {
let dataCaptureContext: DataCaptureContext;
return async () => {
if (!dataCaptureContext) {
dataCaptureContext = await DataCaptureContext.create();
dataCaptureView.setContext(dataCaptureContext);
}
return dataCaptureContext;
};
},
deps: [DataCaptureView],
},
],
providers: [{ provide: SCAN_ADAPTER, useClass: ScanditScanAdapter, multi: true }],
};
}
}

View File

@@ -27,7 +27,7 @@ export class ScanditScanAdapter implements ScanAdapter {
) {}
async init(): Promise<boolean> {
if (true || this._environmentService.isTablet()) {
if (this._environmentService.isTablet()) {
await configure({
licenseKey: this._config.get('licence.scandit'),
libraryLocation: new URL('scandit', document.baseURI).toString(),