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
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.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.ActivityInviteCodeListBinding;
import com.tejia.lijin.app.databinding.ItemRecyclerviewBottom3Binding;
import com.tejia.lijin.app.entity.GoldExchangeState;
import com.tejia.lijin.app.entity.InviteCodeInfo;
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.InviteCodeExchangeContract;
import com.tejia.lijin.app.ui.gold.presenter.InviteCodeExchangePresenter;
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
import com.tejia.lijin.app.ui.mine.ActivationInviteCodeAcitvity;
import com.tejia.lijin.app.util.TopStatusSettings;
import com.tejia.lijin.app.util.clipboard.ClipboardUtil;
 
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
public class InviteCodeExchangeActivity extends BaseMVPActivity<InviteCodeExchangePresenter>
        implements InviteCodeExchangeContract.inviteCodeExchange, View.OnClickListener {
 
    ActivityInviteCodeListBinding binding;
    ItemRecyclerviewBottom3Binding bottomBinding;
 
    InviteCodeExchangeAdapter adapter;
    List<InviteCodeInfo> mList = new ArrayList<>();
 
    int page = 1;
    int count = 0;
    boolean isLoad = true;
 
    String helpLink = "";
 
    @Override
    protected void initActivityView(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_invite_code_list);
    }
 
    @Override
    protected void initView() {
        TopStatusSettings.setStatusView(this, binding.includeStatusBar.vStatusBar);
        binding.includeTopBar.tvTopBarMiddle.setText("兑换邀请码");
        binding.includeTopBar.ivTopBarRight1.setVisibility(View.VISIBLE);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        binding.rvInviteCodeList.setLayoutManager(manager);
        adapter = new InviteCodeExchangeAdapter(this, mList, mPresenter);
        binding.rvInviteCodeList.setAdapter(adapter);
        DividerItemDecoration decoration = new DividerItemDecoration(DividerItemDecoration.VERTICAL);
        decoration.setSize(1);
        binding.rvInviteCodeList.addItemDecoration(decoration);
 
        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.includeTopBar.tvTopBarLeft.setOnClickListener(this);
        binding.includeTopBar.ivTopBarRight1.setOnClickListener(this);
        binding.srlInviteCodeList.setColorSchemeColors(getResources().getColor(R.color.theme));
        binding.rvInviteCodeList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int first = layoutManager.findFirstVisibleItemPosition();
                int last = layoutManager.findLastVisibleItemPosition();
                int total = layoutManager.getItemCount();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (first == 0) {
                        binding.ivSlidTop.setVisibility(View.GONE);
                    }
                    if (last == (total - 1) && mList.size() < count && isLoad) {
                        page++;
                        isLoad = false;
                        bottomBinding.ivLoading.setVisibility(View.VISIBLE);
                        bottomBinding.tvLoading.setText("正在加载更多是数据");
                        mPresenter.getInviteCodeList(page);
                    }
                }
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (Math.abs(dy) > 10) {
                    binding.ivSlidTop.setVisibility(dy > 0 ? View.GONE : View.VISIBLE);
                }
            }
        });
 
        binding.srlInviteCodeList.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mPresenter.getInviteCodeList(page);
            }
        });
 
        mPresenter.getInviteCodeList(page);
    }
 
    @Override
    protected InviteCodeExchangePresenter createPresenter() {
        return new InviteCodeExchangePresenter(this);
    }
 
 
    @Override
    public void onGetInviteCodeListSuccess(JSONObject jsonObject) {
        binding.srlInviteCodeList.setRefreshing(false);
        isLoad = true;
        if (jsonObject.optInt("code") == 0) {
            count = jsonObject.optJSONObject("data").optInt("count");
            helpLink = jsonObject.optJSONObject("data").optString("helpLink");
            Gson gson = new GsonBuilder().serializeNulls().create();
            List<InviteCodeInfo> list = gson.fromJson(jsonObject
                            .optJSONObject("data").optJSONArray("list").toString(),
                    new TypeToken<List<InviteCodeInfo>>() {
                    }.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(InviteCodeExchangeActivity.this,
                    jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    public void onGetInviteCodeListFailure(String errorStr) {
        binding.srlInviteCodeList.setRefreshing(false);
        bottomBinding.getRoot().setVisibility(View.GONE);
        isLoad = true;
    }
 
    @Override
    public void onGoExchangeVerifySuccess(final JSONObject jsonObject) {
        if (jsonObject.optInt("code") == 0) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            final 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 {
                GoldExchangeStateDialog.Builder builder = new GoldExchangeStateDialog.Builder(this);
                builder.setTitle("邀请码激活卡兑换")
                        .setType(1).setInfo(info).setPositiveButton("确认兑换", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.inviteCodeExchange(info.getId());
                        dialog.dismiss();
                    }
                }).create().show();
            }
 
        } else {
            Toast.makeText(InviteCodeExchangeActivity.this, jsonObject
                    .optString("msg"), Toast.LENGTH_LONG).show();
 
        }
    }
 
    @Override
    public void onGoExchangeVerifyFailure(String errorStr) {
 
    }
 
    @Override
    public void onGoExchangeSuccess(JSONObject jsonObject, String id) {
        if (jsonObject.optInt("code") == 0) {
            Toast.makeText(InviteCodeExchangeActivity.this,
                    jsonObject.optJSONObject("data").optString("msg"), Toast.LENGTH_LONG).show();
 
            ClipboardUtil.copy(getApplicationContext(),   jsonObject.optJSONObject("data").optString("inviteCode"));
 
            startActivity(new Intent(InviteCodeExchangeActivity.this, ActivationInviteCodeAcitvity.class));
        } else {
            Toast.makeText(InviteCodeExchangeActivity.this,
                    jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    public void onGoExchangeFailure(String errorStr) {
 
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.iv_top_bar_right1:
                Intent intent = new Intent(InviteCodeExchangeActivity.this, ShareBrowserActivity.class);
                intent.putExtra("url", helpLink);
                startActivity(intent);
                break;
        }
    }
}