Skip to content

Commit 5f3b96d

Browse files
salmamaliHeshamMegid
authored andcommitted
Adds updated onReportSubmitHandler API (#30)
* ✨ add feature where a bug report can be edited before being sent to the native side
1 parent 5a298a1 commit 5f3b96d

File tree

14 files changed

+555
-77
lines changed

14 files changed

+555
-77
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ android {
2525

2626
dependencies {
2727
implementation 'com.facebook.react:react-native:+'
28-
api ('com.instabug.library:instabug:8.2.0'){
28+
api ('com.instabug.library:instabug:8.2.2.1'){
2929
exclude group: 'com.android.support:appcompat-v7'
3030
}
3131
}

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

Lines changed: 145 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88

99
import com.facebook.react.bridge.Arguments;
10+
import com.facebook.react.bridge.Promise;
1011
import com.facebook.react.bridge.ReactApplicationContext;
1112
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1213
import com.facebook.react.bridge.ReactMethod;
@@ -51,6 +52,7 @@
5152
import com.instabug.library.visualusersteps.State;
5253

5354
import com.instabug.reactlibrary.utils.ArrayUtil;
55+
import com.instabug.reactlibrary.utils.ReportUtil;
5456
import com.instabug.reactlibrary.utils.InstabugUtil;
5557
import com.instabug.reactlibrary.utils.MapUtil;
5658
import com.instabug.survey.OnDismissCallback;
@@ -212,6 +214,7 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
212214
private Instabug mInstabug;
213215
private InstabugInvocationEvent invocationEvent;
214216
private InstabugCustomTextPlaceHolder placeHolders;
217+
private Report currentReport;
215218

216219
/**
217220
* Instantiates a new Rn instabug reactnative module.
@@ -452,7 +455,8 @@ public void setFileAttachment(String fileUri, String fileNameWithExtension) {
452455
@ReactMethod
453456
public void sendJSCrash(String exceptionObject) {
454457
try {
455-
sendJSCrashByReflection(exceptionObject, false);
458+
JSONObject jsonObject = new JSONObject(exceptionObject);
459+
sendJSCrashByReflection(jsonObject, false, null);
456460
} catch (Exception e) {
457461
e.printStackTrace();
458462
}
@@ -466,7 +470,8 @@ public void sendJSCrash(String exceptionObject) {
466470
@ReactMethod
467471
public void sendHandledJSCrash(String exceptionObject) {
468472
try {
469-
sendJSCrashByReflection(exceptionObject, true);
473+
JSONObject jsonObject = new JSONObject(exceptionObject);
474+
sendJSCrashByReflection(jsonObject, true, null);
470475
} catch (Exception e) {
471476
e.printStackTrace();
472477
}
@@ -490,23 +495,20 @@ public void setCrashReportingEnabled(boolean isEnabled) {
490495
}
491496
}
492497

493-
private void sendJSCrashByReflection(String exceptionObject, boolean isHandled) {
498+
private void sendJSCrashByReflection(JSONObject exceptionObject, boolean isHandled, Report report) {
494499
try {
495-
JSONObject newJSONObject = new JSONObject(exceptionObject);
496-
Method method = getMethod(Class.forName("com.instabug.crash.CrashReporting"), "reportException", JSONObject.class, boolean.class);
500+
Method method = getMethod(Class.forName("com.instabug.crash.CrashReporting"), "reportException", JSONObject.class, boolean.class, Report.class);
497501
if (method != null) {
498-
method.invoke(null, newJSONObject, isHandled);
502+
method.invoke(null, exceptionObject, isHandled, currentReport);
503+
currentReport = null;
499504
}
500505
} catch (ClassNotFoundException e) {
501506
e.printStackTrace();
502-
} catch (InvocationTargetException e) {
503-
e.printStackTrace();
504507
} catch (IllegalAccessException e) {
505508
e.printStackTrace();
506-
} catch (JSONException e) {
509+
} catch (InvocationTargetException e) {
507510
e.printStackTrace();
508511
}
509-
510512
}
511513

512514
/**
@@ -1185,25 +1187,144 @@ public void onInvoke() {
11851187
*/
11861188
@ReactMethod
11871189
public void setPreSendingHandler(final Callback preSendingHandler) {
1190+
Report.OnReportCreatedListener listener = new Report.OnReportCreatedListener() {
1191+
@Override
1192+
public void onReportCreated(Report report) {
1193+
WritableMap reportParam = Arguments.createMap();
1194+
reportParam.putArray("tagsArray", convertArrayListToWritableArray(report.getTags()));
1195+
reportParam.putArray("consoleLogs", convertArrayListToWritableArray(report.getConsoleLog()));
1196+
reportParam.putString("userData", report.getUserData());
1197+
reportParam.putMap("userAttributes", convertFromHashMapToWriteableMap(report.getUserAttributes()));
1198+
reportParam.putMap("fileAttachments", convertFromHashMapToWriteableMap(report.getFileAttachments()));
1199+
sendEvent(getReactApplicationContext(), "IBGpreSendingHandler", reportParam);
1200+
currentReport = report;
1201+
}
1202+
};
1203+
1204+
Method method = getMethod(Instabug.class, "onReportSubmitHandler_Private", Report.OnReportCreatedListener.class);
1205+
if (method != null) {
1206+
try {
1207+
method.invoke(null, listener);
1208+
} catch (IllegalAccessException e) {
1209+
e.printStackTrace();
1210+
} catch (InvocationTargetException e) {
1211+
e.printStackTrace();
1212+
}
1213+
}
1214+
}
1215+
1216+
@ReactMethod
1217+
public void appendTagToReport(String tag) {
1218+
if (currentReport != null) {
1219+
currentReport.addTag(tag);
1220+
}
1221+
}
1222+
1223+
@ReactMethod
1224+
public void appendConsoleLogToReport(String consoleLog) {
1225+
if (currentReport != null) {
1226+
currentReport.appendToConsoleLogs(consoleLog);
1227+
}
1228+
}
1229+
1230+
@ReactMethod
1231+
public void setUserAttributeToReport(String key, String value) {
1232+
if (currentReport != null) {
1233+
currentReport.setUserAttribute(key, value);
1234+
}
1235+
}
1236+
1237+
@ReactMethod
1238+
public void logDebugToReport(String log) {
1239+
if (currentReport != null) {
1240+
currentReport.logDebug(log);
1241+
}
1242+
}
1243+
1244+
@ReactMethod
1245+
public void logVerboseToReport(String log) {
1246+
if (currentReport != null) {
1247+
currentReport.logVerbose(log);
1248+
}
1249+
}
1250+
1251+
@ReactMethod
1252+
public void logWarnToReport(String log) {
1253+
if (currentReport != null) {
1254+
currentReport.logWarn(log);
1255+
}
1256+
}
1257+
1258+
@ReactMethod
1259+
public void logErrorToReport(String log) {
1260+
if (currentReport != null) {
1261+
currentReport.logError(log);
1262+
}
1263+
}
1264+
1265+
@ReactMethod
1266+
public void logInfoToReport(String log) {
1267+
if (currentReport != null) {
1268+
currentReport.logInfo(log);
1269+
}
1270+
}
1271+
1272+
@ReactMethod
1273+
public void addFileAttachmentWithURLToReport(String urlString, String fileName) {
1274+
if (currentReport != null) {
1275+
Uri uri = Uri.parse(urlString);
1276+
currentReport.addFileAttachment(uri, fileName);
1277+
}
1278+
}
1279+
1280+
@ReactMethod
1281+
public void addFileAttachmentWithDataToReport(String data, String fileName) {
1282+
if (currentReport != null) {
1283+
currentReport.addFileAttachment(data.getBytes(), fileName);
1284+
}
1285+
}
1286+
1287+
@ReactMethod
1288+
public void submitReport() {
1289+
Method method = getMethod(Instabug.class, "setReport", Report.class);
1290+
if (method != null) {
1291+
try {
1292+
method.invoke(null, currentReport);
1293+
currentReport = null;
1294+
} catch (IllegalAccessException e) {
1295+
e.printStackTrace();
1296+
} catch (InvocationTargetException e) {
1297+
e.printStackTrace();
1298+
}
1299+
}
1300+
}
1301+
1302+
@ReactMethod
1303+
public void getReport(Promise promise) {
11881304
try {
1305+
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "getReport");
1306+
if (method != null) {
1307+
Report report = (Report) method.invoke(null);
1308+
WritableMap reportParam = Arguments.createMap();
1309+
reportParam.putArray("tagsArray", convertArrayListToWritableArray(report.getTags()));
1310+
reportParam.putArray("consoleLogs", ReportUtil.parseConsoleLogs(report.getConsoleLog()));
1311+
reportParam.putString("userData", report.getUserData());
1312+
reportParam.putMap("userAttributes", convertFromHashMapToWriteableMap(report.getUserAttributes()));
1313+
reportParam.putMap("fileAttachments", convertFromHashMapToWriteableMap(report.getFileAttachments()));
1314+
promise.resolve(reportParam);
1315+
currentReport = report;
1316+
}
11891317

1190-
Instabug.onReportSubmitHandler(new Report.OnReportCreatedListener() {
1191-
@Override
1192-
public void onReportCreated(Report report) {
1193-
WritableMap reportParam = Arguments.createMap();
1194-
reportParam.putArray("tagsArray", convertArrayListToWritableArray(report.getTags()));
1195-
reportParam.putArray("consoleLogs", convertArrayListToWritableArray(report.getConsoleLog()));
1196-
reportParam.putString("userData", report.getUserData());
1197-
reportParam.putMap("userAttributes", convertFromHashMapToWriteableMap(report.getUserAttributes()));
1198-
reportParam.putMap("fileAttachments", convertFromHashMapToWriteableMap(report.getFileAttachments()));
1199-
sendEvent(getReactApplicationContext(), "IBGpreSendingHandler", reportParam);
1200-
}
1201-
});
1202-
} catch (java.lang.Exception exception) {
1203-
exception.printStackTrace();
1318+
} catch (ClassNotFoundException e) {
1319+
promise.reject(e);
1320+
} catch (IllegalAccessException e) {
1321+
promise.reject(e);
1322+
} catch (InvocationTargetException e) {
1323+
promise.reject(e);
12041324
}
12051325
}
12061326

1327+
12071328
private WritableMap convertFromHashMapToWriteableMap(HashMap hashMap) {
12081329
WritableMap writableMap = new WritableNativeMap();
12091330
for(int i = 0; i < hashMap.size(); i++) {

android/src/main/java/com/instabug/reactlibrary/utils/ArrayUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.facebook.react.bridge.ReadableType;
1111
import com.facebook.react.bridge.WritableArray;
1212

13+
import java.util.ArrayList;
1314
import java.util.Map;
1415

1516
import org.json.JSONArray;
@@ -130,4 +131,15 @@ public static WritableArray toWritableArray(Object[] array) {
130131

131132
return writableArray;
132133
}
134+
135+
public static ArrayList<String> parseReadableArrayOfStrings(ReadableArray readableArray) {
136+
ArrayList<String> array = new ArrayList<>();
137+
for (int i = 0; i < readableArray.size(); i++) {
138+
ReadableType type = readableArray.getType(i);
139+
if (type == ReadableType.String) {
140+
array.add(readableArray.getString(i));
141+
}
142+
}
143+
return array;
144+
}
133145
}

android/src/main/java/com/instabug/reactlibrary/utils/InstabugUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public static Method getMethod(Class clazz, String methodName, Class... paramete
99
for (Method method : methods) {
1010
if (method.getName().equals(methodName) && method.getParameterTypes().length ==
1111
parameterType.length) {
12-
for (int i = 0; i < 2; i++) {
12+
if (parameterType.length == 0) {
13+
method.setAccessible(true);
14+
return method;
15+
}
16+
for (int i = 0; i < parameterType.length; i++) {
1317
if (method.getParameterTypes()[i] == parameterType[i]) {
1418
if (i == method.getParameterTypes().length - 1) {
1519
method.setAccessible(true);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.instabug.reactlibrary.utils;
2+
3+
import android.net.Uri;
4+
5+
import com.facebook.react.bridge.ReadableArray;
6+
import com.facebook.react.bridge.ReadableMap;
7+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
8+
import com.facebook.react.bridge.ReadableType;
9+
import com.facebook.react.bridge.WritableArray;
10+
import com.facebook.react.bridge.WritableMap;
11+
import com.facebook.react.bridge.WritableNativeArray;
12+
import com.instabug.library.model.Report;
13+
import com.instabug.library.model.a;
14+
15+
import org.json.JSONException;
16+
import org.json.JSONObject;
17+
18+
import java.util.ArrayList;
19+
20+
21+
/**
22+
* Created by salmaali on 8/29/18.
23+
*/
24+
25+
public class ReportUtil {
26+
27+
public static Report createReport(ReadableArray tags, ReadableArray consoleLogs, String userData, ReadableMap userAttributes, ReadableMap fileAttachments) {
28+
Report report = new Report();
29+
// map tags
30+
report.addTag(ArrayUtil.parseReadableArrayOfStrings(tags).toArray(new String[0]));
31+
32+
// map consoleLogs
33+
for (int i = 0; i < consoleLogs.size(); i++) {
34+
ReadableType type = consoleLogs.getType(i);
35+
if (type == ReadableType.String) {
36+
report.appendToConsoleLogs(consoleLogs.getString(i));
37+
}
38+
}
39+
40+
// map userData
41+
report.setUserData(userData);
42+
43+
// map userAttributes
44+
ReadableMapKeySetIterator userAttrIterator = userAttributes.keySetIterator();
45+
while (userAttrIterator.hasNextKey()) {
46+
String key = userAttrIterator.nextKey();
47+
ReadableType type = userAttributes.getType(key);
48+
if (type == ReadableType.String) {
49+
report.setUserAttribute(key, userAttributes.getString(key));
50+
}
51+
52+
}
53+
54+
// map fileAttachments
55+
ReadableMapKeySetIterator fileAttachmentsIterator = userAttributes.keySetIterator();
56+
while (fileAttachmentsIterator.hasNextKey()) {
57+
String key = fileAttachmentsIterator.nextKey();
58+
ReadableType type = fileAttachments.getType(key);
59+
if (type == ReadableType.String) {
60+
Uri uri = Uri.parse(key);
61+
report.addFileAttachment(uri, fileAttachments.getString(key));
62+
}
63+
64+
}
65+
66+
return report;
67+
}
68+
69+
public static WritableArray parseConsoleLogs(ArrayList<a> consoleLogs) {
70+
WritableArray writableArray = new WritableNativeArray();
71+
72+
for(int i = 0; i < consoleLogs.size(); i++) {
73+
try {
74+
writableArray.pushString(consoleLogs.get(i).toJson());
75+
} catch (JSONException e) {
76+
e.printStackTrace();
77+
}
78+
79+
}
80+
81+
return writableArray;
82+
}
83+
}

index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export function addFileAttachment(
199199
fileName: string
200200
): void;
201201
export function show(): void;
202-
export function onReportSubmitHandler(preSendingHandler: () => void): void;
202+
export function onReportSubmitHandler(preSendingHandler: (presendingHandler: Report) => void): void;
203203
export function callPrivateApi(
204204
apiName: string,
205205
param: any
@@ -344,3 +344,16 @@ export enum strings {
344344
welcomeMessageLiveWelcomeStepTitle,
345345
welcomeMessageLiveWelcomeStepContent
346346
}
347+
348+
interface Report {
349+
logDebug(log: string);
350+
logVerbose(log: string);
351+
logWarn(log: string);
352+
logError(log: string);
353+
logInfo(log: string);
354+
appendTag(tag: string);
355+
appendConsoleLog(consoleLog: string);
356+
setUserAttribute(key: string, value: string);
357+
addFileAttachmentWithUrl(url: string, filename: string);
358+
addFileAttachmentWithData(data: string, filename: string);
359+
}

0 commit comments

Comments
 (0)