Skip to content

Commit

Permalink
[iOS] Optimize dark scheme logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
qianyuan.wqy committed Dec 3, 2019
1 parent aa93abc commit 8d8751b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ - (JSValue*)exception

- (void)resetEnvironment
{
NSDictionary *data = [WXUtility getEnvironment];
NSDictionary *data = [WXUtility getEnvironmentForJSContext];
_jsContext[@"WXEnvironment"] = data;
}

Expand Down
6 changes: 6 additions & 0 deletions ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ - (instancetype)initWithRenderType:(NSString*)renderType
self.schemeName = @"light";
}

// Update scheme value in JS environment.
if (([WXUtility isEnvironmentUsingDarkScheme] && [self.schemeName isEqualToString:@"light"]) ||
(![WXUtility isEnvironmentUsingDarkScheme] && [self.schemeName isEqualToString:@"dark"])) {
[[WXBridgeManager sharedManager] resetEnvironment];
}

_autoInvertingBehavior = WXAutoInvertingBehaviorDefault;
_renderType = renderType;
_appearState = YES;
Expand Down
6 changes: 3 additions & 3 deletions ios/sdk/WeexSDK/Sources/Module/WXDarkSchemeModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ @implementation WXDarkSchemeModule

@synthesize weexInstance;

WX_EXPORT_METHOD_SYNC(@selector(isUsingDarkScheme))
WX_EXPORT_METHOD_SYNC(@selector(getCurrentScheme))
WX_EXPORT_METHOD(@selector(registerSchemeChangeListener:))
WX_EXPORT_METHOD(@selector(unregisterSchemeChangeListener))

- (BOOL)isUsingDarkScheme
- (NSString*)getCurrentScheme
{
return [self.weexInstance isDarkScheme];
return [self.weexInstance isDarkScheme] ? @"dark" : @"light";
}

- (void)registerSchemeChangeListener:(WXModuleKeepAliveCallback)callback
Expand Down
2 changes: 2 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ _Nonnull SEL WXSwizzledSelectorForSelector(_Nonnull SEL selector);
*
*/
+ (NSDictionary *_Nonnull)getEnvironment;
+ (NSDictionary *_Nonnull)getEnvironmentForJSContext;
+ (BOOL)isEnvironmentUsingDarkScheme;

+ (NSDictionary *_Nonnull)getDebugEnvironment;

Expand Down
14 changes: 14 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ + (NSDictionary *)getEnvironment
return data;
}

static BOOL gIsEnvironmentUsingDarkScheme = NO;

+ (NSDictionary *_Nonnull)getEnvironmentForJSContext
{
NSDictionary* result = [self getEnvironment];
gIsEnvironmentUsingDarkScheme = [result[@"scheme"] isEqualToString:@"dark"];
return result;
}

+ (BOOL)isEnvironmentUsingDarkScheme
{
return gIsEnvironmentUsingDarkScheme;
}

+ (NSDictionary *)getDebugEnvironment {
NSString *platform = @"iOS";
NSString *weexVersion = [WXSDKEngine SDKEngineVersion];
Expand Down
16 changes: 16 additions & 0 deletions ios/sdk/WeexSDK/Sources/View/WXRootView.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#import "WXSDKInstance.h"
#import "WXPageEventNotifyEvent.h"
#import "WXSDKEngine.h"
#import "WXUtility.h"
#import "WXBridgeManager.h"

@interface WXRootView()
{
Expand Down Expand Up @@ -63,6 +65,18 @@ - (BOOL)isHasEvent
return _mHasEvent;
}

- (void)checkUpdateEnvironment:(NSInteger)currentStyle
{
if (@available(iOS 13.0, *)) {
if (([WXUtility isEnvironmentUsingDarkScheme] && (UIUserInterfaceStyle)currentStyle == UIUserInterfaceStyleLight) ||
(![WXUtility isEnvironmentUsingDarkScheme] && (UIUserInterfaceStyle)currentStyle == UIUserInterfaceStyleDark)) {
// Update scheme value in JS environment.
[WXUtility getEnvironmentForJSContext]; // Update gIsEnvironmentUsingDarkScheme
[[WXBridgeManager sharedManager] resetEnvironment];
}
}
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[super traitCollectionDidChange:previousTraitCollection];
Expand All @@ -73,6 +87,7 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
if (currentStyle != previousTraitCollection.userInterfaceStyle) {
if (_hasFirstTraitCollectionChange) {
_allowFirstTraitCollectionChange = NO;
[self checkUpdateEnvironment:(NSInteger)currentStyle];
[self.instance setCurrentSchemeName:currentStyle == UIUserInterfaceStyleDark ? @"dark" : @"light"];
}
else {
Expand All @@ -84,6 +99,7 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
if (strongSelf) {
if (strongSelf->_allowFirstTraitCollectionChange) {
if (strongSelf.instance) {
[self checkUpdateEnvironment:(NSInteger)currentStyle];
[strongSelf.instance setCurrentSchemeName:currentStyle == UIUserInterfaceStyleDark ? @"dark" : @"light"];
}
}
Expand Down

0 comments on commit 8d8751b

Please sign in to comment.