Skip to content

Commit 7fa9a38

Browse files
committed
Add onDismissHandler API to BugReporting
1 parent d8003a8 commit 7fa9a38

File tree

7 files changed

+75
-27
lines changed

7 files changed

+75
-27
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);
@@ -121,6 +134,13 @@ static Map<String, Object> getAll() {
121134
put("bugReportingReportTypeQuestion", BugReporting.ReportType.QUESTION);
122135
}};
123136

137+
static final ArgsMap<OnSdkDismissCallback.ReportType> sdkDismissReportTypes = new ArgsMap<OnSdkDismissCallback.ReportType>() {{
138+
put("bugReportingReportTypeBug", OnSdkDismissCallback.ReportType.BUG);
139+
put("bugReportingReportTypeFeedback", OnSdkDismissCallback.ReportType.FEEDBACK);
140+
put("bugReportingReportTypeQuestion", OnSdkDismissCallback.ReportType.QUESTION);
141+
put("bugReportingReportTypeOther", OnSdkDismissCallback.ReportType.OTHER);
142+
}};
143+
124144
static final ArgsMap<DismissType> dismissTypes = new ArgsMap<DismissType>() {{
125145
put("dismissTypeSubmit", DismissType.SUBMIT);
126146
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
@@ -291,9 +291,13 @@ public void run() {
291291
BugReporting.setOnDismissCallback(new OnSdkDismissCallback() {
292292
@Override
293293
public void call(DismissType dismissType, ReportType reportType) {
294-
WritableMap params = Arguments.createMap();
295-
params.putString("dismissType", dismissType.toString());
296-
params.putString("reportType", reportType.toString());
294+
final String dismissKey = ArgsRegistry.dismissTypes.getKey(dismissType);
295+
final String reportKey = ArgsRegistry.sdkDismissReportTypes.getKey(reportType);
296+
final WritableMap params = Arguments.createMap();
297+
298+
params.putString("dismissType", dismissKey);
299+
params.putString("reportType", reportKey);
300+
297301
sendEvent(Constants.IBG_POST_INVOCATION_HANDLER, params);
298302
}
299303
});

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: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export const onInvokeHandler = (handler: () => void) => {
6060
};
6161

6262
/**
63+
* @deprecated Use {@link onDismissHandler} instead as it has correct types
64+
* for the `handler` parameters.
65+
*
6366
* Sets a block of code to be executed right after the SDK's UI is dismissed.
6467
* This block is executed on the UI thread. Could be used for performing any
6568
* UI changes after the SDK's UI is dismissed.
@@ -68,9 +71,41 @@ export const onInvokeHandler = (handler: () => void) => {
6871
export const onSDKDismissedHandler = (
6972
handler: (dismissType: dismissType | DismissType, reportType: reportType | ReportType) => void,
7073
) => {
71-
emitter.addListener(NativeEvents.ON_DISMISS_HANDLER, (payload) => {
72-
handler(payload.dismissType, payload.reportType);
74+
// Remapped to new API, while keeping the old incorrect behavior.
75+
onDismissHandler((dismiss: dismissType, report: reportType) => {
76+
const dismissTypes: Record<dismissType, string> = {
77+
[dismissType.addAttachment]: 'ADD_ATTACHMENT',
78+
[dismissType.submit]: 'SUBMIT',
79+
[dismissType.cancel]: 'CANCEL',
80+
};
81+
82+
const reportTypes: Record<reportType, string> = {
83+
[reportType.bug]: 'bug',
84+
[reportType.feedback]: 'feedback',
85+
[reportType.question]: 'question',
86+
[reportType.other]: 'other',
87+
};
88+
89+
handler(dismissTypes[dismiss] as any, reportTypes[report] as any);
7390
});
91+
};
92+
93+
/**
94+
* Sets a block of code to be executed right after the SDK's UI is dismissed.
95+
* This block is executed on the UI thread. Could be used for performing any
96+
* UI changes after the SDK's UI is dismissed.
97+
* @param handler A callback to get executed after dismissing the SDK.
98+
*/
99+
export const onDismissHandler = (
100+
handler: (dismissType: dismissType | DismissType, reportType: reportType | ReportType) => void,
101+
) => {
102+
emitter.addListener(
103+
NativeEvents.ON_DISMISS_HANDLER,
104+
(payload: { dismissType: any; reportType: any }) => {
105+
handler(payload.dismissType, payload.reportType);
106+
},
107+
);
108+
74109
NativeBugReporting.setOnSDKDismissedHandler(handler);
75110
};
76111

src/native/NativeConstants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ interface NativeReportType {
7676
bugReportingReportTypeBug: any;
7777
bugReportingReportTypeFeedback: any;
7878
bugReportingReportTypeQuestion: any;
79+
bugReportingReportTypeOther: any;
7980
}
8081

8182
interface NativeDismissType {

src/utils/ArgsRegistry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export enum reportType {
118118
bug = constants.bugReportingReportTypeBug,
119119
feedback = constants.bugReportingReportTypeFeedback,
120120
question = constants.bugReportingReportTypeQuestion,
121+
other = constants.bugReportingReportTypeOther,
121122
}
122123

123124
/**

src/utils/Enums.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export enum ReportType {
7676
bug = constants.bugReportingReportTypeBug,
7777
feedback = constants.bugReportingReportTypeFeedback,
7878
question = constants.bugReportingReportTypeQuestion,
79+
other = constants.bugReportingReportTypeOther,
7980
}
8081

8182
/**

0 commit comments

Comments
 (0)