1+ package com .onesignal .flutter ;
2+
3+ import com .onesignal .OneSignal ;
4+ import com .onesignal .OutcomeEvent ;
5+
6+ import java .util .HashMap ;
7+ import java .util .concurrent .atomic .AtomicBoolean ;
8+
9+ import io .flutter .plugin .common .MethodCall ;
10+ import io .flutter .plugin .common .MethodChannel ;
11+ import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
12+ import io .flutter .plugin .common .MethodChannel .Result ;
13+ import io .flutter .plugin .common .PluginRegistry ;
14+ import io .flutter .plugin .common .PluginRegistry .Registrar ;
15+
16+ class OSFlutterOutcomeEventsHandler extends FlutterRegistrarResponder implements OneSignal .OutcomeCallback {
17+ private Result result ;
18+
19+ // the outcome events callbacks can in some instances be called more than once
20+ // ie. cached vs. server response.
21+ // this property guarantees the callback will never be called more than once.
22+ private AtomicBoolean replySubmitted = new AtomicBoolean (false );
23+
24+ OSFlutterOutcomeEventsHandler (PluginRegistry .Registrar flutterRegistrar , MethodChannel channel , Result result ) {
25+ this .flutterRegistrar = flutterRegistrar ;
26+ this .channel = channel ;
27+ this .result = result ;
28+ }
29+
30+ @ Override
31+ public void onSuccess (OutcomeEvent outcomeEvent ) {
32+ if (this .replySubmitted .getAndSet (true ))
33+ return ;
34+
35+ if (outcomeEvent == null )
36+ replySuccess (result , new HashMap <>());
37+ else
38+ replySuccess (result , OneSignalSerializer .convertOutcomeEventToMap (outcomeEvent ));
39+ }
40+
41+ }
42+
43+ public class OneSignalOutcomeEventsController extends FlutterRegistrarResponder implements MethodCallHandler {
44+ private MethodChannel channel ;
45+ private Registrar registrar ;
46+
47+ static void registerWith (Registrar registrar ) {
48+ OneSignalOutcomeEventsController controller = new OneSignalOutcomeEventsController ();
49+ controller .registrar = registrar ;
50+ controller .channel = new MethodChannel (registrar .messenger (), "OneSignal#outcomes" );
51+ controller .channel .setMethodCallHandler (controller );
52+ controller .flutterRegistrar = registrar ;
53+ }
54+
55+ @ Override
56+ public void onMethodCall (MethodCall call , Result result ) {
57+ if (call .method .contentEquals ("OneSignal#sendOutcome" ))
58+ this .sendOutcome (call , result );
59+ else if (call .method .contentEquals ("OneSignal#sendUniqueOutcome" ))
60+ this .sendUniqueOutcome (call , result );
61+ else if (call .method .contentEquals ("OneSignal#sendOutcomeWithValue" ))
62+ this .sendOutcomeWithValue (call , result );
63+ else
64+ replyNotImplemented (result );
65+ }
66+
67+ private void sendOutcome (MethodCall call , Result result ) {
68+ String name = (String ) call .arguments ;
69+
70+ if (name == null || name .isEmpty ()) {
71+ replyError (result , "OneSignal" , "sendOutcome() name must not be null or empty" , null );
72+ return ;
73+ }
74+
75+ OneSignal .sendOutcome (name , new OSFlutterOutcomeEventsHandler (registrar , channel , result ));
76+ }
77+
78+ private void sendUniqueOutcome (MethodCall call , Result result ) {
79+ String name = (String ) call .arguments ;
80+
81+ if (name == null || name .isEmpty ()) {
82+ replyError (result , "OneSignal" , "sendUniqueOutcome() name must not be null or empty" , null );
83+ return ;
84+ }
85+
86+ OneSignal .sendUniqueOutcome (name , new OSFlutterOutcomeEventsHandler (registrar , channel , result ));
87+ }
88+
89+ private void sendOutcomeWithValue (MethodCall call , Result result ) {
90+ String name = call .argument ("outcome_name" );
91+ Double value = call .argument ("outcome_value" );
92+
93+ if (name == null || name .isEmpty ()) {
94+ replyError (result , "OneSignal" , "sendOutcomeWithValue() name must not be null or empty" , null );
95+ return ;
96+ }
97+
98+ if (value == null ) {
99+ replyError (result , "OneSignal" , "sendOutcomeWithValue() value must not be null" , null );
100+ return ;
101+ }
102+
103+ OneSignal .sendOutcomeWithValue (name , value .floatValue (), new OSFlutterOutcomeEventsHandler (registrar , channel , result ));
104+ }
105+
106+ }
0 commit comments