Update APIs to align with readme (#35)

* Update APIs

* Public UIImage+DarkModeKit

* Refine names

* Refine names

* Remove extra space

* Add a simple test for color

* Correct pbxproj
This commit is contained in:
Bei Li 2019-12-10 11:07:20 +08:00 коммит произвёл GitHub
Родитель ab634ed8b6
Коммит 76c01aa0fe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 180 добавлений и 44 удалений

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

@ -20,6 +20,9 @@
8CAFD9ED2371606D001A63B8 /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CAFD9EC2371606D001A63B8 /* NavigationController.swift */; };
8CB63E41238551F3008ABCE2 /* UIView+DarkModeKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CB63E3F238551F3008ABCE2 /* UIView+DarkModeKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CB63E42238551F3008ABCE2 /* UIView+DarkModeKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CB63E40238551F3008ABCE2 /* UIView+DarkModeKit.m */; };
8CC8D86D2398E4EC0043276A /* UIColor+DarkModeKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CC8D86B2398E4EC0043276A /* UIColor+DarkModeKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CC8D8702398E7520043276A /* DMNamespace.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CC8D86F2398E7520043276A /* DMNamespace.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CC8D8732398E7A30043276A /* UIImage+DarkModeKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CC8D8712398E7A30043276A /* UIImage+DarkModeKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CDA628C2366DAA9004895B5 /* DarkModeKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CDA62702366DAA9004895B5 /* DarkModeKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CDA628D2366DAA9004895B5 /* DMTraitCollection.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CDA62712366DAA9004895B5 /* DMTraitCollection.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CDA628E2366DAA9004895B5 /* DMTraitCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CDA62722366DAA9004895B5 /* DMTraitCollection.m */; };
@ -45,6 +48,8 @@
8CDA62A32366DAA9004895B5 /* DMDynamicImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CDA62892366DAA9004895B5 /* DMDynamicImage.m */; };
8CDA62A42366DAA9004895B5 /* DarkModeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CDA628A2366DAA9004895B5 /* DarkModeManager.swift */; };
8CDA62A52366DAA9004895B5 /* DMDynamicColor.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CDA628B2366DAA9004895B5 /* DMDynamicColor.h */; settings = {ATTRIBUTES = (Public, ); }; };
8CE066CF239E5582002CE16D /* UIColor+DarkModeKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CC8D86C2398E4EC0043276A /* UIColor+DarkModeKit.m */; };
8CE066D0239E5586002CE16D /* UIImage+DarkModeKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CC8D8722398E7A30043276A /* UIImage+DarkModeKit.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -95,6 +100,11 @@
8CAFD9EC2371606D001A63B8 /* NavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationController.swift; sourceTree = "<group>"; };
8CB63E3F238551F3008ABCE2 /* UIView+DarkModeKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIView+DarkModeKit.h"; sourceTree = "<group>"; };
8CB63E40238551F3008ABCE2 /* UIView+DarkModeKit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIView+DarkModeKit.m"; sourceTree = "<group>"; };
8CC8D86B2398E4EC0043276A /* UIColor+DarkModeKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIColor+DarkModeKit.h"; sourceTree = "<group>"; };
8CC8D86C2398E4EC0043276A /* UIColor+DarkModeKit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIColor+DarkModeKit.m"; sourceTree = "<group>"; };
8CC8D86F2398E7520043276A /* DMNamespace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DMNamespace.h; sourceTree = "<group>"; };
8CC8D8712398E7A30043276A /* UIImage+DarkModeKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+DarkModeKit.h"; sourceTree = "<group>"; };
8CC8D8722398E7A30043276A /* UIImage+DarkModeKit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+DarkModeKit.m"; sourceTree = "<group>"; };
8CDA62702366DAA9004895B5 /* DarkModeKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DarkModeKit.h; sourceTree = "<group>"; };
8CDA62712366DAA9004895B5 /* DMTraitCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DMTraitCollection.h; sourceTree = "<group>"; };
8CDA62722366DAA9004895B5 /* DMTraitCollection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DMTraitCollection.m; sourceTree = "<group>"; };
@ -179,6 +189,11 @@
8CDA62722366DAA9004895B5 /* DMTraitCollection.m */,
8C071AEA23683BF5001AB7B2 /* NSObject+DarkModeKit.h */,
8C071AEB23683BF5001AB7B2 /* NSObject+DarkModeKit.m */,
8CC8D86F2398E7520043276A /* DMNamespace.h */,
8CC8D86B2398E4EC0043276A /* UIColor+DarkModeKit.h */,
8CC8D86C2398E4EC0043276A /* UIColor+DarkModeKit.m */,
8CC8D8712398E7A30043276A /* UIImage+DarkModeKit.h */,
8CC8D8722398E7A30043276A /* UIImage+DarkModeKit.m */,
8CB63E3F238551F3008ABCE2 /* UIView+DarkModeKit.h */,
8CB63E40238551F3008ABCE2 /* UIView+DarkModeKit.m */,
);
@ -275,10 +290,13 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
8CC8D86D2398E4EC0043276A /* UIColor+DarkModeKit.h in Headers */,
8CC8D8702398E7520043276A /* DMNamespace.h in Headers */,
8C071AEC23683BF5001AB7B2 /* NSObject+DarkModeKit.h in Headers */,
8CDA628D2366DAA9004895B5 /* DMTraitCollection.h in Headers */,
8CDA628C2366DAA9004895B5 /* DarkModeKit.h in Headers */,
8CB63E41238551F3008ABCE2 /* UIView+DarkModeKit.h in Headers */,
8CC8D8732398E7A30043276A /* UIImage+DarkModeKit.h in Headers */,
8CDA62912366DAA9004895B5 /* DMDynamicImage.h in Headers */,
8CDA62A52366DAA9004895B5 /* DMDynamicColor.h in Headers */,
);
@ -422,6 +440,8 @@
8CDA629E2366DAA9004895B5 /* UIViewController+DarkModeKit.swift in Sources */,
8CDA62992366DAA9004895B5 /* UIImageView+DarkModeKit.swift in Sources */,
8CDA62962366DAA9004895B5 /* UIProgressView+DarkModeKit.swift in Sources */,
8CE066D0239E5586002CE16D /* UIImage+DarkModeKit.m in Sources */,
8CE066CF239E5582002CE16D /* UIColor+DarkModeKit.m in Sources */,
8CDA62A02366DAA9004895B5 /* UISlider+DarkModeKit.swift in Sources */,
8CDA62A22366DAA9004895B5 /* UIApplication+DarkModeKit.swift in Sources */,
8CDA628F2366DAA9004895B5 /* DMDynamicColor.m in Sources */,

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

@ -8,13 +8,10 @@
NS_ASSUME_NONNULL_BEGIN
@interface DMDynamicImageProxy : NSProxy
@end
@interface UIImage (DynamicImage)
@property (nonatomic, readonly) UIImage *resolvedImage;
- (instancetype)initWithLightImage:(nullable UIImage *)lightImage darkImage:(nullable UIImage *)darkImage;
- (BOOL)outlookIsEqual:(nullable UIImage *)other;
- (instancetype)initWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage;
@end

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

@ -14,7 +14,6 @@
@interface DMDynamicImageProxy ()
@property (nonatomic, strong) UIImage *lightImage;
@property (nonatomic, strong) UIImage *darkImage;
@property (nonatomic, readonly) UIImage *resolvedImage;
@end
@implementation DMDynamicImageProxy
@ -108,36 +107,3 @@
}
@end
// MARK: - UIImage
@implementation UIImage (DynamicImage)
+ (void)load
{
[UIImage dm_swizzleInstanceMethod:@selector(isEqual:) to:@selector(outlookIsEqual:)];
}
- (instancetype)initWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage
{
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:lightImage darkImage:darkImage];
}
- (BOOL)outlookIsEqual:(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)
{
realSelf = ((DMDynamicImageProxy *)self).resolvedImage;
}
if (object_getClass(other) == DMDynamicImageProxy.class)
{
realOther = ((DMDynamicImageProxy *)other).resolvedImage;
}
return [realSelf outlookIsEqual:realOther];
}
@end

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

