Skip to content

Commit e0503bb

Browse files
committed
fix: [AND] deprecate start api and introduce init
1 parent 536904c commit e0503bb

File tree

6 files changed

+95
-26
lines changed

6 files changed

+95
-26
lines changed

example/www/js/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
12
document.addEventListener("deviceready", onDeviceReady, false);
23

34
function onDeviceReady() {
45
/** @type {import('instabug-cordova/www/Instabug')} */
56
var Instabug = cordova.require("instabug-cordova.Instabug");
67
/** @type {import('instabug-cordova/www/BugReporting')} */
78
var BugReporting = cordova.require("instabug-cordova.BugReporting");
8-
9+
var ArgsRegistry = cordova.require("instabug-cordova.ArgsRegistry")
910
console.log("Running cordova-" + cordova.platformId + "@" + cordova.version);
1011
document.getElementById("deviceready").classList.add("ready");
12+
Instabug.init({
13+
token: 'cb518c145decef204b0735cb7efa6016',
14+
invocationEvents: [BugReporting.invocationEvents.button],
15+
debugLogsLevel: ArgsRegistry.logLeve.verbose,
16+
});
1117

12-
// Initialize Instabug SDK
13-
Instabug.start(
14-
"cb518c145decef204b0735cb7efa6016",
15-
[BugReporting.invocationEvents.button],
16-
() => console.log("Instabug initialized."),
17-
(error) => console.log("Instabug could not be initialized - " + error)
18-
);
1918
}

example/yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ fill-range@^7.0.1:
239239

240240
fs-extra@^10.1.0:
241241
version "10.1.0"
242+
resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz"
243+
integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==
242244
dependencies:
243245
graceful-fs "^4.2.0"
244246
jsonfile "^6.0.1"

src/android/IBGPlugin.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.instabug.library.invocation.util.InstabugFloatingButtonEdge;
3030
import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition;
3131
import com.instabug.library.logging.InstabugLog;
32+
import com.instabug.library.LogLevel;
3233
import com.instabug.library.model.Report;
3334
import com.instabug.library.ui.onboarding.WelcomeMessage;
3435
import com.instabug.survey.callbacks.*;
@@ -135,6 +136,44 @@ public void start(final CallbackContext callbackContext, JSONArray args) {
135136
callbackContext.error(errorMsg);
136137
}
137138
}
139+
/**
140+
* Initializes Instabug.
141+
*
142+
* @param callbackContext Used when calling back into JavaScript
143+
*/
144+
public void init(final CallbackContext callbackContext, JSONArray args) {
145+
if (args == null || args.length() == 0) {
146+
callbackContext.error("Initialization parameters are required.");
147+
return;
148+
}
149+
150+
try {
151+
JSONObject options = args.getJSONObject(0); // Extract the JSON object from the array
152+
153+
String token = options.optString("token");
154+
JSONArray invocationEventsArray = options.optJSONArray("invocationEvents");
155+
String logLevel = options.optString("debugLogsLevel", "none"); // Default to "none" if missing
156+
157+
String[] invocationEventsNames = toStringArray(invocationEventsArray);
158+
InstabugInvocationEvent[] invocationEvents = new InstabugInvocationEvent[invocationEventsNames.length];
159+
160+
for (int i = 0; i < invocationEventsNames.length; i++) {
161+
invocationEvents[i] = parseInvocationEvent(invocationEventsNames[i]);
162+
}
163+
164+
final Application application = (Application) context.getApplicationContext();
165+
new Instabug.Builder(application, token)
166+
.setInvocationEvents(invocationEvents)
167+
.setSdkDebugLogsLevel(parseLogLevel(logLevel)) // Set the log level properly
168+
.build();
169+
170+
callbackContext.success();
171+
} catch (JSONException e) {
172+
callbackContext.error("Invalid JSON format: " + e.getMessage());
173+
} catch (IllegalStateException e) {
174+
callbackContext.error(errorMsg);
175+
}
176+
}
138177

