Skip to content

Commit b4b89be

Browse files
committed
fix: resolve comments
1 parent fccbc42 commit b4b89be

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
@@ -658,29 +658,27 @@ private void setFontIfPresent(Map<String, Object> themeConfig, com.instabug.libr
658658
}
659659

660660
private Typeface getTypeface(Map<String, Object> map, String fileKey, String assetKey) {
661+
String fontName = null;
662+
663+
if (assetKey != null && map.containsKey(assetKey) && map.get(assetKey) != null) {
664+
fontName = (String) map.get(assetKey);
665+
} else if (fileKey != null && map.containsKey(fileKey) && map.get(fileKey) != null) {
666+
fontName = (String) map.get(fileKey);
667+
}
668+
669+
if (fontName == null) {
670+
return Typeface.DEFAULT;
671+
}
672+
661673
try {
662-
String fontName = null;
663-
664-
if (assetKey != null && map.containsKey(assetKey) && map.get(assetKey) != null) {
665-
fontName = (String) map.get(assetKey);
666-
} else if (fileKey != null && map.containsKey(fileKey) && map.get(fileKey) != null) {
667-
fontName = (String) map.get(fileKey);
668-
}
669-
670-
if (fontName == null) {
671-
return Typeface.DEFAULT;
672-
}
673-
674-
674+
String assetPath = "fonts/" + fontName;
675+
return Typeface.createFromAsset(context.getAssets(), assetPath);
676+
} catch (Exception e) {
675677
try {
676-
String assetPath = "fonts/" + fontName;
677-
return Typeface.createFromAsset(context.getAssets(), assetPath);
678-
} catch (Exception e) {
679678
return Typeface.create(fontName, Typeface.NORMAL);
679+
} catch (Exception e2) {
680+
return Typeface.DEFAULT;
680681
}
681-
682-
} catch (Exception e) {
683-
return Typeface.DEFAULT;
684682
}
685683
}
686684

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

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

707-
api.setTheme(themeConfig);
707+
api.setTheme(themeConfig);
708708

709-
com.instabug.library.model.IBGTheme.Builder builder = mThemeBuilder.constructed().get(0);
710-
709+
com.instabug.library.model.IBGTheme.Builder builder = mThemeBuilder.constructed().get(0);
710+
711711
verify(builder).setPrimaryColor(anyInt());
712712
verify(builder).setBackgroundColor(anyInt());
713713
verify(builder).setTitleTextColor(anyInt());
714714
verify(builder).setPrimaryTextColor(anyInt());
715715
verify(builder).setSecondaryTextColor(anyInt());
716-
717-
verify(builder).setPrimaryTextStyle(Typeface.BOLD);
718-
verify(builder).setSecondaryTextStyle(Typeface.ITALIC);
719-
verify(builder).setCtaTextStyle(Typeface.BOLD_ITALIC);
720-
721-
mInstabug.verify(() -> Instabug.setTheme(any(com.instabug.library.model.IBGTheme.class)));
716+
verify(builder).setPrimaryTextStyle(Typeface.BOLD);
717+
verify(builder).setSecondaryTextStyle(Typeface.ITALIC);
718+
verify(builder).setCtaTextStyle(Typeface.BOLD_ITALIC);
719+
720+
mInstabug.verify(() -> Instabug.setTheme(any(com.instabug.library.model.IBGTheme.class)));
722721
}
723722

724723

example/ios/InstabugTests/InstabugApiTests.m

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

644644
[self.api setThemeThemeConfig:themeConfig error:&error];
645645

646-
OCMVerify([self.mInstabug setTheme:OCMArg.any]);
646+
OCMVerify([self.mInstabug setTheme:[OCMArg isNotNil]]);
647647
}
648648

649649
@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
@@ -456,55 +456,100 @@ - (void)setFontIfPresent:(NSString *)fontPath forTheme:(IBGTheme *)theme type:(N
456456
_registeredFonts = [NSMutableSet set];
457457
}
458458

459-
UIFont *font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
460-
461-
if (!font && ![_registeredFonts containsObject:fontPath]) {
462-
NSString *fontFileName = [fontPath stringByDeletingPathExtension];
463-
NSArray *fontExtensions = @[@"ttf", @"otf", @"woff", @"woff2"];
464-
NSString *fontFilePath = nil;
465-
466-
for (NSString *extension in fontExtensions) {
467-
fontFilePath = [[NSBundle mainBundle] pathForResource:fontFileName ofType:extension];
468-
if (fontFilePath) break;
459+
// Check if font is already registered
460+
if ([_registeredFonts containsObject:fontPath]) {
461+
UIFont *font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
462+
if (font) {
463+
[self setFont:font forTheme:theme type:type];
469464
}
465+
return;
466+
}
470467

471-
if (fontFilePath) {
472-
NSData *fontData = [NSData dataWithContentsOfFile:fontFilePath];
473-
if (fontData) {
474-
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
475-
if (provider) {
476-
CGFontRef cgFont = CGFontCreateWithDataProvider(provider);
477-
if (cgFont) {
478-
CFErrorRef error = NULL;
479-
if (CTFontManagerRegisterGraphicsFont(cgFont, &error)) {
480-
NSString *postScriptName = (__bridge_transfer NSString *)CGFontCopyPostScriptName(cgFont);
481-
if (postScriptName) {
482-
font = [UIFont fontWithName:postScriptName size:UIFont.systemFontSize];
483-
[_registeredFonts addObject:fontPath];
484-
}
485-
} else if (error) {
486-
CFStringRef desc = CFErrorCopyDescription(error);
487-
CFRelease(desc);
488-
CFRelease(error);
489-
}
490-
CGFontRelease(cgFont);
491-
}
492-
CGDataProviderRelease(provider);
493-
}
494-
}
495-
}
496-
} else if (!font && [_registeredFonts containsObject:fontPath]) {
497-
font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
468+
// Try to load font from system fonts first
469+
UIFont *font = [UIFont fontWithName:fontPath size:UIFont.systemFontSize];
470+
if (font) {
471+
[_registeredFonts addObject:fontPath];
472+
[self setFont:font forTheme:theme type:type];
473+
return;
498474
}
499475

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

0 commit comments

Comments
 (0)