admin
2021-05-14 1b4b3fed2569ac21ad6feb0a3a2c051175e7c36e
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
package com.tejia.lijin.app.ui.message;
 
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.provider.Settings;
 
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.ImageView;
import android.widget.LinearLayout;
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.downutil.StringUtils;
import com.wpc.library.content.ConnectivityChangeHelper;
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.entity.UserMessage;
import com.tejia.lijin.app.ui.BaseActivity;
import com.tejia.lijin.app.util.TopStatusSettings;
import com.tejia.lijin.app.util.umengCustomEvent.MessageCustomEvent;
import com.tejia.lijin.app.util.user.LoginAndInviteStatusUtil;
import com.tejia.lijin.app.util.user.UserUtil;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
public class UserMessageActivity extends BaseActivity implements View.OnClickListener {
    TextView tv_middle, tv_top_bar_left, tv_no_data_hint;
    ImageView iv_no_data;
    SwipeRefreshLayout srl_user_message;
    RecyclerView rv_user_message;
    UserMessageAdapter adapter;
    private View bottom;
    private ImageView iv_loading;
    private TextView tv_loading;
 
    private boolean isLoad = false;
    int mPage = 1;
    int count = 1;
    List<UserMessage> mList;
 
    private ConnectivityChangeHelper mChangeHelper;
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
 
    String tag = "msg/getMsgList";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_message);
        TopStatusSettings.setStatusViewAndDeepColor(this);
        init();
        addListener();
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        mChangeHelper.registerReceiver();
        getMsgList();
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
    }
 
    private void init() {
        tv_top_bar_left = findViewById(R.id.tv_top_bar_left);
        tv_middle = findViewById(R.id.tv_top_bar_middle);
        srl_user_message = findViewById(R.id.srl_user_message);
        rv_user_message = findViewById(R.id.rv_user_message);
        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);
        findViewById(R.id.tv_net_setting).setOnClickListener(this);
        findViewById(R.id.tv_refresh).setOnClickListener(this);
 
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv_user_message.setLayoutManager(manager);
        srl_user_message.setColorSchemeColors(getResources().getColor(R.color.theme));
 
        String title = getIntent().getStringExtra("title");
        title = StringUtils.isNullOrEmpty(title) ? "消息" : title;
        tv_middle.setText(title);
 
        if (title.equals("邀请消息")) {
            tv_no_data_hint.setText("主人,你的粉丝已经等你很久了~");
            iv_no_data.setImageDrawable(getResources().getDrawable(R.drawable.ic_msg_invite_null));
        } else if (title.equals("资金消息")) {
            tv_no_data_hint.setText("主人,你难道不想支付宝鼓鼓的吗?");
            iv_no_data.setImageDrawable(getResources().getDrawable(R.drawable.ic_msg_capital_null));
        } else if (title.equals("订单消息")) {
            tv_no_data_hint.setText("主人,来一笔订单吧,求你了~");
            iv_no_data.setImageDrawable(getResources().getDrawable(R.drawable.ic_msg_order_null));
        } else if (title.equals("其它消息") || title.equals("账号消息")) {
            tv_no_data_hint.setText("主人,我这里空空如也~");
            iv_no_data.setImageDrawable(getResources().getDrawable(R.drawable.ic_msg_other_null));
        } else {
            tv_no_data_hint.setText("暂无消息");
        }
 
        MessageCustomEvent.msgItemClick(UserMessageActivity.this, title);
        mList = new ArrayList<>();
        adapter = new UserMessageAdapter(this, mList, new IDeleteMessageListener() {
            @Override
            public void delete(final UserMessage msg) {
                if (LoginAndInviteStatusUtil.acessNext(getApplicationContext(), UserUtil.getUid(getApplicationContext()), true)) {
                    ShoppingApi.deleteMsg(getApplicationContext(), UserUtil.getUid(getApplicationContext()), msg.getType(), msg.getId(), new BasicTextHttpResponseHandler() {
                        @Override
                        public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                            super.onSuccessPerfect(statusCode, headers, jsonObject);
                            if (jsonObject.optInt("code") == 0) {
                                for (int i = 0; i < mList.size(); i++) {
                                    if (mList.get(i) != null && msg.getId().equalsIgnoreCase(mList.get(i).getId()) && msg.getType().equalsIgnoreCase(mList.get(i).getType())) {
                                        mList.remove(i);
                                        adapter.notifyDataSetChanged();
                                        Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                        break;
                                    }
 
                                }
 
                            } else {
                                Toast.makeText(getApplicationContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                            }
                        }
 
                        @Override
                        public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                            super.onFailure(statusCode, headers, jsonObject, e);
                            Toast.makeText(getApplicationContext(), "网络连接出错", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
        rv_user_message.setAdapter(adapter);
 
        bottom = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom, null);
        adapter.addFooterView(bottom);
        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);
    }
 
    private void addListener() {
        tv_top_bar_left.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) {
                            getMsgList();
                        }
                    }
 
                });
        srl_user_message.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                srl_user_message.setRefreshing(true);
                getMsgList();
            }
        });
        rv_user_message.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                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) {
                        mPage++;
                        isLoad = true;
                        iv_loading.setVisibility(View.VISIBLE);
                        tv_loading.setText("正在加载更多是数据");
                        getMsgList();
                    }
                }
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
 
    /**
     *
     */
    private void getMsgList() {
        String type = getIntent().getStringExtra("type");
        ShoppingApi.getMsgList(this, UserUtil.getUid(ShoppingApplication.application), mPage + "", type, new BasicTextHttpResponseHandler() {
 
            @Override
            public void onStart() {
                super.onStart();
                if (mPage > 0) {
                    bottom.setVisibility(View.VISIBLE);
                } else {
                    bottom.setVisibility(View.GONE);
                }
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                bottom.setVisibility(View.GONE);
                if (jsonObject.optInt("code") == 0) {
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    List<UserMessage> list = gson.fromJson(jsonObject.optJSONObject("data").
                                    optJSONArray("data").toString(),
                            new TypeToken<List<UserMessage>>() {
                            }.getType());
                    if (mPage == 1)
                        mList.clear();
                    mList.addAll(list);
                    adapter.notifyDataSetChanged();
//                    adapter.notifyItemRangeChanged(mList.size() - 1, list.size());//倒序批量刷新列表
                    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));
                    }
                    requestState(mList.size() == 0 ? 1 : 0);
                } else {
                    if (mList.size() == 0)
                        requestState(1);
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                srl_user_message.setRefreshing(false);
                isLoad = false;
            }
 
            @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(UserMessageActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        requestState(3);
                    } else {
                        requestState(2);
                    }
                } else {
                    if (NetUtils.getNetworkState(UserMessageActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
//                        SingleToast.showToast(UserMessageActivity.this, "网络未连接,请检测网络设置");
                        Toast.makeText(UserMessageActivity.this, "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(UserMessageActivity.this, "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
//                        SingleToast.showToast(UserMessageActivity.this, "网络连接异常,请检测网络设置");
                    }
                }
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.tv_net_setting:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case R.id.tv_refresh:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
        }
    }
 
    /**
     * 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
     *
     * @param state
     */
    private void requestState(int state) {
        srl_user_message.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        rv_user_message.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);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(tag);
    }
}