Last active
July 18, 2022 15:04
-
-
Save markus-fischbacher/d2ffb49f9e5f5e706bd49246c904941c to your computer and use it in GitHub Desktop.
Flutter / JavaScript bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Inject, Injectable} from '@angular/core'; | |
| import {Observable} from 'rxjs/Observable'; | |
| import {Subscriber} from 'rxjs/Subscriber'; | |
| @Injectable() | |
| export class NfcServiceProvider { | |
| private nfcAvailable: boolean = false; | |
| private tagDataSubscriber: Subscriber<any>; | |
| private tagRead: Observable<number> = new Observable<number>( | |
| observer => this.tagDataSubscriber = observer | |
| ); | |
| public TagDataSubscriber(nfcTag: number) { | |
| this.tagDataSubscriber.next(nfcTag); | |
| } | |
| public get TagRead(): Observable<number> { | |
| return this.tagRead; | |
| } | |
| public get NfcAvailable(): boolean { | |
| return this.nfcAvailable; | |
| } | |
| constructor(@Inject('Window') private window: any) { | |
| /* function called by Flutter if a NFC tag was detected */ | |
| window["nfcTagReceived"] = (nfcTag) => { | |
| // when a NFC tag was read, this point is reached without problems. | |
| // here the this-pointer is not available. | |
| // I need to inform the subscriber about the newly read NFC tag | |
| // that's unfortunately not working | |
| this.tagDataSubscriber.next(nfcTag); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WebViewBridge { | |
| final BuildContext context; | |
| late WebViewBridgeChannelController webViewBridgeChannelController; | |
| WebViewBridge(this.context) { | |
| webViewBridgeChannelController = WebViewBridgeChannelController(webViewBridge: this); | |
| } | |
| WebViewController? webViewController; | |
| Set<JavascriptChannel> get channels => { | |
| webViewBridgeChannelController.onNewLoginToken(), | |
| webViewBridgeChannelController.onPlaySoundCheckInSucceeded(), | |
| webViewBridgeChannelController.onPlaySoundCheckInFailed(), | |
| webViewBridgeChannelController.onRequestInternalNfcAvailability(), | |
| webViewBridgeChannelController.onReadNfcOnce(), | |
| webViewBridgeChannelController.onReadNfcStream(), | |
| webViewBridgeChannelController.onStopNfcReading(), | |
| webViewBridgeChannelController.onChangeUrl(), | |
| webViewBridgeChannelController.onShowMessage(context), | |
| }; | |
| Future<void> login(String token) async { | |
| await webViewController?.runJavascript('login("$token")'); | |
| } | |
| // this function is called, when a new NFC tag was read | |
| Future<void> sendNfcTag(String? nfcId) async { | |
| await webViewController?.runJavascript('nfcTagReceived("$nfcId")'); | |
| } | |
| Future<void> isInternalNfcAvailable(NfcReaderStatus status) async { | |
| await webViewController?.runJavascript('isInternalNfcAvailable("${status.name}")'); | |
| } | |
| Future<void> changeUrl(String url) async { | |
| await webViewController?.loadUrl(url); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment