feat:add set environment value api

This commit is contained in:
pengfei.zhou 2021-07-07 17:30:08 +08:00 committed by osborn
parent 0c10b513b9
commit e6595d5c51
18 changed files with 157 additions and 30 deletions

View File

@ -16,6 +16,13 @@
package pub.doric.demo;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.util.HashMap;
import java.util.Map;
import pub.doric.Doric;
import pub.doric.DoricRegistry;
@ -26,5 +33,16 @@ public class MyApplication extends Application {
super.onCreate();
Doric.init(this);
DoricRegistry.register(new DemoLibrary());
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Map<String, Object> map = new HashMap<>();
map.put("localeLanguage", context.getResources().getConfiguration().locale.getLanguage());
map.put("localeCountry", context.getResources().getConfiguration().locale.getCountry());
DoricRegistry.setEnvironmentValue(map);
}
}, intentFilter);
}
}

View File

@ -73,6 +73,7 @@ public class DoricRegistry {
private static final Map<String, String> bundles = new ConcurrentHashMap<>();
private static final Set<DoricLibrary> doricLibraries = new HashSet<>();
private static final List<WeakReference<DoricRegistry>> registries = new ArrayList<>();
private static final Map<String, Object> envMap = new ConcurrentHashMap<>();
private final Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private final Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
@ -98,6 +99,16 @@ public class DoricRegistry {
}
}
public static void setEnvironmentValue(Map<String, Object> value) {
envMap.putAll(value);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
registry.innerSetEnvironmentValue(value);
}
}
}
private final WeakReference<DoricJSEngine> doricJSEngineWeakReference;
public DoricRegistry(DoricJSEngine doricJSEngine) {
@ -136,6 +147,7 @@ public class DoricRegistry {
this.registerViewNode(SwitchNode.class);
this.registerViewNode(FlexNode.class);
initRegistry(this);
doricJSEngine.setEnvironmentValue(envMap);
registries.add(new WeakReference<>(this));
}
@ -169,12 +181,12 @@ public class DoricRegistry {
return bundles.get(name);
}
public void setEnvironmentVariable(String key, Object val) {
private void innerSetEnvironmentValue(Map<String, Object> value) {
DoricJSEngine doricJSEngine = doricJSEngineWeakReference.get();
if (doricJSEngine == null) {
return;
}
doricJSEngine.setEnvironmentVariable(key, val);
doricJSEngine.setEnvironmentValue(value);
}
public void registerMonitor(IDoricMonitor monitor) {

View File

@ -54,17 +54,17 @@ import pub.doric.utils.DoricUtils;
* @CreateDate: 2019-07-18
*/
public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback, IDoricMonitor {
private final HandlerThread handlerThread;
private final Handler mJSHandler;
private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension();
protected IDoricJSE mDoricJSE;
private final DoricTimerExtension mTimerExtension;
private final DoricRegistry mDoricRegistry = new DoricRegistry(this);
private final Map<String, Object> mEnvironmentMap = new ConcurrentHashMap<>();
private boolean initialized = false;
private final DoricRegistry mDoricRegistry;
private final Map<String, Object> mEnvironmentMap = new ConcurrentHashMap<>();
public DoricJSEngine() {
mDoricRegistry = new DoricRegistry(this);
handlerThread = new HandlerThread(this.getClass().getSimpleName());
handlerThread.start();
Looper looper = handlerThread.getLooper();
@ -90,8 +90,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
mDoricJSE = new DoricNativeJSExecutor();
}
public void setEnvironmentVariable(String key, Object v) {
mEnvironmentMap.put(key, v);
public void setEnvironmentValue(Map<String, Object> values) {
mEnvironmentMap.putAll(values);
if (initialized) {
final JSONBuilder jsonBuilder = new JSONBuilder();
for (String k : mEnvironmentMap.keySet()) {

View File

@ -27,6 +27,12 @@ class CounterView extends ViewHolder {
build(root: Group) {
vlayout(
[
text({
text: `Current language is ${Environment.localeLanguage}`,
}),
text({
text: `Current country is ${Environment.localeCountry}`,
}),
this.number = text({
textSize: 40,
tag: "tvNumber",
@ -67,6 +73,9 @@ class CounterVM extends ViewModel<CountModel, CounterView> {
@Entry
export class CounterPage extends VMPanel<CountModel, CounterView> {
state = {
count: 1,
}
constructor() {
super();
log("Constructor");
@ -80,8 +89,6 @@ export class CounterPage extends VMPanel<CountModel, CounterView> {
}
getState(): CountModel {
return {
count: 1,
};
return this.state;
}
}

View File

@ -9,10 +9,15 @@
#import "AppDelegate.h"
#import "NavigationController.h"
#import "ViewController.h"
#import "DoricRegistry.h"
#if __has_include(<SDWebImage/SDWebImage.h>)
#import <SDWebImage/SDWebImage.h>
#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
#endif
@interface AppDelegate ()
@property(nonatomic, strong) UIViewController *rootVC;
@property(nonatomic, strong) NavigationController *navigationController;
@ -21,8 +26,20 @@ @interface AppDelegate ()
@implementation AppDelegate
- (void)localeChanged {
[DoricRegistry setEnvironmentValue:@{
@"localeLanguage": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode],
@"localeCountry": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode],
}];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(localeChanged)
name:UITextFieldTextDidChangeNotification
object:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.rootVC = [[ViewController alloc] init];

View File

@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>

View File

@ -47,7 +47,7 @@ NS_ASSUME_NONNULL_BEGIN
- (Class)acquireViewNode:(NSString *)name;
- (void)setEnvironment:(NSString *)key variable:(id)value;
+ (void)setEnvironmentValue:(NSDictionary *)value;
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;

View File

@ -58,6 +58,7 @@
@interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@property(nonatomic, strong) NSMutableArray <NSValue *> *registries;
@property(nonatomic, strong) NSMutableDictionary *envDic;
+ (instancetype)instance;
@end
@ -67,6 +68,7 @@ - (instancetype)init {
if (self = [super init]) {
_libraries = [NSMutableSet new];
_registries = [NSMutableArray new];
_envDic = [NSMutableDictionary new];
}
return self;
}
@ -93,13 +95,6 @@ @interface DoricRegistry ()
@implementation DoricRegistry
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
if (self = [super init]) {
_jsEngine = jsEngine;
}
return self;
}
+ (void)register:(DoricLibrary *)library {
[DoricLibraries.instance.libraries addObject:library];
for (NSValue *value in DoricLibraries.instance.registries) {
@ -110,17 +105,29 @@ + (void)register:(DoricLibrary *)library {
}
}
- (instancetype)init {
+ (void)setEnvironmentValue:(NSDictionary *)value {
[DoricLibraries.instance.envDic addEntriesFromDictionary:value];
for (NSValue *val in DoricLibraries.instance.registries) {
DoricRegistry *registry = val.nonretainedObjectValue;
if (registry) {
[registry innerSetEnvironmentValue:value];
}
}
}
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine {
if (self = [super init]) {
_jsEngine = jsEngine;
_bundles = [NSMutableDictionary new];
_plugins = [NSMutableDictionary new];
_nodes = [NSMutableDictionary new];
[self innerRegister];
_monitors = [NSMutableSet new];
NSValue *value = [NSValue valueWithNonretainedObject:self];
[self innerRegister];
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self];
}];
NSValue *value = [NSValue valueWithNonretainedObject:self];
[jsEngine setEnvironmentValue:DoricLibraries.instance.envDic];
[DoricLibraries.instance.registries addObject:value];
}
return self;
@ -185,8 +192,8 @@ - (Class)acquireViewNode:(NSString *)name {
return self.nodes[name];
}
- (void)setEnvironment:(NSString *)key variable:(id)value {
[self.jsEngine setEnvironment:key variable:value];
- (void)innerSetEnvironmentValue:(NSDictionary *)value {
[self.jsEngine setEnvironmentValue:value];
}
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {

View File

@ -51,7 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)teardown;
- (void)setEnvironment:(NSString *)key variable:(id)value;
- (void)setEnvironmentValue:(NSDictionary *)value;
@end
NS_ASSUME_NONNULL_END

View File

@ -106,9 +106,9 @@ - (instancetype)init {
return self;
}
- (void)setEnvironment:(NSString *)key variable:(id)value {
- (void)setEnvironmentValue:(NSDictionary *)value {
[self ensureRunOnJSThread:^{
self.environmentDictionary[key] = value;
[self.environmentDictionary addEntriesFromDictionary:value];
if (self.initialized) {
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
for (DoricContext *doricContext in DoricContextManager.instance.aliveContexts) {

View File

@ -1066,6 +1066,10 @@ var Panel = /** @class */ (function () {
Panel.prototype.onDestroy = function () { };
Panel.prototype.onShow = function () { };
Panel.prototype.onHidden = function () { };
Panel.prototype.onEnvChanged = function () {
this.__root__.children.length = 0;
this.build(this.__root__);
};
Panel.prototype.addHeadView = function (type, v) {
var map = this.headviews.get(type);
if (map) {
@ -1128,6 +1132,9 @@ var Panel = /** @class */ (function () {
this.__root__.children.length = 0;
this.build(this.__root__);
};
Panel.prototype.__onEnvChanged__ = function () {
this.onEnvChanged();
};
Panel.prototype.__response__ = function (viewIds, callbackId) {
var arguments$1 = arguments;
@ -1359,6 +1366,12 @@ var Panel = /** @class */ (function () {
__metadata$b("design:paramtypes", [Object]),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__build__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),
__metadata$b("design:paramtypes", []),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__onEnvChanged__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),

View File

@ -830,6 +830,10 @@ class Panel {
onDestroy() { }
onShow() { }
onHidden() { }
onEnvChanged() {
this.__root__.children.length = 0;
this.build(this.__root__);
}
addHeadView(type, v) {
let map = this.headviews.get(type);
if (map) {
@ -892,6 +896,9 @@ class Panel {
this.__root__.children.length = 0;
this.build(this.__root__);
}
__onEnvChanged__() {
this.onEnvChanged();
}
__response__(viewIds, callbackId) {
const v = this.retrospectView(viewIds);
if (v === undefined) {
@ -1046,6 +1053,12 @@ __decorate$b([
__metadata$b("design:paramtypes", [Object]),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__build__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),
__metadata$b("design:paramtypes", []),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__onEnvChanged__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),

View File

@ -2351,6 +2351,10 @@ class Panel {
onDestroy() { }
onShow() { }
onHidden() { }
onEnvChanged() {
this.__root__.children.length = 0;
this.build(this.__root__);
}
addHeadView(type, v) {
let map = this.headviews.get(type);
if (map) {
@ -2413,6 +2417,9 @@ class Panel {
this.__root__.children.length = 0;
this.build(this.__root__);
}
__onEnvChanged__() {
this.onEnvChanged();
}
__response__(viewIds, callbackId) {
const v = this.retrospectView(viewIds);
if (v === undefined) {
@ -2567,6 +2574,12 @@ __decorate$b([
__metadata$b("design:paramtypes", [Object]),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__build__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),
__metadata$b("design:paramtypes", []),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__onEnvChanged__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),

1
doric-js/index.d.ts vendored
View File

@ -140,6 +140,7 @@ declare module 'doric/lib/src/ui/panel' {
onDestroy(): void;
onShow(): void;
onHidden(): void;
onEnvChanged(): void;
abstract build(rootView: Group): void;
addHeadView(type: string, v: View): void;
allHeadViews(): IterableIterator<Map<string, View>>;

View File

@ -9,6 +9,7 @@ export declare abstract class Panel {
onDestroy(): void;
onShow(): void;
onHidden(): void;
onEnvChanged(): void;
abstract build(rootView: Group): void;
private __data__?;
private __root__;
@ -27,6 +28,7 @@ export declare abstract class Panel {
private __onShow__;
private __onHidden__;
private __build__;
private __onEnvChanged__;
private __response__;
private retrospectView;
private nativeRender;

View File

@ -45,6 +45,10 @@ export class Panel {
onDestroy() { }
onShow() { }
onHidden() { }
onEnvChanged() {
this.__root__.children.length = 0;
this.build(this.__root__);
}
addHeadView(type, v) {
let map = this.headviews.get(type);
if (map) {
@ -107,6 +111,9 @@ export class Panel {
this.__root__.children.length = 0;
this.build(this.__root__);
}
__onEnvChanged__() {
this.onEnvChanged();
}
__response__(viewIds, callbackId) {
const v = this.retrospectView(viewIds);
if (v === undefined) {
@ -261,6 +268,12 @@ __decorate([
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], Panel.prototype, "__build__", null);
__decorate([
NativeCall,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], Panel.prototype, "__onEnvChanged__", null);
__decorate([
NativeCall,
__metadata("design:type", Function),

View File

@ -2405,6 +2405,10 @@ class Panel {
onDestroy() { }
onShow() { }
onHidden() { }
onEnvChanged() {
this.__root__.children.length = 0;
this.build(this.__root__);
}
addHeadView(type, v) {
let map = this.headviews.get(type);
if (map) {
@ -2467,6 +2471,9 @@ class Panel {
this.__root__.children.length = 0;
this.build(this.__root__);
}
__onEnvChanged__() {
this.onEnvChanged();
}
__response__(viewIds, callbackId) {
const v = this.retrospectView(viewIds);
if (v === undefined) {
@ -2621,6 +2628,12 @@ __decorate$b([
__metadata$b("design:paramtypes", [Object]),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__build__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),
__metadata$b("design:paramtypes", []),
__metadata$b("design:returntype", void 0)
], Panel.prototype, "__onEnvChanged__", null);
__decorate$b([
NativeCall,
__metadata$b("design:type", Function),

File diff suppressed because one or more lines are too long