Skip to content

Commit

Permalink
Workaround[dynamic position]: replace dp: and px: calls with direct f…
Browse files Browse the repository at this point in the history
…ind-and-replace

Certain iOS versions don't like when expression tries to call either dp: or px:
  • Loading branch information
khanhduytran0 committed Sep 28, 2023
1 parent d6fd6e1 commit 54fb8a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Natives/customcontrols/ControlButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ - (void)preProcessProperties {
}
}

- (NSString *)processFunctions:(NSString *)string {
NSString *tmpStr;
CGFloat screenScale = UIScreen.mainScreen.scale;

// FIXME: on certain iOS versions, we cannot invoke dp: and px: in _NSPredicateUtilities
// we gotta do direct replaces here

// float dp(float px) => px / screenScale
tmpStr = [string stringByReplacingOccurrencesOfString:@"dp(" withString:[NSString stringWithFormat:@"(1.0 / %f * ", screenScale]];

// float px(float dp) => screenScale * dp
tmpStr = [tmpStr stringByReplacingOccurrencesOfString:@"px(" withString:[NSString stringWithFormat:@"(%f * ", screenScale]];
return tmpStr;
}

- (CGFloat)calculateDynamicPos:(NSString *)string {
CGRect screenBounds = self.superview.bounds;
NSAssert(self.superview, @"Why is it null");
Expand All @@ -119,6 +134,7 @@ - (CGFloat)calculateDynamicPos:(NSString *)string {
INSERT_VALUE("margin", ([NSString stringWithFormat:@"%f", 2.0 * screenScale]));
INSERT_VALUE("preferred_scale", ([NSString stringWithFormat:@"%f", getPrefFloat(@"control.button_scale")]));

string = [self processFunctions:string];
// NSLog(@"After insert: %@", string);

// Calculate, since the dynamic position contains some math equations
Expand Down
2 changes: 2 additions & 0 deletions Natives/customcontrols/NSPredicateUtilitiesExternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

@implementation NSPredicateUtilitiesExternal

#if 0 // FIXME: doesn't work on certain iOS versions
+ (NSNumber *)dp:(NSNumber *)number {
return @(number.doubleValue / UIScreen.mainScreen.scale);
}
+ (NSNumber *)px:(NSNumber *)number {
return @(number.doubleValue * UIScreen.mainScreen.scale);
}
#endif

+ (NSNumber *)cbrt:(NSNumber *)number {
return @(cbrt(number.doubleValue));
Expand Down

0 comments on commit 54fb8a3

Please sign in to comment.