Skip to content

Commit f8a30b9

Browse files
Formatting
1 parent 5dfbf5e commit f8a30b9

File tree

2 files changed

+107
-90
lines changed

2 files changed

+107
-90
lines changed

lib/onesignal_flutter.dart

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ export 'src/outcome_event.dart';
2222
typedef void ReceivedNotificationHandler(OSNotification notification);
2323
typedef void OpenedNotificationHandler(OSNotificationOpenedResult openedResult);
2424
typedef void SubscriptionChangedHandler(OSSubscriptionStateChanges changes);
25-
typedef void EmailSubscriptionChangeHandler(OSEmailSubscriptionStateChanges changes);
26-
typedef void SMSSubscriptionChangeHandler(OSSMSSubscriptionStateChanges changes);
25+
typedef void EmailSubscriptionChangeHandler(
26+
OSEmailSubscriptionStateChanges changes);
27+
typedef void SMSSubscriptionChangeHandler(
28+
OSSMSSubscriptionStateChanges changes);
2729
typedef void PermissionChangeHandler(OSPermissionStateChanges changes);
2830
typedef void InAppMessageClickedHandler(OSInAppMessageAction action);
2931
typedef void OnWillDisplayInAppMessageHandler(OSInAppMessage message);
3032
typedef void OnDidDisplayInAppMessageHandler(OSInAppMessage message);
3133
typedef void OnWillDismissInAppMessageHandler(OSInAppMessage message);
3234
typedef void OnDidDismissInAppMessageHandler(OSInAppMessage message);
33-
typedef void NotificationWillShowInForegroundHandler(OSNotificationReceivedEvent event);
35+
typedef void NotificationWillShowInForegroundHandler(
36+
OSNotificationReceivedEvent event);
3437

