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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.tejia.lijin.app.ui.gold.view;
 
import android.content.DialogInterface;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.databinding.ActivityGoldExchangeBinding;
import com.tejia.lijin.app.databinding.ItemRecyclerviewBottom3Binding;
import com.tejia.lijin.app.entity.GoldExchange;
import com.tejia.lijin.app.entity.GoldExchangeState;
import com.tejia.lijin.app.ui.dialog.GoldExchangeNotEnoughDialog;
import com.tejia.lijin.app.ui.dialog.GoldExchangeStateDialog;
import com.tejia.lijin.app.ui.gold.base.BaseMVPActivity;
import com.tejia.lijin.app.ui.gold.contract.GoldExchangeContract;
import com.tejia.lijin.app.ui.gold.presenter.GoldExchangePresenter;
import com.tejia.lijin.app.ui.goldtask.GoldTaskActivity;
import com.tejia.lijin.app.util.TopStatusSettings;
 
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 金币兑换Activity
 */
public class GoldExchangeActivity extends BaseMVPActivity<GoldExchangePresenter> implements View.OnClickListener, GoldExchangeContract.goldExchageInfo {
 
    ActivityGoldExchangeBinding binding;
    ItemRecyclerviewBottom3Binding bottomBinding;
    GoldExchangeAdapter adapter;
    List<GoldExchange> mList;
    int page = 1;
    int count = 0;
    boolean isLoad = true;
    boolean isTitleInput = false;
 
