dimanche 9 juin 2019

How to configure common data model across android and JS bridge?

I have a JS bridge in Android,

public class WebInterface {
    Context mContext;
    public WebInterface(Context c) { this.mContext = c;}

    @JavascriptInterface
    public void showAlert() {
         Toast.makeText(mContext, "This is being called from the interface", Toast.LENGTH_SHORT).show();
    }
}

I can set interface in my webView,

mWebView.addJavascriptInterface(WebInterface(this), "android"); 

This works fine for simple methods like showAlert() where the is no params when the params or the param is a simple string but when I need to pass a data model as a params while calling native functions from web app how can I bind data models? I am required to call implement a function with param of type custom data model.

public class WebInterface {
    Context mContext;
    public WebInterface(Context c) { this.mContext = c;}

    @JavascriptInterface
    public void showAlert() {
       Toast.makeText(mContext, "This is being called from the interface", Toast.LENGTH_SHORT).show();

    public void saveData(data: DataModel) { // DataModel is custom model
       Toast.makeText(mContext, "Saving data model", Toast.LENGTH_SHORT).show();
}

How can I bind data model across native and web app. Is it possible using TypeScript? If so, how to configure?




Aucun commentaire:

Enregistrer un commentaire