FLEXUtility.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // FLEXUtility.h
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/18/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import <Availability.h>
  9. #import <AvailabilityInternal.h>
  10. #import <Foundation/Foundation.h>
  11. #import <UIKit/UIKit.h>
  12. #import <objc/runtime.h>
  13. #import "FLEXAlert.h"
  14. #import "NSArray+Functional.h"
  15. #import "UIFont+FLEX.h"
  16. #import "NSMapTable+FLEX_Subscripting.h"
  17. /// Rounds down to the nearest "point" coordinate
  18. NS_INLINE CGFloat FLEXFloor(CGFloat x) {
  19. return floor(UIScreen.mainScreen.scale * (x)) / UIScreen.mainScreen.scale;
  20. }
  21. /// Creates a CGRect with all members rounded down to the nearest "point" coordinate
  22. NS_INLINE CGRect FLEXRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
  23. return CGRectMake(FLEXFloor(x), FLEXFloor(y), FLEXFloor(width), FLEXFloor(height));
  24. }
  25. /// Adjusts the origin of an existing rect
  26. NS_INLINE CGRect FLEXRectSetOrigin(CGRect r, CGPoint origin) {
  27. r.origin = origin; return r;
  28. }
  29. /// Adjusts the size of an existing rect
  30. NS_INLINE CGRect FLEXRectSetSize(CGRect r, CGSize size) {
  31. r.size = size; return r;
  32. }
  33. /// Adjusts the origin.x of an existing rect
  34. NS_INLINE CGRect FLEXRectSetX(CGRect r, CGFloat x) {
  35. r.origin.x = x; return r;
  36. }
  37. /// Adjusts the origin.y of an existing rect
  38. NS_INLINE CGRect FLEXRectSetY(CGRect r, CGFloat y) {
  39. r.origin.y = y ; return r;
  40. }
  41. /// Adjusts the size.width of an existing rect
  42. NS_INLINE CGRect FLEXRectSetWidth(CGRect r, CGFloat width) {
  43. r.size.width = width; return r;
  44. }
  45. /// Adjusts the size.height of an existing rect
  46. NS_INLINE CGRect FLEXRectSetHeight(CGRect r, CGFloat height) {
  47. r.size.height = height; return r;
  48. }
  49. #ifdef __IPHONE_13_0
  50. #define FLEX_AT_LEAST_IOS13_SDK (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
  51. #else
  52. #define FLEX_AT_LEAST_IOS13_SDK NO
  53. #endif
  54. #define FLEXPluralString(count, plural, singular) [NSString \
  55. stringWithFormat:@"%@ %@", @(count), (count == 1 ? singular : plural) \
  56. ]
  57. #define FLEXPluralFormatString(count, pluralFormat, singularFormat) [NSString \
  58. stringWithFormat:(count == 1 ? singularFormat : pluralFormat), @(count) \
  59. ]
  60. #if !FLEX_AT_LEAST_IOS13_SDK
  61. @class UIWindowScene;
  62. #endif
  63. @interface FLEXUtility : NSObject
  64. /// The key window of the app, if it is not a \c FLEXWindow.
  65. /// If it is, then \c FLEXWindow.previousKeyWindow is returned.
  66. @property (nonatomic, readonly, class) UIWindow *appKeyWindow;
  67. /// The first active \c UIWindowScene of the app.
  68. @property (nonatomic, readonly, class) UIWindowScene *activeScene API_AVAILABLE(ios(13.0));
  69. /// @return top-most view controller of the given window
  70. + (UIViewController *)topViewControllerInWindow:(UIWindow *)window;
  71. + (UIColor *)consistentRandomColorForObject:(id)object;
  72. + (NSString *)descriptionForView:(UIView *)view includingFrame:(BOOL)includeFrame;
  73. + (NSString *)stringForCGRect:(CGRect)rect;
  74. + (UIViewController *)viewControllerForView:(UIView *)view;
  75. + (UIViewController *)viewControllerForAncestralView:(UIView *)view;
  76. + (UIImage *)previewImageForView:(UIView *)view;
  77. + (UIImage *)previewImageForLayer:(CALayer *)layer;
  78. + (NSString *)detailDescriptionForView:(UIView *)view;
  79. + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius;
  80. + (UIColor *)hierarchyIndentPatternColor;
  81. + (NSString *)pointerToString:(void *)ptr;
  82. + (NSString *)addressOfObject:(id)object;
  83. + (NSString *)stringByEscapingHTMLEntitiesInString:(NSString *)originalString;
  84. + (UIInterfaceOrientationMask)infoPlistSupportedInterfaceOrientationsMask;
  85. + (UIImage *)thumbnailedImageWithMaxPixelDimension:(NSInteger)dimension fromImageData:(NSData *)data;
  86. + (NSString *)stringFromRequestDuration:(NSTimeInterval)duration;
  87. + (NSString *)statusCodeStringFromURLResponse:(NSURLResponse *)response;
  88. + (BOOL)isErrorStatusCodeFromURLResponse:(NSURLResponse *)response;
  89. + (NSArray<NSURLQueryItem *> *)itemsFromQueryString:(NSString *)query;
  90. + (NSString *)prettyJSONStringFromData:(NSData *)data;
  91. + (BOOL)isValidJSONData:(NSData *)data;
  92. + (NSData *)inflatedDataFromCompressedData:(NSData *)compressedData;
  93. + (NSArray<UIWindow *> *)allWindows;
  94. // Swizzling utilities
  95. + (SEL)swizzledSelectorForSelector:(SEL)selector;
  96. + (BOOL)instanceRespondsButDoesNotImplementSelector:(SEL)selector class:(Class)cls;
  97. + (void)replaceImplementationOfKnownSelector:(SEL)originalSelector onClass:(Class)class withBlock:(id)block swizzledSelector:(SEL)swizzledSelector;
  98. + (void)replaceImplementationOfSelector:(SEL)selector withSelector:(SEL)swizzledSelector forClass:(Class)cls withMethodDescription:(struct objc_method_description)methodDescription implementationBlock:(id)implementationBlock undefinedBlock:(id)undefinedBlock;
  99. @end