Skip to content

Commit 82e6e2f

Browse files
committed
Generalize Base Handler
* Add base handler * Add log for handler called more than once
1 parent 2ae6d0b commit 82e6e2f

File tree

1 file changed

+38
-40
lines changed

1 file changed

+38
-40
lines changed

android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -396,23 +396,19 @@ public void notificationWillShowInForeground(OSNotificationReceivedEvent notific
396396
}
397397
}
398398

399-
static class OSFlutterEmailHandle extends FlutterRegistrarResponder
399+
static class OSFlutterEmailHandle extends OSFlutterHandler
400400
implements OneSignal.EmailUpdateHandler {
401-
private final Result result;
402-
private final String methodName;
403-
private final AtomicBoolean replySubmitted = new AtomicBoolean(false);
404401

405402
OSFlutterEmailHandle(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
406-
this.flutterRegistrar = flutterRegistrar;
407-
this.channel = channel;
408-
this.result = res;
409-
this.methodName = methodName;
403+
super(flutterRegistrar, channel, res, methodName);
410404
}
411405

412406
@Override
413407
public void onSuccess() {
414-
if (this.replySubmitted.getAndSet(true))
415-
return;
408+
if (this.replySubmitted.getAndSet(true)) {
409+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "OneSignal " + methodName + " handler called twice, ignoring!");
410+
return;
411+
}
416412

417413
replySuccess(result, null);
418414
}
@@ -428,23 +424,19 @@ public void onFailure(EmailUpdateError error) {
428424
}
429425
}
430426

431-
static class OSFlutterSMSHandler extends FlutterRegistrarResponder
427+
static class OSFlutterSMSHandler extends OSFlutterHandler
432428
implements OneSignal.OSSMSUpdateHandler {
433-
private final Result result;
434-
private final String methodName;
435-
private final AtomicBoolean replySubmitted = new AtomicBoolean(false);
436429

437430
OSFlutterSMSHandler(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
438-
this.flutterRegistrar = flutterRegistrar;
439-
this.channel = channel;
440-
this.result = res;
441-
this.methodName = methodName;
431+
super(flutterRegistrar, channel, res, methodName);
442432
}
443433

444434
@Override
445435
public void onSuccess(JSONObject results) {
446-
if (this.replySubmitted.getAndSet(true))
436+
if (this.replySubmitted.getAndSet(true)) {
437+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "OneSignal " + methodName + " handler called twice, ignoring! response: " + results);
447438
return;
439+
}
448440

449441
try {
450442
replySuccess(result, OneSignalSerializer.convertJSONObjectToHashMap(results));
@@ -462,23 +454,19 @@ public void onFailure(OneSignal.OSSMSUpdateError error) {
462454
}
463455
}
464456

465-
static class OSFlutterExternalUserIdHandler extends FlutterRegistrarResponder
457+
static class OSFlutterExternalUserIdHandler extends OSFlutterHandler
466458
implements OneSignal.OSExternalUserIdUpdateCompletionHandler {
467-
private final Result result;
468-
private final String methodName;
469-
private final AtomicBoolean replySubmitted = new AtomicBoolean(false);
470459

471460
OSFlutterExternalUserIdHandler(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
472-
this.flutterRegistrar = flutterRegistrar;
473-
this.channel = channel;
474-
this.result = res;
475-
this.methodName = methodName;
461+
super(flutterRegistrar, channel, res, methodName);
476462
}
477463

478464
@Override
479465
public void onSuccess(JSONObject results) {
480-
if (this.replySubmitted.getAndSet(true))
466+
if (this.replySubmitted.getAndSet(true)) {
467+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "OneSignal " + methodName + " handler called twice, ignoring! response: " + results);
481468
return;
469+
}
482470

483471
try {
484472
replySuccess(result, OneSignalSerializer.convertJSONObjectToHashMap(results));
@@ -496,23 +484,19 @@ public void onFailure(OneSignal.ExternalIdError error) {
496484
}
497485
}
498486

499-
static class OSFlutterPostNotificationHandler extends FlutterRegistrarResponder
487+
static class OSFlutterPostNotificationHandler extends OSFlutterHandler
500488
implements OneSignal.PostNotificationResponseHandler {
501-
private final Result result;
502-
private final String methodName;
503-
private final AtomicBoolean replySubmitted = new AtomicBoolean(false);
504489

505490
OSFlutterPostNotificationHandler(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
506-
this.flutterRegistrar = flutterRegistrar;
507-
this.channel = channel;
508-
this.result = res;
509-
this.methodName = methodName;
491+
super(flutterRegistrar, channel, res, methodName);
510492
}
511493

512494
@Override
513495
public void onSuccess(JSONObject results) {
514-
if (this.replySubmitted.getAndSet(true))
515-
return;
496+
if (this.replySubmitted.getAndSet(true)) {
497+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "OneSignal " + methodName + " handler called twice, ignoring! response: " + results);
498+
return;
499+
}
516500

517501
try {
518502
replySuccess(result, OneSignalSerializer.convertJSONObjectToHashMap(results));
@@ -523,8 +507,10 @@ public void onSuccess(JSONObject results) {
523507

524508
@Override
525509
public void onFailure(JSONObject response) {
526-
if (this.replySubmitted.getAndSet(true))
527-
return;
510+
if (this.replySubmitted.getAndSet(true)) {
511+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "OneSignal " + methodName + " handler called twice, ignoring! response: " + response);
512+
return;
513+
}
528514

529515
try {
530516
replyError(result, "OneSignal", "Encountered an error attempting to " + methodName + " " + response.toString(), OneSignalSerializer.convertJSONObjectToHashMap(response));
@@ -534,4 +520,16 @@ public void onFailure(JSONObject response) {
534520
}
535521
}
536522

523+
static class OSFlutterHandler extends FlutterRegistrarResponder {
524+
protected final Result result;
525+
protected final String methodName;
526+
protected final AtomicBoolean replySubmitted = new AtomicBoolean(false);
527+
528+
OSFlutterHandler(PluginRegistry.Registrar flutterRegistrar, MethodChannel channel, Result res, String methodName) {
529+
this.flutterRegistrar = flutterRegistrar;
530+
this.channel = channel;
531+
this.result = res;
532+
this.methodName = methodName;
533+
}
534+
}
537535
}

0 commit comments

Comments
 (0)