Skip to content

Commit 277165b

Browse files
abdelhamid-f-nasserHeshamMegid
authored andcommitted
feat: support code push (#1079)
Jira ID: MOB-13384
1 parent b10e519 commit 277165b

File tree

12 files changed

+184
-27
lines changed

12 files changed

+184
-27
lines changed

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

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class RNInstabug {
1919

2020
private static RNInstabug instance;
21-
21+
2222
private RNInstabug() {}
2323

2424

@@ -133,4 +133,117 @@ public void setBaseUrlForDeprecationLogs() {
133133
e.printStackTrace();
134134
}
135135
}
136+
137+
public static class Builder {
138+
139+
/**
140+
* Application instance to initialize Instabug.
141+
*/
142+
private Application application;
143+
144+
/**
145+
* The application token obtained from the Instabug dashboard.
146+
*/
147+
private String applicationToken;
148+
149+
/**
150+
* The level of detail in logs that you want to print.
151+
*/
152+
private int logLevel = LogLevel.ERROR;
153+
154+
/**
155+
* The Code Push version to be used for all reports.
156+
*/
157+
private String codePushVersion;
158+
159+
/**
160+
* The events that trigger the SDK's user interface.
161+
*/
162+
private InstabugInvocationEvent[] invocationEvents;
163+
164+
165+
/**
166+
* Initialize Instabug SDK with application token
167+
*
168+
* @param application Application object for initialization of library
169+
* @param applicationToken The app's identifying token, available on your dashboard.
170+
*/
171+
public Builder(Application application, String applicationToken) {
172+
this.application = application;
173+
this.applicationToken = applicationToken;
174+
}
175+
176+
/**
177+
* Initialize Instabug SDK with application token and invocation trigger events
178+
*
179+
* @param application Application object for initialization of library
180+
* @param applicationToken The app's identifying token, available on your dashboard.
181+
* @param invocationEvents The events that trigger the SDK's user interface.
182+
* <p>Choose from the available events listed in {@link InstabugInvocationEvent}.</p>
183+
*/
184+
public Builder(Application application, String applicationToken, InstabugInvocationEvent... invocationEvents) {
185+
this.application = application;
186+
this.applicationToken = applicationToken;
187+
this.invocationEvents = invocationEvents;
188+
}
189+
190+
/**
191+
* Sets the filtering level for printed SDK logs.
192+
*
193+
* @param logLevel The log filtering level to be set.
194+
* Choose from {@link LogLevel} constants:
195+
* {@link LogLevel#NONE}, {@link LogLevel#ERROR}, {@link LogLevel#DEBUG}, or {@link LogLevel#VERBOSE}.
196+
* <p>Default level is {@link LogLevel#ERROR}.</p>
197+
*/
198+
public Builder setLogLevel(int logLevel) {
199+
this.logLevel = logLevel;
200+
return this;
201+
}
202+
203+
/**
204+
* Sets Code Push version to be used for all reports.
205+
*
206+
* @param codePushVersion the Code Push version to work with.
207+
*/
208+
public Builder setCodePushVersion(String codePushVersion) {
209+
this.codePushVersion = codePushVersion;
210+
return this;
211+
}
212+
213+
/**
214+
* Sets the invocation triggering events for the SDK's user interface
215+
*
216+
* @param invocationEvents The events that trigger the SDK's user interface.
217+
* Choose from the available events listed in {@link InstabugInvocationEvent}.
218+
*/
219+
public Builder setInvocationEvents(InstabugInvocationEvent... invocationEvents) {
220+
this.invocationEvents = invocationEvents;
221+
return this;
222+
}
223+
224+
/**
225+
* Builds the Instabug instance with the provided configurations.
226+
*/
227+
public void build() {
228+
try {
229+
RNInstabug.getInstance().setBaseUrlForDeprecationLogs();
230+
RNInstabug.getInstance().setCurrentPlatform();
231+
232+
Instabug.Builder instabugBuilder = new Instabug.Builder(application, applicationToken)
233+
.setInvocationEvents(invocationEvents)
234+
.setSdkDebugLogsLevel(logLevel);
235+
236+
if (codePushVersion != null) {
237+
instabugBuilder.setCodePushVersion(codePushVersion);
238+
}
239+
240+
instabugBuilder.build();
241+
242+
// Temporarily disabling APM hot launches
243+
APM.setHotAppLaunchEnabled(false);
244+
} catch (Exception e) {
245+
e.printStackTrace();
246+
}
247+
}
248+
}
136249
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import java.util.Locale;
5151
import java.util.Map;
5252

53+
import javax.annotation.Nullable;
54+
5355

5456
/**
5557
* The type Rn instabug reactnative module.
@@ -117,9 +119,16 @@ public void run() {
117119
* Initializes the SDK.
118120
* @param token The token that identifies the app. You can find it on your dashboard.
119121
* @param invocationEventValues The events that invoke the SDK's UI.
122+
* @param logLevel The level of detail in logs that you want to print.
123+
* @param codePushVersion The Code Push version to be used for all reports.
120124
*/
121125
@ReactMethod
122-
public void init(final String token, final ReadableArray invocationEventValues, final String logLevel) {
126+
public void init(
127+
final String token,
128+
final ReadableArray invocationEventValues,
129+
final String logLevel,
130+
@Nullable final String codePushVersion
131+
) {
123132
MainThreadHandler.runOnMainThread(new Runnable() {
124133
@Override
125134
public void run() {
@@ -130,7 +139,14 @@ public void run() {
130139

131140
final Application application = (Application) reactContext.getApplicationContext();
132141

133-
RNInstabug.getInstance().init(application, token, parsedLogLevel, invocationEvents);
142+
RNInstabug.Builder builder = new RNInstabug.Builder(application, token)
143+
.setInvocationEvents(invocationEvents)
144+
.setLogLevel(parsedLogLevel);
145+
146+
if(codePushVersion != null) {
147+
builder.setCodePushVersion(codePushVersion);
148+
}
149+
builder.build();
134150
}
135151
});
136152
}

examples/default/ios/InstabugTests/InstabugSampleTests.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ - (void)testSetEnabled {
6565
}
6666

6767
- (void)testInit {
68+
id mock = OCMClassMock([Instabug class]);
6869
IBGInvocationEvent floatingButtonInvocationEvent = IBGInvocationEventFloatingButton;
6970
NSString *appToken = @"app_token";
71+
NSString *codePushVersion = @"1.0.0(1)";
7072
NSArray *invocationEvents = [NSArray arrayWithObjects:[NSNumber numberWithInteger:floatingButtonInvocationEvent], nil];
7173
IBGSDKDebugLogsLevel sdkDebugLogsLevel = IBGSDKDebugLogsLevelDebug;
74+
75+
OCMStub([mock setCodePushVersion:codePushVersion]);
7276

73-
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel];
77+
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel codePushVersion:codePushVersion];
78+
OCMVerify([mock setCodePushVersion:codePushVersion]);
7479

7580
OCMVerify([self.mRNInstabug initWithToken:appToken invocationEvents:floatingButtonInvocationEvent debugLogsLevel:sdkDebugLogsLevel]);
7681
}

examples/default/ios/InstabugTests/RNInstabugTests.m

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@ - (void)testInitWithoutLogsLevel {
4141
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]);
4242
}
4343

