1. 程式人生 > >Weex Android SDK原始碼分析之Module(modal)

Weex Android SDK原始碼分析之Module(modal)

前言

當您來閱讀這篇文章時,代表您已經是weex粉絲了,我說的對嗎?(-_-) 可是您只會使用可不行,本篇博文介紹Weex Moudle中的model 使用與原始碼分析。

程式碼分析

Weex封裝了一系列的model api,例如:toast, alert, confirm and prompt.

Toast

weex code

使用:

this.$call('modal', 'toast', {
        'message': content,
        'duration': 2.0
    });


Arguments: 

    message、duration
; example: var modal = require('@weex-module/modal'); modal.toast({'message': 'I am toast!', 'duration': 1});

android method

    private Toast toast;

    @WXModuleAnno
    public void toast(String param) {
        String message = "";
        int duration = Toast.LENGTH_SHORT;
        if
(!TextUtils.isEmpty(param)) { try { param = URLDecoder.decode(param, "utf-8"); JSONObject jsObj = new JSONObject(param); message = jsObj.optString(WXConst.MESSAGE); duration = jsObj.optInt(WXConst.DURATION); } catch (Exception e) { WXLogUtils.
e("[WXModalUIModule] alert param parse error "+WXLogUtils.getStackTrace(e)); } } if (TextUtils.isEmpty(message)) { WXLogUtils.e("[WXModalUIModule] toast param parse is null "); return; } if (duration > 3) { duration = Toast.LENGTH_LONG; } else { duration = Toast.LENGTH_SHORT; } if (toast == null) { toast = Toast.makeText(mWXSDKInstance.getContext(), message, duration); } else { toast.setDuration(duration); toast.setText(message); } toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); }
1、utf-8 解碼;
2、取出message值;
3、取出duration值;
4、位置居中;

alert

weex code

Arguments :

    message(string): 顯示內容.
    okTitle(string): 確認按鈕.
    callback(function): 彈出回撥.

Example :

    var arg1 = 'I am alert!';
    var arg2 = 'I am ok';
    var modal = require('@weex-module/modal');
    modal.alert({
      message: arg1,
      okTitle: arg2
    }, function(e) {
      // TODO after the alert is complete.
    })

android method

    @WXModuleAnno
    public void alert(String param, final String callbackId) {
        if (mWXSDKInstance.getContext() instanceof Activity) {
            String message = "";
            String okTitle = WXConst.OK;
            if (!TextUtils.isEmpty(param)) {
                try {
                    param = URLDecoder.decode(param, "utf-8");
                    JSONObject jsObj = new JSONObject(param);
                    message = jsObj.optString(WXConst.MESSAGE);
                    okTitle = jsObj.optString(WXConst.OK_TITLE);
                } catch (Exception e) {
                    WXLogUtils.e("[WXModalUIModule] alert param parse error " + WXLogUtils.getStackTrace(e));
                }
            }
            if (TextUtils.isEmpty(message)) {
                WXLogUtils.e("[WXModalUIModule] alert param parse is null ");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
            builder.setMessage(message);

            final String okTitle_f = TextUtils.isEmpty(okTitle) ? WXConst.OK : okTitle;
            builder.setPositiveButton(okTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, okTitle_f);
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            WXLogUtils.e("[WXModalUIModule] when call alert mWXSDKInstance.getContext() must instanceof Activity");
        }
    }
1、utf-8 解碼;
2、取出message值;
3、取出title值;
4、點選回撥callback;

confirm

weex code

Arguments

    message(string): the message that the confirm shows.
    okTitle(string): the title of confirm button.
    cancelTitle(string): the title of cancel button.
    callback(function): callback when complete.
Example

    var arg1 = 'I am alert!'
    var arg2 = 'I am ok'
    var arg3 = 'I am cancel'
    var modal = require('@weex-module/modal');
    modal.confirm({
      message: arg1,
      okTitle: arg2,
      cancelTitle: arg3
    }, function(e) {
      nativeLog(e.status)
      // TODO after the confirm is complete.
    });

android method

    @WXModuleAnno
    public void confirm(String param, final String callbackId) {
        if (mWXSDKInstance.getContext() instanceof Activity) {
            String message = "";
            String okTitle = WXConst.OK;
            String cancelTitle = WXConst.CANCEL;
            if (!TextUtils.isEmpty(param)) {
                try {
                    param = URLDecoder.decode(param, "utf-8");
                    JSONObject jsObj = new JSONObject(param);
                    message = jsObj.optString(WXConst.MESSAGE);
                    okTitle = jsObj.optString(WXConst.OK_TITLE);
                    cancelTitle = jsObj.optString(WXConst.CANCEL_TITLE);
                } catch (Exception e) {
                    WXLogUtils.e("[WXModalUIModule] confirm param parse error " + WXLogUtils.getStackTrace(e));
                }
            }
            if (TextUtils.isEmpty(message)) {
                WXLogUtils.e("[WXModalUIModule] confirm param parse is null ");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
            builder.setMessage(message);

            final String okTitle_f = TextUtils.isEmpty(okTitle) ? WXConst.OK : okTitle;
            final String cancelTitle_f = TextUtils.isEmpty(cancelTitle) ? WXConst.CANCEL : cancelTitle;

            builder.setPositiveButton(okTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, okTitle_f);
                }
            });
            builder.setNegativeButton(cancelTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, cancelTitle_f);
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            WXLogUtils.e("[WXModalUIModule] when call confirm mWXSDKInstance.getContext() must instanceof Activity");
        }
    }
