admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.tejia.lijin.app.ui.dialog;
 
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.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.bumptech.glide.Glide;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
 
import java.math.BigDecimal;
 
public class CustomGoodsDetailBottomDialog extends Dialog {
 
    public CustomGoodsDetailBottomDialog(Context context) {
        super(context);
    }
 
    public CustomGoodsDetailBottomDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public static class Builder {
        private Activity context;
        private TaoBaoGoodsBrief mInfo;
        private String userHongbao;
        private String positiveButtonText;
        private String negativeButtonText;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
 
        public Builder(Activity context) {
            this.context = context;
        }
 
        public Builder setInfo(TaoBaoGoodsBrief info) {
            this.mInfo = info;
            return this;
        }
 
        public Builder setSurplus(String userHongbao) {
            this.userHongbao = userHongbao;
            return this;
        }
 
 
        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        CustomGoodsDetailBottomDialog dialog;
        int num;
 
        public CustomGoodsDetailBottomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
 
            int width = wm.getDefaultDisplay().getWidth();
            dialog = new CustomGoodsDetailBottomDialog(context,
                    R.style.Dialog);
 
            View layout = inflater.inflate(R.layout.custom_goods_detail_bottom_dialog,
                    null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            // set the cancel button
            dialog.setContentView(layout);
 
            ImageView iv_goods_img = layout.findViewById(R.id.iv_goods_img);
            TextView tv_goods_price_coupon = layout.findViewById(R.id.tv_goods_price_coupon);
            TextView tv_rebate_num = layout.findViewById(R.id.tv_rebate_num);
            TextView tv_goods_price_before = layout.findViewById(R.id.tv_goods_price_before);
            TextView tv_share_red_packet_date = layout.findViewById(R.id.tv_share_red_packet_date);
            TextView tv_share_red_packet_num = layout.findViewById(R.id.tv_share_red_packet_num);
            final ImageView iv_num_subtraction = layout.findViewById(R.id.iv_num_subtraction);
            final TextView tv_num = layout.findViewById(R.id.tv_num);
            final ImageView iv_num_addition = layout.findViewById(R.id.iv_num_addition);
            final TextView tv_need_num = layout.findViewById(R.id.tv_need_num);
            final TextView tv_surplus_num = layout.findViewById(R.id.tv_surplus_num);
            FrameLayout fl_red_packet = layout.findViewById(R.id.fl_red_packet);
            FrameLayout fl_create_share = layout.findViewById(R.id.fl_create_share);
 
            Glide.with(context).load(mInfo.getImgList().get(0)).into(iv_goods_img);
            if (mInfo.isHasCoupon())
                tv_goods_price_coupon.setText(mInfo.getCouponPrice());
            if (mInfo.getMoneyInfo().getMoneyType() == 1) {
                tv_rebate_num.setText(" 返" + mInfo.getMoneyInfo().getFanliMoney());
                tv_rebate_num.setBackgroundResource(R.drawable.shape_red_goods_detail_corner);
                tv_rebate_num.setTextColor(context.getResources().getColor(R.color.main_text_color));
            } else {
                tv_rebate_num.setText(" 奖" + mInfo.getMoneyInfo().getFanliMoney());
                tv_rebate_num.setBackgroundResource(R.drawable.shape_goods_list_price_reward);
                tv_rebate_num.setTextColor(context.getResources().getColor(R.color.orange4));
            }
            tv_goods_price_before.setText(mInfo.getPriceName() + "  ¥" + mInfo.getZkPrice());
            tv_share_red_packet_num.setText("¥ " + mInfo.getOtherInfo().getSpreadHongBao().getMoney());
            tv_share_red_packet_date.setText(mInfo.getOtherInfo().getSpreadHongBao().getTip());
 
            iv_num_subtraction.setEnabled(false);
            if (Double.parseDouble(userHongbao) >= Double.parseDouble(mInfo.getOtherInfo().getSpreadHongBao().getMoney())) {
                num = 1;
                iv_num_addition.setEnabled(true);
            } else {
                iv_num_addition.setEnabled(false);
                num = 0;
                fl_create_share.setBackgroundResource(R.drawable.shape_login_gain_sms);
            }
            tv_num.setText(num + "");
            BigDecimal money = new BigDecimal(mInfo.getOtherInfo().getSpreadHongBao().getMoney()).multiply(new BigDecimal(num));
            tv_need_num.setText(money + "");
            BigDecimal surplusMoney = new BigDecimal(userHongbao).subtract(money);
            tv_surplus_num.setText("剩余推广红包 ¥ " + surplusMoney);
 
            iv_num_subtraction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (num > 1) {
                        num--;
                        iv_num_subtraction.setEnabled(num == 1 ? false : true);
                    }
                    iv_num_addition.setEnabled(Double.parseDouble(userHongbao) >=
                            (Double.parseDouble(mInfo.getOtherInfo().getSpreadHongBao().getMoney()) * num + 1) ? true : false);
                    tv_num.setText(num + "");
                    BigDecimal money = new BigDecimal(mInfo.getOtherInfo().getSpreadHongBao().getMoney()).multiply(new BigDecimal(num));
                    tv_need_num.setText(money + "");
                    BigDecimal surplusMoney = new BigDecimal(userHongbao).subtract(money);
                    tv_surplus_num.setText("剩余推广红包 ¥ " + surplusMoney);
                }
            });
 
            iv_num_addition.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Double.parseDouble(userHongbao) >=
                            (Double.parseDouble(mInfo.getOtherInfo().getSpreadHongBao().getMoney()) * (num + 1))) {
                        num++;
                        iv_num_addition.setEnabled(Double.parseDouble(userHongbao) >=
                                (Double.parseDouble(mInfo.getOtherInfo().getSpreadHongBao()
                                        .getMoney()) * (num + 1)) ? true : false);
                    }
                    iv_num_subtraction.setEnabled(num == 1 ? false : true);
                    tv_num.setText(num + "");
                    BigDecimal money = new BigDecimal(mInfo.getOtherInfo().getSpreadHongBao().getMoney()).multiply(new BigDecimal(num));
                    tv_need_num.setText(money + "");
                    BigDecimal surplusMoney = new BigDecimal(userHongbao).subtract(money);
                    tv_surplus_num.setText("剩余推广红包 ¥ " + surplusMoney);
                }
            });
 
            if (negativeButtonClickListener != null) {
                fl_red_packet.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            }
            if (positiveButtonClickListener != null) {
                fl_create_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (num > 0) {
                            positiveButtonClickListener.onClick(dialog, num);
                        } else {
                            Toast.makeText(context, "推广红包余额不足", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
 
            WindowManager.LayoutParams params = dialog.getWindow()
                    .getAttributes();
            params.gravity = Gravity.BOTTOM;
            params.width = width;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setAttributes(params);
            dialog.getWindow().setWindowAnimations(R.style.bottomstyle);
 
            dialog.setCanceledOnTouchOutside(true);
            return dialog;
        }
 
    }
}