139178
public final void show(final CallbackContext callbackContext) {
140179
try {
@@ -1196,7 +1235,18 @@ public static InstabugInvocationEvent parseInvocationEvent(String event) {
11961235
} else
11971236
return null;
11981237
}
1199-
1238+
public static int parseLogLevel(String log) {
1239+
if ("none".equals(log)) {
1240+
return LogLevel.NONE;
1241+
} else if ("debug".equals(log)) {
1242+
return LogLevel.DEBUG;
1243+
} else if ("error".equals(log)) {
1244+
return LogLevel.ERROR;
1245+
} else if ("verbose".equals(log)) {
1246+
return LogLevel.VERBOSE;
1247+
}else
1248+
return -1;
1249+
}
12001250
/**
12011251
* Convenience method for converting string to InvocationOption.
12021252
*

src/modules/ArgsRegistry.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,12 @@ namespace ArgsRegistry {
128128
swedish = "swedish",
129129
turkish = "turkish",
130130
}
131+
132+
export enum logLeve {
133+
none = "none",
134+
debug = "debug",
135+
error = "error",
136+
verbose = "verbose",
137+
}
131138
}
132139
export = ArgsRegistry;

src/modules/BugReporting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace BugReporting {
2626
swipe = "swipe",
2727
none = "none",
2828
}
29-
29+
3030
export enum extendedBugReportMode {
3131
enabledWithRequiredFields = "enabledWithRequiredFields",
3232
enabledWithOptionalFields = "enabledWithOptionalFields",

src/modules/Instabug.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { exec } from "./IBGCordova";
22
import registry from "./ArgsRegistry";
33
import bugReporting from "./BugReporting";
4+
import ArgsRegistry from "./ArgsRegistry";
45

56
namespace Instabug {
67
export const welcomeMessageMode = registry.welcomeMessageMode;
@@ -11,6 +12,7 @@ namespace Instabug {
1112
export const locale = registry.locale;
1213

1314
/**
15+
* @deprecated This method is deprecated and will be removed in a future version. Use `init` instead.
1416
* Starts the SDK.
1517
* This is the main SDK method that does all the magic. This is the only
1618
* method that SHOULD be called.
@@ -32,6 +34,31 @@ namespace Instabug {
3234
exec("IBGPlugin", "start", [token, invocationEvents], success, error);
3335
};
3436

37+
/**
38+
* Initializes the Instabug SDK with additional configurations.
39+
*
40+
* @param token The token that identifies the app, available on your dashboard.
41+
* @param invocationEvents An array of invocation events that trigger the SDK's UI.
42+
* @param logLevel The level of detail in logs that you want to print.
43+
* @param success Callback on function success.
44+
* @param error Callback on function error.
45+
*/
46+
export const init = (
47+
token: string,
48+
invocationEvents: bugReporting.invocationEvents[],
49+
logLevel: ArgsRegistry.logLeve,
50+
success?: () => void,
51+
error?: (err: any) => void
52+
) => {
53+
exec(
54+
"IBGPlugin", // Plugin name
55+
"init", // Action name
56+
[token, invocationEvents, logLevel], // Arguments
57+
success,
58+
error
59+
);
60+
};
61+
3562
/**
3663
* Shows default Instabug prompt.
3764
*
@@ -379,22 +406,6 @@ export const setReproStepsConfig = (
379406
exec("IBGPlugin", "logOut", [], success, error);
380407
};
381408

382-
/**
383-
* Enable/Disable debug logs from Instabug SDK
384-
* Default state: disabled
385-
*
386-
* @param isDebugEnabled a boolean to control whether debug logs should be printed or not into LogCat.
387-
* @param success callback on function success.
388-
* @param error callback on function error.
389-
*/
390-
export const setDebugEnabled = (
391-
isDebugEnabled: boolean,
392-
success?: () => void,
393-
error?: (err: any) => void
394-
) => {
395-
exec("IBGPlugin", "setDebugEnabled", [isDebugEnabled], success, error);
396-
};
397-
398409
/**
399410
* Sets the SDK's locale.
400411
* Use to change the SDK's UI to different language.

0 commit comments

Comments
 (0)