1616
1717import java .util .List ;
1818import java .util .Map ;
19+ import java .util .concurrent .atomic .AtomicBoolean ;
1920
2021/**
2122 * Created by bradhesse on 7/17/18.
2223 */
2324
24- class OSFlutterChangeTagsHandler implements ChangeTagsUpdateHandler {
25+ class OSFlutterChangeTagsHandler implements ChangeTagsUpdateHandler , GetTagsHandler {
2526 private Result result ;
2627
28+ // the tags callbacks can in some instances be called more than once
29+ // ie. cached vs. server response.
30+ // this property guarantees the callback will never be called more than once.
31+ private AtomicBoolean replySubmitted = new AtomicBoolean (false );
32+
2733 OSFlutterChangeTagsHandler (Result res ) {
2834 this .result = res ;
2935 }
3036
3137 @ Override
3238 public void onSuccess (JSONObject tags ) {
39+ if (this .replySubmitted .getAndSet (true ))
40+ return ;
41+
3342 try {
3443 this .result .success (OneSignalSerializer .convertJSONObjectToHashMap (tags ));
3544 } catch (JSONException exception ) {
@@ -39,8 +48,23 @@ public void onSuccess(JSONObject tags) {
3948
4049 @ Override
4150 public void onFailure (SendTagsError error ) {
51+ if (this .replySubmitted .getAndSet (true ))
52+ return ;
53+
4254 this .result .error ("onesignal" , "Encountered an error updating tags (" + String .valueOf (error .getCode ()) + "): " + error .getMessage (), null );
4355 }
56+
57+ @ Override
58+ public void tagsAvailable (JSONObject jsonObject ) {
59+ if (this .replySubmitted .getAndSet (true ))
60+ return ;
61+
62+ try {
63+ this .result .success (OneSignalSerializer .convertJSONObjectToHashMap (jsonObject ));
64+ } catch (JSONException exception ) {
65+ this .result .error ("onesignal" , "Encountered an error serializing tags into hashmap: " + exception .getMessage () + "\n " + exception .getStackTrace (), null );
66+ }
67+ }
4468}
4569
4670public class OneSignalTagsController implements MethodCallHandler {
@@ -55,17 +79,7 @@ public static void registerWith(Registrar registrar) {
5579 @ Override
5680 public void onMethodCall (MethodCall call , Result result ) {
5781 if (call .method .contentEquals ("OneSignal#getTags" )) {
58- final Result res = result ;
59- OneSignal .getTags (new GetTagsHandler () {
60- @ Override
61- public void tagsAvailable (JSONObject tags ) {
62- try {
63- res .success (OneSignalSerializer .convertJSONObjectToHashMap (tags ));
64- } catch (JSONException exception ) {
65- res .error ("onesignal" , "Encountered an error serializing tags into hashmap: " + exception .getMessage () + "\n " + exception .getStackTrace (), null );
66- }
67- }
68- });
82+ OneSignal .getTags (new OSFlutterChangeTagsHandler (result ));
6983 } else if (call .method .contentEquals ("OneSignal#sendTags" )) {
7084 OneSignal .sendTags (new JSONObject ((Map <String , Object >) call .arguments ), new OSFlutterChangeTagsHandler (result ));
7185 } else if (call .method .contentEquals ("OneSignal#deleteTags" )
0 commit comments