DialogUtil.java 16.6 KB
package com.cnlive.goldenline.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.api.CmsAPI;
import com.cnlive.goldenline.api.UserAPI;
import com.cnlive.goldenline.model.AwardBean;
import com.cnlive.goldenline.model.EventType;
import com.cnlive.goldenline.model.mine.UpdateGoldBalanceBean;
import com.cnlive.goldenline.model.mine.UpdateGoldRequestBean;
import com.cnlive.goldenline.model.request.AwardRequest;
import com.cnlive.goldenline.ui.activity.WalletActivity;

import org.greenrobot.eventbus.EventBus;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * 对话框工具类
 */
@SuppressLint("InflateParams")
public class DialogUtil {
    private static UserService mUserService;

    /**
     * 弹框时设置背景 阴影度 50%
     */
    private static final float WINDOW_ALPHA_DARK = 0.5f;
    private static TextView[] textViews;

    public static Dialog portraitDialog;//竖屏
    public static Dialog landscapeDialog;//横屏


    private static void setAlpha(Activity context, float alpha) {
        LayoutParams params = context.getWindow().getAttributes();
        params.alpha = alpha;

        context.getWindow().setAttributes(params);
    }

    /**
     * 显示dialog
     *
     * @param view
     */
    private static void showDialog(Dialog dialog, View view, int gravity, boolean cancelable) {
        Window window = dialog.getWindow();
        window.setGravity(gravity); // 此处可以设置dialog显示的位置
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.show();
    }

    //    private static String[] number = {"1", "2", "5", "10", "15", "20"};
    private static String[] number = {"1", "2", "5", "10", "15", "20"};
    private static int[] res_back = {R.drawable.bg_dashang, R.drawable.bg_dashang_press};
    private static int[] res_landscape_back = {R.drawable.bg_landscape_dashang, R.drawable.bg_landscape_dashang_press};

