Skip to content

Commit 8f6e67b

Browse files
author
Ali Abdelfattah
authored
Merge pull request #107 from Instabug/feature/color-theme-api
[IBGBUGCHAT-10513] Map API: setColorTheme
2 parents e218630 + 17cd2ba commit 8f6e67b

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

sampleApp/platforms/android/app/src/test/java/com/instabug/cordova/plugin/IBGPluginTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.webkit.WebChromeClient;
1010

1111
import com.instabug.library.Instabug;
12+
import com.instabug.library.InstabugColorTheme;
1213

1314
import org.apache.cordova.CallbackContext;
1415
import org.apache.cordova.CordovaInterface;
@@ -253,5 +254,20 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
253254
Instabug.setPrimaryColor(color);
254255
}
255256

257+
@Test
258+
public void givenArg$setColorTheme_whenQuery_thenShouldCallNativeApiWithArg() {
259+
// given
260+
PowerMockito.mockStatic(Instabug.class);
261+
String colorTheme = "dark";
262+
JSONArray args = new JSONArray();
263+
args.put(colorTheme);
264+
265+
// when
266+
cordovaModule.execute("setColorTheme", args, callbackContext);
267+
268+
// then
269+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
270+
Instabug.setColorTheme(InstabugColorTheme.InstabugColorThemeDark);
271+
}
256272

257273
}

sampleApp/platforms/android/platform_www/plugins/instabug-cordova/www/instabug.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ var getLocales = function () {
4949
};
5050
};
5151

52+
var getColorThemes = function () {
53+
return {
54+
light: 'light',
55+
dark: 'dark',
56+
};
57+
};
58+
5259
var Instabug = function () {
5360
};
5461

@@ -79,7 +86,7 @@ Instabug.startWithToken = function (token, events, options, success, error) {
7986
if (validatedEvents !== undefined || validatedEvents.length != 0) {
8087
exec(success, error, 'IBGPlugin', 'startWithToken', [token, validatedEvents, options]);
8188
} else {
82-
console.log('Could not activate Instabug - invocation event is not valid.');
89+
console.log('Could not activate Instabug - invocation event is not valid.');
8390
}
8491
};
8592

@@ -222,6 +229,16 @@ Instabug.setLocale = function (locale, success, error) {
222229
}
223230
};
224231

232+
Instabug.setColorTheme = function (colorTheme, success, error) {
233+
var validatedColorTheme = getColorThemes()[colorTheme];
234+
235+
if (validatedColorTheme) {
236+
exec(success, error, 'IBGPlugin', 'setColorTheme', [validatedColorTheme]);
237+
} else {
238+
console.log('Could not set color theme - "' + colorTheme + '" is not valid.');
239+
}
240+
};
241+
225242
module.exports = Instabug;
226243

227244
});

src/android/IBGPlugin.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.instabug.featuresrequest.FeatureRequests;
1717
import com.instabug.library.Feature;
1818
import com.instabug.library.Instabug;
19+
import com.instabug.library.InstabugColorTheme;
1920
import com.instabug.library.OnSdkDismissCallback;
2021
import com.instabug.library.extendedbugreport.ExtendedBugReport;
2122
import com.instabug.library.internal.module.InstabugLocale;
@@ -466,6 +467,40 @@ public void run() {
466467
callbackContext.error("A colorInt must be provided.");
467468
}
468469

470+
/**
471+
* Convenience method for parsing and setting
472+
* whether the desired color theme for the SDK
473+
* invocation.
474+
*
475+
* @param callbackContext Used when calling back into JavaScript
476+
* @param args .optString(0) Theme
477+
*
478+
*/
479+
public void setColorTheme(final CallbackContext callbackContext, final JSONArray args) {
480+
final String colorTheme = args.optString(0);
481+
if (colorTheme != null) {
482+
try {
483+
new Handler(Looper.getMainLooper()).post(new Runnable() {
484+
@Override
485+
public void run() {
486+
if ("dark".equals(colorTheme)) {
487+
Instabug.setColorTheme(InstabugColorTheme.InstabugColorThemeDark);
488+
} else if ("light".equals(colorTheme)) {
489+
Instabug.setColorTheme(InstabugColorTheme.InstabugColorThemeLight);
490+
} else {
491+
callbackContext.error("Color theme value is not valid.");
492+
}
493+
}
494+
});
495+
} catch (IllegalStateException e) {
496+
e.printStackTrace();
497+
callbackContext.error(errorMsg);
498+
}
499+
} else {
500+
callbackContext.error("A color theme must be provided.");
501+
}
502+
}
503+
469504
/**
470505
* Sets the threshold value of the shake gesture on the device
471506
*

src/ios/IBGPlugin.m

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ - (void) setWelcomeMessageMode:(NSString*)welcomeMessageMode
10751075
* @param {NSString*} theme
10761076
* NSString representation of color theme
10771077
*/
1078-
- (void) setColorTheme:(NSString*)theme
1078+
- (void) setColorThemeInOptions:(NSString*)theme
10791079
{
10801080
if ([theme isEqualToString:@"dark"]) {
10811081
[Instabug setColorTheme:IBGColorThemeDark];
@@ -1084,6 +1084,36 @@ - (void) setColorTheme:(NSString*)theme
10841084
}
10851085
}
10861086

1087+
/**
1088+
* Sets the SDK color theme
1089+
*
1090+
* @param {CDVInvokedUrlCommand*} command
1091+
* The command sent from JavaScript
1092+
*/
1093+
- (void) setColorTheme:(CDVInvokedUrlCommand*)command
1094+
{
1095+
CDVPluginResult* result;
1096+
NSString* theme = [command argumentAtIndex:0];
1097+
1098+
if ([theme length] > 0) {
1099+
if ([theme isEqualToString:@"dark"]) {
1100+
[Instabug setColorTheme:IBGColorThemeDark];
1101+
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
1102+
} else if ([theme isEqualToString:@"light"]) {
1103+
[Instabug setColorTheme:IBGColorThemeLight];
1104+
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
1105+
} else {
1106+
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
1107+
messageAsString:@"Color theme value is not valid."];
1108+
}
1109+
} else {
1110+
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
1111+
messageAsString:@"Color theme must be provided."];
1112+
}
1113+
1114+
[self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
1115+
}
1116+
10871117
/**
10881118
* Sets a threshold for numbers of sessions and another for number of days
10891119
* required before a survey, that has been dismissed once, would show again.
@@ -1244,7 +1274,7 @@ - (void) applyOptions:(NSDictionary*)options
12441274
[self setTrackingUserStepsEnabled:[[options objectForKey:@"enableTrackingUserSteps"] stringValue]];
12451275
[self setPushNotificationsEnabled:[[options objectForKey:@"enablePushNotifications"] stringValue]];
12461276
[self setSessionProfilerEnabled:[[options objectForKey:@"enableSessionProfiler"] stringValue]];
1247-
[self setColorTheme:[options objectForKey:@"colorTheme"]];
1277+
[self setColorThemeInOptions:[options objectForKey:@"colorTheme"]];
12481278
[self setWelcomeMessageMode:[options objectForKey:@"welcomeMessageMode"]];
12491279
}
12501280

0 commit comments

Comments
 (0)