Skip to content

Commit c828d0a

Browse files
committed
fix: refactor redundant code
1 parent c5409f0 commit c828d0a

File tree

2 files changed

+72
-144
lines changed

2 files changed

+72
-144
lines changed

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

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,32 +1394,9 @@ public void run() {
13941394
if (themeConfig.hasKey("ctaTextStyle")) {
13951395
builder.setCtaTextStyle(getTextStyle(themeConfig, "ctaTextStyle"));
13961396
}
1397-
if (themeConfig.hasKey("primaryFontPath") || themeConfig.hasKey("primaryFontAsset")) {
1398-
Typeface primaryTypeface = getTypeface(themeConfig, "primaryFontPath", "primaryFontAsset");
1399-
if (primaryTypeface != null) {
1400-
builder.setPrimaryTextFont(primaryTypeface);
1401-
} else {
1402-
Log.e("InstabugModule", "Failed to load primary font");
1403-
}
1404-
}
1405-
1406-
if (themeConfig.hasKey("secondaryFontPath") || themeConfig.hasKey("secondaryFontAsset")) {
1407-
Typeface secondaryTypeface = getTypeface(themeConfig, "secondaryFontPath", "secondaryFontAsset");
1408-
if (secondaryTypeface != null) {
1409-
builder.setSecondaryTextFont(secondaryTypeface);
1410-
} else {
1411-
Log.e("InstabugModule", "Failed to load secondary font");
1412-
}
1413-
}
1414-
1415-
if (themeConfig.hasKey("ctaFontPath") || themeConfig.hasKey("ctaFontAsset")) {
1416-
Typeface ctaTypeface = getTypeface(themeConfig, "ctaFontPath", "ctaFontAsset");
1417-
if (ctaTypeface != null) {
1418-
builder.setCtaTextFont(ctaTypeface);
1419-
} else {
1420-
Log.e("InstabugModule", "Failed to load CTA font");
1421-
}
1422-
}
1397+
setFontIfPresent(themeConfig, builder, "primaryFontPath", "primaryFontAsset", "primary");
1398+
setFontIfPresent(themeConfig, builder, "secondaryFontPath", "secondaryFontAsset", "secondary");
1399+
setFontIfPresent(themeConfig, builder, "ctaFontPath", "ctaFontAsset", "CTA");
14231400

14241401
IBGTheme theme = builder.build();
14251402
Instabug.setTheme(theme);
@@ -1479,6 +1456,37 @@ private int getTextStyle(ReadableMap map, String key) {
14791456
return Typeface.NORMAL;
14801457
}
14811458

1459+
/**
1460+
* Sets a font on the theme builder if the font configuration is present in the theme config.
1461+
*
1462+
* @param themeConfig The theme configuration map
1463+
* @param builder The theme builder
1464+
* @param fileKey The key for font file path
1465+
* @param assetKey The key for font asset path
1466+
* @param fontType The type of font (for logging purposes)
1467+
*/
1468+
private void setFontIfPresent(ReadableMap themeConfig, com.instabug.library.model.IBGTheme.Builder builder,
1469+
String fileKey, String assetKey, String fontType) {
1470+
if (themeConfig.hasKey(fileKey) || themeConfig.hasKey(assetKey)) {
1471+
Typeface typeface = getTypeface(themeConfig, fileKey, assetKey);
1472+
if (typeface != null) {
1473+
switch (fontType) {
1474+
case "primary":
1475+
builder.setPrimaryTextFont(typeface);
1476+
break;
1477+
case "secondary":
1478+
builder.setSecondaryTextFont(typeface);
1479+
break;
1480+
case "CTA":
1481+
builder.setCtaTextFont(typeface);
1482+
break;
1483+
}
1484+
} else {
1485+
Log.e("InstabugModule", "Failed to load " + fontType + " font");
1486+
}
1487+
}
1488+
}
1489+
14821490
private Typeface getTypeface(ReadableMap map, String fileKey, String assetKey) {
14831491
try {
14841492
if (fileKey != null && map.hasKey(fileKey) && !map.isNull(fileKey)) {

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 38 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -176,134 +176,54 @@ - (dispatch_queue_t)methodQueue {
176176
RCT_EXPORT_METHOD(setTheme:(NSDictionary *)themeConfig) {
177177
IBGTheme *theme = [[IBGTheme alloc] init];
178178

179-
if (themeConfig[@"primaryColor"]) {
180-
NSString *colorString = themeConfig[@"primaryColor"];
181-
UIColor *color = [self colorFromHexString:colorString];
182-
if (color) {
183-
theme.primaryColor = color;
184-
}
185-
}
186-
187-
if (themeConfig[@"backgroundColor"]) {
188-
NSString *colorString = themeConfig[@"backgroundColor"];
189-
UIColor *color = [self colorFromHexString:colorString];
190-
if (color) {
191-
theme.backgroundColor = color;
192-
}
193-
}
194-
195-
if (themeConfig[@"titleTextColor"]) {
196-
NSString *colorString = themeConfig[@"titleTextColor"];
197-
UIColor *color = [self colorFromHexString:colorString];
198-
if (color) {
199-
theme.titleTextColor = color;
200-
}
201-
}
202-
203-
if (themeConfig[@"subtitleTextColor"]) {
204-
NSString *colorString = themeConfig[@"subtitleTextColor"];
205-
UIColor *color = [self colorFromHexString:colorString];
206-
if (color) {
207-
theme.subtitleTextColor = color;
208-
}
209-
}
210-
211-
if (themeConfig[@"primaryTextColor"]) {
212-
NSString *colorString = themeConfig[@"primaryTextColor"];
213-
UIColor *color = [self colorFromHexString:colorString];
214-
if (color) {
215-
theme.primaryTextColor = color;
216-
}
217-
}
218-
219-
if (themeConfig[@"secondaryTextColor"]) {
220-
NSString *colorString = themeConfig[@"secondaryTextColor"];
221-
UIColor *color = [self colorFromHexString:colorString];
222-
if (color) {
223-
theme.secondaryTextColor = color;
224-
}
225-
}
226-
227-
if (themeConfig[@"callToActionTextColor"]) {
228-
NSString *colorString = themeConfig[@"callToActionTextColor"];
229-
UIColor *color = [self colorFromHexString:colorString];
230-
if (color) {
231-
theme.callToActionTextColor = color;
232-
}
233-
}
234-
235-
if (themeConfig[@"headerBackgroundColor"]) {
236-
NSString *colorString = themeConfig[@"headerBackgroundColor"];
237-
UIColor *color = [self colorFromHexString:colorString];
238-
if (color) {
239-
theme.headerBackgroundColor = color;
240-
}
241-
}
242-
243-
if (themeConfig[@"footerBackgroundColor"]) {
244-
NSString *colorString = themeConfig[@"footerBackgroundColor"];
245-
UIColor *color = [self colorFromHexString:colorString];
246-
if (color) {
247-
theme.footerBackgroundColor = color;
248-
}
249-
}
250-
251-
if (themeConfig[@"rowBackgroundColor"]) {
252-
NSString *colorString = themeConfig[@"rowBackgroundColor"];
253-
UIColor *color = [self colorFromHexString:colorString];
254-
if (color) {
255-
theme.rowBackgroundColor = color;
256-
}
257-
}
258-
259-
if (themeConfig[@"selectedRowBackgroundColor"]) {
260-
NSString *colorString = themeConfig[@"selectedRowBackgroundColor"];
261-
UIColor *color = [self colorFromHexString:colorString];
262-
if (color) {
263-
theme.selectedRowBackgroundColor = color;
264-
}
265-
}
266-
267-
if (themeConfig[@"rowSeparatorColor"]) {
268-
NSString *colorString = themeConfig[@"rowSeparatorColor"];
269-
UIColor *color = [self colorFromHexString:colorString];
270-
if (color) {
271-
theme.rowSeparatorColor = color;
272-
}
273-
}
179+
NSDictionary *colorMapping = @{
180+
@"primaryColor": ^(UIColor *color) { theme.primaryColor = color; },
181+
@"backgroundColor": ^(UIColor *color) { theme.backgroundColor = color; },
182+
@"titleTextColor": ^(UIColor *color) { theme.titleTextColor = color; },
183+
@"subtitleTextColor": ^(UIColor *color) { theme.subtitleTextColor = color; },
184+
@"primaryTextColor": ^(UIColor *color) { theme.primaryTextColor = color; },
185+
@"secondaryTextColor": ^(UIColor *color) { theme.secondaryTextColor = color; },
186+
@"callToActionTextColor": ^(UIColor *color) { theme.callToActionTextColor = color; },
187+
@"headerBackgroundColor": ^(UIColor *color) { theme.headerBackgroundColor = color; },
188+
@"footerBackgroundColor": ^(UIColor *color) { theme.footerBackgroundColor = color; },
189+
@"rowBackgroundColor": ^(UIColor *color) { theme.rowBackgroundColor = color; },
190+
@"selectedRowBackgroundColor": ^(UIColor *color) { theme.selectedRowBackgroundColor = color; },
191+
@"rowSeparatorColor": ^(UIColor *color) { theme.rowSeparatorColor = color; }
192+
};
274193

275-
// Set fonts
276-
if (themeConfig[@"primaryFontPath"]) {
277-
NSString *fontName = themeConfig[@"primaryFontPath"];
278-
NSString *fileName = [fontName lastPathComponent];
279-
NSString *nameWithoutExtension = [fileName stringByDeletingPathExtension];
280-
UIFont *font = [UIFont fontWithName:nameWithoutExtension size:17.0];
281-
if (font) {
282-
theme.primaryTextFont = font;
194+
for (NSString *key in colorMapping) {
195+
if (themeConfig[key]) {
196+
NSString *colorString = themeConfig[key];
197+
UIColor *color = [self colorFromHexString:colorString];
198+
if (color) {
199+
void (^setter)(UIColor *) = colorMapping[key];
200+
setter(color);
201+
}
283202
}
284203
}
285204

286-
if (themeConfig[@"secondaryFontPath"]) {
287-
NSString *fontName = themeConfig[@"secondaryFontPath"];
288-
NSString *fileName = [fontName lastPathComponent];
289-
NSString *nameWithoutExtension = [fileName stringByDeletingPathExtension];
290-
UIFont *font = [UIFont fontWithName:nameWithoutExtension size:17.0];
291-
if (font) {
292-
theme.secondaryTextFont = font;
293-
}
294-
}
205+
[self setFontIfPresent:themeConfig[@"primaryFontPath"] forTheme:theme type:@"primary"];
206+
[self setFontIfPresent:themeConfig[@"secondaryFontPath"] forTheme:theme type:@"secondary"];
207+
[self setFontIfPresent:themeConfig[@"ctaFontPath"] forTheme:theme type:@"cta"];
295208

296-
if (themeConfig[@"ctaFontPath"]) {
297-
NSString *fontName = themeConfig[@"ctaFontPath"];
298-
NSString *fileName = [fontName lastPathComponent];
209+
Instabug.theme = theme;
210+
}
211+
212+
- (void)setFontIfPresent:(NSString *)fontPath forTheme:(IBGTheme *)theme type:(NSString *)type {
213+
if (fontPath) {
214+
NSString *fileName = [fontPath lastPathComponent];
299215
NSString *nameWithoutExtension = [fileName stringByDeletingPathExtension];
300216
UIFont *font = [UIFont fontWithName:nameWithoutExtension size:17.0];
301217
if (font) {
302-
theme.callToActionTextFont = font;
218+
if ([type isEqualToString:@"primary"]) {
219+
theme.primaryTextFont = font;
220+
} else if ([type isEqualToString:@"secondary"]) {
221+
theme.secondaryTextFont = font;
222+
} else if ([type isEqualToString:@"cta"]) {
223+
theme.callToActionTextFont = font;
224+
}
303225
}
304226
}
305-
306-
Instabug.theme = theme;
307227
}
308228

309229
- (UIColor *)colorFromHexString:(NSString *)hexString {

0 commit comments

Comments
 (0)