Skip to content

Commit 4214a27

Browse files
committed
fix: refactor functions
1 parent c828d0a commit 4214a27

File tree

1 file changed

+101
-57
lines changed

1 file changed

+101
-57
lines changed

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

Lines changed: 101 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,31 +1369,17 @@ public void run() {
13691369
try {
13701370
com.instabug.library.model.IBGTheme.Builder builder = new com.instabug.library.model.IBGTheme.Builder();
13711371

1372-
if (themeConfig.hasKey("primaryColor")) {
1373-
builder.setPrimaryColor(getColor(themeConfig, "primaryColor"));
1374-
}
1375-
if (themeConfig.hasKey("secondaryTextColor")) {
1376-
builder.setSecondaryTextColor(getColor(themeConfig, "secondaryTextColor"));
1377-
}
1378-
if (themeConfig.hasKey("primaryTextColor")) {
1379-
builder.setPrimaryTextColor(getColor(themeConfig, "primaryTextColor"));
1380-
}
1381-
if (themeConfig.hasKey("titleTextColor")) {
1382-
builder.setTitleTextColor(getColor(themeConfig, "titleTextColor"));
1383-
}
1384-
if (themeConfig.hasKey("backgroundColor")) {
1385-
builder.setBackgroundColor(getColor(themeConfig, "backgroundColor"));
1386-
}
1387-
1388-
if (themeConfig.hasKey("primaryTextStyle")) {
1389-
builder.setPrimaryTextStyle(getTextStyle(themeConfig, "primaryTextStyle"));
1390-
}
1391-
if (themeConfig.hasKey("secondaryTextStyle")) {
1392-
builder.setSecondaryTextStyle(getTextStyle(themeConfig, "secondaryTextStyle"));
1393-
}
1394-
if (themeConfig.hasKey("ctaTextStyle")) {
1395-
builder.setCtaTextStyle(getTextStyle(themeConfig, "ctaTextStyle"));
1396-
}
1372+
// Apply colors
1373+
applyColorIfPresent(themeConfig, builder, "primaryColor", (b, color) -> b.setPrimaryColor(color));
1374+
applyColorIfPresent(themeConfig, builder, "secondaryTextColor", (b, color) -> b.setSecondaryTextColor(color));
1375+
applyColorIfPresent(themeConfig, builder, "primaryTextColor", (b, color) -> b.setPrimaryTextColor(color));
1376+
applyColorIfPresent(themeConfig, builder, "titleTextColor", (b, color) -> b.setTitleTextColor(color));
1377+
applyColorIfPresent(themeConfig, builder, "backgroundColor", (b, color) -> b.setBackgroundColor(color));
1378+
1379+
// Apply text styles
1380+
applyTextStyleIfPresent(themeConfig, builder, "primaryTextStyle", (b, style) -> b.setPrimaryTextStyle(style));
1381+
applyTextStyleIfPresent(themeConfig, builder, "secondaryTextStyle", (b, style) -> b.setSecondaryTextStyle(style));
1382+
applyTextStyleIfPresent(themeConfig, builder, "ctaTextStyle", (b, style) -> b.setCtaTextStyle(style));
13971383
setFontIfPresent(themeConfig, builder, "primaryFontPath", "primaryFontAsset", "primary");
13981384
setFontIfPresent(themeConfig, builder, "secondaryFontPath", "secondaryFontAsset", "secondary");
13991385
setFontIfPresent(themeConfig, builder, "ctaFontPath", "ctaFontAsset", "CTA");
@@ -1456,6 +1442,40 @@ private int getTextStyle(ReadableMap map, String key) {
14561442
return Typeface.NORMAL;
14571443
}
14581444

1445+
1446+
1447+
/**
1448+
* Applies a color to the theme builder if present in the configuration.
1449+
*
1450+
* @param themeConfig The theme configuration map
1451+
* @param builder The theme builder
1452+
* @param key The configuration key
1453+
* @param setter The color setter function
1454+
*/
1455+
private void applyColorIfPresent(ReadableMap themeConfig, com.instabug.library.model.IBGTheme.Builder builder,
1456+
String key, java.util.function.BiConsumer<com.instabug.library.model.IBGTheme.Builder, Integer> setter) {
1457+
if (themeConfig.hasKey(key)) {
1458+
int color = getColor(themeConfig, key);
1459+
setter.accept(builder, color);
1460+
}
1461+
}
1462+
1463+
/**
1464+
* Applies a text style to the theme builder if present in the configuration.
1465+
*
1466+
* @param themeConfig The theme configuration map
1467+
* @param builder The theme builder
1468+
* @param key The configuration key
1469+
* @param setter The text style setter function
1470+
*/
1471+
private void applyTextStyleIfPresent(ReadableMap themeConfig, com.instabug.library.model.IBGTheme.Builder builder,
1472+
String key, java.util.function.BiConsumer<com.instabug.library.model.IBGTheme.Builder, Integer> setter) {
1473+
if (themeConfig.hasKey(key)) {
1474+
int style = getTextStyle(themeConfig, key);
1475+
setter.accept(builder, style);
1476+
}
1477+
}
1478+
14591479
/**
14601480
* Sets a font on the theme builder if the font configuration is present in the theme config.
14611481
*
@@ -1487,45 +1507,69 @@ private void setFontIfPresent(ReadableMap themeConfig, com.instabug.library.mode
14871507
}
14881508
}
14891509

1490-
private Typeface getTypeface(ReadableMap map, String fileKey, String assetKey) {
1491-
try {
1492-
if (fileKey != null && map.hasKey(fileKey) && !map.isNull(fileKey)) {
1493-
String fontPath = map.getString(fileKey);
1494-
String fileName = getFileName(fontPath);
1495-
1496-
try {
1497-
Typeface typeface = Typeface.create(fileName, Typeface.NORMAL);
1498-
if (typeface != null && !typeface.equals(Typeface.DEFAULT)) {
1499-
return typeface;
1500-
}
1501-
} catch (Exception e) {
1502-
e.printStackTrace();
1503-
}
1504-
1505-
try {
1506-
Typeface typeface = Typeface.createFromAsset(getReactApplicationContext().getAssets(), "fonts/" + fileName);
1510+
/**
1511+
* Loads a Typeface from a file path.
1512+
*
1513+
* @param fileName The filename to load
1514+
* @return The loaded Typeface or null if failed
1515+
*/
1516+
private Typeface loadTypefaceFromFile(String fileName) {
1517+
try {
1518+
Typeface typeface = Typeface.create(fileName, Typeface.NORMAL);
1519+
if (typeface != null && !typeface.equals(Typeface.DEFAULT)) {
15071520
return typeface;
1508-
} catch (Exception e) {
1509-
e.printStackTrace();
15101521
}
1522+
} catch (Exception e) {
1523+
e.printStackTrace();
15111524
}
1525+
return null;
1526+
}
15121527

1513-
if (assetKey != null && map.hasKey(assetKey) && !map.isNull(assetKey)) {
1514-
String assetPath = map.getString(assetKey);
1515-
String fileName = getFileName(assetPath);
1516-
try {
1517-
Typeface typeface = Typeface.createFromAsset(getReactApplicationContext().getAssets(), "fonts/" + fileName);
1518-
return typeface;
1519-
} catch (Exception e) {
1520-
e.printStackTrace();
1521-
}
1528+
/**
1529+
* Loads a Typeface from assets.
1530+
*
1531+
* @param fileName The filename in assets/fonts/ directory
1532+
* @return The loaded Typeface or null if failed
1533+
*/
1534+
private Typeface loadTypefaceFromAssets(String fileName) {
1535+
try {
1536+
return Typeface.createFromAsset(getReactApplicationContext().getAssets(), "fonts/" + fileName);
1537+
} catch (Exception e) {
1538+
e.printStackTrace();
1539+
return null;
15221540
}
1523-
} catch (Exception e) {
1524-
e.printStackTrace();
15251541
}
15261542

1527-
return Typeface.DEFAULT;
1528-
}
1543+
private Typeface getTypeface(ReadableMap map, String fileKey, String assetKey) {
1544+
try {
1545+
if (fileKey != null && map.hasKey(fileKey) && !map.isNull(fileKey)) {
1546+
String fontPath = map.getString(fileKey);
1547+
String fileName = getFileName(fontPath);
1548+
1549+
// Try loading from file first
1550+
Typeface typeface = loadTypefaceFromFile(fileName);
1551+
if (typeface != null) {
1552+
return typeface;
1553+
}
1554+
1555+
// Try loading from assets
1556+
typeface = loadTypefaceFromAssets(fileName);
1557+
if (typeface != null) {
1558+
return typeface;
1559+
}
1560+
}
1561+
1562+
if (assetKey != null && map.hasKey(assetKey) && !map.isNull(assetKey)) {
1563+
String assetPath = map.getString(assetKey);
1564+
String fileName = getFileName(assetPath);
1565+
return loadTypefaceFromAssets(fileName);
1566+
}
1567+
} catch (Exception e) {
1568+
e.printStackTrace();
1569+
}
1570+
1571+
return Typeface.DEFAULT;
1572+
}
15291573

15301574
/**
15311575
* Extracts the filename from a path, removing any directory prefixes.

0 commit comments

Comments
 (0)