Skip to content

Commit edb337e

Browse files
authored
Feedback widgets callbacks added (#100)
* Feedback widgets callbacks added * Added constant values for callback with updated names
1 parent dbbe256 commit edb337e

File tree

4 files changed

+121
-34
lines changed

4 files changed

+121
-34
lines changed

Countly.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ _isPushInitialized = false;
2323
* Listener for rating widget callback, when callback recieve we will remove the callback using listener.
2424
*/
2525
var _ratingWidgetListener;
26+
/*
27+
* Callback to be executed when feedback widget is displayed
28+
*/
29+
var _widgetShownCallback;
30+
31+
/*
32+
* Callback to be executed when feedback widget is closed
33+
*/
34+
var _widgetClosedCallback;
35+
36+
const widgetShownCallbackName = "widgetShownCallback"
37+
const widgetClosedCallbackName = "widgetClosedCallback"
38+
const ratingWidgetCallbackName = "ratingWidgetCallback"
39+
const pushNotificationCallbackName = "pushNotificationCallback"
2640

2741
Countly.messagingMode = {"DEVELOPMENT":"1","PRODUCTION":"0", "ADHOC": "2"};
2842
if (Platform.OS.match("android")) {
@@ -211,7 +225,7 @@ Countly.askForNotificationPermission = function(customSoundPath = "null"){
211225
* @param {callback listener } theListener
212226
*/
213227
Countly.registerForNotification = function(theListener){
214-
var event = eventEmitter.addListener('onCountlyPushNotification', theListener);
228+
var event = eventEmitter.addListener(pushNotificationCallbackName, theListener);
215229
CountlyReactNative.registerForNotification([]);
216230
return event;
217231
};
@@ -1343,7 +1357,7 @@ Countly.presentRatingWidgetWithID = function(widgetId, closeButtonText, ratingWi
13431357
}
13441358
if(ratingWidgetCallback){
13451359
// eventEmitter.addListener('ratingWidgetCallback', ratingWidgetCallback);
1346-
_ratingWidgetListener = eventEmitter.addListener('ratingWidgetCallback', (error) => {
1360+
_ratingWidgetListener = eventEmitter.addListener(ratingWidgetCallbackName, (error) => {
13471361
ratingWidgetCallback(error);
13481362
_ratingWidgetListener.remove();
13491363
}
@@ -1354,15 +1368,25 @@ Countly.presentRatingWidgetWithID = function(widgetId, closeButtonText, ratingWi
13541368

13551369

13561370
/**
1357-
* Get a list of available feedback widgets as array of object to handle multiple widgets of same type.
1371+
* Get a list of available feedback widgets as array of object to handle multiple widgets of same type.
1372+
* @param {callback listener} onFinished - returns (retrievedWidgets, error)
13581373
*/
1359-
Countly.getFeedbackWidgets = async function(){
1374+
Countly.getFeedbackWidgets = async function(onFinished){
13601375
if(!_isInitialized) {
13611376
var message = "'init' must be called before 'getFeedbackWidgets'";
13621377
Countly.logError("getFeedbackWidgets", message);
13631378
return message;
13641379
}
1365-
const result = await CountlyReactNative.getFeedbackWidgets();
1380+
var result = [];
1381+
var error = null;
1382+
try {
1383+
result = await CountlyReactNative.getFeedbackWidgets();
1384+
} catch (e) {
1385+
error = e.message;
1386+
}
1387+
if(onFinished) {
1388+
onFinished(result, error);
1389+
}
13661390
return result;
13671391
}
13681392

@@ -1388,8 +1412,10 @@ Countly.getAvailableFeedbackWidgets = async function(){
13881412
*
13891413
* @param {Object} feedbackWidget - feeback Widget with id, type and name
13901414
* @param {String} closeButtonText - text for cancel/close button
1415+
* @param {callback listener} widgetShownCallback - Callback to be executed when feedback widget is displayed
1416+
* @param {callback listener} widgetClosedCallback - Callback to be executed when feedback widget is closed
13911417
*/
1392-
Countly.presentFeedbackWidgetObject = async function(feedbackWidget, closeButtonText){
1418+
Countly.presentFeedbackWidgetObject = async function(feedbackWidget, closeButtonText, widgetShownCallback, widgetClosedCallback){
13931419
if(!_isInitialized) {
13941420
var msg = "'init' must be called before 'presentFeedbackWidgetObject'";
13951421
Countly.logError("presentFeedbackWidgetObject", msg);
@@ -1411,10 +1437,26 @@ Countly.presentFeedbackWidgetObject = async function(feedbackWidget, closeButton
14111437
Countly.logError("presentFeedbackWidgetObject", message);
14121438
return message;
14131439
}
1414-
if (typeof closeButtonText != "string") {
1440+
if(typeof closeButtonText != "string") {
14151441
closeButtonText = "";
14161442
Countly.logWarning("presentFeedbackWidgetObject", "unsupported data type of closeButtonText : '" + (typeof args) + "'");
14171443
}
1444+
1445+
if(widgetShownCallback) {
1446+
_widgetShownCallback = eventEmitter.addListener(widgetShownCallbackName, () => {
1447+
widgetShownCallback();
1448+
_widgetShownCallback.remove();
1449+
}
1450+
);
1451+
}
1452+
if(widgetClosedCallback) {
1453+
_widgetClosedCallback = eventEmitter.addListener(widgetClosedCallbackName, () => {
1454+
widgetClosedCallback();
1455+
_widgetClosedCallback.remove();
1456+
}
1457+
);
1458+
}
1459+
14181460
feedbackWidget.name = feedbackWidget.name || "";
14191461
closeButtonText = closeButtonText || "";
14201462
CountlyReactNative.presentFeedbackWidget([feedbackWidget.id, feedbackWidget.type, feedbackWidget.name, closeButtonText]);

android/src/main/java/ly/count/android/sdk/react/CountlyReactNative.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ public class CountlyReactNative extends ReactContextBaseJavaModule implements Li
101101
private boolean useAdditionalIntentRedirectionChecks = true;
102102

103103
private final ReactApplicationContext _reactContext;
104-
104+
105+
private static String widgetShownCallbackName = "widgetShownCallback";
106+
private static String widgetClosedCallbackName = "widgetClosedCallback";
107+
private static String ratingWidgetCallbackName = "ratingWidgetCallback";
108+
private static String pushNotificationCallbackName = "pushNotificationCallback";
109+
105110
private final Set<String> validConsentFeatureNames = new HashSet<>(Arrays.asList(
106111
Countly.CountlyFeatureNames.sessions,
107112
Countly.CountlyFeatureNames.events,
@@ -550,7 +555,7 @@ public void callback(String result) {
550555
log("registerForNotification callback result [" + result + "]", LogLevel.WARNING);
551556
((ReactApplicationContext) _reactContext)
552557
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
553-
.emit("onCountlyPushNotification", result);
558+
.emit(pushNotificationCallbackName, result);
554559
}
555560
};
556561
log("registerForNotification theCallback", LogLevel.INFO);
@@ -1087,7 +1092,7 @@ public void presentRatingWidgetWithID(ReadableArray args){
10871092
public void callback(String error) {
10881093
((ReactApplicationContext) _reactContext)
10891094
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
1090-
.emit("ratingWidgetCallback", error);
1095+
.emit(ratingWidgetCallbackName, error);
10911096
}
10921097
});
10931098
}
@@ -1099,7 +1104,7 @@ public void getFeedbackWidgets(final Promise promise)
10991104
@Override
11001105
public void onFinished(List<CountlyFeedbackWidget> retrievedWidgets, String error) {
11011106
if(error != null) {
1102-
promise.reject("getFeedbackWidgets", error);
1107+
promise.reject("getFeedbackWidgets_failure", error);
11031108
return;
11041109
}
11051110
WritableArray retrievedWidgetsArray = new WritableNativeArray();
@@ -1159,11 +1164,17 @@ public void onFinished(String error) {
11591164
}
11601165
else {
11611166
promise.resolve("presentFeedbackWidget success");
1167+
((ReactApplicationContext) _reactContext)
1168+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
1169+
.emit(widgetShownCallbackName, null);
11621170
}
11631171
}
11641172
@Override
11651173
public void onClosed() {
1166-
1174+
promise.resolve("presentFeedbackWidget success");
1175+
((ReactApplicationContext) _reactContext)
1176+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
1177+
.emit(widgetClosedCallbackName, null);
11671178
}
11681179
});
11691180
}

example/Example.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,26 +422,44 @@ class Example extends Component {
422422
}
423423

424424
showSurvey = function(){
425-
Countly.getFeedbackWidgets().then((retrivedWidgets) => {
425+
Countly.getFeedbackWidgets(function(retrivedWidgets, error){
426+
if (error != null) {
427+
console.log("showSurvey Error : " + error);
428+
}
429+
else {
430+
console.log("showSurvey Success : " + retrivedWidgets.length);
426431
var surveyWidget = retrivedWidgets.find(x => x.type === 'survey')
427432
if(surveyWidget) {
428-
Countly.presentFeedbackWidgetObject(surveyWidget, "Close")
433+
Countly.presentFeedbackWidgetObject(surveyWidget, "Close", function() {
434+
console.log("showSurvey presentFeedbackWidgetObject : " + "Widgetshown");
435+
},
436+
function() {
437+
console.log("showSurvey presentFeedbackWidgetObject : " + "Widgetclosed");
438+
})
429439
}
430-
},(err) => {
431-
console.error("showSurvey getFeedbackWidgets error : " +err);
440+
}
432441
});
433-
}
442+
}
434443

435-
showNPS = function(){
436-
Countly.getFeedbackWidgets().then((retrivedWidgets) => {
437-
var npsWidget = retrivedWidgets.find(x => x.type === 'nps')
438-
if(npsWidget) {
439-
Countly.presentFeedbackWidgetObject(npsWidget, "Close")
440-
}
441-
},(err) => {
442-
console.error("showNPS getFeedbackWidgets error : " +err);
443-
});
444-
}
444+
showNPS = function(){
445+
Countly.getFeedbackWidgets(function(retrivedWidgets, error){
446+
if (error != null) {
447+
console.log("showNPS Error : " + error);
448+
}
449+
else {
450+
console.log("showNPS Success : " + retrivedWidgets.length);
451+
var npsWidget = retrivedWidgets.find(x => x.type === 'nps')
452+
if(npsWidget) {
453+
Countly.presentFeedbackWidgetObject(npsWidget, "Close", function() {
454+
console.log("showNPS presentFeedbackWidgetObject : " + "Widgetshown");
455+
},
456+
function() {
457+
console.log("showNPS presentFeedbackWidgetObject : " + "Widgetclosed");
458+
})
459+
}
460+
}
461+
});
462+
}
445463

446464
addCrashLog(){
447465
Countly.addCrashLog("My crash log in string.");

ios/src/CountlyReactNative.m

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ + (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
4646
NSString* const BYEAR_KEY = @"byear";
4747
NSString* const CUSTOM_KEY = @"custom";
4848

49+
NSString* const widgetShownCallbackName = @"widgetShownCallback";
50+
NSString* const widgetClosedCallbackName = @"widgetClosedCallback";
51+
NSString* const ratingWidgetCallbackName = @"ratingWidgetCallback";
52+
NSString* const pushNotificationCallbackName = @"pushNotificationCallback";
53+
4954
@implementation CountlyReactNative
5055
NSString* const kCountlyNotificationPersistencyKey = @"kCountlyNotificationPersistencyKey";
5156

5257
- (NSArray<NSString *> *)supportedEvents {
53-
return @[@"onCountlyPushNotification", @"ratingWidgetCallback"];
58+
return @[pushNotificationCallbackName, ratingWidgetCallbackName, widgetShownCallbackName, widgetClosedCallbackName];
5459
}
5560

5661
RCT_EXPORT_MODULE();
@@ -87,7 +92,6 @@ @implementation CountlyReactNative
8792
[[Countly sharedInstance] startWithConfig:config];
8893
[self recordPushAction];
8994
resolve(@"Success");
90-
9195
});
9296
}
9397
});
@@ -234,11 +238,11 @@ - (void) saveListener:(Result) result{
234238
{
235239
dispatch_async(dispatch_get_main_queue(), ^ {
236240
[self saveListener: ^(id _Nullable result) {
237-
[self sendEventWithName:@"onCountlyPushNotification" body: [CountlyReactNative toJSON:lastStoredNotification]];
241+
[self sendEventWithName:pushNotificationCallbackName body: [CountlyReactNative toJSON:lastStoredNotification]];
238242
lastStoredNotification = nil;
239243
}];
240244
if(lastStoredNotification != nil){
241-
[self sendEventWithName:@"onCountlyPushNotification" body: [CountlyReactNative toJSON:lastStoredNotification]];
245+
[self sendEventWithName:pushNotificationCallbackName body: [CountlyReactNative toJSON:lastStoredNotification]];
242246
lastStoredNotification = nil;
243247
}
244248
});
@@ -1075,7 +1079,7 @@ - (CLLocationCoordinate2D) getCoordinate:(NSString*) gpsCoordinate
10751079
if (error){
10761080
errorStr = error.localizedDescription;
10771081
}
1078-
[self sendEventWithName:@"ratingWidgetCallback" body: errorStr];
1082+
[self sendEventWithName:ratingWidgetCallbackName body: errorStr];
10791083
}];
10801084
});
10811085
}
@@ -1086,7 +1090,12 @@ - (CLLocationCoordinate2D) getCoordinate:(NSString*) gpsCoordinate
10861090
{
10871091
dispatch_async(dispatch_get_main_queue(), ^ {
10881092
[Countly.sharedInstance getFeedbackWidgets:^(NSArray<CountlyFeedbackWidget *> * _Nonnull feedbackWidgets, NSError * _Nonnull error) {
1089-
NSMutableArray* feedbackWidgetsArray = [NSMutableArray arrayWithCapacity:feedbackWidgets.count];
1093+
if (error){
1094+
NSString* errorStr = error.localizedDescription;
1095+
reject(@"getFeedbackWidgets_failure", errorStr, nil);
1096+
}
1097+
else {
1098+
NSMutableArray* feedbackWidgetsArray = [NSMutableArray arrayWithCapacity:feedbackWidgets.count];
10901099
for (CountlyFeedbackWidget* retrievedWidget in feedbackWidgets) {
10911100
NSMutableDictionary* feedbackWidget = [NSMutableDictionary dictionaryWithCapacity:3];
10921101
feedbackWidget[@"id"] = retrievedWidget.ID;
@@ -1095,6 +1104,7 @@ - (CLLocationCoordinate2D) getCoordinate:(NSString*) gpsCoordinate
10951104
[feedbackWidgetsArray addObject:feedbackWidget];
10961105
}
10971106
resolve(feedbackWidgetsArray);
1107+
}
10981108
}];
10991109
});
11001110
}
@@ -1126,7 +1136,12 @@ - (CLLocationCoordinate2D) getCoordinate:(NSString*) gpsCoordinate
11261136
feedbackWidgetsDict[@"type"] = widgetType;
11271137
feedbackWidgetsDict[@"name"] = widgetName;
11281138
CountlyFeedbackWidget *feedback = [CountlyFeedbackWidget createWithDictionary:feedbackWidgetsDict];
1129-
[feedback present];
1139+
[feedback presentWithAppearBlock:^{
1140+
[self sendEventWithName:widgetShownCallbackName body: nil];
1141+
}
1142+
andDismissBlock:^{
1143+
[self sendEventWithName:widgetClosedCallbackName body: nil];
1144+
}];
11301145
});
11311146
}
11321147

@@ -1453,3 +1468,4 @@ - (void)openURL:(NSString *)URLString
14531468
}
14541469

14551470
@end
1471+

0 commit comments

Comments
 (0)