diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 7f1b866..517db34 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -31,6 +31,13 @@ + + + + + diff --git a/src/app/core/services/capacitor/capacitor-nfc/capacitor-nfc.service.ts b/src/app/core/services/capacitor/capacitor-nfc/capacitor-nfc.service.ts index 84c776d..8f56bb3 100644 --- a/src/app/core/services/capacitor/capacitor-nfc/capacitor-nfc.service.ts +++ b/src/app/core/services/capacitor/capacitor-nfc/capacitor-nfc.service.ts @@ -7,7 +7,7 @@ import { TransceiveResult, WriteOptions, } from '@capawesome-team/capacitor-nfc'; -import { Observable, Subject } from 'rxjs'; +import { Observable, ReplaySubject, Subject } from 'rxjs'; import { PlatformService } from '../../platform/platform.service'; @Injectable({ @@ -15,6 +15,7 @@ import { PlatformService } from '../../platform/platform.service'; }) export class CapacitorNfcService { private readonly scannedTagSubject = new Subject(); + private readonly lastScannedTagSubject = new ReplaySubject(1); private readonly sessionCanceledSubject = new Subject(); private readonly sessionErrorSubject = new Subject(); @@ -24,19 +25,17 @@ export class CapacitorNfcService { ) { Nfc.removeAllListeners().then(() => { Nfc.addListener('nfcTagScanned', event => { - console.log('nfcTagScanned', { event }); this.ngZone.run(() => { this.scannedTagSubject.next(event.nfcTag); + this.lastScannedTagSubject.next(event.nfcTag); }); }); Nfc.addListener('scanSessionCanceled', () => { - console.log('scanSessionCanceled'); this.ngZone.run(() => { this.sessionCanceledSubject.next(); }); }); Nfc.addListener('scanSessionError', event => { - console.log('scanSessionError', { event }); this.ngZone.run(() => { this.sessionErrorSubject.next(event.message); }); @@ -48,6 +47,10 @@ export class CapacitorNfcService { return this.scannedTagSubject.asObservable(); } + public get lastScannedTag$(): Observable { + return this.lastScannedTagSubject.asObservable(); + } + public get sessionCanceled$(): Observable { return this.sessionCanceledSubject.asObservable(); } diff --git a/src/app/core/services/nfc/nfc/nfc.service.ts b/src/app/core/services/nfc/nfc/nfc.service.ts index 27c7e8a..74e83f1 100644 --- a/src/app/core/services/nfc/nfc/nfc.service.ts +++ b/src/app/core/services/nfc/nfc/nfc.service.ts @@ -21,6 +21,10 @@ export class NfcService { return this.capacitorNfcService.scannedTag$; } + public get lastScannedTag$(): Observable { + return this.capacitorNfcService.lastScannedTag$; + } + public async startScanSession(): Promise { const isSupported = await this.isSupported(); if (!isSupported) { diff --git a/src/app/core/services/router/router.service.ts b/src/app/core/services/router/router.service.ts index fe911dc..8d09dd8 100644 --- a/src/app/core/services/router/router.service.ts +++ b/src/app/core/services/router/router.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { Router } from '@angular/router'; +import { NavigationExtras, Router } from '@angular/router'; @Injectable({ providedIn: 'root', @@ -7,8 +7,14 @@ import { Router } from '@angular/router'; export class RouterService { constructor(private readonly router: Router) {} - public navigateToReadPage(): Promise { - return this.router.navigate(['read']); + public navigateToReadPage(options?: { + showLastScannedTag?: boolean; + }): Promise { + let extras: NavigationExtras = {}; + if (options?.showLastScannedTag) { + extras.queryParams = { showLastScannedTag: 'true' }; + } + return this.router.navigate(['read'], extras); } public navigateToWritePage(): Promise { diff --git a/src/app/modules/home/pages/home/home.page.ts b/src/app/modules/home/pages/home/home.page.ts index 3d209f4..ab9f99b 100644 --- a/src/app/modules/home/pages/home/home.page.ts +++ b/src/app/modules/home/pages/home/home.page.ts @@ -1,5 +1,12 @@ -import { ChangeDetectionStrategy, Component } from '@angular/core'; -import { DialogService, PlatformService, RouterService } from '@app/core'; +import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; +import { + DialogService, + NfcService, + PlatformService, + RouterService, +} from '@app/core'; +import { ViewDidEnter, ViewDidLeave } from '@ionic/angular'; +import { skipWhile } from 'rxjs'; @Component({ selector: 'app-home', @@ -7,15 +14,36 @@ import { DialogService, PlatformService, RouterService } from '@app/core'; styleUrls: ['./home.page.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class HomePage { +export class HomePage implements OnInit, ViewDidEnter, ViewDidLeave { public readonly isNativePlatform = this.platformService.isNativePlatform(); + private viewDidLeave = false; + constructor( private readonly routerService: RouterService, private readonly platformService: PlatformService, private readonly dialogService: DialogService, + private readonly nfcService: NfcService, ) {} + public ngOnInit(): void { + this.nfcService.lastScannedTag$ + .pipe(skipWhile(() => this.viewDidLeave)) + .subscribe(() => { + void this.routerService.navigateToReadPage({ + showLastScannedTag: true, + }); + }); + } + + public ionViewDidEnter(): void { + this.viewDidLeave = false; + } + + public ionViewDidLeave(): void { + this.viewDidLeave = true; + } + public async navigateToReadPage(): Promise { await this.routerService.navigateToReadPage(); } diff --git a/src/app/modules/read/pages/read/read.page.ts b/src/app/modules/read/pages/read/read.page.ts index c832e51..df6be3d 100644 --- a/src/app/modules/read/pages/read/read.page.ts +++ b/src/app/modules/read/pages/read/read.page.ts @@ -1,8 +1,10 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; import { NfcService, PlatformService } from '@app/core'; +import { NfcTag } from '@capawesome-team/capacitor-nfc'; import { ViewDidEnter, ViewWillLeave } from '@ionic/angular'; import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; -import { take } from 'rxjs'; +import { Observable, take } from 'rxjs'; @UntilDestroy() @Component({ @@ -12,12 +14,21 @@ import { take } from 'rxjs'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class ReadPage implements ViewDidEnter, ViewWillLeave { - public scannedTag$ = this.nfcService.scannedTag$; + public scannedTag$: Observable; + + private showLastScannedTag = false; constructor( private readonly nfcService: NfcService, private readonly platformService: PlatformService, - ) {} + private readonly activatedRoute: ActivatedRoute, + ) { + this.showLastScannedTag = + this.activatedRoute.snapshot.queryParams.showLastScannedTag === 'true'; + this.scannedTag$ = this.showLastScannedTag + ? this.nfcService.lastScannedTag$ + : this.nfcService.scannedTag$; + } public ionViewDidEnter(): void { this.nfcService.startScanSession(); @@ -30,7 +41,7 @@ export class ReadPage implements ViewDidEnter, ViewWillLeave { private subscribeToObservables(): void { if (this.platformService.isIos()) { - this.nfcService.scannedTag$ + this.scannedTag$ .pipe(take(1), untilDestroyed(this)) .subscribe(() => this.nfcService.stopScanSession()); }