Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Add configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Jul 24, 2020
1 parent 67ce9fb commit 0646f9c
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 13 deletions.
8 changes: 8 additions & 0 deletions FluentDarkModeKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
EA1BAF7024C58447006E755F /* UIImage+DarkModeKitSwizzling.m in Sources */ = {isa = PBXBuildFile; fileRef = EA1BAF6E24C58447006E755F /* UIImage+DarkModeKitSwizzling.m */; };
EA2EA50024A1CBF2001AE312 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA2EA4FE24A1CAD5001AE312 /* SceneDelegate.swift */; };
EA7316F1248F5055009AE037 /* UILabelVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA7316EF248F5050009AE037 /* UILabelVC.swift */; };
EAC41E2F24CADD6E0033C9AC /* DMEnvironmentConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = EAC41E2D24CADD6E0033C9AC /* DMEnvironmentConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
EAC41E3024CADD6E0033C9AC /* DMEnvironmentConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = EAC41E2E24CADD6E0033C9AC /* DMEnvironmentConfiguration.m */; };
EAE6065624A9DA1B001304D1 /* UIImageViewVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAE6065324A9D9E0001304D1 /* UIImageViewVC.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -158,6 +160,8 @@
EA1BAF6E24C58447006E755F /* UIImage+DarkModeKitSwizzling.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+DarkModeKitSwizzling.m"; sourceTree = "<group>"; };
EA2EA4FE24A1CAD5001AE312 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
EA7316EF248F5050009AE037 /* UILabelVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILabelVC.swift; sourceTree = "<group>"; };
EAC41E2D24CADD6E0033C9AC /* DMEnvironmentConfiguration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DMEnvironmentConfiguration.h; sourceTree = "<group>"; };
EAC41E2E24CADD6E0033C9AC /* DMEnvironmentConfiguration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DMEnvironmentConfiguration.m; sourceTree = "<group>"; };
EAE6065324A9D9E0001304D1 /* UIImageViewVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImageViewVC.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -222,6 +226,8 @@
8CDA62732366DAA9004895B5 /* DMDynamicColor.m */,
8CDA62762366DAA9004895B5 /* DMDynamicImage.h */,
8CDA62892366DAA9004895B5 /* DMDynamicImage.m */,
EAC41E2D24CADD6E0033C9AC /* DMEnvironmentConfiguration.h */,
EAC41E2E24CADD6E0033C9AC /* DMEnvironmentConfiguration.m */,
8CDA62712366DAA9004895B5 /* DMTraitCollection.h */,
8CDA62722366DAA9004895B5 /* DMTraitCollection.m */,
8CC8D86F2398E7520043276A /* DMNamespace.h */,
Expand Down Expand Up @@ -352,6 +358,7 @@
EA1BAF6724C581C5006E755F /* UIView+DarkModeKitSwizzling.h in Headers */,
EA1BAF6F24C58447006E755F /* UIImage+DarkModeKitSwizzling.h in Headers */,
8CC8D8702398E7520043276A /* DMNamespace.h in Headers */,
EAC41E2F24CADD6E0033C9AC /* DMEnvironmentConfiguration.h in Headers */,
8CDA628D2366DAA9004895B5 /* DMTraitCollection.h in Headers */,
8CDA628C2366DAA9004895B5 /* FluentDarkModeKit.h in Headers */,
8CB63E41238551F3008ABCE2 /* UIView+DarkModeKit.h in Headers */,
Expand Down Expand Up @@ -545,6 +552,7 @@
buildActionMask = 2147483647;
files = (
8CDA62972366DAA9004895B5 /* UIScrollView+DarkModeKit.swift in Sources */,
EAC41E3024CADD6E0033C9AC /* DMEnvironmentConfiguration.m in Sources */,
8CB63E42238551F3008ABCE2 /* UIView+DarkModeKit.m in Sources */,
8CDA62A12366DAA9004895B5 /* UITextField+DarkModeKit.swift in Sources */,
8CDA62952366DAA9004895B5 /* UIWindow+DarkModeKit.swift in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions Sources/DarkModeCore/DMEnvironmentConfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DMEnvironmentConfiguration : NSObject

@property (nonatomic) BOOL useImageAsset; // Defaults to NO

- (instancetype)init;

@end

NS_ASSUME_NONNULL_END
18 changes: 18 additions & 0 deletions Sources/DarkModeCore/DMEnvironmentConfiguration.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//

#import "DMEnvironmentConfiguration.h"

@implementation DMEnvironmentConfiguration

- (instancetype)init {
self = [super init];
if (self) {
_useImageAsset = NO;
}
return self;
}

@end
5 changes: 3 additions & 2 deletions Sources/DarkModeCore/DMTraitCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#import <UIKit/UIKit.h>

@class UITraitCollection;
@class UITraitCollection, DMEnvironmentConfiguration;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -36,7 +36,8 @@ typedef NS_ENUM(NSInteger, DMUserInterfaceStyle) {
+ (void)unregister;

// MARK: - Setup
+ (void)setupEnvironment:(BOOL)useUIImageAsset;
// TODO: Move to private header
+ (void)setupEnvironmentWithConfiguration:(DMEnvironmentConfiguration *)configuration;

@end

Expand Down
5 changes: 3 additions & 2 deletions Sources/DarkModeCore/DMTraitCollection.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT License.
//

#import "DMEnvironmentConfiguration.h"
#import "DMTraitCollection.h"
#import "UIView+DarkModeKitSwizzling.h"
#import "UIImage+DarkModeKitSwizzling.h"
Expand Down Expand Up @@ -259,14 +260,14 @@ + (void)swizzleUIScreenTraitCollectionDidChange API_AVAILABLE(ios(13.0)) {
});
}

+ (void)setupEnvironment:(BOOL)useUIImageAsset {
+ (void)setupEnvironmentWithConfiguration:(DMEnvironmentConfiguration *)configuration {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(iOS 13.0, *)) {
[DMTraitCollection swizzleUIScreenTraitCollectionDidChange];
[UIView swizzleTraitCollectionDidChangeToDMTraitCollectionDidChange];
[UIViewController swizzleTraitCollectionDidChangeToDMTraitCollectionDidChange];
if (!useUIImageAsset)
if (!configuration.useImageAsset)
[UIImage dm_swizzleIsEqual];
}
else {
Expand Down
1 change: 1 addition & 0 deletions Sources/DarkModeCore/FluentDarkModeKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#import <FluentDarkModeKit/DMDynamicColor.h>
#import <FluentDarkModeKit/DMDynamicImage.h>
#import <FluentDarkModeKit/DMEnvironmentConfiguration.h>
#import <FluentDarkModeKit/DMTraitCollection.h>
#import <FluentDarkModeKit/UIColor+DarkModeKit.h>
#import <FluentDarkModeKit/UIImage+DarkModeKit.h>
Expand Down
1 change: 1 addition & 0 deletions Sources/DarkModeCore/include/DarkModeCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import "../DMDynamicColor.h"
#import "../DMDynamicImage.h"
#import "../DMEnvironmentConfiguration"
#import "../DMNamespace.h"
#import "../DMTraitCollection.h"
#import "../UIColor+DarkModeKit.h"
Expand Down
12 changes: 6 additions & 6 deletions Sources/FluentDarkModeKit/DarkModeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import UIKit
public final class DarkModeManager: NSObject {
private static var swizzlingConfigured = false

public class func register(with application: UIApplication, useUIImageAsset: Bool, syncImmediately: Bool = false, animated: Bool = false) {
commonSetup(useUIImageAsset)
public class func register(with configuration: DMEnvironmentConfiguration, for application: UIApplication, syncImmediately: Bool = false, animated: Bool = false) {
commonSetup(with: configuration)
DMTraitCollection.register(with: application, syncImmediately: syncImmediately, animated: animated)
}

public class func register(with viewController: UIViewController, useUIImageAsset: Bool, syncImmediately: Bool = false, animated: Bool = false) {
commonSetup(useUIImageAsset)
public class func register(with configuration: DMEnvironmentConfiguration, for viewController: UIViewController, syncImmediately: Bool = false, animated: Bool = false) {
commonSetup(with: configuration)
DMTraitCollection.register(with: viewController, syncImmediately: syncImmediately, animated: animated)
}

public class func unregister() {
DMTraitCollection.unregister()
}

private class func commonSetup(_ useUIImageAsset: Bool) {
private class func commonSetup(with configuration: DMEnvironmentConfiguration) {
guard !swizzlingConfigured else {
return
}

defer { swizzlingConfigured = true }

DMTraitCollection.setupEnvironment(useUIImageAsset)
DMTraitCollection.setupEnvironment(with: configuration)
guard #available(iOS 13.0, *) else {
// Colors
UIView.swizzleWillMoveToWindowOnce
Expand Down
3 changes: 2 additions & 1 deletion Sources/FluentDarkModeKitExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {

DarkModeManager.register(with: application, useUIImageAsset: true)
let configuration = DMEnvironmentConfiguration()
DarkModeManager.register(with: configuration, for: application)

if #available(iOS 13.0, *) {
return true
Expand Down
4 changes: 2 additions & 2 deletions Tests/FluentDarkModeKitTests/DarkModeKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
final class DarkModeKitTests: XCTestCase {
func testSetBackgroundColorSwizzling() {
UIWindow.appearance().backgroundColor = .white
DarkModeManager.register(with: UIApplication.shared)
DarkModeManager.register(with: DMEnvironmentConfiguration(), for: .shared)
_ = UIWindow()
}

Expand Down Expand Up @@ -180,7 +180,7 @@ final class DarkModeKitTests: XCTestCase {
}
}
else {
let saved = DMTraitCollection.current
let saved = DMTraitCollection.override
DMTraitCollection.setOverride(DMTraitCollection(userInterfaceStyle: userInterfaceStyle), animated: false)
expression()
DMTraitCollection.setOverride(saved, animated: false)
Expand Down

0 comments on commit 0646f9c

Please sign in to comment.