admin
2021-05-28 98cc34dbca6d6218ec5e72baffda2d3a1dd72a55
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package com.tejia.lijin.app.ui.mine;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.tejia.lijin.app.ShoppingApplication;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.content.ConnectivityChangeHelper;
import com.wpc.library.dialog.dateselect.CustomDatePicker;
import com.wpc.library.dialog.dateselect.DateFormatUtils;
import com.wpc.library.util.NetUtils;
import com.wpc.library.util.SystemCommon;
import com.wpc.library.util.common.StringUtils;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.entity.Account;
import com.tejia.lijin.app.entity.AccountMonth;
import com.tejia.lijin.app.ui.BaseActivity;
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
import com.tejia.lijin.app.util.TopStatusSettings;
import com.tejia.lijin.app.util.umengCustomEvent.UserCustomEvent;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
/**
 * 金币明细
 */
public class GoldDescActivity extends BaseActivity implements View.OnClickListener {
 
    private TextView tv_left, tv_middle, tv_right, tv_no_data_hint,
            tv_select_time, tv_all_income, tv_all_expenses;
    private SwipeRefreshLayout srl_account_desc;
    private RecyclerView rv_account_desc;
    View bottom;
    private ImageView iv_loading,iv_no_data;
    private TextView tv_loading;
    private String TYPE = "accountDetail";
    private TextView tv_notice_content;
    private LinearLayout ll_notice;
    private ImageView iv_close;
    private LinearLayout ll_account_layout;
 
    private ConnectivityChangeHelper mChangeHelper;
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture, ll_title;
    List<Account> mList = new ArrayList<>();
    private String index = "";
    private String mYear = "";
    private String mMonth = "";
    private int type = 0;
    GoldDescAdapter adapter;
    private float mSuspensionHeight = 0;
    LinearLayoutManager linearLayoutManager;
    int mCurrentPosition = 0;
    int count = 0;
    private CustomDatePicker mDatePicker;
    private boolean isLoad = false;
 
    PopupWindow mTopRightPopupWindow;
    private List<String> mulList = new ArrayList<>();
    GoldDescFilterAdapter filterAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_desc);
        TopStatusSettings.setStatusViewAndDeepColor(this);
        initView();
        addListener();
    }
 
    private void initView() {
        tv_left = findViewById(R.id.tv_top_bar_left);
        tv_middle = findViewById(R.id.tv_top_bar_middle);
        tv_right = findViewById(R.id.tv_top_bar_right);
        ll_title = findViewById(R.id.ll_title);
        tv_select_time = findViewById(R.id.tv_select_time);
        tv_all_income = findViewById(R.id.tv_all_income);
        tv_all_expenses = findViewById(R.id.tv_all_expenses);
        srl_account_desc = findViewById(R.id.srl_account_desc);
        rv_account_desc = findViewById(R.id.rv_account_desc);
        ll_account_layout = findViewById(R.id.ll_account_layout);
        linearLayoutManager = new LinearLayoutManager(this);
        rv_account_desc.setLayoutManager(linearLayoutManager);
        adapter = new GoldDescAdapter(this, mList);
        rv_account_desc.setAdapter(adapter);
        srl_account_desc.setColorSchemeColors(getResources().getColor(R.color.theme));
        bottom = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom, null);
        tv_loading = bottom.findViewById(R.id.tv_loading);
        iv_loading = bottom.findViewById(R.id.iv_loading);
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
                R.drawable.anim_list_loading);
        iv_loading.setImageDrawable(animationDrawable);
        animationDrawable.start();
