Skip to content

Commit fd92b0a

Browse files
committed
Add onDismissHandler API to BugReporting
1 parent 14e4fff commit fd92b0a

File tree

5 files changed

+68
-25
lines changed

5 files changed

+68
-25
lines changed

android/src/main/java/com/instabug/reactlibrary/ArgsRegistry.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.instabug.reactlibrary;
22

33
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
45

56
import com.instabug.apm.model.LogLevel;
67
import com.instabug.bug.BugReporting;
@@ -41,6 +42,17 @@ public ArrayList<T> getAll(ArrayList<String> keys) {
4142
}
4243
return values;
4344
}
45+
46+
@Nullable
47+
public String getKey(T value) {
48+
for (Entry<String, T> entry : this.entrySet()) {
49+
if (entry.getValue().equals(value)) {
50+
return entry.getKey();
51+
}
52+
}
53+
54+
return null;
55+
}
4456
}
4557

4658
static Map<String, Object> getAll() {
@@ -53,6 +65,7 @@ static Map<String, Object> getAll() {
5365
putAll(recordButtonPositions);
5466
putAll(welcomeMessageStates);
5567
putAll(reportTypes);
68+
putAll(sdkDismissReportTypes);
5669
putAll(dismissTypes);
5770
putAll(actionTypes);
5871
putAll(extendedBugReportStates);
@@ -120,6 +133,13 @@ static Map<String, Object> getAll() {
120133
put("bugReportingReportTypeQuestion", BugReporting.ReportType.QUESTION);
121134
}};
122135

136+
static final ArgsMap<OnSdkDismissCallback.ReportType> sdkDismissReportTypes = new ArgsMap<OnSdkDismissCallback.ReportType>() {{
137+
put("bugReportingReportTypeBug", OnSdkDismissCallback.ReportType.BUG);
138+
put("bugReportingReportTypeFeedback", OnSdkDismissCallback.ReportType.FEEDBACK);
139+
put("bugReportingReportTypeQuestion", OnSdkDismissCallback.ReportType.QUESTION);
140+
put("bugReportingReportTypeOther", OnSdkDismissCallback.ReportType.OTHER);
141+
}};
142+
123143
static final ArgsMap<DismissType> dismissTypes = new ArgsMap<DismissType>() {{
124144
put("dismissTypeSubmit", DismissType.SUBMIT);
125145
put("dismissTypeCancel", DismissType.CANCEL);

android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,13 @@ public void run() {
283283
BugReporting.setOnDismissCallback(new OnSdkDismissCallback() {
284284
@Override
285285
public void call(DismissType dismissType, ReportType reportType) {
286-
WritableMap params = Arguments.createMap();
287-
params.putString("dismissType", dismissType.toString());
288-
params.putString("reportType", reportType.toString());
286+
final String dismissKey = ArgsRegistry.dismissTypes.getKey(dismissType);
287+
final String reportKey = ArgsRegistry.sdkDismissReportTypes.getKey(reportType);
288+
final WritableMap params = Arguments.createMap();
289+
290+
params.putString("dismissType", dismissKey);
291+
params.putString("reportType", reportKey);
292+
289293
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_POST_INVOCATION_HANDLER, params);
290294
}
291295
});

ios/RNInstabug/InstabugBugReportingBridge.m

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,14 @@ + (BOOL)requiresMainQueueSetup
6060
RCT_EXPORT_METHOD(setOnSDKDismissedHandler:(RCTResponseSenderBlock)callBack) {
6161
if (callBack != nil) {
6262
IBGBugReporting.didDismissHandler = ^(IBGDismissType dismissType, IBGReportType reportType) {
63-
64-
//parse dismiss type enum
65-
NSString* dismissTypeString;
66-
if (dismissType == IBGDismissTypeCancel) {
67-
dismissTypeString = @"CANCEL";
68-
} else if (dismissType == IBGDismissTypeSubmit) {
69-
dismissTypeString = @"SUBMIT";
70-
} else if (dismissType == IBGDismissTypeAddAttachment) {
71-
dismissTypeString = @"ADD_ATTACHMENT";
72-
}
73-
74-
//parse report type enum
75-
NSString* reportTypeString;
76-
if (reportType == IBGReportTypeBug) {
77-
reportTypeString = @"bug";
78-
} else if (reportType == IBGReportTypeFeedback) {
79-
reportTypeString = @"feedback";
80-
} else {
81-
reportTypeString = @"other";
82-
}
83-
NSDictionary *result = @{ @"dismissType": dismissTypeString,
84-
@"reportType": reportTypeString};
63+
// Unlinke Android, we do NOT need to map the iOS Enums to their JS constant names.
64+
// This is because the JS Enums are mapped to the actual values of the
65+
// iOS Enums (NSInteger), not strings as it's implemented on Android.
66+
NSDictionary *result = @{
67+
@"dismissType": @(dismissType),
68+
@"reportType": @(reportType)
69+
};
70+
8571
[self sendEventWithName:@"IBGpostInvocationHandler" body: result];
8672
};
8773
} else {

src/modules/BugReporting.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,44 @@ export const onInvokeHandler = (handler: () => void) => {
5353
};
5454

5555
/**
56+
* @deprecated Use {@link onDismissHandler} instead as it has correct types
57+
* for the `handler` parameters.
58+
*
5659
* Sets a block of code to be executed right after the SDK's UI is dismissed.
5760
* This block is executed on the UI thread. Could be used for performing any
5861
* UI changes after the SDK's UI is dismissed.
5962
* @param handler A callback to get executed after dismissing the SDK.
6063
*/
6164
export const onSDKDismissedHandler = (
6265
handler: (dismissType: dismissType, reportType: reportType) => void,
66+
) => {
67+
// Remapped to new API, while keeping the old incorrect behavior.
68+
onDismissHandler((dismiss: dismissType, report: reportType) => {
69+
const dismissTypes: Record<dismissType, string> = {
70+
[dismissType.addAttachment]: 'ADD_ATTACHMENT',
71+
[dismissType.submit]: 'SUBMIT',
72+
[dismissType.cancel]: 'CANCEL',
73+
};
74+
75+
const reportTypes: Record<reportType, string> = {
76+
[reportType.bug]: 'bug',
77+
[reportType.feedback]: 'feedback',
78+
[reportType.question]: 'question',
79+
[reportType.other]: 'other',
80+
};
81+
82+
handler(dismissTypes[dismiss] as any, reportTypes[report] as any);
83+
});
84+
};
85+
86+
/**
87+
* Sets a block of code to be executed right after the SDK's UI is dismissed.
88+
* This block is executed on the UI thread. Could be used for performing any
89+
* UI changes after the SDK's UI is dismissed.
90+
* @param handler A callback to get executed after dismissing the SDK.
91+
*/
92+
export const onDismissHandler = (
93+
handler: (dismissType: dismissType, reportType: reportType) => void,
6394
) => {
6495
IBGEventEmitter.addListener(
6596
NativeBugReporting,
@@ -68,6 +99,7 @@ export const onSDKDismissedHandler = (
6899
handler(payload.dismissType, payload.reportType);
69100
},
70101
);
102+
71103
NativeBugReporting.setOnSDKDismissedHandler(handler);
72104
};
73105

src/utils/ArgsRegistry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export enum reportType {
9696
bug = NativeInstabug.bugReportingReportTypeBug,
9797
feedback = NativeInstabug.bugReportingReportTypeFeedback,
9898
question = NativeInstabug.bugReportingReportTypeQuestion,
99+
other = NativeInstabug.bugReportingReportTypeOther,
99100
}
100101

101102
/**

0 commit comments

Comments
 (0)