You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
3.0 KiB

5 years ago
  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. #define FLEXFloor(x) (floor(UIScreen.mainScreen.scale * (x)) / UIScreen.mainScreen.scale)
  15. #ifdef __IPHONE_13_0
  16. #define FLEX_AT_LEAST_IOS13_SDK (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
  17. #else
  18. #define FLEX_AT_LEAST_IOS13_SDK NO
  19. #endif
  20. #define FLEXPluralString(count, plural, singular) [NSString \
  21. stringWithFormat:@"%@ %@", @(count), (count == 1 ? singular : plural) \
  22. ]
  23. @interface FLEXUtility : NSObject
  24. + (UIColor *)consistentRandomColorForObject:(id)object;
  25. + (NSString *)descriptionForView:(UIView *)view includingFrame:(BOOL)includeFrame;
  26. + (NSString *)stringForCGRect:(CGRect)rect;
  27. + (UIViewController *)viewControllerForView:(UIView *)view;
  28. + (UIViewController *)viewControllerForAncestralView:(UIView *)view;
  29. + (NSString *)detailDescriptionForView:(UIView *)view;
  30. + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius;
  31. + (UIColor *)hierarchyIndentPatternColor;
  32. + (NSString *)applicationImageName;
  33. + (NSString *)applicationName;
  34. + (NSString *)safeDescriptionForObject:(id)object;
  35. + (NSString *)safeDebugDescriptionForObject:(id)object;
  36. + (NSString *)addressOfObject:(id)object;
  37. + (UIFont *)defaultFontOfSize:(CGFloat)size;
  38. + (UIFont *)defaultTableViewCellLabelFont;
  39. + (NSString *)stringByEscapingHTMLEntitiesInString:(NSString *)originalString;
  40. + (UIInterfaceOrientationMask)infoPlistSupportedInterfaceOrientationsMask;
  41. + (UIImage *)thumbnailedImageWithMaxPixelDimension:(NSInteger)dimension fromImageData:(NSData *)data;
  42. + (NSString *)stringFromRequestDuration:(NSTimeInterval)duration;
  43. + (NSString *)statusCodeStringFromURLResponse:(NSURLResponse *)response;
  44. + (BOOL)isErrorStatusCodeFromURLResponse:(NSURLResponse *)response;
  45. + (NSArray<NSURLQueryItem *> *)itemsFromQueryString:(NSString *)query;
  46. + (NSString *)prettyJSONStringFromData:(NSData *)data;
  47. + (BOOL)isValidJSONData:(NSData *)data;
  48. + (NSData *)inflatedDataFromCompressedData:(NSData *)compressedData;
  49. /// Actually more like flatmap, but it seems like the objc way to allow returning nil to omit objects.
  50. /// So, return nil from the block to omit objects, and return an object to include it in the new array.
  51. + (NSArray *)map:(NSArray *)array block:(id(^)(id obj, NSUInteger idx))mapFunc;
  52. + (NSArray<UIWindow *> *)allWindows;
  53. // Swizzling utilities
  54. + (SEL)swizzledSelectorForSelector:(SEL)selector;
  55. + (BOOL)instanceRespondsButDoesNotImplementSelector:(SEL)selector class:(Class)cls;
  56. + (void)replaceImplementationOfKnownSelector:(SEL)originalSelector onClass:(Class)class withBlock:(id)block swizzledSelector:(SEL)swizzledSelector;
  57. + (void)replaceImplementationOfSelector:(SEL)selector withSelector:(SEL)swizzledSelector forClass:(Class)cls withMethodDescription:(struct objc_method_description)methodDescription implementationBlock:(id)implementationBlock undefinedBlock:(id)undefinedBlock;
  58. @end