    /**
     * @param context
     * @param title   标题
     * @param url     头像url
     * @param yue     余额
     * @param right   没用
     * @param key     暂时没用
     */
    public static void showPlaytour(final Activity context, final String videoId, String title, final String url, String yue, String right, final String key) {
        portraitDialog = new Dialog(context, R.style.NoTitleDialog);
        //默认1元
        final String[] key_choose = {"1"};
        setAlpha(context, 50);
        final View[] view = {LayoutInflater.from(context).inflate(R.layout.layout_dialog_dashang, null)};
        final ImageView[] ima_diahead = {(ImageView) view[0].findViewById(R.id.ima_diahead)};
        ImageButton imbtn_colse = (ImageButton) view[0].findViewById(R.id.imbtn_colse);
        TextView tv_dianame = (TextView) view[0].findViewById(R.id.tv_dianame);
        TextView text_pay_1 = (TextView) view[0].findViewById(R.id.text_pay_1);
        TextView text_pay_2 = (TextView) view[0].findViewById(R.id.text_pay_2);
        TextView text_pay_5 = (TextView) view[0].findViewById(R.id.text_pay_5);
        TextView text_pay_10 = (TextView) view[0].findViewById(R.id.text_pay_10);
        TextView text_pay_15 = (TextView) view[0].findViewById(R.id.text_pay_15);
        TextView text_pay_20 = (TextView) view[0].findViewById(R.id.text_pay_20);
        Button button_pay = (Button) view[0].findViewById(R.id.button_pay);
        TextView tv_yue = (TextView) view[0].findViewById(R.id.tv_yue);
        TextView tv_button_recharge = (TextView) view[0].findViewById(R.id.tv_button_recharge);
        textViews = new TextView[]{text_pay_1, text_pay_2, text_pay_5, text_pay_10, text_pay_15, text_pay_20};
        setTextColorSize(context, 0);//默认选中第一个
        //Glide.with(context).load(url).error(R.mipmap.ic_launcher).transform(new GlideRoundTransform(context)).fitCenter().into(ima_diahead[0]);
        Glide.with(context).load(url).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new CircleTransform(context))
                .placeholder(R.mipmap.find_default_theatre)
                .error(R.mipmap.find_default_theatre)
                .crossFade()
                .into(ima_diahead[0]);
        if(title!=null){
            tv_dianame.setText(title);
        }else{
            tv_dianame.setText("正在上演推荐");
        }
        tv_yue.setText("金币余额:" + yue);
        for (int i = 0; i < textViews.length; i++) {
            final int finalI = i;
            textViews[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTextColorSize(context, finalI);
                    key_choose[0] = number[finalI];
//                    ToastUtil.showToast("充值金额=="+number[finalI]);
                }
            });
        }
        imbtn_colse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                portraitDialog.dismiss();
            }
        });
        mUserService = new UserService(context);
        //打赏
        button_pay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RestAdapterUtils.getRestAPI(CmsAPI.class).getAward(new AwardRequest(mUserService.getUserInfo().getCn_uid(), mUserService.getUserInfo().getNickname(), key_choose[0], "4", videoId, mUserService.getUserInfo().getAvatar()), new Callback<AwardBean>() {
                    @Override
                    public void success(AwardBean awardBean, Response response) {
                        if ("0".equals(awardBean.getCode())) {
                            ToastUtil.showToast("您已打赏成功,感谢您的支持!");
                            portraitDialog.dismiss();
                            updataUserGold();
                            EventBus.getDefault().post(new EventType(EventType.EVENT_REFRESH));
                        } else if ("5".equals(awardBean.getCode())) {
                            ToastUtil.showToast("金币不足 请充值.");
                            WalletActivity.start(context);
                            portraitDialog.dismiss();
                        } else {
                            ToastUtil.showToast("打赏失败");
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        if (NetworkUtils.isNetworkAvaliable(context)) {
                            ToastUtil.showToast("打赏失败");
                        } else {
                            ToastUtil.showToast( "网络跑路了,请连接网络");
                        }
                    }
                });
                //ToastUtil.showToast("充值金额=="+key_choose[0]);
            }
        });
        //充值
        tv_button_recharge.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //EventBus.getDefault().post("button_pay");
                //跳转充值页面
                if (NetworkUtils.isNetworkAvaliable(context)) {
                    WalletActivity.start(context);
                    portraitDialog.dismiss();
                } else {
                    ToastUtil.showToast( "网络跑路了,请连接网络");
                }
            }
        });

        showDialog(portraitDialog, view[0], Gravity.CENTER, true);

        portraitDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                setAlpha(context, 1f);
            }
        });
    }


    /**
     * 横屏打赏
     */
    public static void showLandscapePlaytour(final Activity context, final String videoId, String title, final String url, String yue, String right, final String key) {
        landscapeDialog = new Dialog(context, R.style.NoTitleDialog);
        //默认1元
        final String[] key_choose = {"1"};
        setAlpha(context, 50);
        final View[] view = {LayoutInflater.from(context).inflate(R.layout.layout_landscape_dialog_dashang, null)};
        final ImageView[] ima_diahead_landscape = {(ImageView) view[0].findViewById(R.id.ima_diahead_landscape)};
        ImageButton imbtn_colse = (ImageButton) view[0].findViewById(R.id.imbtn_colse);
        TextView tv_dianame = (TextView) view[0].findViewById(R.id.tv_dianame);
        TextView text_pay_1 = (TextView) view[0].findViewById(R.id.text_pay_1);
        TextView text_pay_2 = (TextView) view[0].findViewById(R.id.text_pay_2);
        TextView text_pay_5 = (TextView) view[0].findViewById(R.id.text_pay_5);
        TextView text_pay_10 = (TextView) view[0].findViewById(R.id.text_pay_10);
        TextView text_pay_15 = (TextView) view[0].findViewById(R.id.text_pay_15);
        TextView text_pay_20 = (TextView) view[0].findViewById(R.id.text_pay_20);
        Button button_pay = (Button) view[0].findViewById(R.id.button_pay);
        TextView tv_yue = (TextView) view[0].findViewById(R.id.tv_yue);
        TextView tv_button_recharge = (TextView) view[0].findViewById(R.id.tv_button_recharge);
        textViews = new TextView[]{text_pay_1, text_pay_2, text_pay_5, text_pay_10, text_pay_15, text_pay_20};
        setLandscapeTextColorSize(context, 0);//默认选中第一个
        Glide.with(context).load(url).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new CircleTransform(context))
                .placeholder(R.mipmap.find_default_theatre)
                .error(R.mipmap.find_default_theatre)
                .crossFade()
                .into(ima_diahead_landscape[0]);
        if(title!=null){
            tv_dianame.setText(title);
        }else{
            tv_dianame.setText("正在上演推荐");
        }
        tv_yue.setText("金币余额:" + yue);
        for (int i = 0; i < textViews.length; i++) {
            final int finalI = i;
            textViews[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setLandscapeTextColorSize(context, finalI);
                    key_choose[0] = number[finalI];
//                    ToastUtil.showToast("充值金额=="+number[finalI]);
                }
            });
        }
        imbtn_colse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                landscapeDialog.dismiss();
            }
        });
        mUserService = new UserService(context);
        //打赏
        button_pay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RestAdapterUtils.getRestAPI(CmsAPI.class).getAward(new AwardRequest(mUserService.getUserInfo().getCn_uid(), mUserService.getUserInfo().getNickname(), key_choose[0], "4", videoId, mUserService.getUserInfo().getAvatar()), new Callback<AwardBean>() {
                    @Override
                    public void success(AwardBean awardBean, Response response) {
                        if ("0".equals(awardBean.getCode())) {
                            ToastUtil.showToast("您已打赏成功,感谢您的支持!");
                            landscapeDialog.dismiss();
                            updataUserGold();
                            EventBus.getDefault().post(new EventType(EventType.EVENT_REFRESH));
                        } else if ("5".equals(awardBean.getCode())) {
                            ToastUtil.showToast("金币不足 请充值.");
                            WalletActivity.start(context);
                            landscapeDialog.dismiss();
                        } else {
                            ToastUtil.showToast("打赏失败");
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        if (NetworkUtils.isNetworkAvaliable(context)) {
                            ToastUtil.showToast("打赏失败");
                        } else {
                            ToastUtil.showToast( "网络跑路了,请连接网络");
                        }
                    }
                });
                //ToastUtil.showToast("充值金额=="+key_choose[0]);
            }
        });
        //充值
        tv_button_recharge.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //EventBus.getDefault().post("button_pay");
                //跳转充值页面
                if (NetworkUtils.isNetworkAvaliable(context)) {
                    WalletActivity.start(context);
                    landscapeDialog.dismiss();
                } else {
                    ToastUtil.showToast( "网络跑路了,请连接网络");
                }
            }
        });

        showDialog(landscapeDialog, view[0], Gravity.CENTER, true);

        landscapeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                setAlpha(context, 1f);
            }
        });
    }


    private static String gold;

    private static void updataUserGold() {
        RestAdapterUtils.getRestAPI(UserAPI.class).getUsersInfo(new UpdateGoldRequestBean(mUserService.getUserInfo().getCn_uid()), new Callback<UpdateGoldBalanceBean>() {
            @Override
            public void success(UpdateGoldBalanceBean giftInfo, Response response) {
                if (giftInfo.getCode() == 0) {
                    gold = giftInfo.getData().getGold();
                    mUserService.updateGlod(gold);
                }
            }

            @Override
            public void failure(RetrofitError error) {
            }
        });
    }


    /**
     * 设置文字大小颜色
     *
     * @param key
     */
    private static void setTextColorSize(Context context, int key) {
        for (int i = 0; i < textViews.length; i++) {
            if (i == key) {//默认30
                textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "金币", R.style.textstyle_15_ff633, R.style.textstyle_11_ff633, 2));
            } else {
                textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "金币", R.style.textstyle_15_999, R.style.textstyle_11_999, 2));
            }
        }
        for (int i = 0; i < textViews.length; i++) {//设置点击背景颜色
            if (i == key) {
                textViews[i].setBackgroundResource(res_back[1]);
            } else {
                textViews[i].setBackgroundResource(res_back[0]);
            }
        }
    }

    /**
     * 设置横屏文字大小颜色
     *
     * @param key
     */
    private static void setLandscapeTextColorSize(Context context, int key) {
        for (int i = 0; i < textViews.length; i++) {
            if (i == key) {//默认30
                textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "元/金币", R.style.textstyle_14_fff, R.style.textstyle_14_fff, 2));
            } else {
                textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "元/金币", R.style.textstyle_14_fff, R.style.textstyle_14_fff, 2));
            }
        }
        for (int i = 0; i < textViews.length; i++) {//设置点击背景颜色
            if (i == key) {
                textViews[i].setBackgroundResource(res_landscape_back[1]);
            } else {
                textViews[i].setBackgroundResource(res_landscape_back[0]);
            }
        }
    }

    /**
     * 打赏
     *
     * @param context
     * @param url
     */
    public static void showShareView(final Activity context, String url) {
        final Dialog dialog = new Dialog(context, R.style.NoTitleDialog);
        setAlpha(context, 50);
        final View[] view = {LayoutInflater.from(context).inflate(R.layout.layout_dialog_dashang, null)};


        showDialog(dialog, view[0], Gravity.CENTER, true);


    }
}