    @Override
    protected void initActivityView(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_gold_exchange);
    }
 
    @Override
    protected void initView() {
        TopStatusSettings.setStatusView(this, binding.includeStatusBar.vStatusBar);
        binding.includeTopBar.tvTopBarMiddle.setText("金币兑换");
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        binding.rvGoldExchange.setLayoutManager(manager);
        DividerItemDecoration decoration = new DividerItemDecoration(DividerItemDecoration.VERTICAL);
        decoration.setSize(1);
        binding.rvGoldExchange.addItemDecoration(decoration);
 
        mList = new ArrayList<>();
        adapter = new GoldExchangeAdapter(this, mList, mPresenter);
        adapter.setHasStableIds(true);
        binding.rvGoldExchange.setAdapter(adapter);
 
        bottomBinding = DataBindingUtil.inflate(LayoutInflater.from(this),
                R.layout.item_recyclerview_bottom3, null, false);
        adapter.addFooterView(bottomBinding.getRoot());
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
                R.drawable.anim_list_loading);
        bottomBinding.ivLoading.setImageDrawable(animationDrawable);
        animationDrawable.start();
        bottomBinding.getRoot().setVisibility(View.GONE);
        binding.llAvailableGold.setVisibility(View.GONE);
 
        isTitleInput = Boolean.parseBoolean(getIntent().getStringExtra("balanceMore"));
        binding.tvAvailableGold.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                isTitleInput ? R.drawable.ic_gold_exchange_input : 0, 0);
        binding.includeTopBar.tvTopBarLeft.setOnClickListener(this);
        binding.llAvailableGold.setOnClickListener(this);
        binding.srlGoldExchange.setColorSchemeColors(getResources().getColor(R.color.theme));
        binding.rvGoldExchange.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int last = layoutManager.findLastVisibleItemPosition();
                int total = layoutManager.getItemCount();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (last == total - 1 && mList.size() < count && isLoad) {
                        page++;
                        isLoad = false;
                        bottomBinding.ivLoading.setVisibility(View.VISIBLE);
                        bottomBinding.tvLoading.setText("正在加载更多是数据");
                        mPresenter.getGoldExchangeList(GoldExchangeActivity.this, page);
                    }
                }
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        binding.srlGoldExchange.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mPresenter.getGoldExchangeList(GoldExchangeActivity.this, page);
            }
        });
        mPresenter.getGoldExchangeList(GoldExchangeActivity.this, page);
    }
 
    @Override
    protected GoldExchangePresenter createPresenter() {
        return new GoldExchangePresenter(this);
    }
 
    @Override
    public void onGainListDataSuccess(JSONObject jsonObject) {
        isLoad = true;
        binding.srlGoldExchange.setRefreshing(false);
        if (jsonObject.optInt("code") == 0) {
            binding.llAvailableGold.setVisibility(View.VISIBLE);
            count = jsonObject.optJSONObject("data").optInt("count");
            binding.tvAvailableGold.setText(jsonObject.optJSONObject("data").optString("goldCoin"));
            Gson gson = new GsonBuilder().serializeNulls().create();
            List<GoldExchange> list = gson.fromJson(jsonObject
                            .optJSONObject("data").optJSONArray("list").toString(),
                    new TypeToken<List<GoldExchange>>() {
                    }.getType());
            if (page == 1) {
                mList.clear();
            }
            mList.addAll(list);
            adapter.notifyDataSetChanged();
 
            if (mList.size() == count) {
                bottomBinding.getRoot().setVisibility(View.VISIBLE);
                bottomBinding.ivLoading.setVisibility(View.GONE);
                bottomBinding.tvLoading.setText("更多金币兑换,敬请期待^_^");
            }
        } else {
            Toast.makeText(GoldExchangeActivity.this,
                    jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    public void onGainListDataFailure(String errorStr) {
        binding.srlGoldExchange.setRefreshing(false);
        bottomBinding.getRoot().setVisibility(View.GONE);
        isLoad = true;
        Log.e("GoldExchangeActivity", "onGainListDataFailure---" + errorStr);
    }
 
    @Override
    public void onGoExchangeVerifySuccess(JSONObject jsonObject) {
        if (jsonObject.optInt("code") == 0) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            GoldExchangeState info = gson.fromJson(jsonObject.optJSONObject("data")
                    .optJSONObject("result").toString(), new TypeToken<GoldExchangeState>() {
            }.getType());
 
            if (info.getType().equals("notEnough")) {
                GoldExchangeNotEnoughDialog.Builder builder = new GoldExchangeNotEnoughDialog.Builder(this);
                builder.setTitle("金币不足提醒")
                        .setMessage(info.getTip())
                        .setInfo(info.getGoldCoin())
                        .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            } else if (info.getType().equals("inviteCodeActivate")) {//直接跳转邀请码激活页面
                startActivity(new Intent(GoldExchangeActivity.this, InviteCodeExchangeActivity.class));
            } else {
                goldExchangeState(info);
            }
 
        } else {
            Toast.makeText(GoldExchangeActivity.this,
                    jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    public void onGoExchangeVerifyFailure(String errorStr) {
        Log.e("GoldExchangeActivity", "onGoExchangeVerifyFailure---" + errorStr);
    }
 
    @Override
    public void onGoExchangeSuccess(JSONObject jsonObject, String id) {
        if (jsonObject.optInt("code") == 0) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            GoldExchange info = gson.fromJson(jsonObject.optJSONObject("data")
                    .optJSONObject("result").toString(), new TypeToken<GoldExchange>() {
            }.getType());
 
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getId().equals(id)) {
                    mList.remove(i);
                    if (info != null) {
                        mList.add(i, info);
                        adapter.notifyItemChanged(i);
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                    break;
                }
            }
            binding.tvAvailableGold.setText(jsonObject.optJSONObject("data").optString("goldCoin"));
            Toast.makeText(GoldExchangeActivity.this,
                    "兑换成功,金币已消耗", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(GoldExchangeActivity.this,
                    jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    public void onGoExchangeFailure(String errorStr) {
        Log.e("GoldExchangeActivity", "onGoExchangeFailure---" + errorStr);
    }
 
    private void goldExchangeState(final GoldExchangeState info) {
        GoldExchangeStateDialog.Builder builder = new GoldExchangeStateDialog.Builder(this);
        if (info.getType().equals("freeCouponBuy")) {
            builder.setTitle("自购免单券兑换")
                    .setType(1);
        } else if (info.getType().equals("freeCouponGive")) {
            builder.setTitle("赠送免单券兑换")
                    .setType(1);
        } else if (info.getType().equals("rebatePercentCoupon")) {
            builder.setTitle("返利奖励券兑换")
                    .setType(1);
        } else if (info.getType().equals("inviteCodePublish")) {
            builder.setTitle("邀请码发布卡兑换")
                    .setType(2);
        } else if (info.getType().equals("taoLiJin")) {
            builder.setTitle("推广红包兑换")
                    .setType(3);
        } else if (info.getType().equals("cash")) {
            builder.setTitle("现金红包兑换")
                    .setType(4);
        }
        builder.setInfo(info).setPositiveButton("确认兑换", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPresenter.goExchangeGoods(GoldExchangeActivity.this, info.getId());
                dialog.dismiss();
            }
        }).create().show();
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.ll_available_gold:
                if (isTitleInput) {
                    startActivity(new Intent(GoldExchangeActivity.this, GoldTaskActivity.class));
                }
                break;
        }
    }
}