Skip to content

Commit cd807c4

Browse files
committed
fix: resolve comments
1 parent 20add37 commit cd807c4

File tree

5 files changed

+114
-73
lines changed

5 files changed

+114
-73
lines changed

android/src/main/java/com/instabug/flutter/modules/InstabugApi.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -638,29 +638,27 @@ private void setFontIfPresent(Map<String, Object> themeConfig, com.instabug.libr
638638
}
639639

640640
private Typeface getTypeface(Map<String, Object> map, String fileKey, String assetKey) {
641+
String fontName = null;
642+
643+
if (assetKey != null && map.containsKey(assetKey) && map.get(assetKey) != null) {
644+
fontName = (String) map.get(assetKey);
645+
} else if (fileKey != null && map.containsKey(fileKey) && map.get(fileKey) != null) {
646+
fontName = (String) map.get(fileKey);
647+
}
648+
649+
if (fontName == null) {
650+
return Typeface.DEFAULT;
651+
}
652+
641653
try {
642-
String fontName = null;
643-
644-
if (assetKey != null && map.containsKey(assetKey) && map.get(assetKey) != null) {
645-
fontName = (String) map.get(assetKey);
646-
} else if (fileKey != null && map.containsKey(fileKey) && map.get(fileKey) != null) {
647-
fontName = (String) map.get(fileKey);
648-
}
649-
650-
if (fontName == null) {
651-
return Typeface.DEFAULT;
652-
}
653-
654-
654+
String assetPath = "fonts/" + fontName;
655+
return Typeface.createFromAsset(context.getAssets(), assetPath);
656+
} catch (Exception e) {
655657
try {
656-
String assetPath = "fonts/" + fontName;
657-
return Typeface.createFromAsset(context.getAssets(), assetPath);
658-
} catch (Exception e) {
659658
return Typeface.create(fontName, Typeface.NORMAL);
659+
} catch (Exception e2) {
660+
return Typeface.DEFAULT;
660661
}
661-
662-
} catch (Exception e) {
663-
return Typeface.DEFAULT;
664662
}
665663
}
666664

android/src/test/java/com/instabug/flutter/InstabugApiTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -692,21 +692,20 @@ public void testSetThemeWithAllProperties() {
692692
when(mock.build()).thenReturn(mock(com.instabug.library.model.IBGTheme.class));
693693
});
694694

695-
api.setTheme(themeConfig);
695+
api.setTheme(themeConfig);
696696

697-
com.instabug.library.model.IBGTheme.Builder builder = mThemeBuilder.constructed().get(0);
698-
697+
com.instabug.library.model.IBGTheme.Builder builder = mThemeBuilder.constructed().get(0);
698+
699699
verify(builder).setPrimaryColor(anyInt());
700700
verify(builder).setBackgroundColor(anyInt());
701701
verify(builder).setTitleTextColor(anyInt());
702702
verify(builder).setPrimaryTextColor(anyInt());
703703
verify(builder).setSecondaryTextColor(anyInt());
704-
705-
verify(builder).setPrimaryTextStyle(Typeface.BOLD);
706-
verify(builder).setSecondaryTextStyle(Typeface.ITALIC);
707-
verify(builder).setCtaTextStyle(Typeface.BOLD_ITALIC);
708-
709-
mInstabug.verify(() -> Instabug.setTheme(any(com.instabug.library.model.IBGTheme.class)));
704+
verify(builder).setPrimaryTextStyle(Typeface.BOLD);
705+
verify(builder).setSecondaryTextStyle(Typeface.ITALIC);
706+
verify(builder).setCtaTextStyle(Typeface.BOLD_ITALIC);
707+
708+
mInstabug.verify(() -> Instabug.setTheme(any(com.instabug.library.model.IBGTheme.class)));
710709
}
711710

712711

example/ios/InstabugTests/InstabugApiTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ - (void)testSetThemeWithAllProperties {
636636

637637
[self.api setThemeThemeConfig:themeConfig error:&error];
638638

639-
OCMVerify([self.mInstabug setTheme:OCMArg.any]);
639+
OCMVerify([self.mInstabug setTheme:[OCMArg isNotNil]]);
640640
}
641641

642642
@end

example/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ flutter:
5252
# To add assets to your application, add an assets section, like this:
5353
# assets:
5454
# - assets/fonts/
55-
# assets:
5655
# - images/a_dot_burr.jpeg
5756
# - images/a_dot_ham.jpeg
5857

ios/Classes/Modules/InstabugApi.m

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -443,55 +443,100 @@ - (void)setFontIfPresent:(NSString *)fontPath forTheme:(IBGTheme *)theme type:(N
443443
_registeredFonts = [NSMutableSet set];
444444
}
445445