@ -0,0 +1,10 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, DMNamespace) {
DMNamespaceDM NS_SWIFT_NAME(dm),
};

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

@ -7,6 +7,8 @@
#import <DarkModeKit/DMDynamicImage.h>
#import <DarkModeKit/DMTraitCollection.h>
#import <DarkModeKit/NSObject+DarkModeKit.h>
#import <DarkModeKit/UIColor+DarkModeKit.h>
#import <DarkModeKit/UIImage+DarkModeKit.h>
#import <DarkModeKit/UIView+DarkModeKit.h>
//! Project version number for DarkModeKit.

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

@ -0,0 +1,24 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
#import <UIKit/UIKit.h>
#import <DarkModeKit/DMNamespace.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIColor (DarkModeKit)
+ (UIColor *)dm_colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor
NS_SWIFT_UNAVAILABLE("Use init(_:light:dark:) instead.");
#if __swift__
+ (UIColor *)dm_namespace:(DMNamespace)namespace
colorWithLightColor:(UIColor *)lightColor
darkColor:(UIColor *)darkColor NS_SWIFT_NAME(init(_:light:dark:));
#endif
@end
NS_ASSUME_NONNULL_END

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

@ -0,0 +1,21 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
#import "UIColor+DarkModeKit.h"
#import "DMDynamicColor.h"
@implementation UIColor (DarkModeKit)
+ (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 {
return [UIColor dm_colorWithLightColor:lightColor darkColor:darkColor];
}
@end

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

@ -0,0 +1,24 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
#import <UIKit/UIKit.h>
#import <DarkModeKit/DMNamespace.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIImage (DarkModeKit)
+ (UIImage *)dm_imageWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage
NS_SWIFT_UNAVAILABLE("Use init(_:light:dark:) instead.");
#if __swift__
+ (UIImage *)dm_namespace:(DMNamespace)namespace
dm_imageWithLightImage:(UIImage *)lightImage
darkImage:(UIImage *)darkImage NS_SWIFT_NAME(init(_:light:dark:));
#endif
@end
NS_ASSUME_NONNULL_END

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

@ -0,0 +1,42 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
#import "UIImage+DarkModeKit.h"
#import "DMDynamicImage.h"
#import "NSObject+DarkModeKit.h"
@import ObjectiveC;
@implementation UIImage (DarkModeKit)
+ (void)load {
[UIImage dm_swizzleInstanceMethod:@selector(isEqual:) to:@selector(dm_isEqual:)];
}
+ (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 {
return [UIImage dm_imageWithLightImage:lightImage darkImage:darkImage];
}
- (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) {
realSelf = ((DMDynamicImageProxy *)self).resolvedImage;
}
if (object_getClass(other) == DMDynamicImageProxy.class) {
realOther = ((DMDynamicImageProxy *)other).resolvedImage;
}
return [realSelf dm_isEqual:realOther];
}
@end

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

@ -7,9 +7,39 @@ import XCTest
@testable import DarkModeKit
final class DarkModeKitTests: XCTestCase {
func testSetBackgroundColorSwizzling() {
UIWindow.appearance().backgroundColor = .white
DarkModeManager.setup()
_ = UIWindow()
}
func testSetBackgroundColorSwizzling() {
UIWindow.appearance().backgroundColor = .white
DarkModeManager.setup()
_ = UIWindow()
}
func testColorInitializer() {
let color = UIColor(.dm, light: .white, dark: .black)
DMTraitCollection.current = DMTraitCollection(userInterfaceStyle: .light)
XCTAssertEqual(color.rgba, UIColor.white.rgba)
DMTraitCollection.current = DMTraitCollection(userInterfaceStyle: .dark)
XCTAssertEqual(color.rgba, UIColor.black.rgba)
}
}
extension UIColor {
struct RGBA: Equatable {
var red: CGFloat
var green: CGFloat
var blue: CGFloat
var alpha: CGFloat
}
var rgba: RGBA {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return RGBA(red: red, green: green, blue: blue, alpha: alpha)
}
}