From 821cb1823c8e8a4f977baba1254cd9242650a2cb Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 4 Sep 2020 18:51:08 +0800 Subject: [PATCH] iOS support internal class --- .../doric/loader/DoricJSLoaderManager.java | 2 +- doric-demo/src/Counter.ts | 14 ++-- doric-demo/src/MultiPanelDemo.ts | 78 ++++--------------- .../Pod/Classes/Loader/DoricJSLoaderManager.m | 22 ++++++ doric-js/bundle/doric-lib.es5.js | 6 +- doric-js/bundle/doric-lib.js | 6 +- doric-js/bundle/doric-vm.js | 6 +- doric-js/index.d.ts | 1 + doric-js/lib/src/native/navigator.d.ts | 1 + doric-js/lib/src/native/navigator.js | 5 +- doric-js/src/native/navigator.ts | 6 +- 11 files changed, 70 insertions(+), 77 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java b/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java index aaa4a1a9..666280d6 100644 --- a/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java +++ b/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java @@ -57,7 +57,7 @@ public class DoricJSLoaderManager { public AsyncResult loadJSBundle(String source) { if (!TextUtils.isEmpty(source)) { - if (source.startsWith("_internal_")) { + if (source.startsWith("_internal_://")) { Uri uri = Uri.parse(source); String srcContextId = uri.getQueryParameter("context"); String className = uri.getQueryParameter("class"); diff --git a/doric-demo/src/Counter.ts b/doric-demo/src/Counter.ts index dc3633bc..7bcb2361 100644 --- a/doric-demo/src/Counter.ts +++ b/doric-demo/src/Counter.ts @@ -9,15 +9,15 @@ class CounterView extends ViewHolder { build(root: Group) { vlayout( [ - text({ - textSize: 40, - tag:"tvNumber" + text({ + textSize: 40, + tag: "tvNumber" }), text({ text: "Click To Count 1", textSize: 20, - tag:"tvCounter" + tag: "tvCounter" }), ], { @@ -26,8 +26,8 @@ class CounterView extends ViewHolder { space: 20, } ).in(root) - this.number= root.findViewByTag("tvNumber")! - this.counter= root.findViewByTag("tvCounter")! + this.number = root.findViewByTag("tvNumber")! + this.counter = root.findViewByTag("tvCounter")! } } @@ -45,7 +45,7 @@ class CounterVM extends ViewModel { } @Entry -class MyPage extends VMPanel{ +export class CounterPage extends VMPanel{ getViewHolderClass() { diff --git a/doric-demo/src/MultiPanelDemo.ts b/doric-demo/src/MultiPanelDemo.ts index 651f7742..c7f0e608 100644 --- a/doric-demo/src/MultiPanelDemo.ts +++ b/doric-demo/src/MultiPanelDemo.ts @@ -1,77 +1,16 @@ import { Group, Panel, navbar, text, gravity, Color, LayoutSpec, vlayout, Gravity, hlayout, scroller, layoutConfig, image, modal, navigator, ViewHolder, Text, ViewModel, VMPanel } from "doric"; import { title, label, colors } from "./utils"; import { ModalDemo } from "./ModalDemo"; +import { CounterPage } from './Counter' -interface CountModel { - count: number -} -class CounterView extends ViewHolder { - number!: Text - counter!: Text - build(root: Group) { - vlayout( - [ - text({ - textSize: 40, - tag: "tvNumber" - }), - - text({ - text: "Click To Count 1", - textSize: 20, - tag: "tvCounter" - }), - ], - { - layoutConfig: layoutConfig().most(), - gravity: Gravity.Center, - space: 20, - } - ).in(root) - this.number = root.findViewByTag("tvNumber")! - this.counter = root.findViewByTag("tvCounter")! - } -} - -class CounterVM extends ViewModel { - onAttached(s: CountModel, vh: CounterView) { - vh.counter.onClick = () => { - this.updateState(state => { - state.count++ - }) - } - } - onBind(s: CountModel, vh: CounterView) { - vh.number.text = `${s.count}` - } -} - -class MyPage extends VMPanel{ - - - getViewHolderClass() { - return CounterView - } - - getViewModelClass() { - return CounterVM - } - - getState(): CountModel { - return { - count: 0 - } - } -} - -@Entry(exports = [ModalDemo, MyPage]) +@Entry(exports = [ModalDemo, CounterPage]) class MultiPanelDemo extends Panel { build(rootView: Group): void { scroller( vlayout( [ title("Multi Panel"), - label('isHidden').apply({ + label('ModalDemo').apply({ width: 200, height: 50, backgroundColor: colors[0], @@ -82,6 +21,17 @@ class MultiPanelDemo extends Panel { navigator(context).push(ModalDemo) } }), + label('Counter').apply({ + width: 200, + height: 50, + backgroundColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().just(), + onClick: () => { + navigator(context).push(CounterPage) + } + }), ], { layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT), diff --git a/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m b/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m index 016620a0..0b288aa0 100644 --- a/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m +++ b/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m @@ -57,6 +57,28 @@ - (void)addJSLoader:(id )loader { - (DoricAsyncResult *)request:(NSString *)source { __block DoricAsyncResult *ret; + if ([source hasPrefix:@"_internal_://"]) { + ret = [DoricAsyncResult new]; + __block NSString *contextId = nil; + __block NSString *className = nil; + NSURLComponents *components = [NSURLComponents componentsWithString:source]; + [components.queryItems forEach:^(NSURLQueryItem *obj) { + if ([obj.name isEqualToString:@"class"]) { + className = obj.value; + } else if ([obj.name isEqualToString:@"context"]) { + contextId = obj.value; + } + }]; + if (contextId && className) { + [ret setupResult:[NSString stringWithFormat:@"Entry('%@','%@')", contextId, className]]; + } else { + [ret setupError:[NSException exceptionWithName:@"LoadingError" + reason:[NSString stringWithFormat:@"Scheme %@ format error", source] + userInfo:nil]]; + } + return ret; + + } [self.loaders enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { if ([obj filter:source]) { ret = [obj request:source]; diff --git a/doric-js/bundle/doric-lib.es5.js b/doric-js/bundle/doric-lib.es5.js index adec5420..f22ffdd2 100644 --- a/doric-js/bundle/doric-lib.es5.js +++ b/doric-js/bundle/doric-lib.es5.js @@ -2959,12 +2959,15 @@ function navbar(context) { }; } +function internalScheme(context, panelClass) { + return "_internal_://export?class=" + encodeURIComponent(panelClass.name) + "&context=" + context.id; +} function navigator(context) { var moduleName = "navigator"; return { push: function (source, config) { if (typeof source === 'function') { - source = "_internal_://export?class=" + encodeURIComponent(source.name) + "&context=" + context.id; + source = internalScheme(context, source); } if (config && config.extra) { config.extra = JSON.stringify(config.extra); @@ -3536,6 +3539,7 @@ exports.gravity = gravity; exports.hlayout = hlayout; exports.image = image; exports.input = input; +exports.internalScheme = internalScheme; exports.layoutConfig = layoutConfig; exports.list = list; exports.listItem = listItem; diff --git a/doric-js/bundle/doric-lib.js b/doric-js/bundle/doric-lib.js index 5de3bf9b..d15e4094 100644 --- a/doric-js/bundle/doric-lib.js +++ b/doric-js/bundle/doric-lib.js @@ -2291,12 +2291,15 @@ function navbar(context) { }; } +function internalScheme(context, panelClass) { + return `_internal_://export?class=${encodeURIComponent(panelClass.name)}&context=${context.id}`; +} function navigator(context) { const moduleName = "navigator"; return { push: (source, config) => { if (typeof source === 'function') { - source = `_internal_://export?class=${encodeURIComponent(source.name)}&context=${context.id}`; + source = internalScheme(context, source); } if (config && config.extra) { config.extra = JSON.stringify(config.extra); @@ -2754,6 +2757,7 @@ exports.gravity = gravity; exports.hlayout = hlayout; exports.image = image; exports.input = input; +exports.internalScheme = internalScheme; exports.layoutConfig = layoutConfig; exports.list = list; exports.listItem = listItem; diff --git a/doric-js/bundle/doric-vm.js b/doric-js/bundle/doric-vm.js index 32fa2c31..dd5fad09 100644 --- a/doric-js/bundle/doric-vm.js +++ b/doric-js/bundle/doric-vm.js @@ -3786,12 +3786,15 @@ function navbar(context) { }; } +function internalScheme(context, panelClass) { + return `_internal_://export?class=${encodeURIComponent(panelClass.name)}&context=${context.id}`; +} function navigator(context) { const moduleName = "navigator"; return { push: (source, config) => { if (typeof source === 'function') { - source = `_internal_://export?class=${encodeURIComponent(source.name)}&context=${context.id}`; + source = internalScheme(context, source); } if (config && config.extra) { config.extra = JSON.stringify(config.extra); @@ -4384,6 +4387,7 @@ exports.gravity = gravity; exports.hlayout = hlayout; exports.image = image; exports.input = input; +exports.internalScheme = internalScheme; exports.layoutConfig = layoutConfig; exports.list = list; exports.listItem = listItem; diff --git a/doric-js/index.d.ts b/doric-js/index.d.ts index 16fb61aa..1d4c80b6 100644 --- a/doric-js/index.d.ts +++ b/doric-js/index.d.ts @@ -842,6 +842,7 @@ declare module 'doric/lib/src/native/navigator' { import { BridgeContext } from "doric/lib/src/runtime/global"; import { ClassType } from "doric/lib/src/pattern/mvvm"; import { Panel } from "doric/lib/src/ui/panel"; + export function internalScheme(context: BridgeContext, panelClass: ClassType): string; export function navigator(context: BridgeContext): { push: (source: string | ClassType, config?: { alias?: string | undefined; diff --git a/doric-js/lib/src/native/navigator.d.ts b/doric-js/lib/src/native/navigator.d.ts index 98f3f042..6e93c384 100644 --- a/doric-js/lib/src/native/navigator.d.ts +++ b/doric-js/lib/src/native/navigator.d.ts @@ -1,6 +1,7 @@ import { BridgeContext } from "../runtime/global"; import { ClassType } from "../pattern/mvvm"; import { Panel } from "../ui/panel"; +export declare function internalScheme(context: BridgeContext, panelClass: ClassType): string; export declare function navigator(context: BridgeContext): { push: (source: string | ClassType, config?: { alias?: string | undefined; diff --git a/doric-js/lib/src/native/navigator.js b/doric-js/lib/src/native/navigator.js index 838bb3e9..b0a2a5b8 100644 --- a/doric-js/lib/src/native/navigator.js +++ b/doric-js/lib/src/native/navigator.js @@ -1,9 +1,12 @@ +export function internalScheme(context, panelClass) { + return `_internal_://export?class=${encodeURIComponent(panelClass.name)}&context=${context.id}`; +} export function navigator(context) { const moduleName = "navigator"; return { push: (source, config) => { if (typeof source === 'function') { - source = `_internal_://export?class=${encodeURIComponent(source.name)}&context=${context.id}`; + source = internalScheme(context, source); } if (config && config.extra) { config.extra = JSON.stringify(config.extra); diff --git a/doric-js/src/native/navigator.ts b/doric-js/src/native/navigator.ts index cb9ff808..e3b27d34 100644 --- a/doric-js/src/native/navigator.ts +++ b/doric-js/src/native/navigator.ts @@ -17,6 +17,10 @@ import { BridgeContext } from "../runtime/global" import { ClassType } from "../pattern/mvvm" import { Panel } from "../ui/panel" +export function internalScheme(context: BridgeContext, panelClass: ClassType) { + return `_internal_://export?class=${encodeURIComponent(panelClass.name)}&context=${context.id}` +} + export function navigator(context: BridgeContext) { const moduleName = "navigator" return { @@ -27,7 +31,7 @@ export function navigator(context: BridgeContext) { singlePage?: boolean, }) => { if (typeof source === 'function') { - source = `_internal_://export?class=${encodeURIComponent(source.name)}&context=${context.id}` + source = internalScheme(context, source) } if (config && config.extra) { (config as any).extra = JSON.stringify(config.extra)