From 235549eea47411bea5caad80e752843ba3015b87 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 25 Oct 2021 16:01:44 +0800 Subject: [PATCH] Add Resource Loader for iOS --- .../doric/resource/DoricAndroidResource.java | 23 +- .../doric/resource/DoricAssetsResource.java | 19 +- .../doric/resource/DoricBase64Resource.java | 9 +- .../doric/resource/DoricLocalResource.java | 23 +- .../doric/resource/DoricRemoteResource.java | 23 +- .../pub/doric/resource/DoricResource.java | 2 +- .../main/java/pub/doric/shader/ImageNode.java | 19 +- doric-demo/src/ImageDemo.ts | 284 +++++++++--------- doric-iOS/Pod/Classes/DoricRegistry.h | 2 + doric-iOS/Pod/Classes/DoricRegistry.m | 12 + .../Classes/Resource/DoricBase64Resource.h | 25 ++ .../Classes/Resource/DoricBase64Resource.m | 35 +++ .../Resource/DoricBase64ResourceLoader.h | 25 ++ .../Resource/DoricBase64ResourceLoader.m | 31 ++ .../Classes/Resource/DoricBundleResource.h | 26 ++ .../Classes/Resource/DoricBundleResource.m | 31 ++ .../Resource/DoricBundleResourceLoader.h | 26 ++ .../Resource/DoricBundleResourceLoader.m | 45 +++ .../Pod/Classes/Resource/DoricLocalResource.h | 24 ++ .../Pod/Classes/Resource/DoricLocalResource.m | 30 ++ .../Resource/DoricLocalResourceLoader.h | 24 ++ .../Resource/DoricLocalResourceLoader.m | 32 ++ .../Classes/Resource/DoricRemoteResource.h | 24 ++ .../Classes/Resource/DoricRemoteResource.m | 33 ++ .../Resource/DoricRemoteResourceLoader.h | 25 ++ .../Resource/DoricRemoteResourceLoader.m | 31 ++ .../Pod/Classes/Resource/DoricResource.h | 32 ++ .../Pod/Classes/Resource/DoricResource.m | 33 ++ .../Classes/Resource/DoricResourceLoader.h | 27 ++ .../Resource/DoricResourceLoaderManager.h | 31 ++ .../Resource/DoricResourceLoaderManager.m | 49 +++ doric-iOS/Pod/Classes/Shader/DoricImageNode.m | 52 +++- doric-js/bundle/doric-lib.es5.js | 24 +- doric-js/bundle/doric-lib.js | 18 +- doric-js/bundle/doric-vm.js | 18 +- doric-js/index.d.ts | 8 +- doric-js/lib/src/util/resource.d.ts | 8 +- doric-js/lib/src/util/resource.js | 16 +- doric-js/src/util/resource.ts | 4 +- doric-web/dist/index.js | 18 +- 40 files changed, 979 insertions(+), 242 deletions(-) create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBase64Resource.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBase64Resource.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBundleResource.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBundleResource.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricLocalResource.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricLocalResource.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricRemoteResource.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricRemoteResource.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricResource.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricResource.m create mode 100644 doric-iOS/Pod/Classes/Resource/DoricResourceLoader.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.h create mode 100644 doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.m diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricAndroidResource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricAndroidResource.java index 11ef4223..42acf1f6 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricAndroidResource.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricAndroidResource.java @@ -15,6 +15,7 @@ */ package pub.doric.resource; +import java.io.IOException; import java.io.InputStream; import pub.doric.DoricContext; @@ -34,14 +35,30 @@ public class DoricAndroidResource extends DoricResource { } @Override - public AsyncResult asInputStream() { - AsyncResult result = new AsyncResult<>(); + public AsyncResult fetchRaw() { + AsyncResult result = new AsyncResult<>(); int resId = doricContext.getContext().getResources().getIdentifier( identifier, defType, doricContext.getContext().getPackageName()); if (resId > 0) { - result.setResult(doricContext.getContext().getResources().openRawResource(resId)); + InputStream inputStream = null; + try { + inputStream = doricContext.getContext().getResources().openRawResource(resId); + byte[] data = new byte[inputStream.available()]; + inputStream.read(data); + result.setResult(data); + } catch (Exception e) { + result.setError(e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } else { result.setError(new Throwable("Cannot find resource for :" + identifier + ",type = " + defType)); } diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricAssetsResource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricAssetsResource.java index e4f7172d..3c884778 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricAssetsResource.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricAssetsResource.java @@ -32,13 +32,24 @@ public class DoricAssetsResource extends DoricResource { } @Override - public AsyncResult asInputStream() { - AsyncResult result = new AsyncResult<>(); + public AsyncResult fetchRaw() { + AsyncResult result = new AsyncResult<>(); + InputStream inputStream = null; try { - InputStream inputStream = doricContext.getContext().getAssets().open(identifier); - result.setResult(inputStream); + inputStream = doricContext.getContext().getAssets().open(identifier); + byte[] data = new byte[inputStream.available()]; + inputStream.read(data); + result.setResult(data); } catch (IOException e) { result.setError(e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } return result; } diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricBase64Resource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricBase64Resource.java index d7608db6..eef0dbbb 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricBase64Resource.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricBase64Resource.java @@ -19,9 +19,6 @@ import android.text.TextUtils; import android.util.Base64; import android.util.Pair; -import java.io.ByteArrayInputStream; -import java.io.InputStream; - import pub.doric.DoricContext; import pub.doric.async.AsyncResult; import pub.doric.utils.DoricUtils; @@ -38,8 +35,8 @@ class DoricBase64Resource extends DoricResource { } @Override - public AsyncResult asInputStream() { - AsyncResult ret = new AsyncResult<>(); + public AsyncResult fetchRaw() { + AsyncResult ret = new AsyncResult<>(); Pair result = DoricUtils.translateBase64(identifier); if (result != null) { String imageType = result.first; @@ -48,7 +45,7 @@ class DoricBase64Resource extends DoricResource { if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { try { byte[] data = Base64.decode(base64, Base64.DEFAULT); - ret.setResult(new ByteArrayInputStream(data)); + ret.setResult(data); } catch (Exception e) { e.printStackTrace(); } diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricLocalResource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricLocalResource.java index 5e54504e..b48f75af 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricLocalResource.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricLocalResource.java @@ -16,8 +16,7 @@ package pub.doric.resource; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; +import java.io.IOException; import pub.doric.DoricContext; import pub.doric.async.AsyncResult; @@ -34,12 +33,24 @@ public class DoricLocalResource extends DoricResource { } @Override - public AsyncResult asInputStream() { - AsyncResult result = new AsyncResult<>(); + public AsyncResult fetchRaw() { + AsyncResult result = new AsyncResult<>(); + FileInputStream fis = null; try { - result.setResult(new FileInputStream(identifier)); - } catch (FileNotFoundException e) { + fis = new FileInputStream(identifier); + byte[] data = new byte[fis.available()]; + fis.read(data); + result.setResult(data); + } catch (Exception e) { result.setError(e); + }finally { + if(fis!=null){ + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } return result; } diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricRemoteResource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricRemoteResource.java index d1ef71bf..98f4f037 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricRemoteResource.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricRemoteResource.java @@ -23,8 +23,7 @@ import com.bumptech.glide.request.target.Target; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; +import java.io.IOException; import androidx.annotation.Nullable; import pub.doric.DoricContext; @@ -42,8 +41,8 @@ public class DoricRemoteResource extends DoricResource { } @Override - public AsyncResult asInputStream() { - final AsyncResult result = new AsyncResult<>(); + public AsyncResult fetchRaw() { + final AsyncResult result = new AsyncResult<>(); Glide.with(doricContext.getContext()).download(identifier) .listener(new RequestListener() { @Override @@ -54,10 +53,22 @@ public class DoricRemoteResource extends DoricResource { @Override public boolean onResourceReady(File resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + FileInputStream fis = null; try { - result.setResult(new FileInputStream(resource)); - } catch (FileNotFoundException e) { + fis = new FileInputStream(resource); + byte[] data = new byte[fis.available()]; + fis.read(data); + result.setResult(data); + } catch (Exception e) { result.setError(e); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } return false; } diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricResource.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricResource.java index 98fa4596..0d667fa9 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricResource.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricResource.java @@ -35,5 +35,5 @@ public abstract class DoricResource { this.identifier = identifier; } - public abstract AsyncResult asInputStream(); + public abstract AsyncResult fetchRaw(); } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java index 388005ab..a52fbba7 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java @@ -57,7 +57,6 @@ import java.io.InputStream; import jp.wasabeef.glide.transformations.BlurTransformation; import pub.doric.DoricContext; -import pub.doric.DoricSingleton; import pub.doric.async.AsyncResult; import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricPlugin; @@ -371,22 +370,10 @@ public class ImageNode extends ViewNode { final String identifier = resource.getProperty("identifier").asString().value(); DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(getDoricContext(), type, identifier); if (doricResource != null) { - doricResource.asInputStream().setCallback(new AsyncResult.Callback() { + doricResource.fetchRaw().setCallback(new AsyncResult.Callback() { @Override - public void onResult(InputStream result) { - try { - byte[] imageData = new byte[result.available()]; - result.read(imageData, 0, result.available()); - loadIntoTarget(Glide.with(getContext()).load(imageData)); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - result.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } + public void onResult(byte[] imageData) { + loadIntoTarget(Glide.with(getContext()).load(imageData)); } @Override diff --git a/doric-demo/src/ImageDemo.ts b/doric-demo/src/ImageDemo.ts index 576e741a..35824685 100644 --- a/doric-demo/src/ImageDemo.ts +++ b/doric-demo/src/ImageDemo.ts @@ -1,4 +1,4 @@ -import { Group, Panel, coordinator, text, gravity, Color, LayoutSpec, log, vlayout, scroller, layoutConfig, image, ScaleType, Image, modal } from "doric"; +import { Base64Resource, 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"; @@ -6,6 +6,7 @@ const imageUrl = 'https://img.zcool.cn/community/01e75b5da933daa801209e1ffa4649. import logo from "./images/logo_w.png" import button from "./images/button.png" +import { DrawableResource } from "doric/lib/src/util/resource"; @Entry class ImageDemo extends Panel { @@ -26,157 +27,156 @@ class ImageDemo extends Panel { label('Button'), image({ - imageBase64: button, - scaleType: ScaleType.ScaleToFill, - layoutConfig: { - widthSpec: LayoutSpec.FIT, - heightSpec: LayoutSpec.FIT, - }, - imageScale: 2, + image: Environment.platform === 'Android' + ? new DrawableResource("doric_icon_back") + : new MainBundleResource("Hanabi.ttf"), }), image({ - imageBase64: button, + image: new RemoteResource("https://p.upyun.com/demo/webp/webp/jpg-0.webp"), + }), + image({ + image: new Base64Resource(img_base64[0]), scaleType: ScaleType.ScaleToFill, layoutConfig: { widthSpec: LayoutSpec.FIT, heightSpec: LayoutSpec.FIT, }, }), - image({ - imageBase64: button, - scaleType: ScaleType.ScaleToFill, - layoutConfig: { - widthSpec: LayoutSpec.JUST, - heightSpec: LayoutSpec.JUST, - }, - width: 200, - height: 150 / 2.75, - stretchInset: { - left: 100, - top: 0, - right: 100, - bottom: 0 - }, - imageScale: 2.75, - }), - image({ - imageBase64: button, - scaleType: ScaleType.ScaleToFill, - layoutConfig: { - widthSpec: LayoutSpec.JUST, - heightSpec: LayoutSpec.JUST, - }, - width: 200, - height: 75, - stretchInset: { - left: 100, - top: 0, - right: 100, - bottom: 0 - }, - imageScale: 2, + // image({ + // imageBase64: button, + // scaleType: ScaleType.ScaleToFill, + // layoutConfig: { + // widthSpec: LayoutSpec.JUST, + // heightSpec: LayoutSpec.JUST, + // }, + // width: 200, + // height: 150 / 2.75, + // stretchInset: { + // left: 100, + // top: 0, + // right: 100, + // bottom: 0 + // }, + // imageScale: 2.75, + // }), + // image({ + // imageBase64: button, + // scaleType: ScaleType.ScaleToFill, + // layoutConfig: { + // widthSpec: LayoutSpec.JUST, + // heightSpec: LayoutSpec.JUST, + // }, + // width: 200, + // height: 75, + // stretchInset: { + // left: 100, + // top: 0, + // right: 100, + // bottom: 0 + // }, + // imageScale: 2, - }), - label('Gif '), - image({ - imageUrl: "https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_animation.gif", - scaleType: ScaleType.ScaleToFill, - imageScale: 3, - }), - label('APNG'), - image({ - imageUrl: "https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png", - }), - label('Animated WebP'), - image({ - imageUrl: "https://p.upyun.com/demo/webp/webp/animated-gif-0.webp", + // }), + // label('Gif '), + // image({ + // imageUrl: "https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_animation.gif", + // scaleType: ScaleType.ScaleToFill, + // imageScale: 3, + // }), + // label('APNG'), + // image({ + // imageUrl: "https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png", + // }), + // label('Animated WebP'), + // image({ + // imageUrl: "https://p.upyun.com/demo/webp/webp/animated-gif-0.webp", - }), - label('WebP'), - imageView = image({ - imageUrl: "https://p.upyun.com/demo/webp/webp/jpg-0.webp", - layoutConfig: layoutConfig().just(), - width: 200, - height: 200, - loadCallback: (ret) => { - if (ret) { - imageView.width = ret.width - imageView.height = ret.height - } - } - }), - label('ScaleToFill'), - image({ - imageUrl, - width: 300, - height: 300, - isBlur: true, - border: { - width: 2, - color: Color.GRAY, - }, - scaleType: ScaleType.ScaleToFill, - layoutConfig: layoutConfig().just(), - loadCallback: (ret) => { - } - }), - label('ScaleAspectFit'), - image({ - imageUrl, - width: 300, - height: 300, - border: { - width: 2, - color: Color.GRAY, - }, - scaleType: ScaleType.ScaleAspectFit, - layoutConfig: layoutConfig().just(), - }), - label('ScaleAspectFill'), - image({ - imageUrl, - width: 300, - height: 300, - border: { - width: 2, - color: Color.GRAY, - }, - scaleType: ScaleType.ScaleAspectFill, - layoutConfig: layoutConfig().just(), - }), - label('ImageBase64'), - image({ - imageBase64: img_base64[0], - width: 300, - height: 300, - border: { - width: 2, - color: Color.GRAY, - }, - scaleType: ScaleType.ScaleAspectFill, - layoutConfig: layoutConfig().just(), - }), - label('StretchInset'), - image({ - imageBase64: img_base64[1], - height: 60, - width: 134, - scaleType: ScaleType.ScaleAspectFill, - layoutConfig: layoutConfig().just(), - }), - image({ - imageBase64: img_base64[1], - height: 60, - width: 294, - scaleType: ScaleType.ScaleToFill, - layoutConfig: layoutConfig().just(), - stretchInset: { - left: 0.85, - top: 0, - right: 0.149, - bottom: 0 - } - }), + // }), + // label('WebP'), + // imageView = image({ + // imageUrl: "https://p.upyun.com/demo/webp/webp/jpg-0.webp", + // layoutConfig: layoutConfig().just(), + // width: 200, + // height: 200, + // loadCallback: (ret) => { + // if (ret) { + // imageView.width = ret.width + // imageView.height = ret.height + // } + // } + // }), + // label('ScaleToFill'), + // image({ + // imageUrl, + // width: 300, + // height: 300, + // isBlur: true, + // border: { + // width: 2, + // color: Color.GRAY, + // }, + // scaleType: ScaleType.ScaleToFill, + // layoutConfig: layoutConfig().just(), + // loadCallback: (ret) => { + // } + // }), + // label('ScaleAspectFit'), + // image({ + // imageUrl, + // width: 300, + // height: 300, + // border: { + // width: 2, + // color: Color.GRAY, + // }, + // scaleType: ScaleType.ScaleAspectFit, + // layoutConfig: layoutConfig().just(), + // }), + // label('ScaleAspectFill'), + // image({ + // imageUrl, + // width: 300, + // height: 300, + // border: { + // width: 2, + // color: Color.GRAY, + // }, + // scaleType: ScaleType.ScaleAspectFill, + // layoutConfig: layoutConfig().just(), + // }), + // label('ImageBase64'), + // image({ + // imageBase64: img_base64[0], + // width: 300, + // height: 300, + // border: { + // width: 2, + // color: Color.GRAY, + // }, + // scaleType: ScaleType.ScaleAspectFill, + // layoutConfig: layoutConfig().just(), + // }), + // label('StretchInset'), + // image({ + // imageBase64: img_base64[1], + // height: 60, + // width: 134, + // scaleType: ScaleType.ScaleAspectFill, + // layoutConfig: layoutConfig().just(), + // }), + // image({ + // imageBase64: img_base64[1], + // height: 60, + // width: 294, + // scaleType: ScaleType.ScaleToFill, + // layoutConfig: layoutConfig().just(), + // stretchInset: { + // left: 0.85, + // top: 0, + // right: 0.149, + // bottom: 0 + // } + // }), ], { layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT), diff --git a/doric-iOS/Pod/Classes/DoricRegistry.h b/doric-iOS/Pod/Classes/DoricRegistry.h index 1f800976..03e64a70 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.h +++ b/doric-iOS/Pod/Classes/DoricRegistry.h @@ -23,6 +23,7 @@ #import #import "DoricMonitorProtocol.h" #import +#import "DoricResourceLoaderManager.h" NS_ASSUME_NONNULL_BEGIN @class DoricLibrary; @@ -33,6 +34,7 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, strong) UIImage *defaultPlaceHolderImage; @property(nonatomic, strong) UIImage *defaultErrorImage; @property(nonatomic, strong) id globalPerformanceAnchorHook; +@property(nonatomic, strong) DoricResourceLoaderManager *loaderManager; - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine; diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index 107b6dfa..9883200a 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -56,6 +56,10 @@ #import "DoricJSEngine.h" #import "DoricSingleton.h" #import "DoricGestureContainerNode.h" +#import "DoricBundleResourceLoader.h" +#import "DoricBase64ResourceLoader.h" +#import "DoricLocalResourceLoader.h" +#import "DoricRemoteResourceLoader.h" @interface DoricRegistry () @@ -83,6 +87,7 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine { _plugins = [NSMutableDictionary new]; _nodes = [NSMutableDictionary new]; _monitors = [NSMutableSet new]; + _loaderManager = [DoricResourceLoaderManager new]; [self innerRegister]; [DoricSingleton.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { [obj load:self]; @@ -127,6 +132,13 @@ - (void)innerRegister { [self registerViewNode:DoricSwitchNode.class withName:@"Switch"]; [self registerViewNode:DoricFlexNode.class withName:@"FlexLayout"]; [self registerViewNode:DoricGestureContainerNode.class withName:@"GestureContainer"]; + + [self.loaderManager registerLoader:[[DoricBundleResourceLoader alloc] + initWithResourceType:@"mainBundle" + bundle:[NSBundle mainBundle]]]; + [self.loaderManager registerLoader:[DoricLocalResourceLoader new]]; + [self.loaderManager registerLoader:[DoricRemoteResourceLoader new]]; + [self.loaderManager registerLoader:[DoricBase64ResourceLoader new]]; } - (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name { diff --git a/doric-iOS/Pod/Classes/Resource/DoricBase64Resource.h b/doric-iOS/Pod/Classes/Resource/DoricBase64Resource.h new file mode 100644 index 00000000..57c3f56e --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBase64Resource.h @@ -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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import + +#import "DoricResource.h" + +@interface DoricBase64Resource : DoricResource +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBase64Resource.m b/doric-iOS/Pod/Classes/Resource/DoricBase64Resource.m new file mode 100644 index 00000000..ef5c3782 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBase64Resource.m @@ -0,0 +1,35 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricBase64Resource.h" + + +@implementation DoricBase64Resource +- (DoricAsyncResult *)fetchRaw { + DoricAsyncResult *result = [DoricAsyncResult new]; + NSString *inString = nil; + if ([self.identifier hasPrefix:@"data:image"]) { + inString = [self.identifier componentsSeparatedByString:@","].lastObject; + } + NSData *data = [[NSData alloc] initWithBase64EncodedString:inString + options:NSDataBase64DecodingIgnoreUnknownCharacters]; + [result setupResult:data]; + return result; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.h b/doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.h new file mode 100644 index 00000000..0e4bd379 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.h @@ -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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import + +#import "DoricResourceLoader.h" + +@interface DoricBase64ResourceLoader : NSObject +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.m b/doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.m new file mode 100644 index 00000000..2b0a7e96 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBase64ResourceLoader.m @@ -0,0 +1,31 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricBase64ResourceLoader.h" +#import "DoricBase64Resource.h" + +@implementation DoricBase64ResourceLoader +- (NSString *)resourceType { + return @"base64"; +} + +- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context { + return [[DoricBase64Resource alloc] initWithContext:context identifier:identifier]; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBundleResource.h b/doric-iOS/Pod/Classes/Resource/DoricBundleResource.h new file mode 100644 index 00000000..23cf1814 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBundleResource.h @@ -0,0 +1,26 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import +#import "DoricResource.h" +#import "DoricContext.h" + +@interface DoricBundleResource : DoricResource +@property(nonatomic, strong) NSBundle *bundle; +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBundleResource.m b/doric-iOS/Pod/Classes/Resource/DoricBundleResource.m new file mode 100644 index 00000000..8adc059f --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBundleResource.m @@ -0,0 +1,31 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricBundleResource.h" + +@implementation DoricBundleResource +- (DoricAsyncResult *)fetchRaw { + DoricAsyncResult *result = [DoricAsyncResult new]; + NSString *path = [self.bundle bundlePath]; + NSString *fullPath = [path stringByAppendingPathComponent:self.identifier]; + NSData *imgData = [[NSData alloc] initWithContentsOfFile:fullPath]; + [result setupResult:imgData]; + return result; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.h b/doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.h new file mode 100644 index 00000000..cabe6e7c --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.h @@ -0,0 +1,26 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import + +#import "DoricResourceLoader.h" + +@interface DoricBundleResourceLoader : NSObject +- (instancetype)initWithResourceType:(NSString *)resourceType bundle:(NSBundle *)bundle; +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.m b/doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.m new file mode 100644 index 00000000..5af090da --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricBundleResourceLoader.m @@ -0,0 +1,45 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import +#import "DoricBundleResourceLoader.h" +#import "DoricBundleResource.h" + +@interface DoricBundleResourceLoader () +@property(nonatomic, strong) NSBundle *bundle; +@property(nonatomic, copy) NSString *resourceType; +@end + +@implementation DoricBundleResourceLoader +- (instancetype)initWithResourceType:(NSString *)resourceType bundle:(NSBundle *)bundle { + if (self = [super init]) { + _resourceType = resourceType; + _bundle = bundle; + } + return self; +} + +- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context { + return [[[DoricBundleResource alloc] initWithContext:context identifier:identifier] + also:^(DoricBundleResource *it) { + it.bundle = self.bundle; + }]; +} + +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricLocalResource.h b/doric-iOS/Pod/Classes/Resource/DoricLocalResource.h new file mode 100644 index 00000000..81e93a6b --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricLocalResource.h @@ -0,0 +1,24 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import +#import "DoricResource.h" + +@interface DoricLocalResource : DoricResource +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricLocalResource.m b/doric-iOS/Pod/Classes/Resource/DoricLocalResource.m new file mode 100644 index 00000000..852421c9 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricLocalResource.m @@ -0,0 +1,30 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricLocalResource.h" + + +@implementation DoricLocalResource +- (DoricAsyncResult *)fetchRaw { + DoricAsyncResult *result = [DoricAsyncResult new]; + NSData *imgData = [[NSData alloc] initWithContentsOfFile:self.identifier]; + [result setupResult:imgData]; + return result; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.h b/doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.h new file mode 100644 index 00000000..e2a70e4a --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.h @@ -0,0 +1,24 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import +#import "DoricResourceLoader.h" + +@interface DoricLocalResourceLoader : NSObject +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.m b/doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.m new file mode 100644 index 00000000..be2393c4 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricLocalResourceLoader.m @@ -0,0 +1,32 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricLocalResourceLoader.h" +#import "DoricLocalResource.h" + +@implementation DoricLocalResourceLoader +- (NSString *)resourceType { + return @"local"; +} + +- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context { + return [[DoricLocalResource alloc] initWithContext:context identifier:identifier]; +} + +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricRemoteResource.h b/doric-iOS/Pod/Classes/Resource/DoricRemoteResource.h new file mode 100644 index 00000000..93742144 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricRemoteResource.h @@ -0,0 +1,24 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import +#import "DoricResource.h" + +@interface DoricRemoteResource : DoricResource +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricRemoteResource.m b/doric-iOS/Pod/Classes/Resource/DoricRemoteResource.m new file mode 100644 index 00000000..52ed8ba8 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricRemoteResource.m @@ -0,0 +1,33 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricRemoteResource.h" + + +@implementation DoricRemoteResource +- (DoricAsyncResult *)fetchRaw { + DoricAsyncResult *result = [DoricAsyncResult new]; + NSURL *url = [NSURL URLWithString:self.identifier]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + NSData *urlData = [NSData dataWithContentsOfURL:url]; + [result setupResult:urlData]; + }); + return result; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.h b/doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.h new file mode 100644 index 00000000..98a85822 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.h @@ -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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import + +#import "DoricResourceLoader.h" + +@interface DoricRemoteResourceLoader : NSObject +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.m b/doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.m new file mode 100644 index 00000000..1edd23a9 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricRemoteResourceLoader.m @@ -0,0 +1,31 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/25. +// + +#import "DoricRemoteResourceLoader.h" +#import "DoricRemoteResource.h" + +@implementation DoricRemoteResourceLoader +- (NSString *)resourceType { + return @"remote"; +} + +- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context { + return [[DoricRemoteResource alloc] initWithContext:context identifier:identifier]; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricResource.h b/doric-iOS/Pod/Classes/Resource/DoricResource.h new file mode 100644 index 00000000..e65b2333 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricResource.h @@ -0,0 +1,32 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/22. +// + +#import +#import "DoricAsyncResult.h" + +@class DoricContext; + +@interface DoricResource : NSObject +@property(nonatomic, weak) DoricContext *context; +@property(nonatomic, copy) NSString *identifier; + +- (instancetype)initWithContext:(DoricContext *)context identifier:(NSString *)identifier; + +- (DoricAsyncResult *)fetchRaw; +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Resource/DoricResource.m b/doric-iOS/Pod/Classes/Resource/DoricResource.m new file mode 100644 index 00000000..f8c988b1 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricResource.m @@ -0,0 +1,33 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/22. +// + +#import "DoricResource.h" + +@implementation DoricResource +- (instancetype)initWithContext:(DoricContext *)context identifier:(NSString *)identifier { + if (self = [super init]) { + _context = context; + _identifier = identifier; + } + return self; +} +- (DoricAsyncResult *)fetchRaw { + return nil; +} +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricResourceLoader.h b/doric-iOS/Pod/Classes/Resource/DoricResourceLoader.h new file mode 100644 index 00000000..d1fc6b4d --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricResourceLoader.h @@ -0,0 +1,27 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/22. +// + +#import +#import "DoricResource.h" + +@protocol DoricResourceLoader +@property(nonatomic, readonly) NSString *resourceType; + +- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context; +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.h b/doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.h new file mode 100644 index 00000000..b2843990 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.h @@ -0,0 +1,31 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/22. +// + +#import +#import "DoricResourceLoader.h" + +@interface DoricResourceLoaderManager : NSObject +- (void)registerLoader:(id )loader; + +- (void)unRegisterLoader:(id )loader; + +- (__kindof DoricResource *)load:(NSString *)identifier + withResourceType:(NSString *)resourceType + withContext:(DoricContext *)context; +@end diff --git a/doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.m b/doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.m new file mode 100644 index 00000000..0d90e9e1 --- /dev/null +++ b/doric-iOS/Pod/Classes/Resource/DoricResourceLoaderManager.m @@ -0,0 +1,49 @@ +/* + * 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. + */ +// +// Created by pengfei.zhou on 2021/10/22. +// + +#import "DoricResourceLoaderManager.h" + +@interface DoricResourceLoaderManager () +@property(nonatomic, strong) NSMutableDictionary > *loaders; +@end + +@implementation DoricResourceLoaderManager +- (instancetype)init { + if (self = [super init]) { + _loaders = [NSMutableDictionary new]; + } + return self; +} + +- (void)registerLoader:(id )loader { + self.loaders[loader.resourceType] = loader; +} + +- (void)unRegisterLoader:(id )loader { + [self.loaders removeObjectForKey:loader.resourceType]; +} + +- (__kindof DoricResource *)load:(NSString *)identifier + withResourceType:(NSString *)resourceType + withContext:(DoricContext *)context { + id loader = self.loaders[resourceType]; + return [loader load:identifier withContext:context]; +} + +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m index 47e156fa..683e0692 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m @@ -233,7 +233,57 @@ - (UIImage *)currentErrorImage { } - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id)prop { - if ([@"imageUrl" isEqualToString:name]) { + if ([@"image" isEqualToString:name]) { + NSString *type = prop[@"type"]; + NSString *identifier = prop[@"identifier"]; + DoricAsyncResult *asyncResult = [[self.doricContext.driver.registry.loaderManager + load:identifier withResourceType:type withContext:self.doricContext] fetchRaw]; + [asyncResult setResultCallback:^(NSData *imageData) { + [self.doricContext dispatchToMainQueue:^{ +#if DORIC_USE_YYWEBIMAGE + YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale]; +#elif DORIC_USE_SDWEBIMAGE + UIImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale]; + if (!image) { + image = [UIImage imageWithData:imageData scale:self.imageScale]; + } +#else + UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale]; +#endif + view.image = image; + DoricSuperNode *node = self.superNode; + while (node.superNode != nil) { + node = node.superNode; + } + [node requestLayout]; + if (self.loadCallbackId.length > 0) { + if (image) { + [self callJSResponse:self.loadCallbackId, + @{ + @"width": @(image.size.width), + @"height": @(image.size.height), +#if DORIC_USE_YYWEBIMAGE + @"animated": (image.animatedImageData != nil) ? @(YES) : @(NO), +#elif DORIC_USE_SDWEBIMAGE + @"animated": ([image isKindOfClass:SDAnimatedImage.class] + && ((SDAnimatedImage *) image).animatedImageFrameCount > 0) + ? @(YES) + : @(NO), +#else + @"animated": @(NO), +#endif + }, + nil]; + } else { + [self callJSResponse:self.loadCallbackId, nil]; + } + } + }]; + }]; + [asyncResult setExceptionCallback:^(NSException *e) { + DoricLog(@"Cannot load resource type = %@, identifier = %@, %@", type, identifier, e.reason); + }]; + } else if ([@"imageUrl" isEqualToString:name]) { __weak typeof(self) _self = self; __block BOOL async = NO; view.doricLayout.undefined = YES; diff --git a/doric-js/bundle/doric-lib.es5.js b/doric-js/bundle/doric-lib.es5.js index 0da77885..b35c0194 100644 --- a/doric-js/bundle/doric-lib.es5.js +++ b/doric-js/bundle/doric-lib.es5.js @@ -2147,12 +2147,12 @@ var Resource = /** @class */ (function () { }; return Resource; }()); -var FileResource = /** @class */ (function (_super) { - __extends$e(FileResource, _super); - function FileResource(path) { - return _super.call(this, "file", path) || this; +var LocalResource = /** @class */ (function (_super) { + __extends$e(LocalResource, _super); + function LocalResource(path) { + return _super.call(this, "local", path) || this; } - return FileResource; + return LocalResource; }(Resource)); var RemoteResource = /** @class */ (function (_super) { __extends$e(RemoteResource, _super); @@ -2163,8 +2163,8 @@ var RemoteResource = /** @class */ (function (_super) { }(Resource)); var Base64Resource = /** @class */ (function (_super) { __extends$e(Base64Resource, _super); - function Base64Resource(url) { - return _super.call(this, "base64", url) || this; + function Base64Resource(content) { + return _super.call(this, "base64", content) || this; } return Base64Resource; }(Resource)); @@ -2173,15 +2173,15 @@ var Base64Resource = /** @class */ (function (_super) { */ var DrawableResource = /** @class */ (function (_super) { __extends$e(DrawableResource, _super); - function DrawableResource(url) { - return _super.call(this, "drawable", url) || this; + function DrawableResource(name) { + return _super.call(this, "drawable", name) || this; } return DrawableResource; }(Resource)); var RawResource = /** @class */ (function (_super) { __extends$e(RawResource, _super); - function RawResource(url) { - return _super.call(this, "raw", url) || this; + function RawResource(name) { + return _super.call(this, "raw", name) || this; } return RawResource; }(Resource)); @@ -4395,7 +4395,6 @@ exports.CENTER_Y = CENTER_Y; exports.Color = Color; exports.Draggable = Draggable; exports.DrawableResource = DrawableResource; -exports.FileResource = FileResource; exports.FlexLayout = FlexLayout; exports.FlexTypedValue = FlexTypedValue; exports.FlowLayout = FlowLayout; @@ -4411,6 +4410,7 @@ exports.LEFT = LEFT; exports.LayoutConfigImpl = LayoutConfigImpl; exports.List = List; exports.ListItem = ListItem; +exports.LocalResource = LocalResource; exports.MainBundleResource = MainBundleResource; exports.ModularPanel = ModularPanel; exports.Module = Module; diff --git a/doric-js/bundle/doric-lib.js b/doric-js/bundle/doric-lib.js index 0260a816..953083f6 100644 --- a/doric-js/bundle/doric-lib.js +++ b/doric-js/bundle/doric-lib.js @@ -1615,9 +1615,9 @@ class Resource { }; } } -class FileResource extends Resource { +class LocalResource extends Resource { constructor(path) { - super("file", path); + super("local", path); } } class RemoteResource extends Resource { @@ -1626,21 +1626,21 @@ class RemoteResource extends Resource { } } class Base64Resource extends Resource { - constructor(url) { - super("base64", url); + constructor(content) { + super("base64", content); } } /** * This is for android platform */ class DrawableResource extends Resource { - constructor(url) { - super("drawable", url); + constructor(name) { + super("drawable", name); } } class RawResource extends Resource { - constructor(url) { - super("raw", url); + constructor(name) { + super("raw", name); } } class AssetResource extends Resource { @@ -3378,7 +3378,6 @@ exports.CENTER_Y = CENTER_Y; exports.Color = Color; exports.Draggable = Draggable; exports.DrawableResource = DrawableResource; -exports.FileResource = FileResource; exports.FlexLayout = FlexLayout; exports.FlexTypedValue = FlexTypedValue; exports.FlowLayout = FlowLayout; @@ -3394,6 +3393,7 @@ exports.LEFT = LEFT; exports.LayoutConfigImpl = LayoutConfigImpl; exports.List = List; exports.ListItem = ListItem; +exports.LocalResource = LocalResource; exports.MainBundleResource = MainBundleResource; exports.ModularPanel = ModularPanel; exports.Module = Module; diff --git a/doric-js/bundle/doric-vm.js b/doric-js/bundle/doric-vm.js index 47c81421..0abd9268 100644 --- a/doric-js/bundle/doric-vm.js +++ b/doric-js/bundle/doric-vm.js @@ -3136,9 +3136,9 @@ class Resource { }; } } -class FileResource extends Resource { +class LocalResource extends Resource { constructor(path) { - super("file", path); + super("local", path); } } class RemoteResource extends Resource { @@ -3147,21 +3147,21 @@ class RemoteResource extends Resource { } } class Base64Resource extends Resource { - constructor(url) { - super("base64", url); + constructor(content) { + super("base64", content); } } /** * This is for android platform */ class DrawableResource extends Resource { - constructor(url) { - super("drawable", url); + constructor(name) { + super("drawable", name); } } class RawResource extends Resource { - constructor(url) { - super("raw", url); + constructor(name) { + super("raw", name); } } class AssetResource extends Resource { @@ -5140,7 +5140,6 @@ exports.CENTER_Y = CENTER_Y; exports.Color = Color; exports.Draggable = Draggable; exports.DrawableResource = DrawableResource; -exports.FileResource = FileResource; exports.FlexLayout = FlexLayout; exports.FlexTypedValue = FlexTypedValue; exports.FlowLayout = FlowLayout; @@ -5156,6 +5155,7 @@ exports.LEFT = LEFT; exports.LayoutConfigImpl = LayoutConfigImpl; exports.List = List; exports.ListItem = ListItem; +exports.LocalResource = LocalResource; exports.MainBundleResource = MainBundleResource; exports.ModularPanel = ModularPanel; exports.Module = Module; diff --git a/doric-js/index.d.ts b/doric-js/index.d.ts index 54f1ca2e..66bc4aa7 100644 --- a/doric-js/index.d.ts +++ b/doric-js/index.d.ts @@ -1635,23 +1635,23 @@ declare module 'doric/lib/src/util/resource' { identifier: string; }; } - export class FileResource extends Resource { + export class LocalResource extends Resource { constructor(path: string); } export class RemoteResource extends Resource { constructor(url: string); } export class Base64Resource extends Resource { - constructor(url: string); + constructor(content: string); } /** * This is for android platform */ export class DrawableResource extends Resource { - constructor(url: string); + constructor(name: string); } export class RawResource extends Resource { - constructor(url: string); + constructor(name: string); } export class AssetResource extends Resource { constructor(path: string); diff --git a/doric-js/lib/src/util/resource.d.ts b/doric-js/lib/src/util/resource.d.ts index 2d55b6a8..675bea1f 100644 --- a/doric-js/lib/src/util/resource.d.ts +++ b/doric-js/lib/src/util/resource.d.ts @@ -8,23 +8,23 @@ export declare abstract class Resource implements Modeling { identifier: string; }; } -export declare class FileResource extends Resource { +export declare class LocalResource extends Resource { constructor(path: string); } export declare class RemoteResource extends Resource { constructor(url: string); } export declare class Base64Resource extends Resource { - constructor(url: string); + constructor(content: string); } /** * This is for android platform */ export declare class DrawableResource extends Resource { - constructor(url: string); + constructor(name: string); } export declare class RawResource extends Resource { - constructor(url: string); + constructor(name: string); } export declare class AssetResource extends Resource { constructor(path: string); diff --git a/doric-js/lib/src/util/resource.js b/doric-js/lib/src/util/resource.js index 6e583e2c..50812b3d 100644 --- a/doric-js/lib/src/util/resource.js +++ b/doric-js/lib/src/util/resource.js @@ -10,9 +10,9 @@ export class Resource { }; } } -export class FileResource extends Resource { +export class LocalResource extends Resource { constructor(path) { - super("file", path); + super("local", path); } } export class RemoteResource extends Resource { @@ -21,21 +21,21 @@ export class RemoteResource extends Resource { } } export class Base64Resource extends Resource { - constructor(url) { - super("base64", url); + constructor(content) { + super("base64", content); } } /** * This is for android platform */ export class DrawableResource extends Resource { - constructor(url) { - super("drawable", url); + constructor(name) { + super("drawable", name); } } export class RawResource extends Resource { - constructor(url) { - super("raw", url); + constructor(name) { + super("raw", name); } } export class AssetResource extends Resource { diff --git a/doric-js/src/util/resource.ts b/doric-js/src/util/resource.ts index 9b8c359c..bfc561c5 100644 --- a/doric-js/src/util/resource.ts +++ b/doric-js/src/util/resource.ts @@ -15,9 +15,9 @@ export abstract class Resource implements Modeling { } } -export class FileResource extends Resource { +export class LocalResource extends Resource { constructor(path: string) { - super("file", path); + super("local", path); } } diff --git a/doric-web/dist/index.js b/doric-web/dist/index.js index dd541bd0..4dc44ac8 100644 --- a/doric-web/dist/index.js +++ b/doric-web/dist/index.js @@ -3190,9 +3190,9 @@ class Resource { }; } } -class FileResource extends Resource { +class LocalResource extends Resource { constructor(path) { - super("file", path); + super("local", path); } } class RemoteResource extends Resource { @@ -3201,21 +3201,21 @@ class RemoteResource extends Resource { } } class Base64Resource extends Resource { - constructor(url) { - super("base64", url); + constructor(content) { + super("base64", content); } } /** * This is for android platform */ class DrawableResource extends Resource { - constructor(url) { - super("drawable", url); + constructor(name) { + super("drawable", name); } } class RawResource extends Resource { - constructor(url) { - super("raw", url); + constructor(name) { + super("raw", name); } } class AssetResource extends Resource { @@ -4953,7 +4953,6 @@ exports.CENTER_Y = CENTER_Y; exports.Color = Color; exports.Draggable = Draggable; exports.DrawableResource = DrawableResource; -exports.FileResource = FileResource; exports.FlexLayout = FlexLayout; exports.FlexTypedValue = FlexTypedValue; exports.FlowLayout = FlowLayout; @@ -4969,6 +4968,7 @@ exports.LEFT = LEFT; exports.LayoutConfigImpl = LayoutConfigImpl; exports.List = List; exports.ListItem = ListItem; +exports.LocalResource = LocalResource; exports.MainBundleResource = MainBundleResource; exports.ModularPanel = ModularPanel; exports.Module = Module;