From 50458857580a4ff52d3f37b35cd68ec12b32dc44 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Wed, 26 Jul 2023 21:00:52 +0800 Subject: [PATCH] feat(ios): add NightMode change event --- docs/en-us/hippy-react/native-event.md | 15 +++++++++++++++ docs/en-us/hippy-vue/native-event.md | 13 +++++++++++++ docs/hippy-react/native-event.md | 15 +++++++++++++++ docs/hippy-vue/native-event.md | 13 +++++++++++++ ios/sdk/base/HippyBatchedBridge.mm | 21 +++++++++++++++------ ios/sdk/base/HippyBridge+Private.h | 2 -- ios/sdk/base/HippyBridge.h | 4 ++-- ios/sdk/base/HippyBridge.mm | 4 ++-- ios/sdk/base/HippyRootView.mm | 2 +- 9 files changed, 76 insertions(+), 13 deletions(-) diff --git a/docs/en-us/hippy-react/native-event.md b/docs/en-us/hippy-react/native-event.md index 14de00df451..0434eb62252 100644 --- a/docs/en-us/hippy-react/native-event.md +++ b/docs/en-us/hippy-react/native-event.md @@ -159,3 +159,18 @@ hippyEventEmitter.addListener('onSizeChanged', ({ oldWidth, oldHeight, width, he console.log('size', oldWidth, oldHeight, width, height); }); ``` + +# System night mode change event + +`Only supported by iOS, the minimum supported version is 2.16.6, (Note: The page will be recreated when Android modifies the night mode)` + +This event is triggered when the system night mode changes + +```jsx +import { HippyEventEmitter } from '@hippy/react'; +const hippyEventEmitter = new HippyEventEmitter(); +hippyEventEmitter. addListener('onNightModeChanged', ({ NightMode, RootViewTag }) => { + // NightMode: whether the current night mode, the value is 0 or 1; RootViewTag: the Tag of the HippyRootView that sends the event + console.log(`onDarkModeChanged: ${NightMode}, rootViewTag: ${RootViewTag}`); +}); +``` diff --git a/docs/en-us/hippy-vue/native-event.md b/docs/en-us/hippy-vue/native-event.md index 896cac692e6..2f458344fc3 100644 --- a/docs/en-us/hippy-vue/native-event.md +++ b/docs/en-us/hippy-vue/native-event.md @@ -86,3 +86,16 @@ app.$on('onSizeChanged', ({ oldWidth, oldHeight, width, height }) => { console.log('size', oldWidth, oldHeight, width, height); }); ``` + +# System night mode change event + +`Only supported by iOS, the minimum supported version is 2.16.6, (Note: The page will be recreated when Android modifies the night mode)` + +This event is triggered when the system night mode changes + +```jsx +app.$on('onNightModeChanged', ({ NightMode, RootViewTag }) => { + // NightMode: whether the current night mode, the value is 0 or 1; RootViewTag: the Tag of the HippyRootView that sends the event + console.log(`onDarkModeChanged: ${NightMode}, rootViewTag: ${RootViewTag}`); +}); +``` diff --git a/docs/hippy-react/native-event.md b/docs/hippy-react/native-event.md index 0580baf235a..559f169b8b0 100644 --- a/docs/hippy-react/native-event.md +++ b/docs/hippy-react/native-event.md @@ -158,3 +158,18 @@ hippyEventEmitter.addListener('onSizeChanged', ({ oldWidth, oldHeight, width, he console.log('size', oldWidth, oldHeight, width, height); }); ``` + +# 系统夜间模式改变事件 + +`仅iOS支持,最低支持版本 2.16.6,(注:Android修改夜间模式时页面将被重新创建)` + +在当系统夜间模式发生改变时,会触发该事件 + +```jsx +import { HippyEventEmitter } from '@hippy/react'; +const hippyEventEmitter = new HippyEventEmitter(); +hippyEventEmitter.addListener('onNightModeChanged', ({ NightMode, RootViewTag }) => { + // NightMode: 当前是否夜间模式,取值0或1;RootViewTag: 发送事件的HippyRootView的Tag + console.log(`onDarkModeChanged: ${NightMode}, rootViewTag: ${RootViewTag}`); +}); +``` diff --git a/docs/hippy-vue/native-event.md b/docs/hippy-vue/native-event.md index c40effae445..6ccbcad1083 100644 --- a/docs/hippy-vue/native-event.md +++ b/docs/hippy-vue/native-event.md @@ -86,3 +86,16 @@ app.$on('onSizeChanged', ({ oldWidth, oldHeight, width, height }) => { console.log('size', oldWidth, oldHeight, width, height); }); ``` + +# 系统夜间模式改变事件 + +`仅iOS支持,最低支持版本 2.16.6,(注:Android修改夜间模式时页面将被重新创建)` + +在当系统夜间模式发生改变时,会触发该事件 + +```jsx +app.$on('onNightModeChanged', ({ NightMode, RootViewTag }) => { + // NightMode: 当前是否夜间模式,取值0或1;RootViewTag: 发送事件的HippyRootView的Tag + console.log(`onDarkModeChanged: ${NightMode}, rootViewTag: ${RootViewTag}`); +}); +``` diff --git a/ios/sdk/base/HippyBatchedBridge.mm b/ios/sdk/base/HippyBatchedBridge.mm index 6dfb028f3bc..be0cfda8830 100644 --- a/ios/sdk/base/HippyBatchedBridge.mm +++ b/ios/sdk/base/HippyBatchedBridge.mm @@ -41,8 +41,9 @@ #import "HippyBundleURLProvider.h" #import "HippyTurboModuleManager.h" #import "HippyDeviceBaseInfo.h" +#import "HippyEventDispatcher.h" #import -#include "core/scope.h" +#import "core/scope.h" #define HippyAssertJSThread() // TODO: add assert imp @@ -54,7 +55,7 @@ static NSString *const HippyNativeGlobalKeyAppVersion = @"AppVersion"; static NSString *const HippyNativeGlobalKeyDimensions = @"Dimensions"; static NSString *const HippyNativeGlobalKeyLocalization = @"Localization"; -NSString *const HippyNativeGlobalKeyNightMode = @"NightMode"; +static NSString *const HippyNativeGlobalKeyNightMode = @"NightMode"; /** @@ -1235,11 +1236,19 @@ - (void)updateNativeInfoToHippyGlobalObject:(NSDictionary *)nativeInfo { [self.javaScriptExecutor updateNativeInfoToHippyGlobalObject:nativeInfo]; } -- (void)setOSNightMode:(BOOL)isOSNightMode notifyToJS:(BOOL)shouldNotify { +static NSString *const hippyOnNightModeChangedEvent = @"onNightModeChanged"; +static NSString *const hippyOnNightModeChangedParam1 = @"NightMode"; +static NSString *const hippyOnNightModeChangedParam2 = @"RootViewTag"; + +- (void)setOSNightMode:(BOOL)isOSNightMode withRootViewTag:(nonnull NSNumber *)rootViewTag { _isOSNightMode = isOSNightMode; - if (shouldNotify) { - [self updateNativeInfoToHippyGlobalObject:@{HippyNativeGlobalKeyNightMode: @(isOSNightMode)}]; - } + // notify to js side + [self updateNativeInfoToHippyGlobalObject:@{HippyNativeGlobalKeyNightMode: @(isOSNightMode)}]; + NSDictionary *args = @{@"eventName": hippyOnNightModeChangedEvent, + @"extra": @{ hippyOnNightModeChangedParam1 : @(isOSNightMode), + hippyOnNightModeChangedParam2 : rootViewTag } }; + [self.parentBridge.eventDispatcher dispatchEvent:@"EventDispatcher" + methodName:@"receiveNativeEvent" args:args]; } @end diff --git a/ios/sdk/base/HippyBridge+Private.h b/ios/sdk/base/HippyBridge+Private.h index 1d9be2d62e1..90ac95beafe 100644 --- a/ios/sdk/base/HippyBridge+Private.h +++ b/ios/sdk/base/HippyBridge+Private.h @@ -23,8 +23,6 @@ #import "HippyBridge.h" #import "HippyJavaScriptExecutor.h" -FOUNDATION_EXTERN NSString *const HippyNativeGlobalKeyNightMode; - @class HippyModuleData; @protocol HippyJavaScriptExecutor; diff --git a/ios/sdk/base/HippyBridge.h b/ios/sdk/base/HippyBridge.h index 75b106b4d93..fe39c390e6e 100644 --- a/ios/sdk/base/HippyBridge.h +++ b/ios/sdk/base/HippyBridge.h @@ -266,8 +266,8 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass); /// update `NightMode` state when changed /// - Parameter isOSNightMode: bool -/// - Parameter shouldNotify: should notify js side or not -- (void)setOSNightMode:(BOOL)isOSNightMode notifyToJS:(BOOL)shouldNotify; +/// - Parameter rootViewTag: rootView's hippyTag +- (void)setOSNightMode:(BOOL)isOSNightMode withRootViewTag:(NSNumber *)rootViewTag; #pragma mark - Turbo Module diff --git a/ios/sdk/base/HippyBridge.mm b/ios/sdk/base/HippyBridge.mm index 5585ee9af3d..50aeb9cb676 100644 --- a/ios/sdk/base/HippyBridge.mm +++ b/ios/sdk/base/HippyBridge.mm @@ -409,8 +409,8 @@ - (BOOL)isOSNightMode { return self.batchedBridge.isOSNightMode; } -- (void)setOSNightMode:(BOOL)isOSNightMode notifyToJS:(BOOL)shouldNotify { - [self.batchedBridge setOSNightMode:isOSNightMode notifyToJS:shouldNotify]; +- (void)setOSNightMode:(BOOL)isOSNightMode withRootViewTag:(NSNumber *)rootViewTag { + [self.batchedBridge setOSNightMode:isOSNightMode withRootViewTag:rootViewTag]; } #pragma mark - diff --git a/ios/sdk/base/HippyRootView.mm b/ios/sdk/base/HippyRootView.mm index 322f48d1a3b..f71aa7117ae 100644 --- a/ios/sdk/base/HippyRootView.mm +++ b/ios/sdk/base/HippyRootView.mm @@ -401,7 +401,7 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { if (currentStyle != previousTraitCollection.userInterfaceStyle) { BOOL isNightMode = (UIUserInterfaceStyleDark == currentStyle); if (self.bridge.isOSNightMode != isNightMode) { - [self.bridge setOSNightMode:isNightMode notifyToJS:YES]; + [self.bridge setOSNightMode:isNightMode withRootViewTag:self.hippyTag]; } } }