446-
UIFont *font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
447-
448-
if (!font && ![_registeredFonts containsObject:fontPath]) {
449-
NSString *fontFileName = [fontPath stringByDeletingPathExtension];
450-
NSArray *fontExtensions = @[@"ttf", @"otf", @"woff", @"woff2"];
451-
NSString *fontFilePath = nil;
452-
453-
for (NSString *extension in fontExtensions) {
454-
fontFilePath = [[NSBundle mainBundle] pathForResource:fontFileName ofType:extension];
455-
if (fontFilePath) break;
446+
// Check if font is already registered
447+
if ([_registeredFonts containsObject:fontPath]) {
448+
UIFont *font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
449+
if (font) {
450+
[self setFont:font forTheme:theme type:type];
456451
}
452+
return;
453+
}
457454

458-
if (fontFilePath) {
459-
NSData *fontData = [NSData dataWithContentsOfFile:fontFilePath];
460-
if (fontData) {
461-
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
462-
if (provider) {
463-
CGFontRef cgFont = CGFontCreateWithDataProvider(provider);
464-
if (cgFont) {
465-
CFErrorRef error = NULL;
466-
if (CTFontManagerRegisterGraphicsFont(cgFont, &error)) {
467-
NSString *postScriptName = (__bridge_transfer NSString *)CGFontCopyPostScriptName(cgFont);
468-
if (postScriptName) {
469-
font = [UIFont fontWithName:postScriptName size:UIFont.systemFontSize];
470-
[_registeredFonts addObject:fontPath];
471-
}
472-
} else if (error) {
473-
CFStringRef desc = CFErrorCopyDescription(error);
474-
CFRelease(desc);
475-
CFRelease(error);
476-
}
477-
CGFontRelease(cgFont);
478-
}
479-
CGDataProviderRelease(provider);
480-
}
481-
}
482-
}
483-
} else if (!font && [_registeredFonts containsObject:fontPath]) {
484-
font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
455+
// Try to load font from system fonts first
456+
UIFont *font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
457+
if (font) {
458+
[_registeredFonts addObject:fontPath];
459+
[self setFont:font forTheme:theme type:type];
460+
return;
485461
}
486462

463+
// Try to load font from bundle
464+
font = [self loadFontFromPath:fontPath];
487465
if (font) {
488-
if ([type isEqualToString:@"primary"]) {
489-
theme.primaryTextFont = font;
490-
} else if ([type isEqualToString:@"secondary"]) {
491-
theme.secondaryTextFont = font;
492-
} else if ([type isEqualToString:@"cta"]) {
493-
theme.callToActionTextFont = font;
466+
[_registeredFonts addObject:fontPath];
467+
[self setFont:font forTheme:theme type:type];
468+
}
469+
}
470+
471+
- (UIFont *)loadFontFromPath:(NSString *)fontPath {
472+
NSString *fontFileName = [fontPath stringByDeletingPathExtension];
473+
NSArray *fontExtensions = @[@"ttf", @"otf", @"woff", @"woff2"];
474+
475+
// Find font file in bundle
476+
NSString *fontFilePath = nil;
477+
for (NSString *extension in fontExtensions) {
478+
fontFilePath = [[NSBundle mainBundle] pathForResource:fontFileName ofType:extension];
479+
if (fontFilePath) break;
480+
}
481+
482+
if (!fontFilePath) {
483+
return nil;
484+
}
485+
486+
// Load font data
487+
NSData *fontData = [NSData dataWithContentsOfFile:fontFilePath];
488+
if (!fontData) {
489+
return nil;
490+
}
491+
492+
// Create data provider
493+
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
494+
if (!provider) {
495+
return nil;
496+
}
497+
498+
// Create CG font
499+
CGFontRef cgFont = CGFontCreateWithDataProvider(provider);
500+
CGDataProviderRelease(provider);
501+
502+
if (!cgFont) {
503+
return nil;
504+
}
505+
506+
// Register font
507+
CFErrorRef error = NULL;
508+
BOOL registered = CTFontManagerRegisterGraphicsFont(cgFont, &error);
509+
510+
if (!registered) {
511+
if (error) {
512+
CFStringRef description = CFErrorCopyDescription(error);
513+
CFRelease(description);
514+
CFRelease(error);
494515
}
516+
CGFontRelease(cgFont);
517+
return nil;
518+
}
519+
520+
// Get PostScript name and create UIFont
521+
NSString *postScriptName = (__bridge_transfer NSString *)CGFontCopyPostScriptName(cgFont);
522+
CGFontRelease(cgFont);
523+
524+
if (!postScriptName) {
525+
return nil;
526+
}
527+
528+
return [UIFont fontWithName:postScriptName size:UIFont.systemFontSize];
529+
}
530+
531+
- (void)setFont:(UIFont *)font forTheme:(IBGTheme *)theme type:(NSString *)type {
532+
if (!font || !theme || !type) return;
533+
534+
if ([type isEqualToString:@"primary"]) {
535+
theme.primaryTextFont = font;
536+
} else if ([type isEqualToString:@"secondary"]) {
537+
theme.secondaryTextFont = font;
538+
} else if ([type isEqualToString:@"cta"]) {
539+
theme.callToActionTextFont = font;
495540
}
496541
}
497542

0 commit comments

Comments
 (0)