1、utf-8 解碼;
2、取出message值;
3、取出cancel title值;
4、取出confirm title值;
5、點選回撥callback;

prompt

weex code

Arguments

    message(string): the message that the prompt shows.
    okTitle(string): the title of confirm button.
    cancelTitle(string): the title of cancel button.
    callback(function): callback when complete.

    //返回引數  狀態(字串):單擊使用者單擊的按鈕的標題。
    //         資料(字串):由使用者輸入的文字的值。

Example

var arg1 = 'I am prompt!'
var arg2 = 'I am ok'
var arg3 = 'I am cancel'
var modal = require('@weex-module/modal');
modal.prompt({
  message: arg1,
  okTitle: arg2,
  cancelTitle: arg3
}, function(e) {
  nativeLog(e.status + ', ' + e.data);
  // TODO after the prompt is complete.
});

android method

    @WXModuleAnno
    public void prompt(String param, final String callbackId) {
        if (mWXSDKInstance.getContext() instanceof Activity) {
            String message = "";
            String defaultValue = "";
            String okTitle = WXConst.OK;
            String cancelTitle = WXConst.CANCEL;

            if (!TextUtils.isEmpty(param)) {
                try {
                    param = URLDecoder.decode(param, "utf-8");
                    JSONObject jsObj = new JSONObject(param);
                    message = jsObj.optString("message");
                    okTitle = jsObj.optString("okTitle");
                    cancelTitle = jsObj.optString("cancelTitle");
                    defaultValue = jsObj.optString("default");
                } catch (Exception e) {
                    WXLogUtils.e("[WXModalUIModule] confirm param parse error " + WXLogUtils.getStackTrace(e));
                }
            }

            if (TextUtils.isEmpty(message)) {
                WXLogUtils.e("[WXModalUIModule] confirm param parse is null ");
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
            builder.setMessage(message);

            final EditText editText = new EditText(mWXSDKInstance.getContext());
            editText.setText(defaultValue);
            builder.setView(editText);
            final String okTitle_f = TextUtils.isEmpty(okTitle) ? WXConst.OK : okTitle;
            final String cancelTitle_f = TextUtils.isEmpty(cancelTitle) ? WXConst.CANCEL : cancelTitle;
            builder.setPositiveButton(okTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put(WXConst.RESULT, okTitle_f);
                    result.put(WXConst.DATA, editText.getText().toString());
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, result);

                }
            });
            builder.setNegativeButton(cancelTitle_f, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callbackId, cancelTitle_f);
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            WXLogUtils.e("when call prompt mWXSDKInstance.getContext() must instanceof Activity");
        }
    }
1、utf-8 解碼;
2、取出message值;
3、取出cancel title值;
4、取出confirm title值;
5、點選獲取輸入內容回撥callback;