diff --git a/Android/doric/src/main/java/pub/doric/DoricRegistry.java b/Android/doric/src/main/java/pub/doric/DoricRegistry.java index 8560ce0f..bbcd374a 100644 --- a/Android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -19,6 +19,7 @@ import android.text.TextUtils; import pub.doric.plugin.NetworkPlugin; import pub.doric.plugin.ShaderPlugin; +import pub.doric.plugin.StoragePlugin; import pub.doric.shader.HLayoutNode; import pub.doric.shader.ImageNode; import pub.doric.shader.ScrollerNode; @@ -67,6 +68,7 @@ public class DoricRegistry { this.registerNativePlugin(ShaderPlugin.class); this.registerNativePlugin(ModalPlugin.class); this.registerNativePlugin(NetworkPlugin.class); + this.registerNativePlugin(StoragePlugin.class); this.registerViewNode(RootNode.class); this.registerViewNode(TextNode.class); this.registerViewNode(ImageNode.class); diff --git a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java index 3746964c..5d9f09ad 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java @@ -50,7 +50,7 @@ public class ModalPlugin extends DoricJavaPlugin { super(doricContext); } - @DoricMethod(name = "toast", thread = ThreadMode.UI) + @DoricMethod(thread = ThreadMode.UI) public void toast(JSDecoder decoder, DoricPromise promise) { try { JSObject jsObject = decoder.decode().asObject(); @@ -77,7 +77,7 @@ public class ModalPlugin extends DoricJavaPlugin { } } - @DoricMethod(name = "alert", thread = ThreadMode.UI) + @DoricMethod(thread = ThreadMode.UI) public void alert(JSDecoder decoder, final DoricPromise promise) { try { JSObject jsObject = decoder.decode().asObject(); @@ -113,7 +113,7 @@ public class ModalPlugin extends DoricJavaPlugin { } - @DoricMethod(name = "confirm", thread = ThreadMode.UI) + @DoricMethod(thread = ThreadMode.UI) public void confirm(JSDecoder decoder, final DoricPromise promise) { try { JSObject jsObject = decoder.decode().asObject(); @@ -160,7 +160,7 @@ public class ModalPlugin extends DoricJavaPlugin { } - @DoricMethod(name = "prompt", thread = ThreadMode.UI) + @DoricMethod(thread = ThreadMode.UI) public void prompt(JSDecoder decoder, final DoricPromise promise) { try { JSObject jsObject = decoder.decode().asObject(); diff --git a/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java index 004412ce..45fdc272 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java @@ -57,7 +57,7 @@ public class NetworkPlugin extends DoricJavaPlugin { super(doricContext); } - @DoricMethod(name = "request") + @DoricMethod public void request(JSDecoder decoder, final DoricPromise promise) { try { JSObject requestVal = decoder.decode().asObject(); diff --git a/Android/doric/src/main/java/pub/doric/plugin/StoragePlugin.java b/Android/doric/src/main/java/pub/doric/plugin/StoragePlugin.java new file mode 100644 index 00000000..f5293235 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/plugin/StoragePlugin.java @@ -0,0 +1,114 @@ +/* + * 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.content.Context; + +import com.github.pengfeizhou.jscore.JSDecoder; +import com.github.pengfeizhou.jscore.JSObject; +import com.github.pengfeizhou.jscore.JSValue; +import com.github.pengfeizhou.jscore.JavaValue; + +import pub.doric.DoricContext; +import pub.doric.extension.bridge.DoricMethod; +import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.extension.bridge.DoricPromise; + +/** + * @Description: pub.doric.plugin + * @Author: pengfei.zhou + * @CreateDate: 2019-11-22 + */ +@DoricPlugin(name = "storage") +public class StoragePlugin extends DoricJavaPlugin { + private static final String PREF_NAME = "pref_doric"; + + public StoragePlugin(DoricContext doricContext) { + super(doricContext); + } + + @DoricMethod + public void setItem(JSDecoder decoder, final DoricPromise promise) { + try { + JSObject jsObject = decoder.decode().asObject(); + JSValue zone = jsObject.getProperty("zone"); + String key = jsObject.getProperty("key").asString().value(); + String value = jsObject.getProperty("value").asString().value(); + String prefName = zone.isString() ? PREF_NAME + "_" + zone.asString() : PREF_NAME; + getDoricContext().getContext().getSharedPreferences( + prefName, + Context.MODE_PRIVATE).edit().putString(key, value).apply(); + promise.resolve(); + } catch (Exception e) { + e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); + } + } + + @DoricMethod + public void getItem(JSDecoder decoder, final DoricPromise promise) { + try { + JSObject jsObject = decoder.decode().asObject(); + JSValue zone = jsObject.getProperty("zone"); + String key = jsObject.getProperty("key").asString().value(); + String prefName = zone.isString() ? PREF_NAME + "_" + zone.asString() : PREF_NAME; + String ret = getDoricContext().getContext().getSharedPreferences( + prefName, + Context.MODE_PRIVATE).getString(key, ""); + promise.resolve(new JavaValue(ret)); + } catch (Exception e) { + e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); + } + } + + @DoricMethod + public void remove(JSDecoder decoder, final DoricPromise promise) { + try { + JSObject jsObject = decoder.decode().asObject(); + JSValue zone = jsObject.getProperty("zone"); + String key = jsObject.getProperty("key").asString().value(); + String prefName = zone.isString() ? PREF_NAME + "_" + zone.asString() : PREF_NAME; + getDoricContext().getContext().getSharedPreferences( + prefName, + Context.MODE_PRIVATE).edit().remove(key).apply(); + promise.resolve(); + } catch (Exception e) { + e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); + } + } + + @DoricMethod + public void clear(JSDecoder decoder, final DoricPromise promise) { + try { + JSObject jsObject = decoder.decode().asObject(); + JSValue zone = jsObject.getProperty("zone"); + if (zone.isString()) { + String prefName = PREF_NAME + "_" + zone.asString(); + getDoricContext().getContext().getSharedPreferences( + prefName, + Context.MODE_PRIVATE).edit().clear().apply(); + promise.resolve(); + } else { + promise.reject(new JavaValue("Zone is empty")); + } + } catch (Exception e) { + e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); + } + } +} diff --git a/js-framework/src/util/nativeModules.ts b/js-framework/src/util/nativeModules.ts index 3c9c1cde..7f1c8448 100644 --- a/js-framework/src/util/nativeModules.ts +++ b/js-framework/src/util/nativeModules.ts @@ -149,4 +149,21 @@ export function network(context: BridgeContext) { return context.network.request(transformRequest(finalConfig)) as Promise }, } +} + +export function storage(context: BridgeContext) { + return { + setItem: (key: string, value: string, zone?: string) => { + return context.storage.store({ key, value, zone }) + }, + getItem: (key: string, zone?: string) => { + return context.storage.retreive({ key, zone }) as Promise + }, + remove: (key: string, zone?: string) => { + return context.storage.remove({ key, zone }) + }, + clear: (zone: string) => { + return context.storage.clear(zone) + }, + } } \ No newline at end of file