@@ -176,134 +176,54 @@ - (dispatch_queue_t)methodQueue {
176176RCT_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