package com.tejia.lijin.app.ui.message;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
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.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.FrameLayout;
|
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.wpc.library.content.ConnectivityChangeHelper;
|
import com.wpc.library.okhttp.OkHttpUtils;
|
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
|
import com.wpc.library.util.NetUtils;
|
import com.wpc.library.util.common.DimenUtils;
|
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.Message;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2018/1/18.
|
*/
|
|
public class AppMailActivity extends BaseActivity implements View.OnClickListener {
|
TextView tv_left, tv_middle, tv_no_data_hint;
|
private ImageView iv_slid_top, iv_no_data;
|
SwipeRefreshLayout rl_app_mail;
|
RecyclerView rv_app_mail;
|
AppMailAdapter adapter;
|
int page = 1;
|
int count = 1;
|
List<Message> mList = new ArrayList<>();
|
View bottom;
|
private boolean upLoad = false;
|
private String TYPE = "msgCenter";
|
private TextView tv_notice_content;
|
private LinearLayout ll_notice;
|
private ImageView iv_close;
|
private ConnectivityChangeHelper mChangeHelper;
|
private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
|
|
String tag1="help/getAppPageNotification";
|
String tag2="customer/findAccountMessageList";
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_app_mail);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
tv_left = findViewById(R.id.tv_top_bar_left);
|
tv_middle = findViewById(R.id.tv_top_bar_middle);
|
rl_app_mail = findViewById(R.id.srl_app_mail);
|
rv_app_mail = findViewById(R.id.rv_app_mail);
|
iv_slid_top = findViewById(R.id.iv_slid_top);
|
|
float hRatio = 260 / 720f;
|
float wRatio = 23 / 720f;
|
int bottomPo = (int) (DimenUtils.getScreenWidth(this) * hRatio);
|
int rightPo = (int) (DimenUtils.getScreenWidth(this) * wRatio);
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(DimenUtils.dip2px(this, 40), DimenUtils.dip2px(this, 40));
|
params.setMargins(0, 0, rightPo, bottomPo);
|
params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
|
iv_slid_top.setLayoutParams(params);
|
|
bottom = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom, null);
|
adapter = new AppMailAdapter(AppMailActivity.this, mList);
|
|
adapter.addFooterView(bottom);
|
bottom.setVisibility(View.GONE);
|
rv_app_mail.setLayoutManager(new LinearLayoutManager(AppMailActivity.this));
|
DividerItemDecoration vDivider = new DividerItemDecoration();
|
vDivider.setColor(getResources().getColor(R.color.register_gray));
|
vDivider.setSize(DimenUtils.dip2px(AppMailActivity.this, 2));
|
rv_app_mail.addItemDecoration(vDivider);
|
rv_app_mail.setAdapter(adapter);
|
|
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_no_data_hint.setText("还没有消息~");
|
iv_no_data.setImageDrawable(getResources().getDrawable(R.drawable.ic_no_friends));
|
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) {
|
getMailList();
|
}
|
}
|
|
});
|
rl_app_mail.setColorSchemeColors(getResources().getColor(R.color.theme));
|
rl_app_mail.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
getMailList();
|
}
|
});
|
rv_app_mail.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 (first == 0) {
|
iv_slid_top.setVisibility(View.GONE);
|
}
|
if (last == total - 1 && mList.size() < count) {
|
page++;
|
getMailList();
|
} else if (mList.size() >= count && last >= mList.size() - 1 && upLoad) {
|
// SingleToast.showToast(AppMailActivity.this, "没有更多了");
|
Toast.makeText(AppMailActivity.this, "没有更多了", Toast.LENGTH_SHORT).show();
|
}
|
}
|
// else {
|
// iv_slid_top.setVisibility(View.GONE);
|
// }
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
if (Math.abs(dy) > 10) {
|
if (dy > 0) {
|
upLoad = true;
|
iv_slid_top.setVisibility(View.GONE);
|
} else {
|
upLoad = false;
|
iv_slid_top.setVisibility(View.VISIBLE);
|
}
|
}
|
}
|
});
|
tv_left.setOnClickListener(this);
|
tv_middle.setText("消息中心");
|
tv_notice_content = findViewById(R.id.tv_notice_content);
|
ll_notice = findViewById(R.id.ll_notice);
|
iv_close = findViewById(R.id.iv_close);
|
getAppPageNotification();
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
mChangeHelper.registerReceiver();
|
}
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
}
|
|
@Override
|
public void onClick(View view) {
|
switch (view.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));
|
// requestState(0);
|
// getMailList();
|
break;
|
}
|
}
|
|
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(AppMailActivity.this, ShareBrowserActivity.class);
|
intent.putExtra("url", contentUrl);
|
startActivity(intent);
|
}
|
});
|
}
|
}
|
});
|
}
|
|
private void getMailList() {
|
String uid = getSharedPreferences("user", MODE_PRIVATE).getString("uid", "");
|
ShoppingApi.getMailList(AppMailActivity.this, uid, page + "", 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<Message> list = gson.fromJson(jsonObject.optJSONObject("data").optJSONArray("list").toString(), new TypeToken<List<Message>>() {
|
}.getType());
|
if (page == 1 && mList.size() > 0)
|
mList.clear();
|
mList.addAll(list);
|
adapter.notifyDataSetChanged();
|
// adapter.notifyItemRangeChanged(mList.size(), list.size());//倒序批量刷新列表
|
count = Integer.parseInt(jsonObject.optJSONObject("data").optString("count"));
|
requestState(mList.size() == 0 ? 1 : 0);
|
} else {
|
if (mList.size() == 0)
|
requestState(2);
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
rl_app_mail.setRefreshing(false);
|
bottom.setVisibility(View.GONE);
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
if (mList.size() == 0) {
|
if (NetUtils.getNetworkState(AppMailActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
} else {
|
requestState(2);
|
}
|
} else {
|
if (NetUtils.getNetworkState(AppMailActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
// SingleToast.showToast(AppMailActivity.this, "网络未连接,请检测网络设置");
|
Toast.makeText(AppMailActivity.this, "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
|
} else {
|
// SingleToast.showToast(AppMailActivity.this, "网络连接异常,请检测网络设置");
|
Toast.makeText(AppMailActivity.this, "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
|
}
|
}
|
}
|
});
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
rl_app_mail.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
rv_app_mail.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(tag1);
|
OkHttpUtils.getInstance().cancelTag(tag2);
|
}
|
}
|