44-
- (void)testInitStartsOnce {
45-
NSString *token = @"app-token";
46-
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake;
47-
48-
// Call init twice to check that only 1 call to native methods happens.
49-
[RNInstabug initWithToken:token invocationEvents:invocationEvents];
50-
[RNInstabug initWithToken:token invocationEvents:invocationEvents];
51-
52-
OCMVerify(times(1), [self.mInstabug startWithToken:token invocationEvents:invocationEvents]);
53-
OCMVerify(times(1), [self.mInstabug setCurrentPlatform:IBGPlatformReactNative]);
54-
OCMVerify(times(1), [self.mIBGNetworkLogger disableAutomaticCapturingOfNetworkLogs]);
55-
OCMVerify(times(1), [self.mIBGNetworkLogger setEnabled:YES]);
56-
}
57-
5844
- (void)testInitWithLogsLevel {
5945
NSString *token = @"app-token";
6046
IBGInvocationEvent invocationEvents = IBGInvocationEventFloatingButton | IBGInvocationEventShake;
@@ -68,4 +54,11 @@ - (void)testInitWithLogsLevel {
6854
OCMVerify([self.mIBGNetworkLogger setEnabled:YES]);
6955
}
7056

57+
- (void) testSetCodePushVersion {
58+
NSString *codePushVersion = @"1.0.0(1)";
59+
[RNInstabug setCodePushVersion:codePushVersion];
60+
61+
OCMVerify([self.mInstabug setCodePushVersion:codePushVersion]);
62+
}
63+
7164
@end

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
- (void)setEnabled:(BOOL)isEnabled;
2929

30-
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel;
30+
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel codePushVersion:(NSString *)codePushVersion;
3131

3232
- (void)setUserData:(NSString *)userData;
3333

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ - (dispatch_queue_t)methodQueue {
3737
Instabug.enabled = isEnabled;
3838
}
3939

40-
RCT_EXPORT_METHOD(init:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel) {
40+
RCT_EXPORT_METHOD(init:(NSString *)token
41+
invocationEvents:(NSArray *)invocationEventsArray
42+
debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel
43+
codePushVersion:(NSString *)codePushVersion) {
4144
IBGInvocationEvent invocationEvents = 0;
4245

4346
for (NSNumber *boxedValue in invocationEventsArray) {
4447
invocationEvents |= [boxedValue intValue];
4548
}
4649

47-
[RNInstabug initWithToken:token invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel];
50+
[Instabug setCodePushVersion:codePushVersion];
51+
52+
[RNInstabug initWithToken:token
53+
invocationEvents:invocationEvents
54+
debugLogsLevel:sdkDebugLogsLevel];
4855
}
4956

5057
RCT_EXPORT_METHOD(setReproStepsConfig:(IBGUserStepsMode)bugMode :(IBGUserStepsMode)crashMode:(IBGUserStepsMode)sessionReplayMode) {

ios/RNInstabug/RNInstabug.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents debugLogsLevel:(IBGSDKDebugLogsLevel)debugLogsLevel;
99
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents;
1010

11+
/**
12+
@brief Set codePush version before starting the SDK.
13+
14+
@discussion Sets Code Push version to be used for all reports.
15+
should be called from `-[UIApplicationDelegate application:didFinishLaunchingWithOptions:]`
16+
and before `startWithToken`.
17+
18+
@param codePushVersion the Code Push version to be used for all reports.
19+
*/
20+
+ (void)setCodePushVersion:(NSString *)codePushVersion;
21+
1122
@end
1223

1324
#endif /* RNInstabug_h */

ios/RNInstabug/RNInstabug.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ + (void)reset {
1414
}
1515

1616
+ (void)initWithToken:(NSString *)token invocationEvents:(IBGInvocationEvent)invocationEvents {
17-
// Initialization is performed only once to avoid unexpected behavior.
18-
if (didInit) {
19-
NSLog(@"IBG-RN: Skipped iOS SDK re-initialization");
20-
return;
21-
}
2217

2318
didInit = YES;
2419

@@ -49,6 +44,10 @@ + (void)initWithToken:(NSString *)token
4944
[self initWithToken:token invocationEvents:invocationEvents];
5045
}
5146

47+
+ (void)setCodePushVersion:(NSString *)codePushVersion {
48+
[Instabug setCodePushVersion:codePushVersion];
49+
}
50+
5251
// Note: This function is used to bridge IBGNSLog with RCTLogFunction.
5352
// This log function should not be used externally and is only an implementation detail.
5453
void RNIBGLog(IBGLogLevel logLevel, NSString *format, ...) {

src/models/InstabugConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ export interface InstabugConfig {
1313
* An optional LogLevel to indicate the verbosity of SDK logs. Default is Error.
1414
*/
1515
debugLogsLevel?: LogLevel;
16+
17+
/**
18+
* An optional code push version to be used for all reports.
19+
*/
20+
codePushVersion?: string;
1621
}

src/modules/Instabug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const init = (config: InstabugConfig) => {
6767
config.token,
6868
config.invocationEvents,
6969
config.debugLogsLevel ?? LogLevel.error,
70+
config.codePushVersion,
7071
);
7172

7273
_isFirstScreen = true;

0 commit comments

Comments
 (0)