admin
2021-05-17 06a80d5c4b3a971cdc1ca3d91717ec3f6e03a443
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
package com.tejia.lijin.app.ui.BrandRebate;
 
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.app.hubert.guide.util.ScreenUtils;
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.okhttp.OkHttpUtils;
import com.wpc.library.util.NetUtils;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.ui.BaseActivity;
import com.tejia.lijin.app.ui.dialog.OrderAppealHistoryDeleteHintDialog;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 店铺足迹
 */
public class BrandFootprintInfoActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_middle, tv_top_bar_left;
    private LinearLayout ll_no_net, ll_request_failture;
    private TextView tv_top_bar_right;
    private RecyclerView brandfootprintinfo_rv;
    private BrandFootprintInfoAdapter adapter;//淘宝商品适配
    private List<History> mlist = new ArrayList<>();
    private LinearLayoutCompat brandfootprintinfo_buttom;
    protected CheckBox brandfootprintinfo_cb;//全选
    private TextView brandfootprintinfo_delete;//删除
    private LinearLayout brandfootprintinfo_go_scan;//无数据
    View bottom;
    LinearLayout ff_loading;
    TextView tv_loading;
    ImageView iv_loading;
    private int page = 1;
    private boolean isLoad = true;
    private int count = 1;
    private boolean cb_touch = false;
 
    private String tag1 = "brand/getHistory";
    private String tag2 = "brand/deleteHistory";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brandfootprintinfo);
        inint();//初始化控件
        OnClickListener();//註冊单击事件
        tv_middle.setText("店铺足迹");
        tv_top_bar_right.setText("编辑");
 
        brandfootprintinfo_rv.setLayoutManager(new LinearLayoutManager(this));
        brandfootprintinfo_rv.setBackgroundColor(getResources().getColor(R.color.white));
 
        adapter = new BrandFootprintInfoAdapter(this, mlist, null, false);
        adapter.isHorizontal = true;
        adapter.isFirst = true;
        brandfootprintinfo_rv.setAdapter(adapter);
        bottom = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom2, null);
        tv_loading = bottom.findViewById(R.id.tv_loading2);
        iv_loading = bottom.findViewById(R.id.iv_loading2);
        bottom.findViewById(R.id.ff_loading2).setBackgroundColor(Color.parseColor("#ffffffff"));
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.anim_list_loading);
        iv_loading.setImageDrawable(animationDrawable);
        animationDrawable.start();
        bottom.setVisibility(View.GONE);
        adapter.addFooterView(bottom);
 
        brandfootprintinfo_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int first = manager.findFirstVisibleItemPosition();
                int last = manager.findLastVisibleItemPosition();
                int total = manager.getItemCount();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (last == total - 1 && mlist.size() < count && isLoad) {
                        page++;
                        isLoad = false;
                        tv_loading.setText(getResources().getString(R.string.loading_more_text));
                        iv_loading.setVisibility(View.VISIBLE);
                        getHistory();
                    }
                }
            }
        });
        brandfootprintinfo_cb.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                cb_touch = true;
                return false;
            }
        });
        //全选
        brandfootprintinfo_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    adapter.setMdisplay(true);
                } else {
                    if (cb_touch)
                        adapter.setMdisplay(false);
                }
                cb_touch = false;
            }
        });
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        getHistory();
    }
 
    private void getHistory() {
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        ShoppingApi.getHistory(this, page + "", UserUtil.getUid(ShoppingApplication.application), "2", new BasicTextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                if (page > 1) {
                    bottom.setVisibility(View.VISIBLE);
                } else {
                    bottom.setVisibility(View.GONE);
                }
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    List<History> list = gson.fromJson(jsonObject.optJSONObject("data").optJSONArray("list").toString(),
                            new TypeToken<List<History>>() {
                            }.getType());
                    count = Integer.parseInt(jsonObject.optJSONObject("data").optString("count"));
                    if (page == 1 && mlist.size() > 0)//防止 删除刷新后数据重复
                        mlist.clear();
                    if (count == 0) {
                        brandfootprintinfo_rv.setVisibility(View.GONE);
                        brandfootprintinfo_buttom.setVisibility(View.GONE);
                        tv_top_bar_right.setVisibility(View.GONE);
                        brandfootprintinfo_go_scan.setVisibility(View.VISIBLE);
                    } else {
                        mlist.addAll(list);
                    }
 
                    if (mlist.size() >= count && mlist.size() > 5) {
                        bottom.setVisibility(View.VISIBLE);
                        iv_loading.setVisibility(View.GONE);
                        tv_loading.setText(getResources().getString(R.string.list_no_more));
                    } else {
                        bottom.setVisibility(View.GONE);
                    }
                    adapter.notifyDataSetChanged();
                    isLoad = true;
                }
            }
 
            @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(BrandFootprintInfoActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        requestState(3);
                    } else {
                        requestState(2);
                    }
                } else {
                    if (NetUtils.getNetworkState(BrandFootprintInfoActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
//                        SingleToast.showToast(MyFootmarkActivity.this, "网络未连接,请检测网络设置");
                        Toast.makeText(BrandFootprintInfoActivity.this, "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
                    } else {
//                        SingleToast.showToast(MyFootmarkActivity.this, "网络连接异常,请检测网络设置");
                        Toast.makeText(BrandFootprintInfoActivity.this, "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
                    }
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                isLoad = true;
            }
        });
    }
 
    private void requestState(int state) {
        tv_top_bar_right.setVisibility(state == 0 ? View.VISIBLE : View.INVISIBLE);
        brandfootprintinfo_go_scan.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);
    }
 
    /**
     * 初始化控件
     */
    private void inint() {
        tv_middle = findViewById(R.id.tv_top_bar_middle);
        tv_top_bar_left = findViewById(R.id.tv_top_bar_left);
        tv_top_bar_right = findViewById(R.id.tv_top_bar_right);
        brandfootprintinfo_rv = findViewById(R.id.brandfootprintinfo_rv);
        brandfootprintinfo_delete = findViewById(R.id.brandfootprintinfo_delete);
        brandfootprintinfo_cb = findViewById(R.id.brandfootprintinfo_cb);
        brandfootprintinfo_buttom = findViewById(R.id.brandfootprintinfo_buttom);
        brandfootprintinfo_go_scan = findViewById(R.id.brandfootprintinfo_go_scan);
        ll_request_failture = findViewById(R.id.ll_request_failture);
        ll_no_net = findViewById(R.id.ll_no_net);
        tv_top_bar_right.setVisibility(View.VISIBLE);
 
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv_top_bar_right.getLayoutParams();
        lp.leftMargin = ScreenUtils.dp2px(this, 15);
        tv_top_bar_right.setLayoutParams(lp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            int result = 0;
            int resourceId = getResources().getIdentifier("status_bar_height",
                    "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
            //设置状态栏文字颜色及图标为深色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    result);
            findViewById(R.id.v_status_bar).setLayoutParams(params);
        } else {
            findViewById(R.id.v_status_bar).setVisibility(View.GONE);
        }
    }
 
    /**
     * 註冊单击事件
     */
    private void OnClickListener() {
        tv_top_bar_left.setOnClickListener(this);
        tv_top_bar_right.setOnClickListener(this);
        brandfootprintinfo_delete.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //j返回
            case R.id.tv_top_bar_left:
                finish();
                break;
            //编辑or完成
            case R.id.tv_top_bar_right:
                if (tv_top_bar_right.getText().toString().equals("完成")) {//完成 状态
                    tv_top_bar_right.setText("编辑");
                    adapter.setSelectedMode(false);//设置 全选取消
                    adapter.setMdisplay(false);
                    brandfootprintinfo_buttom.setVisibility(View.GONE);
                } else {//编辑状态
                    tv_top_bar_right.setText("完成");
                    adapter.setSelectedMode(true);//设置全选
                    brandfootprintinfo_buttom.setVisibility(View.VISIBLE);
                    if (brandfootprintinfo_cb.isChecked()) {
                        brandfootprintinfo_cb.setChecked(false);
                    }
                    adapter.setMdisplay(false);
                }
 
                break;
 
            case R.id.brandfootprintinfo_delete://删除
                Map<String, String> map = adapter.getMaps();
                if (map.size() > 0) {
                    String sid = "";
                    for (String key : map.keySet()) {
                        String value = map.get(key);
                        System.out.println(key + "  " + value);
                        if (sid.equals("")) {
                            sid = value;
                        } else {
                            sid += "," + value;
                        }
                    }
 
                    OrderAppealHistoryDeleteHintDialog.Builder builder = new OrderAppealHistoryDeleteHintDialog.Builder(this);
                    final String finalSid = sid;
                    builder.setMessage("确定要删除足迹吗?").setNegativeButton("不了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            deleteHistory(finalSid);
                            if (brandfootprintinfo_cb.isChecked()) {
                                brandfootprintinfo_cb.setChecked(false);
                            } else {
                                adapter.setMdisplay(false);
                            }
                        }
                    }).create().show();
 
                } else {
                    Toast.makeText(this, "请选择删除项", Toast.LENGTH_LONG).show();
                }
                break;
 
            default:
                break;
        }
    }
 
    /**
     * .删除选中店铺
     *
     * @param sid
     */
    private void deleteHistory(String sid) {
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        ShoppingApi.deleteHistory(this, sid, UserUtil.getUid(ShoppingApplication.application), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    page = 1;
                    getHistory();
                }
 
            }
        });
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        brandfootprintinfo_rv = null;
        adapter = null;//淘宝商品适配
        mlist = null;
        brandfootprintinfo_buttom = null;
        brandfootprintinfo_cb = null;//全选
        brandfootprintinfo_delete = null;//删除
        brandfootprintinfo_go_scan = null;//无数据
        bottom = null;
        ff_loading = null;
        tv_loading = null;
        iv_loading = null;
        page = 0;
        isLoad = false;
        count = 0;
        OkHttpUtils.getInstance().cancelTag(tag1);
        OkHttpUtils.getInstance().cancelTag(tag2);
    }
}