xref: /expo/ios/Exponent/Versioned/Core/Api/EXUtil.m (revision 5db43c74)
1// Copyright 2016-present 650 Industries. All rights reserved.
2
3#import "EXUtil.h"
4#import "EXScopedModuleRegistry.h"
5
6@interface EXUtil ()
7
8@property (nonatomic, weak) id<EXUtilService> kernelUtilService;
9
10@end
11
12EX_DEFINE_SCOPED_MODULE_GETTER(EXUtil, util)
13
14@implementation EXUtil
15
16EX_EXPORT_SCOPED_MODULE(ExponentUtil, UtilService);
17
18- (instancetype)initWithExperienceStableLegacyId:(NSString *)experienceStableLegacyId
19                                        scopeKey:(NSString *)scopeKey
20                                    easProjectId:(NSString *)easProjectId
21                           kernelServiceDelegate:(id<EXUtilService>)kernelServiceInstance
22                                          params:(NSDictionary *)params
23{
24  if (self = [super initWithExperienceStableLegacyId:experienceStableLegacyId
25                                            scopeKey:scopeKey
26                                        easProjectId:easProjectId
27                               kernelServiceDelegate:kernelServiceInstance
28                                              params:params]) {
29    _kernelUtilService = kernelServiceInstance;
30  }
31  return self;
32}
33
34+ (NSString *)escapedResourceName:(NSString *)name
35{
36  NSString *charactersToEscape = @"!*'();:@&=+$,/?%#[]";
37  NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
38  return [name stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
39}
40
41+ (void)performSynchronouslyOnMainThread:(void (^)(void))block
42{
43  if ([NSThread isMainThread]) {
44    block();
45  } else {
46    dispatch_sync(dispatch_get_main_queue(), block);
47  }
48}
49
50// https://stackoverflow.com/questions/14051807/how-can-i-get-a-hex-string-from-uicolor-or-from-rgb
51+ (NSString *)hexStringWithCGColor:(CGColorRef)color
52{
53  const CGFloat *components = CGColorGetComponents(color);
54  size_t count = CGColorGetNumberOfComponents(color);
55
56  if (count == 2) {
57    return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
58            lroundf(components[0] * 255.0),
59            lroundf(components[0] * 255.0),
60            lroundf(components[0] * 255.0)];
61  } else {
62    return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
63            lroundf(components[0] * 255.0),
64            lroundf(components[1] * 255.0),
65            lroundf(components[2] * 255.0)];
66  }
67}
68
69+ (UIColor *)colorWithRGB:(unsigned int)rgbValue
70{
71  return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
72                         green:((float)((rgbValue & 0xFF00) >> 8))/255.0
73                          blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];
74}
75
76+ (UIColor *)colorWithHexString:(NSString *)hexString
77{
78  if (!hexString || hexString.length != 7 || [hexString characterAtIndex:0] != '#') {
79    return nil;
80  }
81  hexString = [hexString substringWithRange:NSMakeRange(1, 6)];
82  NSScanner *scanner = [NSScanner scannerWithString:hexString];
83  unsigned int hex;
84  if ([scanner scanHexInt:&hex]) {
85    int r = (hex >> 16) & 0xFF;
86    int g = (hex >> 8) & 0xFF;
87    int b = (hex) & 0xFF;
88
89    return [UIColor colorWithRed:r / 255.0f
90                           green:g / 255.0f
91                            blue:b / 255.0f
92                           alpha:1.0f];
93  }
94  return nil;
95}
96
97- (UIViewController *)currentViewController
98{
99  return [_kernelUtilService currentViewController];
100}
101
102- (nullable NSDictionary *)launchOptions
103{
104  return [_kernelUtilService launchOptions];
105}
106
107@end
108