//        Glide.with(this).load(R.drawable.gif_loading).into(iv_loading);
        bottom.setVisibility(View.GONE);
        adapter.addFooterView(bottom);
 
        tv_notice_content = findViewById(R.id.tv_notice_content);
        ll_notice = findViewById(R.id.ll_notice);
        iv_close = findViewById(R.id.iv_close);
        getAppPageNotification();
 
        tv_right.setText("筛选");
        tv_right.setOnClickListener(this);
        tv_right.setVisibility(View.GONE);
 
        ll_no_net = findViewById(R.id.ll_no_net);
        ll_no_data = findViewById(R.id.ll_no_data);
        ll_request_failture = findViewById(R.id.ll_request_failture);
        tv_no_data_hint = findViewById(R.id.tv_no_data_hint);
        iv_no_data = findViewById(R.id.iv_no_data);
 
        tv_middle.setText("金币明细");
        initPopubWindow();
    }
 
    private void addListener() {
        tv_left.setOnClickListener(this);
        ll_title.setOnClickListener(this);
        findViewById(R.id.tv_net_setting).setOnClickListener(this);
        findViewById(R.id.tv_refresh).setOnClickListener(this);
        mChangeHelper = new ConnectivityChangeHelper(this,
                new ConnectivityChangeHelper.OnConnectivityChangeListener() {
 
                    @Override
                    public void onNetworkUnAvailable() {
                        if (mList.size()==0)
                            requestState(3);
                    }
 
                    @Override
                    public void onNetworkAvailable() {
                        requestState(0);
                        if (mList.size() == 0) {
                            getAccountDesc();
                        }
                    }
 
                });
        srl_account_desc.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                index = "";
                mYear = "";
                mMonth = "";
                getAccountDesc();
            }
        });
        rv_account_desc.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                mSuspensionHeight = ll_title.getHeight();
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int last = manager.findLastVisibleItemPosition();
                int total = manager.getItemCount();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (last == total - 1 && mList.size() < count && isLoad) {
                        index = mList.get(mList.size() - 1).getDetail().getId();
                        isLoad = false;
                        iv_loading.setVisibility(View.VISIBLE);
                        tv_loading.setText("正在加载更多是数据");
                        getAccountDesc();
                    }
                }
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);
 
                //获取到的view顶部坐标小于悬浮条最底部的y轴坐标就是要重叠了
                //datas.get(mCurrentPosition+1).isTo 表示下个item存在悬浮条
                if (view != null && view.getTop() <= mSuspensionHeight && mList.get(mCurrentPosition + 1).getMonth() != null) {
                    ll_title.setY(-(mSuspensionHeight - view.getTop()));
                } else {
                    ll_title.setY(0);
                    if (mList.size() - 1 <= mCurrentPosition) {
                        return;
                    }
                    if (dy >= 0) {
                        if (mList.get(mCurrentPosition).getMonth() != null) {
                            tv_select_time.setText(mList.get(mCurrentPosition).getMonth().getYear() + "年"
                                    + mList.get(mCurrentPosition).getMonth().getMonth() + "月");
                            tv_all_income.setText("总获取:" + mList.get(mCurrentPosition).getMonth().getIncome() + "枚");
                            tv_all_expenses.setText("总消耗:" + mList.get(mCurrentPosition).getMonth().getExpend() + "枚");
                        }
                    } else {
                        if (mCurrentPosition >= 0 && mList.get(mCurrentPosition).getDetail() != null &&
                                mList.get(mCurrentPosition).getDetail().getAccountMonth() != null) {
//                            Log.e("mResult", mCurrentPosition + "");
                            tv_select_time.setText(mList.get(mCurrentPosition).getDetail().getAccountMonth().getYear() + "年"
                                    + mList.get(mCurrentPosition).getDetail().getAccountMonth().getMonth() + "月");
                            tv_all_income.setText("总获取:" + mList.get(mCurrentPosition).getDetail().getAccountMonth().getIncome() + "枚");
                            tv_all_expenses.setText("总消耗:" + mList.get(mCurrentPosition).getDetail().getAccountMonth().getExpend() + "枚");
                        }
                    }
                }
 
                //mCurrentPosition 不是 当前显示最顶部的position
                if (mCurrentPosition != linearLayoutManager.findFirstVisibleItemPosition()) {
                    mCurrentPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    ll_title.setY(0);
                }
            }
        });
    }
 
    /**
     * 初始化右上角提示
     */
    private void initPopubWindow() {
        View view = getLayoutInflater().inflate(R.layout.item_share_from_filter,
                null, false);
        // 创建PopupWindow实例,200,150分别是宽度和高度
        mTopRightPopupWindow = new PopupWindow(view, (int) (SystemCommon.getScreenWidth(this) / 2), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
//        pw_filter.setAnimationStyle(R.style.pop_multiple);
        mTopRightPopupWindow.setTouchable(true);
        mTopRightPopupWindow.setFocusable(true);
        ListView lv = view.findViewById(R.id.lv_filter_pw);
        mulList.add("全部");
        mulList.add("获取");
        mulList.add("消耗");
        filterAdapter = new GoldDescFilterAdapter(this, mulList);
        lv.setAdapter(filterAdapter);
 
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mTopRightPopupWindow.dismiss();
                type = position;
                setBackgroundAlpha(1f);
                getAccountDesc();
            }
        });
        mTopRightPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f);
            }
        });
    }
 
    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
 
    private void getAccountDesc() {
        ShoppingApi.getGoldDetails(this, UserUtil.getUid(ShoppingApplication.application), index, mYear, mMonth, type + "", new BasicTextHttpResponseHandler() {
 
            @Override
            public void onStart() {
                super.onStart();
                if (!StringUtils.isEmpty(index)) {
                    bottom.setVisibility(View.VISIBLE);
                } else {
                    bottom.setVisibility(View.GONE);
                }
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    List<Account> list = gson.fromJson(jsonObject.optJSONObject("data").optJSONArray("data").toString(), new TypeToken<List<Account>>() {
                    }.getType());
                    boolean isSelectTime = false;
                    if (StringUtils.isEmpty(index)) {
                        mList.clear();
                        isSelectTime = true;
                    }
                    AccountMonth month = null;
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getMonth() != null) {
                            month = list.get(i).getMonth();
                        }
                        if (list.get(i).getDetail() != null) {
                            list.get(i).getDetail().setAccountMonth(month);
                        }
                    }
                    mList.addAll(list);
 
                    if (isSelectTime) {
                        rv_account_desc.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
//                        adapter.notifyItemRangeChanged(mList.size() - 1, list.size());//倒序批量刷新列表
                    }
 
                    if (mList.size() == 1) {
                        tv_select_time.setText(mList.get(mCurrentPosition).getMonth().getYear() + "年"
                                + mList.get(mCurrentPosition).getMonth().getMonth() + "月");
                        tv_all_income.setText("总获取:" + mList.get(mCurrentPosition).getMonth().getIncome() + "枚");
                        tv_all_expenses.setText("总消耗:" + mList.get(mCurrentPosition).getMonth().getExpend() + "枚");
                    }
                    count = Integer.parseInt(jsonObject.optJSONObject("data").optString("count"));
                    if (mList.size() == count) {
                        bottom.setVisibility(View.VISIBLE);
                        iv_loading.setVisibility(View.GONE);
                        tv_loading.setText(getResources().getString(R.string.list_no_more));
                    } else {
                        bottom.setVisibility(View.GONE);
                    }
                    //没有记录 显示无数据
                    requestState(mList.size() == 0 ? 1 : 0);
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                bottom.setVisibility(View.GONE);
                if (mList.size() == 0) {
                    if (NetUtils.getNetworkState(GoldDescActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        requestState(3);
                    } else {
                        requestState(2);
                    }
                } else {
                    if (NetUtils.getNetworkState(GoldDescActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
//                        SingleToast.showToast(Collect28Activity.this, "网络未连接,请检测网络设置");
                        Toast.makeText(GoldDescActivity.this, "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
                    } else {
//                        SingleToast.showToast(Collect28Activity.this, "网络连接异常,请检测网络设置");
                        Toast.makeText(GoldDescActivity.this, "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
                    }
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                isLoad = true;
                srl_account_desc.setRefreshing(false);
 
            }
        });
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        mChangeHelper.registerReceiver();
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
    }
 
    private void getAppPageNotification() {
        ShoppingApi.getAppPageNotification(this, TYPE, new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    boolean isClose = jsonObject.optJSONObject("data").optBoolean("canClose");
                    String content = jsonObject.optJSONObject("data").optString("content");
                    final String contentUrl = jsonObject.optJSONObject("data").optString("contentUrl");
                    final String md5 = jsonObject.optJSONObject("data").optString("md5");
 
                    tv_notice_content.setText(content);
                    final SharedPreferences sp = getSharedPreferences("notice", Context.MODE_PRIVATE);
                    if (!sp.getBoolean(md5, false)) {//false 展示,true不展示
                        ll_notice.setVisibility(View.VISIBLE);
                    } else {
                        ll_notice.setVisibility(View.GONE);
                    }
 
                    if (isClose) {
                        iv_close.setImageDrawable(getResources().getDrawable(R.drawable.ic_notice_close));
                        iv_close.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ll_notice.setVisibility(View.GONE);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putBoolean(md5, true);
                                editor.commit();
                            }
                        });
                    } else {
                        iv_close.setImageDrawable(getResources().getDrawable(R.drawable.ic_notice_input));
                    }
                    ll_notice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (StringUtils.isEmpty(contentUrl))
                                return;
                            Intent intent = new Intent(GoldDescActivity.this, ShareBrowserActivity.class);
                            intent.putExtra("url", contentUrl);
                            startActivity(intent);
                        }
                    });
                } else {
 
                }
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.tv_top_bar_right:
                setBackgroundAlpha(0.5f);
                mTopRightPopupWindow.showAsDropDown(tv_right);
                break;
            case R.id.ll_title:
                UserCustomEvent.userMoneyDetailMonthChoice(GoldDescActivity.this);
                long endTimestamp = System.currentTimeMillis();
                int year = Calendar.getInstance().get(Calendar.YEAR);
                int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
                long beginTimestamp = DateFormatUtils.str2Long((year - 4) + "年" + month + "月", false);
                // 通过时间戳初始化日期,毫秒级别
                mDatePicker = new CustomDatePicker(GoldDescActivity.this, new CustomDatePicker.Callback() {
                    @Override
                    public void onTimeSelected(long timestamp) {
                        tv_select_time.setText(DateFormatUtils.long2Str(timestamp, false));
                        String cMonth = tv_select_time.getText().toString();
                        mYear = cMonth.substring(0, cMonth.indexOf("年"));
                        String temp = cMonth.substring(0, cMonth.indexOf("月"));
                        mMonth = temp.substring(temp.indexOf("年") + 1);
                        index = "";
                        mCurrentPosition = 0;
                        getAccountDesc();
                    }
                }, beginTimestamp, endTimestamp, false);
                // 不允许点击屏幕或物理返回键关闭
                mDatePicker.setCancelable(true);
                // 不显示时和分
                mDatePicker.setCanShowPreciseTime(false);
                // 不允许循环滚动
                mDatePicker.setScrollLoop(false);
                // 不允许滚动动画
                mDatePicker.setCanShowAnim(false);
                mDatePicker.show(tv_select_time.getText().toString());
                break;
        }
    }
 
 
    /**
     * 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
     *
     * @param state
     */
    private void requestState(int state) {
        ll_account_layout.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        rv_account_desc.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        ll_no_data.setVisibility(state == 1 ? View.VISIBLE : View.GONE);
        ll_request_failture.setVisibility(state == 2 ? View.VISIBLE : View.GONE);
        ll_no_net.setVisibility(state == 3 ? View.VISIBLE : View.GONE);
    }
}