Skip to content

Commit

Permalink
fix: do not override addListener
Browse files Browse the repository at this point in the history
It seems overriding capacitor method was not a good idea

Fixes #2
  • Loading branch information
jkbz64 committed Feb 18, 2022
1 parent a0961e8 commit d79735b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ npx cap sync
* [`startScanning()`](#startscanning)
* [`stopScanning()`](#stopscanning)
* [`addListener('scan', ...)`](#addlistenerscan-)
* [`__registerReceiver()`](#__registerreceiver)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)

Expand Down Expand Up @@ -144,6 +145,20 @@ Listen for successful barcode readings
--------------------


### __registerReceiver()

```typescript
__registerReceiver() => Promise<void>
```

Internal method to register intent broadcast receiver
THIS METHOD IS FOR INTERNAL USE ONLY

**Since:** 0.1.3

--------------------


### Interfaces


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,24 @@ public void stopScanning(PluginCall call) {
}

@PluginMethod
@Override
public void addListener(PluginCall call) {
if (!isReceiverRegistered) {
registerReceiver();
isReceiverRegistered = true;
}

super.addListener(call);
}
public void __registerReceiver(PluginCall call) {
if (isReceiverRegistered) return;

private void broadcast(Intent intent) {
Context context = getBridge().getContext();
context.sendBroadcast(intent);
}

private void registerReceiver() {
Context context = getBridge().getContext();
try {
IntentFilter filter = new IntentFilter(DataWedge.DATAWEDGE_INPUT_FILTER);
context.registerReceiver(broadcastReceiver, filter);
isReceiverRegistered = true;
} catch(Exception e) {
Log.d("Capacitor/DataWedge", "Failed to register event receiver");
}
}

private void broadcast(Intent intent) {
Context context = getBridge().getContext();
context.sendBroadcast(intent);
}

private boolean isReceiverRegistered = false;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
Expand Down
9 changes: 9 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ export interface DataWedgePlugin {
eventName: 'scan',
listenerFunc: ScanListener
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Internal method to register intent broadcast receiver
* THIS METHOD IS FOR INTERNAL USE ONLY
*
* @since 0.1.3
* @private
*/
__registerReceiver(): Promise<void>;
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { registerPlugin } from '@capacitor/core';
import { Capacitor, registerPlugin } from '@capacitor/core';

import type { DataWedgePlugin } from './definitions';

const DataWedge = registerPlugin<DataWedgePlugin>('DataWedge', {
web: () => import('./web').then(m => new m.DataWedgeWeb()),
});

if (Capacitor.isNativePlatform()) {
DataWedge.__registerReceiver();
}

export * from './definitions';
export { DataWedge };
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class DataWedgeWeb extends WebPlugin implements DataWedgePlugin {
async stopScanning(): Promise<void> {
throw 'DataWedge is not supported on web';
}

async __registerReceiver(): Promise<void> {
// no-op
}
}

0 comments on commit d79735b

Please sign in to comment.