Add theme change handler
This commit is contained in:
Родитель
5883735aa9
Коммит
3091c0ad93
|
@ -10,6 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
@interface DMEnvironmentConfiguration : NSObject
|
||||
|
||||
@property (nonatomic) BOOL useImageAsset; // Defaults to NO
|
||||
@property (nullable, nonatomic) void (^themeChangeHandler)(void); // Defaults to nil
|
||||
|
||||
- (instancetype)init;
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
self = [super init];
|
||||
if (self) {
|
||||
_useImageAsset = NO;
|
||||
_themeChangeHandler = nil;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
|
|
@ -51,10 +51,4 @@ typedef NS_ENUM(NSInteger, DMUserInterfaceStyle) {
|
|||
|
||||
@end
|
||||
|
||||
@interface NSObject (DMTraitEnvironment)
|
||||
|
||||
+ (void)swizzleTraitCollectionDidChangeToDMTraitCollectionDidChange API_AVAILABLE(ios(13.0));
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
|
@ -10,6 +10,12 @@
|
|||
|
||||
@import ObjectiveC;
|
||||
|
||||
@interface NSObject (DMTraitEnvironment)
|
||||
|
||||
+ (void)swizzleTraitCollectionDidChangeToDMTraitCollectionDidChange API_AVAILABLE(ios(13.0));
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSObject (DMTraitEnvironment)
|
||||
|
||||
+ (void)swizzleTraitCollectionDidChangeToDMTraitCollectionDidChange {
|
||||
|
@ -49,6 +55,7 @@
|
|||
|
||||
static DMTraitCollection *_overrideTraitCollection = nil; // This is set manually in setOverrideTraitCollection:animated
|
||||
static void (^_userInterfaceStyleChangeHandler)(DMTraitCollection *, BOOL) = nil;
|
||||
static void (^_themeChangeHandler)(void) = nil;
|
||||
static BOOL _isObservingNewWindowAddNotification = NO;
|
||||
|
||||
+ (DMTraitCollection *)currentTraitCollection {
|
||||
|
@ -195,6 +202,9 @@ static BOOL _isObservingNewWindowAddNotification = NO;
|
|||
return;
|
||||
|
||||
[weakSelf updateUIWithViews:strongApp.windows viewControllers:nil traitCollection:traitCollection animated:animated];
|
||||
|
||||
if (_themeChangeHandler)
|
||||
_themeChangeHandler();
|
||||
};
|
||||
|
||||
[self observeNewWindowNotificationIfNeeded];
|
||||
|
@ -212,6 +222,9 @@ static BOOL _isObservingNewWindowAddNotification = NO;
|
|||
return;
|
||||
|
||||
[weakSelf updateUIWithViews:nil viewControllers:[NSArray arrayWithObject:strongVc] traitCollection:traitCollection animated:animated];
|
||||
|
||||
if (_themeChangeHandler)
|
||||
_themeChangeHandler();
|
||||
};
|
||||
|
||||
if (syncImmediately)
|
||||
|
@ -275,6 +288,8 @@ static BOOL _isObservingNewWindowAddNotification = NO;
|
|||
[UIView dm_swizzleSetBackgroundColor];
|
||||
[UIImage dm_swizzleIsEqual];
|
||||
}
|
||||
|
||||
_themeChangeHandler = configuration.themeChangeHandler;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#import "../DMDynamicColor.h"
|
||||
#import "../DMDynamicImage.h"
|
||||
#import "../DMEnvironmentConfiguration"
|
||||
#import "../DMEnvironmentConfiguration.h"
|
||||
#import "../DMNamespace.h"
|
||||
#import "../DMTraitCollection.h"
|
||||
#import "../UIColor+DarkModeKit.h"
|
||||
|
|
|
@ -11,21 +11,24 @@ import UIKit
|
|||
public final class DarkModeManager: NSObject {
|
||||
private static var swizzlingConfigured = false
|
||||
|
||||
public class func register(with configuration: DMEnvironmentConfiguration, for application: UIApplication, syncImmediately: Bool = false, animated: Bool = false) {
|
||||
public static func setup(with configuration: DMEnvironmentConfiguration) {
|
||||
commonSetup(with: configuration)
|
||||
}
|
||||
|
||||
public static func register(with application: UIApplication, syncImmediately: Bool = false, animated: Bool = false) {
|
||||
DMTraitCollection.register(with: application, syncImmediately: syncImmediately, animated: animated)
|
||||
}
|
||||
|
||||
public class func register(with configuration: DMEnvironmentConfiguration, for viewController: UIViewController, syncImmediately: Bool = false, animated: Bool = false) {
|
||||
commonSetup(with: configuration)
|
||||
@available(iOSApplicationExtension 11.0, *)
|
||||
public static func register(with viewController: UIViewController, syncImmediately: Bool = false, animated: Bool = false) {
|
||||
DMTraitCollection.register(with: viewController, syncImmediately: syncImmediately, animated: animated)
|
||||
}
|
||||
|
||||
public class func unregister() {
|
||||
public static func unregister() {
|
||||
DMTraitCollection.unregister()
|
||||
}
|
||||
|
||||
private class func commonSetup(with configuration: DMEnvironmentConfiguration) {
|
||||
private static func commonSetup(with configuration: DMEnvironmentConfiguration) {
|
||||
guard !swizzlingConfigured else {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -16,7 +16,11 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||
) -> Bool {
|
||||
|
||||
let configuration = DMEnvironmentConfiguration()
|
||||
DarkModeManager.register(with: configuration, for: application)
|
||||
configuration.themeChangeHandler = {
|
||||
print("theme changed")
|
||||
}
|
||||
DarkModeManager.setup(with: configuration)
|
||||
DarkModeManager.register(with: application)
|
||||
|
||||
if #available(iOS 13.0, *) {
|
||||
return true
|
||||
|
|
|
@ -9,7 +9,8 @@ import XCTest
|
|||
final class DarkModeKitTests: XCTestCase {
|
||||
func testSetBackgroundColorSwizzling() {
|
||||
UIWindow.appearance().backgroundColor = .white
|
||||
DarkModeManager.register(with: DMEnvironmentConfiguration(), for: .shared)
|
||||
DarkModeManager.setup(with: DMEnvironmentConfiguration())
|
||||
DarkModeManager.register(with: .shared)
|
||||
_ = UIWindow()
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче