feat:add StoragePlugin for Android

This commit is contained in:
pengfei.zhou 2019-11-22 09:48:45 +08:00
parent 1545f71293
commit 83d71e1703
5 changed files with 138 additions and 5 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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();

View File

@ -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()));
}
}
}

View File

@ -149,4 +149,21 @@ export function network(context: BridgeContext) {
return context.network.request(transformRequest(finalConfig)) as Promise<IResponse>
},
}
}
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<string>
},
remove: (key: string, zone?: string) => {
return context.storage.remove({ key, zone })
},
clear: (zone: string) => {
return context.storage.clear(zone)
},
}
}