add ResourceLoader plugin

This commit is contained in:
pengfei.zhou 2021-11-18 16:38:35 +08:00 committed by osborn
parent ea85559977
commit d746c5b4d4
22 changed files with 312 additions and 7 deletions

View File

@ -31,7 +31,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
api 'com.github.penfeizhou:jsc4a:0.2.5'
api 'com.github.penfeizhou:jsc4a:0.3.0'
//api "pub.doric:jse:${rootProject.ext.Version}"
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation('com.github.penfeizhou.android.animation:glide-plugin:2.17.0') {

View File

@ -37,6 +37,7 @@ import pub.doric.plugin.NetworkPlugin;
import pub.doric.plugin.NotchPlugin;
import pub.doric.plugin.NotificationPlugin;
import pub.doric.plugin.PopoverPlugin;
import pub.doric.plugin.ResourceLoaderPlugin;
import pub.doric.plugin.ShaderPlugin;
import pub.doric.plugin.StatusBarPlugin;
import pub.doric.plugin.StoragePlugin;
@ -112,6 +113,7 @@ public class DoricRegistry {
this.registerNativePlugin(CoordinatorPlugin.class);
this.registerNativePlugin(NotchPlugin.class);
this.registerNativePlugin(KeyboardPlugin.class);
this.registerNativePlugin(ResourceLoaderPlugin.class);
this.registerViewNode(RootNode.class);
this.registerViewNode(TextNode.class);

View File

@ -1,3 +1,18 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.plugin;
import android.animation.Animator;

View File

@ -0,0 +1,71 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.plugin;
import com.bumptech.glide.Glide;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JavaValue;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricPromise;
import pub.doric.resource.DoricResource;
import pub.doric.utils.DoricLog;
/**
* @Description: This is for loading resource into js as ArrayBuffer
* @Author: pengfei.zhou
* @CreateDate: 2021/11/18
*/
@DoricPlugin(name = "resourceLoader")
public class ResourceLoaderPlugin extends DoricJavaPlugin {
public ResourceLoaderPlugin(DoricContext doricContext) {
super(doricContext);
}
@DoricMethod
public void load(JSObject resource, final DoricPromise promise) {
final String type = resource.getProperty("type").asString().value();
final String identifier = resource.getProperty("identifier").asString().value();
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(getDoricContext(), type, identifier);
if (doricResource != null) {
doricResource.fetchRaw().setCallback(new AsyncResult.Callback<byte[]>() {
@Override
public void onResult(byte[] rawData) {
promise.resolve(new JavaValue(rawData));
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
DoricLog.e("Cannot load resource type = %s, identifier = %s, %s", type, identifier, t.getLocalizedMessage());
promise.reject(new JavaValue("Load error"));
}
@Override
public void onFinish() {
}
});
} else {
DoricLog.e("Cannot find loader for resource type = %s, identifier = %s", type, identifier);
promise.reject(new JavaValue("Load error"));
}
}
}

View File

@ -15,12 +15,9 @@
*/
package pub.doric.resource;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: This represents a resource entity
* @Author: pengfei.zhou

View File

@ -1,4 +1,4 @@
import { Base64Resource, Group, Panel, coordinator, text, gravity, Color, LayoutSpec, log, vlayout, scroller, layoutConfig, image, ScaleType, Image, modal, RemoteResource, MainBundleResource, AndroidAssetsResource, AssetsResource } from "doric";
import { Base64Resource, DrawableResource, Group, Panel, coordinator, text, gravity, Color, LayoutSpec, log, vlayout, scroller, layoutConfig, image, ScaleType, Image, modal, RemoteResource, MainBundleResource } from "doric";
import { colors, label } from "./utils";
import { img_base64 } from "./image_base64";

View File

@ -0,0 +1,67 @@
import {
Base64Resource,
DrawableResource,
Group,
Panel,
jsx,
Color,
layoutConfig,
Image,
RemoteResource,
MainBundleResource,
Scroller,
VLayout,
Text,
Gravity,
resourceLoader,
} from "doric";
import { colors, label } from "./utils";
import { img_base64 } from "./image_base64";
import { loge } from "doric/lib/src/util/log";
@Entry
export class ResourceDemo extends Panel {
build(root: Group): void {
<Scroller parent={root} layoutConfig={layoutConfig().most()}>
<VLayout
layoutConfig={layoutConfig().mostWidth().fitHeight()}
space={10}
gravity={Gravity.Center}
>
<Text
layoutConfig={layoutConfig().mostWidth().justHeight()}
textSize={30}
textColor={Color.WHITE}
backgroundColor={colors[5]}
textAlignment={Gravity.Center}
height={50}
>
Image Demo
</Text>
{label("Button")}
<Image
image={
Environment.platform === "Android"
? new DrawableResource("doric_icon_back")
: new MainBundleResource("Hanabi.ttf")
}
/>
<Image
image={
new RemoteResource("https://p.upyun.com/demo/webp/webp/jpg-0.webp")
}
/>
<Image
image={new Base64Resource(img_base64[0])}
onClick={async () => {
const resource = new RemoteResource(
"https://p.upyun.com/demo/webp/webp/jpg-0.webp"
);
const rawData = await resourceLoader(context).load(resource);
loge(rawData.byteLength);
}}
/>
</VLayout>
</Scroller>;
}
}

View File

@ -4203,6 +4203,14 @@ function keyboard(context) {
};
}
function resourceLoader(context) {
return {
load: function (resource) {
return context.callNative('resourceLoader', 'load', resource.toModel());
},
};
}
var __values = (undefined && undefined.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) { return m.call(o); }
@ -4628,6 +4636,7 @@ exports.popover = popover;
exports.pullable = pullable;
exports.refreshable = refreshable;
exports.repeat = repeat;
exports.resourceLoader = resourceLoader;
exports.scroller = scroller;
exports.slideItem = slideItem;
exports.slider = slider;

View File

@ -3222,6 +3222,14 @@ function keyboard(context) {
};
}
function resourceLoader(context) {
return {
load: (resource) => {
return context.callNative('resourceLoader', 'load', resource.toModel());
},
};
}
class Observable {
constructor(provider, clz) {
this.observers = new Set;
@ -3559,6 +3567,7 @@ exports.popover = popover;
exports.pullable = pullable;
exports.refreshable = refreshable;
exports.repeat = repeat;
exports.resourceLoader = resourceLoader;
exports.scroller = scroller;
exports.slideItem = slideItem;
exports.slider = slider;

View File

@ -4750,6 +4750,14 @@ function keyboard(context) {
};
}
function resourceLoader(context) {
return {
load: (resource) => {
return context.callNative('resourceLoader', 'load', resource.toModel());
},
};
}
class Observable {
constructor(provider, clz) {
this.observers = new Set;
@ -5328,6 +5336,7 @@ exports.popover = popover;
exports.pullable = pullable;
exports.refreshable = refreshable;
exports.repeat = repeat;
exports.resourceLoader = resourceLoader;
exports.scroller = scroller;
exports.slideItem = slideItem;
exports.slider = slider;

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

@ -112,6 +112,7 @@ declare module 'doric/lib/src/native/index.native' {
export * from 'doric/lib/src/native/coordinator';
export * from 'doric/lib/src/native/notch';
export * from 'doric/lib/src/native/keyboard';
export * from 'doric/lib/src/native/resourceLoader';
}
declare module 'doric/lib/src/util/index.util' {
@ -1344,6 +1345,14 @@ declare module 'doric/lib/src/native/keyboard' {
};
}
declare module 'doric/lib/src/native/resourceLoader' {
import { Resource } from "doric/lib/src/util/resource";
import { BridgeContext } from "doric/lib/src/runtime/global";
export function resourceLoader(context: BridgeContext): {
load: (resource: Resource) => Promise<ArrayBuffer>;
};
}
declare module 'doric/lib/src/util/color' {
import { Modeling } from "doric/lib/src/util/types";
/**

View File

@ -0,0 +1,6 @@
import { Resource } from "../util/resource";
import { BridgeContext } from "../runtime/global";
export declare function imageDecoder(context: BridgeContext): {
getBitmapInfo: (resource: Resource) => Promise<ArrayBuffer>;
decodeToPixels: (resource: Resource) => Promise<ArrayBuffer>;
};

View File

@ -0,0 +1,25 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function imageDecoder(context) {
return {
getBitmapInfo: (resource) => {
return context.callNative('imageDecoder', 'getBitmapInfo', resource);
},
decodeToPixels: (resource) => {
return context.callNative('imageDecoder', 'decode', resource);
},
};
}

View File

@ -10,3 +10,4 @@ export * from './statusbar';
export * from './coordinator';
export * from './notch';
export * from './keyboard';
export * from './resourceLoader';

View File

@ -25,3 +25,4 @@ export * from './statusbar';
export * from './coordinator';
export * from './notch';
export * from './keyboard';
export * from './resourceLoader';

View File

@ -0,0 +1,5 @@
import { Resource } from "../util/resource";
import { BridgeContext } from "../runtime/global";
export declare function resourceLoader(context: BridgeContext): {
load: (resource: Resource) => Promise<ArrayBuffer>;
};

View File

@ -0,0 +1,7 @@
export function resourceLoader(context) {
return {
load: (resource) => {
return context.callNative('resourceLoader', 'load', resource.toModel());
},
};
}

View File

@ -0,0 +1,29 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Resource } from "../util/resource"
import { BridgeContext } from "../runtime/global"
export function imageDecoder(context: BridgeContext) {
return {
getBitmapInfo: (resource: Resource) => {
return context.callNative('imageDecoder', 'getBitmapInfo', resource) as Promise<ArrayBuffer>
},
decodeToPixels: (resource: Resource) => {
return context.callNative('imageDecoder', 'decode', resource) as Promise<ArrayBuffer>
},
}
}

View File

@ -25,3 +25,4 @@ export * from './statusbar'
export * from './coordinator'
export * from './notch'
export * from './keyboard'
export * from './resourceLoader'

View File

@ -0,0 +1,25 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Resource } from "../util/resource"
import { BridgeContext } from "../runtime/global"
export function resourceLoader(context: BridgeContext) {
return {
load: (resource: Resource) => {
return context.callNative('resourceLoader', 'load', resource.toModel()) as Promise<ArrayBuffer>;
},
}
}

View File

@ -4824,6 +4824,14 @@ function keyboard(context) {
};
}
function resourceLoader(context) {
return {
load: (resource) => {
return context.callNative('resourceLoader', 'load', resource.toModel());
},
};
}
class Observable {
constructor(provider, clz) {
this.observers = new Set;
@ -5161,6 +5169,7 @@ exports.popover = popover;
exports.pullable = pullable;
exports.refreshable = refreshable;
exports.repeat = repeat;
exports.resourceLoader = resourceLoader;
exports.scroller = scroller;
exports.slideItem = slideItem;
exports.slider = slider;

File diff suppressed because one or more lines are too long