Scanner test

This commit is contained in:
Lorenz Hilpert
2023-03-08 18:39:27 +01:00
parent da1978bf21
commit ccbec5bcff
8 changed files with 89 additions and 80 deletions

View File

@@ -12,7 +12,7 @@ export class NativeScanAdapter implements ScanAdapter {
init(): Promise<boolean> {
return new Promise((resolve, reject) => {
resolve(this.nativeContainerService.isUiWebview().isNative);
resolve(this.nativeContainerService.isNative);
});
}

View File

@@ -17,7 +17,7 @@ export class EnvironmentService {
}
isNative(): boolean {
return this._nativeContainer.isUiWebview().isNative;
return this._nativeContainer.isNative;
}
isSafari(): boolean {

View File

@@ -22,7 +22,7 @@ export class PrintCompartmentLabelActionHandler extends ActionHandler<OrderItems
content: PrintModalComponent,
config: { showScrollbarY: false },
data: {
printImmediately: !this.nativeContainerService.isUiWebview().isNative,
printImmediately: !this.nativeContainerService.isNative,
printerType: 'Label',
print: (printer) =>
this.domainPrinterService

View File

@@ -23,7 +23,7 @@ export class PrintShippingNoteActionHandler extends ActionHandler<OrderItemsCont
content: PrintModalComponent,
config: { showScrollbarY: false },
data: {
printImmediately: !this.nativeContainerService.isUiWebview().isNative,
printImmediately: !this.nativeContainerService.isNative,
printerType: 'Label',
print: async (printer) => {
try {

View File

@@ -34,11 +34,18 @@ import { RootStateService } from './store/root-state.service';
import * as Commands from './commands';
import { UiIconModule } from '@ui/icon';
import { PreviewComponent } from './preview';
import { NativeContainerService } from 'native-container';
registerLocaleData(localeDe, localeDeExtra);
registerLocaleData(localeDe, 'de', localeDeExtra);
export function _appInitializerFactory(config: Config, auth: AuthService, injector: Injector, scanAdapter: ScanAdapterService) {
export function _appInitializerFactory(
config: Config,
auth: AuthService,
injector: Injector,
scanAdapter: ScanAdapterService,
nativeContainer: NativeContainerService
) {
return async () => {
const statusElement = document.querySelector('#init-status');
statusElement.innerHTML = 'Konfigurationen werden geladen...';
@@ -52,6 +59,10 @@ export function _appInitializerFactory(config: Config, auth: AuthService, inject
await state.init();
}
statusElement.innerHTML = 'Native Container wird initialisiert...';
await nativeContainer.init();
statusElement.innerHTML = 'Scanner wird initialisiert...';
await scanAdapter.init();
};
}
@@ -104,7 +115,7 @@ export function _notificationsHubOptionsFactory(config: Config, auth: AuthServic
provide: APP_INITIALIZER,
useFactory: _appInitializerFactory,
multi: true,
deps: [Config, AuthService, Injector, ScanAdapterService],
deps: [Config, AuthService, Injector, ScanAdapterService, NativeContainerService],
},
{
provide: NOTIFICATIONS_HUB_OPTIONS,

View File

@@ -54,7 +54,7 @@ export class IsAuthenticatedGuard implements CanActivate {
const result = await this._scanService
.scan({
exclude: ['Dev', 'Native'],
exclude: ['Dev'],
})
?.toPromise();

View File

@@ -1,99 +1,97 @@
import { Injectable } from '@angular/core';
import { Observable, fromEvent, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { WindowRef } from './window-ref.service';
import { fromEvent, Observable } from 'rxjs';
import { filter, first, map, take } from 'rxjs/operators';
import { ScanRequestType } from './scan-request.type';
import { EnvironmentService } from '@core/environment';
import { Platform } from '@angular/cdk/platform';
@Injectable({
providedIn: 'root',
})
const STATUS_FOR_MESSAGES = ['INIT', 'IN_PROGRESS', 'ERROR', 'SUCCESS'];
export interface NativeContainerMessage {
status: 'INIT' | 'IN_PROGRESS' | 'ERROR' | 'SUCCESS';
data: string;
}
@Injectable({ providedIn: 'root' })
export class NativeContainerService {
private wm: Observable<any>;
public windowMessages = new Subject<any>();
get window(): Window {
return window;
}
private webViewDetected = false;
private webViewEventRecieved = false;
private browserDetected = false;
messages$ = fromEvent<MessageEvent<NativeContainerMessage>>(this.window, 'message').pipe(
filter((event) => STATUS_FOR_MESSAGES.includes(event.data.status)),
map((event) => event.data)
);
constructor(private windowRef: WindowRef, private _platform: Platform) {
this.defineWindowCallback();
private _isNative: boolean;
get isNative() {
return !!this._isNative;
}
this.wm = fromEvent(this.windowRef.nativeWindow, 'message').pipe(
map((e: MessageEvent) => {
return e.data;
})
);
this.wm.subscribe((data) => {
if (data.status === 'INIT') {
this.webViewEventRecieved = true;
constructor() {
this.messages$.subscribe((message) => {
if (message.status === 'INIT') {
this._isNative = true;
this._postMessage({ status: 'INIT', data: 'Is a WebView' });
}
this.windowMessages.next(data);
});
}
public openScanner(scanRequestType: ScanRequestType) {
openScanner(scanRequestType: ScanRequestType) {
const scanRequest = {
[scanRequestType]: true,
};
this.sendMessage(scanRequest);
return new Observable<NativeContainerMessage>((observer) => {
const sub = this.messages$
.pipe(
filter((f) => STATUS_FOR_MESSAGES.includes(f.status)),
take(1)
)
.subscribe({
next: (message) => {
observer.next(message);
},
error: (error) => {
observer.error(error);
},
complete: () => {
observer.complete();
},
});
return this.windowMessages.asObservable().pipe(map((message) => message as { status: string; data?: any }));
this._scanRequestMessageHandler(scanRequest);
return () => {
sub.unsubscribe();
};
});
}
public sendMessage(message: any) {
this.windowRef.nativeWindow.postMessage({ status: 'IN_PROGRESS', data: 'Scan Started' }, '*');
init(): Promise<boolean> {
const result = this.messages$
.pipe(
filter((f) => f.status === 'INIT' || f.status === 'ERROR'),
first(),
map((m) => m.status === 'INIT')
)
.toPromise();
try {
// if (this.isUiWebview() && this.isUiWebview().isNative) {
(this.windowRef.nativeWindow as any).webkit.messageHandlers.scanRequest.postMessage(message);
// send ping message to check if it is native app
this._scanRequestMessageHandler('PING');
} catch (error) {
this.windowRef.nativeWindow.postMessage({ status: 'ERROR', data: 'Not a WebView' }, '*');
this.windowRef.nativeWindow.postMessage(message, '*');
// if it is not native app, send error message
this._postMessage({ status: 'ERROR', data: 'Not a WebView' });
}
return result;
}
public isUiWebview() {
// const navigator = this.windowRef.nativeWindow.navigator as Navigator;
// alert(this.deviceDetector.browser);
// const standalone = (navigator as any).standalone,
// userAgent = navigator.userAgent.toLowerCase(),
// safari = /safari/.test(userAgent),
// ios = /iphone|ipod|ipad/.test(userAgent),
// chrome = /chrome/.test(userAgent) && /Google Inc/.test(navigator.vendor),
// crios = /crios/.test(userAgent);
// this.webViewDetected = ios && !standalone && !safari;
// this.browserDetected = !standalone && (safari || chrome) && !crios;
return {
isSafari: this._platform.SAFARI,
isNative: this.webViewEventRecieved,
};
private _scanRequestMessageHandler(message: any) {
const postMessage = (this.window as any).webkit.messageHandlers.scanRequest.postMessage;
postMessage(message);
}
public isIpadMini6() {
const width = window.innerWidth > 0 ? window.innerWidth : screen.width;
return width === 744;
}
private defineWindowCallback() {
if (this.windowRef.nativeWindow['scanResults'] === undefined) {
this.windowRef.nativeWindow['scanResults'] = (result) => window.postMessage(result, '*');
}
if (this.windowRef.nativeWindow['isRunningNative'] === undefined) {
this.windowRef.nativeWindow['isRunningNative'] = (_) => window.postMessage({ status: 'INIT', data: 'Is a WebView' }, '*');
}
// Try sending ping request, to invoke the containers isRunningNative event
try {
(this.windowRef.nativeWindow as any).webkit.messageHandlers.scanRequest.postMessage('PING');
} catch (error) {
this.windowRef.nativeWindow.postMessage({ status: 'ERROR', data: 'Not a WebView' }, '*');
this.windowRef.nativeWindow.postMessage('PING', '*');
}
private _postMessage(message: { status: string; data: string }) {
this.window.postMessage(message, '*');
}
}

View File

@@ -44,7 +44,7 @@ export class UiSliderComponent implements AfterViewInit, OnDestroy {
@HostBinding('class.show-slide-arrows')
get showSlideArrows() {
const { clientWidth, scrollWidth } = this.slideWrapperNativeElement;
return !this._nativeContainerService.isUiWebview()?.isNative && scrollWidth > clientWidth;
return !this._nativeContainerService.isNative && scrollWidth > clientWidth;
}
private subscriptions = new Subscription();