Change to a more common Obj-C style (#44)

This commit is contained in:
Bei Li 2020-02-07 17:54:39 +08:00 коммит произвёл GitHub
Родитель e1793072d9
Коммит 192e18c7be
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 57 добавлений и 114 удалений

Просмотреть файл

@ -18,50 +18,41 @@
@implementation DMDynamicColorProxy
// TODO: We need a more generic initializer.
- (instancetype)initWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor
{
- (instancetype)initWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
self.lightColor = lightColor;
self.darkColor = darkColor;
return self;
}
- (UIColor *)resolvedColor
{
if (DMTraitCollection.currentTraitCollection.userInterfaceStyle == DMUserInterfaceStyleDark)
{
- (UIColor *)resolvedColor {
if (DMTraitCollection.currentTraitCollection.userInterfaceStyle == DMUserInterfaceStyleDark) {
return self.darkColor;
}
else
{
} else {
return self.lightColor;
}
}
// MARK: UIColor
- (UIColor *)colorWithAlphaComponent:(CGFloat)alpha
{
- (UIColor *)colorWithAlphaComponent:(CGFloat)alpha {
return [[DMDynamicColor alloc] initWithLightColor:[self.lightColor colorWithAlphaComponent:alpha]
darkColor:[self.darkColor colorWithAlphaComponent:alpha]];
}
// MARK: NSProxy
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.resolvedColor methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:self.resolvedColor];
}
// MARK: NSObject
- (BOOL)isKindOfClass:(Class)aClass
{
- (BOOL)isKindOfClass:(Class)aClass {
static DMDynamicColor *dynamicColor = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@ -72,13 +63,11 @@
// MARK: NSCopying
- (id)copy
{
- (id)copy {
return [self copyWithZone:nil];
}
- (id)copyWithZone:(NSZone *)zone
{
- (id)copyWithZone:(NSZone *)zone {
return [[DMDynamicColorProxy alloc] initWithLightColor:self.lightColor darkColor:self.darkColor];
}
@ -88,19 +77,16 @@
@implementation DMDynamicColor
- (UIColor *)initWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor
{
- (UIColor *)initWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
return (DMDynamicColor *)[[DMDynamicColorProxy alloc] initWithLightColor:lightColor darkColor:darkColor];
}
- (UIColor *)lightColor
{
- (UIColor *)lightColor {
NSAssert(NO, @"This should never be called");
return nil;
}
- (UIColor *)darkColor
{
- (UIColor *)darkColor {
NSAssert(NO, @"This should never be called");
return nil;
}

Просмотреть файл

@ -18,8 +18,7 @@
@implementation DMDynamicImageProxy
- (instancetype)initWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage
{
- (instancetype)initWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage {
self.lightImage = lightImage;
self.darkImage = darkImage;
// For now, we don't support `nil` images as it will cause bad result
@ -27,38 +26,30 @@
// `someOptionalDynamicImage as? UIImage` will return `true`
// even when the internal `lightImage` or `darkImage` is nil
NSAssert(self.darkImage != nil, @"Nil image is not supported yet");
if (self.lightImage == nil)
{
if (self.lightImage == nil) {
NSAssert(NO, @"Nil light image is not supported yet");
self.lightImage = [UIImage new];
}
if (self.darkImage == nil)
{
if (self.darkImage == nil) {
NSAssert(NO, @"Nil dark image is not supported yet");
self.darkImage = [UIImage new];
}
return self;
}
- (UIImage *)resolvedImage
{
if (DMTraitCollection.currentTraitCollection.userInterfaceStyle == DMUserInterfaceStyleDark)
{
- (UIImage *)resolvedImage {
if (DMTraitCollection.currentTraitCollection.userInterfaceStyle == DMUserInterfaceStyleDark) {
return self.darkImage;
}
else
{
} else {
return self.lightImage;
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.resolvedImage methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:self.resolvedImage];
}
@ -67,49 +58,42 @@
/// Passing these public methods to both light and dark images
/// instead of only to the `resolvedImage`
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
{
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets {
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:[self.lightImage resizableImageWithCapInsets:capInsets]
darkImage:[self.darkImage resizableImageWithCapInsets:capInsets]];
}
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
{
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:[self.lightImage resizableImageWithCapInsets:capInsets resizingMode:resizingMode]
darkImage:[self.darkImage resizableImageWithCapInsets:capInsets resizingMode:resizingMode]];
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode {
UIImage *lightImage = [self.lightImage resizableImageWithCapInsets:capInsets resizingMode:resizingMode];
UIImage *darkImage = [self.darkImage resizableImageWithCapInsets:capInsets resizingMode:resizingMode];
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:lightImage darkImage:darkImage];
}
- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets
{
- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets {
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:[self.lightImage imageWithAlignmentRectInsets:alignmentInsets]
darkImage:[self.darkImage imageWithAlignmentRectInsets:alignmentInsets]];
}
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
{
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode {
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:[self.lightImage imageWithRenderingMode:renderingMode]
darkImage:[self.darkImage imageWithRenderingMode:renderingMode]];
}
- (UIImage *)imageFlippedForRightToLeftLayoutDirection
{
- (UIImage *)imageFlippedForRightToLeftLayoutDirection {
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:[self.lightImage imageFlippedForRightToLeftLayoutDirection]
darkImage:[self.darkImage imageFlippedForRightToLeftLayoutDirection]];
}
- (UIImage *)imageWithHorizontallyFlippedOrientation
{
- (UIImage *)imageWithHorizontallyFlippedOrientation {
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:[self.lightImage imageWithHorizontallyFlippedOrientation]
darkImage:[self.darkImage imageWithHorizontallyFlippedOrientation]];
}
- (id)copy
{
- (id)copy {
return [self copyWithZone:nil];
}
- (id)copyWithZone:(NSZone *)zone
{
- (id)copyWithZone:(NSZone *)zone {
return [[DMDynamicImageProxy alloc] initWithLightImage:self.lightImage darkImage:self.darkImage];
}

Просмотреть файл

@ -5,7 +5,6 @@
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, DMNamespace)
{
typedef NS_ENUM(NSInteger, DMNamespace) {
DMNamespaceDM NS_SWIFT_NAME(dm),
};

Просмотреть файл

@ -7,8 +7,7 @@
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, DMUserInterfaceStyle)
{
typedef NS_ENUM(NSInteger, DMUserInterfaceStyle) {
DMUserInterfaceStyleUnspecified,
DMUserInterfaceStyleLight,
DMUserInterfaceStyleDark,

Просмотреть файл

@ -9,28 +9,23 @@
static DMTraitCollection *_currentTraitCollection = nil;
+ (DMTraitCollection *)currentTraitCollection
{
+ (DMTraitCollection *)currentTraitCollection {
return _currentTraitCollection;
}
+ (void)setCurrentTraitCollection:(DMTraitCollection *)currentTraitCollection
{
+ (void)setCurrentTraitCollection:(DMTraitCollection *)currentTraitCollection {
_currentTraitCollection = currentTraitCollection;
}
+ (DMTraitCollection *)traitCollectionWithUserInterfaceStyle:(DMUserInterfaceStyle)userInterfaceStyle
{
+ (DMTraitCollection *)traitCollectionWithUserInterfaceStyle:(DMUserInterfaceStyle)userInterfaceStyle {
DMTraitCollection *traitCollection = [[DMTraitCollection alloc] init];
traitCollection->_userInterfaceStyle = userInterfaceStyle;
return traitCollection;
}
- (instancetype)init
{
- (instancetype)init {
self = [super init];
if (self)
{
if (self) {
_userInterfaceStyle = DMUserInterfaceStyleUnspecified;
}
return self;

Просмотреть файл

@ -9,22 +9,17 @@
@implementation NSObject (DarkModeKit)
+ (BOOL)dm_swizzleInstanceMethod:(SEL)selector1 to:(SEL)selector2
{
+ (BOOL)dm_swizzleInstanceMethod:(SEL)selector1 to:(SEL)selector2 {
Method method1 = class_getInstanceMethod(self, selector1);
Method method2 = class_getInstanceMethod(self, selector2);
if (!method1 || !method2)
{
if (!method1 || !method2) {
return NO;
}
if (class_addMethod(self, selector1, method_getImplementation(method2), method_getTypeEncoding(method2)))
{
if (class_addMethod(self, selector1, method_getImplementation(method2), method_getTypeEncoding(method2))) {
class_replaceMethod(self, selector2, method_getImplementation(method1), method_getTypeEncoding(method1));
}
else
{
} else {
method_exchangeImplementations(method1, method2);
}

Просмотреть файл

@ -8,15 +8,13 @@
@implementation UIColor (DarkModeKit)
+ (UIColor *)dm_colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor
{
+ (UIColor *)dm_colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
return (UIColor *)[[DMDynamicColor alloc] initWithLightColor:lightColor darkColor:darkColor];
}
+ (UIColor *)dm_namespace:(DMNamespace)namespace
colorWithLightColor:(UIColor *)lightColor
darkColor:(UIColor *)darkColor
{
darkColor:(UIColor *)darkColor {
return [UIColor dm_colorWithLightColor:lightColor darkColor:darkColor];
}

Просмотреть файл

@ -11,35 +11,29 @@
@implementation UIImage (DarkModeKit)
+ (void)load
{
+ (void)load {
[UIImage dm_swizzleInstanceMethod:@selector(isEqual:) to:@selector(dm_isEqual:)];
}
+ (UIImage *)dm_imageWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage
{
+ (UIImage *)dm_imageWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage {
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:lightImage darkImage:darkImage];
}
+ (UIImage *)dm_namespace:(DMNamespace)namespace
imageWithLightImage:(UIImage *)lightImage
darkImage:(UIImage *)darkImage
{
darkImage:(UIImage *)darkImage {
return [UIImage dm_imageWithLightImage:lightImage darkImage:darkImage];
}
- (BOOL)dm_isEqual:(UIImage *)other
{
- (BOOL)dm_isEqual:(UIImage *)other {
/// On iOS 13, UIImage `isEqual:` somehow changes internally and doesn't work for `NSProxy`,
/// here we forward the message to internal images manually
UIImage *realSelf = self;
UIImage *realOther = other;
if (object_getClass(self) == DMDynamicImageProxy.class)
{
if (object_getClass(self) == DMDynamicImageProxy.class) {
realSelf = ((DMDynamicImageProxy *)self).resolvedImage;
}
if (object_getClass(other) == DMDynamicImageProxy.class)
{
if (object_getClass(other) == DMDynamicImageProxy.class) {
realOther = ((DMDynamicImageProxy *)other).resolvedImage;
}
return [realSelf dm_isEqual:realOther];

Просмотреть файл

@ -12,22 +12,17 @@
static void (*dm_original_setBackgroundColor)(UIView *, SEL, UIColor *);
static void dm_setBackgroundColor(UIView *self, SEL _cmd, UIColor *color)
{
if ([color isKindOfClass:[DMDynamicColor class]])
{
static void dm_setBackgroundColor(UIView *self, SEL _cmd, UIColor *color) {
if ([color isKindOfClass:[DMDynamicColor class]]) {
self.dm_dynamicBackgroundColor = (DMDynamicColor *)color;
}
else
{
} else {
self.dm_dynamicBackgroundColor = nil;
}
dm_original_setBackgroundColor(self, _cmd, color);
}
// https://stackoverflow.com/questions/42677534/swizzling-on-properties-that-conform-to-ui-appearance-selector
+ (void)dm_swizzleSetBackgroundColor
{
+ (void)dm_swizzleSetBackgroundColor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method method = class_getInstanceMethod(self, @selector(setBackgroundColor:));
@ -36,13 +31,11 @@ static void dm_setBackgroundColor(UIView *self, SEL _cmd, UIColor *color)
});
}
- (DMDynamicColor *)dm_dynamicBackgroundColor
{
- (DMDynamicColor *)dm_dynamicBackgroundColor {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setDm_dynamicBackgroundColor:(DMDynamicColor *)dm_dynamicBackgroundColor
{
- (void)setDm_dynamicBackgroundColor:(DMDynamicColor *)dm_dynamicBackgroundColor {
objc_setAssociatedObject(self,
@selector(dm_dynamicBackgroundColor),
dm_dynamicBackgroundColor,