3538
class OneSignal {
3639
/// A singleton representing the OneSignal SDK.
@@ -42,7 +45,8 @@ class OneSignal {
4245
// private channels used to bridge to ObjC/Java
4346
MethodChannel _channel = const MethodChannel('OneSignal');
4447
MethodChannel _tagsChannel = const MethodChannel('OneSignal#tags');
45-
MethodChannel _inAppMessagesChannel = const MethodChannel('OneSignal#inAppMessages');
48+
MethodChannel _inAppMessagesChannel =
49+
const MethodChannel('OneSignal#inAppMessages');
4650
MethodChannel _outcomesChannel = const MethodChannel('OneSignal#outcomes');
4751

4852
// event handlers
@@ -56,7 +60,8 @@ class OneSignal {
5660
OnDidDisplayInAppMessageHandler? _onDidDisplayInAppMessageHandler;
5761
OnWillDismissInAppMessageHandler? _onWillDismissInAppMessageHandler;
5862
OnDidDismissInAppMessageHandler? _onDidDismissInAppMessageHandler;
59-
NotificationWillShowInForegroundHandler? _onNotificationWillShowInForegroundHandler;
63+
NotificationWillShowInForegroundHandler?
64+
_onNotificationWillShowInForegroundHandler;
6065

6166
// constructor method
6267
OneSignal() {
@@ -69,8 +74,7 @@ class OneSignal {
6974
_onesignalLog(OSLogLevel.verbose,
7075
"Initializing the OneSignal Flutter SDK ($sdkVersion)");
7176

72-
await _channel.invokeMethod(
73-
'OneSignal#setAppId', {'appId': appId});
77+
await _channel.invokeMethod('OneSignal#setAppId', {'appId': appId});
7478
}
7579

7680
/// Sets the log level for the SDK. The first parameter (logLevel) controls
@@ -153,9 +157,11 @@ class OneSignal {
153157

154158
/// The notification foreground handler is called whenever a notification arrives
155159
/// and the application is in foreground
156-
void setNotificationWillShowInForegroundHandler(NotificationWillShowInForegroundHandler handler) {
160+
void setNotificationWillShowInForegroundHandler(
161+
NotificationWillShowInForegroundHandler handler) {
157162
_onNotificationWillShowInForegroundHandler = handler;
158-
_channel.invokeMethod("OneSignal#initNotificationWillShowInForegroundHandlerParams");
163+
_channel.invokeMethod(
164+
"OneSignal#initNotificationWillShowInForegroundHandlerParams");
159165
}
160166

161167
/// The notification foreground handler is called whenever a notification arrives
@@ -171,7 +177,7 @@ class OneSignal {
171177
Future<void> setLaunchURLsInApp(bool isEnabled) async {
172178
if (Platform.isIOS) {
173179
await _channel.invokeMethod(
174-
"OneSignal#setLaunchURLsInApp", {'isEnabled': isEnabled});
180+
"OneSignal#setLaunchURLsInApp", {'isEnabled': isEnabled});
175181
} else {
176182
_onesignalLog(OSLogLevel.info,
177183
"setLaunchURLsInApp: this function is not supported on Android");
@@ -274,11 +280,9 @@ class OneSignal {
274280

275281
/// Returns an `OSDeviceState` object, which contains the current device state
276282
Future<OSDeviceState?> getDeviceState() async {
277-
var json =
278-
await _channel.invokeMethod("OneSignal#getDeviceState");
283+
var json = await _channel.invokeMethod("OneSignal#getDeviceState");
279284

280-
if ((json.cast<String, dynamic>()).isEmpty)
281-
return null;
285+
if ((json.cast<String, dynamic>()).isEmpty) return null;
282286

283287
return OSDeviceState(json.cast<String, dynamic>());
284288
}
@@ -314,8 +318,8 @@ class OneSignal {
314318

315319
/// Allows you to manually cancel a single OneSignal notification based on its Android notification integer ID
316320
void removeNotification(int notificationId) {
317-
_channel.invokeMethod("OneSignal#removeNotification",
318-
{'notificationId': notificationId});
321+
_channel.invokeMethod(
322+
"OneSignal#removeNotification", {'notificationId': notificationId});
319323
}
320324

321325
/// Allows you to prompt the user for permission to use location services
@@ -334,7 +338,8 @@ class OneSignal {
334338
/// Identity Verification. The email auth hash is a hash of your app's API key and the
335339
/// user ID. We recommend you generate this token from your backend server, do NOT
336340
/// store your API key in your app as this is highly insecure.
337-
Future<void> setEmail({required String email, String? emailAuthHashToken}) async {
341+
Future<void> setEmail(
342+
{required String email, String? emailAuthHashToken}) async {
338343
return await _channel.invokeMethod("OneSignal#setEmail",
339344
{'email': email, 'emailAuthHashToken': emailAuthHashToken});
340345
}
@@ -350,9 +355,11 @@ class OneSignal {
350355
/// Identity Verification. The SMS auth hash is a hash of your app's API key and the
351356
/// user ID. We recommend you generate this token from your backend server, do NOT
352357
/// store your API key in your app as this is highly insecure.
353-
Future<Map<String, dynamic>> setSMSNumber({required String smsNumber, String? smsAuthHashToken}) async {
354-
Map<dynamic, dynamic> results =
355-
await _channel.invokeMethod("OneSignal#setSMSNumber", {'smsNumber': smsNumber, 'smsAuthHashToken': smsAuthHashToken});
358+
Future<Map<String, dynamic>> setSMSNumber(
359+
{required String smsNumber, String? smsAuthHashToken}) async {
360+
Map<dynamic, dynamic> results = await _channel.invokeMethod(
361+
"OneSignal#setSMSNumber",
362+
{'smsNumber': smsNumber, 'smsAuthHashToken': smsAuthHashToken});
356363
return results.cast<String, dynamic>();
357364
}
358365

@@ -367,9 +374,11 @@ class OneSignal {
367374
/// OneSignal allows you to set a custom ID for your users. This makes it so that
368375
/// if your app has its own user ID's, you can use your own custom user ID's with
369376
/// our API instead of having to save their OneSignal user ID's.
370-
Future<Map<String, dynamic>> setExternalUserId(String externalId, [String? authHashToken]) async {
371-
Map<dynamic, dynamic> results =
372-
await (_channel.invokeMethod("OneSignal#setExternalUserId", {'externalUserId' : externalId, 'authHashToken' : authHashToken}));
377+
Future<Map<String, dynamic>> setExternalUserId(String externalId,
378+
[String? authHashToken]) async {
379+
Map<dynamic, dynamic> results = await (_channel.invokeMethod(
380+
"OneSignal#setExternalUserId",
381+
{'externalUserId': externalId, 'authHashToken': authHashToken}));
373382
return results.cast<String, dynamic>();
374383
}
375384

@@ -383,74 +392,81 @@ class OneSignal {
383392
/// Sets the user's language.
384393
/// Applies also to the email and/or SMS player if those are logged in on the device.
385394
Future<Map<String, dynamic>> setLanguage(String language) async {
386-
Map<dynamic, dynamic> results =
387-
await (_channel.invokeMethod("OneSignal#setLanguage", {'language' : language}));
395+
Map<dynamic, dynamic> results = await (_channel
396+
.invokeMethod("OneSignal#setLanguage", {'language': language}));
388397
return results.cast<String, dynamic>();
389398
}
390399

391400
/// Adds a single key, value trigger, which will trigger an in app message
392401
/// if one exists matching the specific trigger added
393402
Future<void> addTrigger(String key, Object value) async {
394-
return await _inAppMessagesChannel.invokeMethod("OneSignal#addTrigger", {key : value});
403+
return await _inAppMessagesChannel
404+
.invokeMethod("OneSignal#addTrigger", {key: value});
395405
}
396406

397407
/// Adds one or more key, value triggers, which will trigger in app messages
398408
/// (one at a time) if any exist matching the specific triggers added
399409
Future<void> addTriggers(Map<String, Object> triggers) async {
400-
return await _inAppMessagesChannel.invokeMethod("OneSignal#addTriggers", triggers);
410+
return await _inAppMessagesChannel.invokeMethod(
411+
"OneSignal#addTriggers", triggers);
401412
}
402413

403414
/// Remove a single key, value trigger to prevent an in app message from
404415
/// showing with that trigger
405416
Future<void> removeTriggerForKey(String key) async {
406-
return await _inAppMessagesChannel.invokeMethod("OneSignal#removeTriggerForKey", key);
417+
return await _inAppMessagesChannel.invokeMethod(
418+
"OneSignal#removeTriggerForKey", key);
407419
}
408420

409421
/// Remove one or more key, value triggers to prevent any in app messages
410422
/// from showing with those triggers
411423
Future<void> removeTriggersForKeys(List<String> keys) async {
412-
return await _inAppMessagesChannel.invokeMethod("OneSignal#removeTriggersForKeys", keys);
424+
return await _inAppMessagesChannel.invokeMethod(
425+
"OneSignal#removeTriggersForKeys", keys);
413426
}
414427

415428
/// Get the trigger value associated with the key provided
416429
Future<Object?> getTriggerValueForKey(String key) async {
417-
return await _inAppMessagesChannel.invokeMethod("OneSignal#getTriggerValueForKey", key);
430+
return await _inAppMessagesChannel.invokeMethod(
431+
"OneSignal#getTriggerValueForKey", key);
418432
}
419433

420434
/// Toggles the showing of all in app messages
421435
Future<void> pauseInAppMessages(bool pause) async {
422-
return await _inAppMessagesChannel.invokeMethod("OneSignal#pauseInAppMessages", pause);
436+
return await _inAppMessagesChannel.invokeMethod(
437+
"OneSignal#pauseInAppMessages", pause);
423438
}
424439

425440
/// Send a normal outcome event for the current session and notifications with the attribution window
426441
/// Counted each time sent successfully, failed ones will be cached and reattempted in future
427442
Future<OSOutcomeEvent> sendOutcome(String name) async {
428-
var json = await _outcomesChannel.invokeMethod("OneSignal#sendOutcome", name);
443+
var json =
444+
await _outcomesChannel.invokeMethod("OneSignal#sendOutcome", name);
429445

430-
if (json == null)
431-
return new OSOutcomeEvent();
446+
if (json == null) return new OSOutcomeEvent();
432447

433448
return new OSOutcomeEvent.fromMap(json.cast<String, dynamic>());
434449
}
435450

436451
/// Send a unique outcome event for the current session and notifications with the attribution window
437452
/// Counted once per notification when sent successfully, failed ones will be cached and reattempted in future
438453
Future<OSOutcomeEvent> sendUniqueOutcome(String name) async {
439-
var json = await _outcomesChannel.invokeMethod("OneSignal#sendUniqueOutcome", name);
454+
var json = await _outcomesChannel.invokeMethod(
455+
"OneSignal#sendUniqueOutcome", name);
440456

441-
if (json == null)
442-
return new OSOutcomeEvent();
457+
if (json == null) return new OSOutcomeEvent();
443458

444459
return new OSOutcomeEvent.fromMap(json.cast<String, dynamic>());
445460
}
446461

447462
/// Send an outcome event with a value for the current session and notifications with the attribution window
448463
/// Counted each time sent successfully, failed ones will be cached and reattempted in future
449464
Future<OSOutcomeEvent> sendOutcomeWithValue(String name, double value) async {
450-
var json = await _outcomesChannel.invokeMethod("OneSignal#sendOutcomeWithValue", {"outcome_name" : name, "outcome_value" : value});
465+
var json = await _outcomesChannel.invokeMethod(
466+
"OneSignal#sendOutcomeWithValue",
467+
{"outcome_name": name, "outcome_value": value});
451468

452-
if (json == null)
453-
return new OSOutcomeEvent();
469+
if (json == null) return new OSOutcomeEvent();
454470

455471
return new OSOutcomeEvent.fromMap(json.cast<String, dynamic>());
456472
}
@@ -468,15 +484,15 @@ class OneSignal {
468484
} else if (call.method == 'OneSignal#permissionChanged' &&
469485
this._onPermissionChangedHandler != null) {
470486
this._onPermissionChangedHandler!(
471-
OSPermissionStateChanges(call.arguments.cast<String, dynamic>()));
487+
OSPermissionStateChanges(call.arguments.cast<String, dynamic>()));
472488
} else if (call.method == 'OneSignal#emailSubscriptionChanged' &&
473489
this._onEmailSubscriptionChangedHandler != null) {
474-
this._onEmailSubscriptionChangedHandler!(
475-
OSEmailSubscriptionStateChanges(call.arguments.cast<String, dynamic>()));
490+
this._onEmailSubscriptionChangedHandler!(OSEmailSubscriptionStateChanges(
491+
call.arguments.cast<String, dynamic>()));
476492
} else if (call.method == 'OneSignal#smsSubscriptionChanged' &&
477493
this._onSMSSubscriptionChangedHandler != null) {
478-
this._onSMSSubscriptionChangedHandler!(
479-
OSSMSSubscriptionStateChanges(call.arguments.cast<String, dynamic>()));
494+
this._onSMSSubscriptionChangedHandler!(OSSMSSubscriptionStateChanges(
495+
call.arguments.cast<String, dynamic>()));
480496
} else if (call.method == 'OneSignal#handleClickedInAppMessage' &&
481497
this._onInAppMessageClickedHandler != null) {
482498
this._onInAppMessageClickedHandler!(
@@ -497,7 +513,8 @@ class OneSignal {
497513
this._onDidDismissInAppMessageHandler != null) {
498514
this._onDidDismissInAppMessageHandler!(
499515
OSInAppMessage(call.arguments.cast<String, dynamic>()));
500-
} else if (call.method == 'OneSignal#handleNotificationWillShowInForeground' &&
516+
} else if (call.method ==
517+
'OneSignal#handleNotificationWillShowInForeground' &&
501518
this._onNotificationWillShowInForegroundHandler != null) {
502519
this._onNotificationWillShowInForegroundHandler!(
503520
OSNotificationReceivedEvent(call.arguments.cast<String, dynamic>()));
@@ -514,7 +531,7 @@ class OneSignal {
514531
Map<String, dynamic> _processSettings(Map<OSiOSSettings, dynamic> settings) {
515532
var finalSettings = Map<String, dynamic>();
516533

517-
if (settings == null) return finalSettings;
534+
if (settings.isEmpty) return finalSettings;
518535

519536
for (OSiOSSettings key in settings.keys) {
520537
var settingsKey = convertEnumCaseToValue(key);

lib/src/outcome_event.dart

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ import 'package:onesignal_flutter/src/defines.dart';
33
import 'package:onesignal_flutter/src/utils.dart';
44

55
OSSession sessionFromString(String session) {
6-
session = session.toLowerCase();
7-
if (session == 'direct') {
8-
return OSSession.DIRECT;
9-
} else if (session == 'indirect') {
10-
return OSSession.INDIRECT;
11-
} else if (session == 'unattributed') {
12-
return OSSession.UNATTRIBUTED;
13-
}
6+
session = session.toLowerCase();
7+
if (session == 'direct') {
8+
return OSSession.DIRECT;
9+
} else if (session == 'indirect') {
10+
return OSSession.INDIRECT;
11+
} else if (session == 'unattributed') {
12+
return OSSession.UNATTRIBUTED;
13+
}
1414

15-
return OSSession.DISABLED;
15+
return OSSession.DISABLED;
1616
}
1717

1818
/// When an outcome is sent the onSuccess will return an OutcomeEvent
1919
/// This object is converted from the native OutcomeEvent into the OSOutcomeEvent
2020
/// for Dart to use
2121
class OSOutcomeEvent extends JSONStringRepresentable {
22-
2322
// The session when the outcome event is sent (DIRECT, INDIRECT, UNATTRIBUTED)
2423
OSSession session = OSSession.DISABLED;
2524

@@ -35,42 +34,44 @@ class OSOutcomeEvent extends JSONStringRepresentable {
3534
// Value if one exists (default 0.0) that was sent with the outcome
3635
double weight = 0.0;
3736

38-
OSOutcomeEvent() {
39-
40-
}
37+
OSOutcomeEvent();
4138

4239
OSOutcomeEvent.fromMap(Map<String, dynamic> outcome) {
43-
// Make sure session exists
44-
this.session = outcome.containsKey("session") && outcome["session"] != null ?
45-
sessionFromString(outcome["session"] as String) :
46-
OSSession.DISABLED;
47-
48-
// Make sure notification_ids exists
49-
if (outcome.containsKey("notification_ids") && outcome["notification_ids"] != null) {
50-
if (outcome["notification_ids"] is List) {
51-
// Handle if type comes in as a List
52-
this.notificationIds = (outcome["notification_ids"] as List).map<String>((s) => s).toList();
53-
}
54-
else if (outcome["notification_ids"] is String) {
55-
// Handle if type comes in as a String
56-
this.notificationIds = new List<String>.from(json.decode(outcome["notification_ids"]));
57-
}
40+
// Make sure session exists
41+
this.session = outcome.containsKey("session") && outcome["session"] != null
42+
? sessionFromString(outcome["session"] as String)
43+
: OSSession.DISABLED;
44+
45+
// Make sure notification_ids exists
46+
if (outcome.containsKey("notification_ids") &&
47+
outcome["notification_ids"] != null) {
48+
if (outcome["notification_ids"] is List) {
49+
// Handle if type comes in as a List
50+
this.notificationIds = (outcome["notification_ids"] as List)
51+
.map<String>((s) => s)
52+
.toList();
53+
} else if (outcome["notification_ids"] is String) {
54+
// Handle if type comes in as a String
55+
this.notificationIds =
56+
new List<String>.from(json.decode(outcome["notification_ids"]));
5857
}
58+
}
5959

60-
// Make sure name exists
61-
this.name = outcome.containsKey("id") && outcome["id"] != null ?
62-
outcome["id"] as String :
63-
"";
64-
65-
// Make sure timestamp exists
66-
this.timestamp = outcome.containsKey("timestamp") && outcome["timestamp"] != null ?
67-
outcome["timestamp"] as int :
68-
0;
69-
70-
// Make sure weight exists
71-
this.weight = outcome.containsKey("weight") && outcome["weight"] != null ?
72-
double.parse(outcome["weight"] as String) :
73-
0.0;
60+
// Make sure name exists
61+
this.name = outcome.containsKey("id") && outcome["id"] != null
62+
? outcome["id"] as String
63+
: "";
64+
65+
// Make sure timestamp exists
66+
this.timestamp =
67+
outcome.containsKey("timestamp") && outcome["timestamp"] != null
68+
? outcome["timestamp"] as int
69+
: 0;
70+
71+
// Make sure weight exists
72+
this.weight = outcome.containsKey("weight") && outcome["weight"] != null
73+
? double.parse(outcome["weight"] as String)
74+
: 0.0;
7475
}
7576

7677
String jsonRepresentation() {
@@ -82,5 +83,4 @@ class OSOutcomeEvent extends JSONStringRepresentable {
8283
'weight': this.weight,
8384
});
8485
}
85-
8686
}

0 commit comments

Comments
 (0)