|
|
@@ -1,15 +1,45 @@
|
|
|
package com.usai.redant.apexdrivers.message;
|
|
|
|
|
|
+import android.app.ProgressDialog;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
import android.os.Bundle;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.Gravity;
|
|
|
+import android.view.LayoutInflater;
|
|
|
import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.AbsListView;
|
|
|
+import android.widget.AdapterView;
|
|
|
+import android.widget.BaseAdapter;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.ListView;
|
|
|
+import android.widget.TextView;
|
|
|
|
|
|
+import com.usai.redant.apexdrivers.ApexDriverApplication;
|
|
|
import com.usai.redant.apexdrivers.R;
|
|
|
+import com.usai.redant.apexdrivers.detail.DetailActivity;
|
|
|
+import com.usai.redant.apexdrivers.message.model.MessageModel;
|
|
|
+import com.usai.redant.apexdrivers.network.Network;
|
|
|
+import com.usai.redant.apexdrivers.utils.OperationQueue;
|
|
|
|
|
|
-public class MessageActivity extends AppCompatActivity {
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+import org.w3c.dom.Text;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+import static com.usai.redant.rautils.utils.RAUtil.dp2px;
|
|
|
+import static com.usai.redant.rautils.utils.RAUtil.sp2px;
|
|
|
+
|
|
|
+public class MessageActivity extends AppCompatActivity implements AbsListView.OnScrollListener {
|
|
|
|
|
|
public static void startMessageActivity(Context context) {
|
|
|
if (context == null) {
|
|
|
@@ -21,6 +51,21 @@ public class MessageActivity extends AppCompatActivity {
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
|
|
|
+ private static final String SavedMessagesKey = "SavedMessages";
|
|
|
+
|
|
|
+ private Context mCtx = this;
|
|
|
+ private MessageActivity self = this;
|
|
|
+
|
|
|
+ private SwipeRefreshLayout mRefresh;
|
|
|
+ private ListView mListView;
|
|
|
+ private MessageAdapter mAdapter;
|
|
|
+ private TextView footer;
|
|
|
+ private TextView mEmptyView;
|
|
|
+
|
|
|
+ private ArrayList<MessageModel> messages = new ArrayList<>();
|
|
|
+ private int offset = 0;
|
|
|
+ private static final int limit = 20;
|
|
|
+
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
@@ -31,6 +76,36 @@ public class MessageActivity extends AppCompatActivity {
|
|
|
actionBar.setHomeButtonEnabled(true);
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
}
|
|
|
+
|
|
|
+ mAdapter = new MessageAdapter();
|
|
|
+
|
|
|
+ mRefresh = findViewById(R.id.message_refresh_view);
|
|
|
+ mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
+ @Override
|
|
|
+ public void onRefresh() {
|
|
|
+ reload();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ mListView = findViewById(R.id.message_list_view);
|
|
|
+ configureListView();
|
|
|
+
|
|
|
+ if (savedInstanceState != null) {
|
|
|
+ String messagesStr = savedInstanceState.getString(SavedMessagesKey);
|
|
|
+ if (!TextUtils.isEmpty(messagesStr)) {
|
|
|
+ try {
|
|
|
+ JSONArray array = new JSONArray(messagesStr);
|
|
|
+ handleJsonArray(array);
|
|
|
+
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } //
|
|
|
+
|
|
|
+ if (messages.size() == 0) {
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -43,4 +118,329 @@ public class MessageActivity extends AppCompatActivity {
|
|
|
}
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onSaveInstanceState(Bundle outState) {
|
|
|
+ super.onSaveInstanceState(outState);
|
|
|
+
|
|
|
+ JSONArray array = prepareMessages2Save();
|
|
|
+ if (array != null) {
|
|
|
+ outState.putString(SavedMessagesKey,array.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Scroll Listener
|
|
|
+ * */
|
|
|
+ private int last_index;
|
|
|
+ private int total_index;
|
|
|
+ private boolean isLoading = false;//表示是否正处于加载状态
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onScrollStateChanged(AbsListView view, int scrollState) {
|
|
|
+ if(last_index == total_index && (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE))
|
|
|
+ {
|
|
|
+ // 表示此时需要显示刷新视图界面进行新数据的加载(要等滑动停止)
|
|
|
+ if(!isLoading)
|
|
|
+ {
|
|
|
+ // 设置刷新界面可见
|
|
|
+ footer.setVisibility(View.VISIBLE);
|
|
|
+ loadMore();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
|
|
+ last_index = firstVisibleItem+visibleItemCount;
|
|
|
+ total_index = totalItemCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void configureListView() {
|
|
|
+
|
|
|
+ footer = new TextView(mCtx);
|
|
|
+ footer.setBackgroundColor(Color.WHITE);
|
|
|
+ footer.setGravity(Gravity.CENTER);
|
|
|
+ footer.setText("loading...");
|
|
|
+ footer.setTextSize(sp2px(mCtx,7));
|
|
|
+ footer.setTextColor(Color.BLACK);
|
|
|
+ footer.setVisibility(View.INVISIBLE);
|
|
|
+
|
|
|
+ AbsListView.LayoutParams footerLayoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,dp2px(mCtx,40));
|
|
|
+ footer.setLayoutParams(footerLayoutParams);
|
|
|
+
|
|
|
+ mListView.addFooterView(footer);
|
|
|
+ mListView.setOnScrollListener(this);
|
|
|
+
|
|
|
+ mListView.setAdapter(mAdapter);
|
|
|
+
|
|
|
+ mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
+
|
|
|
+ MessageModel model = self.messages.get(position);
|
|
|
+ if (model.isNew) {
|
|
|
+ model.setIsNew(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ Intent intent = DetailActivity.build(mCtx,model.orderID,model.orderType,model.orderType2,model.statusNo);
|
|
|
+ startActivity(intent);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ mEmptyView = findViewById(R.id.message_empty_btn);
|
|
|
+ mEmptyView.setText("There is no data\nPlease click to reload");
|
|
|
+ mEmptyView.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mListView.setEmptyView(findViewById(R.id.message_empty_view));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Data
|
|
|
+ * */
|
|
|
+
|
|
|
+ private ProgressDialog mProgressDialog;
|
|
|
+ private void showProgressDialog() {
|
|
|
+ if (mProgressDialog == null) {
|
|
|
+ mProgressDialog = new ProgressDialog(this);
|
|
|
+ mProgressDialog.setMessage("loading...");
|
|
|
+ mProgressDialog.setCancelable(false);
|
|
|
+ mProgressDialog.show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void dismissProgressDialog() {
|
|
|
+ if (mProgressDialog != null && mProgressDialog.isShowing()) {
|
|
|
+ mProgressDialog.dismiss();
|
|
|
+ }
|
|
|
+ mProgressDialog = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showWarningMsg(String msg) {
|
|
|
+ if (msg == null || msg.length() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ new AlertDialog.Builder(mCtx).setTitle("Warning")
|
|
|
+ .setMessage(msg)
|
|
|
+ .setPositiveButton("Ok",null)
|
|
|
+ .show();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final int LoadActionTypeInitial = 0;
|
|
|
+ private static final int LoadActionTypeReload = 1;
|
|
|
+ private static final int LoadActionTypeMore = 2;
|
|
|
+
|
|
|
+
|
|
|
+ private void loadData() {
|
|
|
+ offset = 0;
|
|
|
+ loadData(LoadActionTypeInitial);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reload() {
|
|
|
+ offset = 0;
|
|
|
+ loadData(LoadActionTypeReload);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadMore() {
|
|
|
+ offset = messages.size();
|
|
|
+ loadData(LoadActionTypeMore);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadData(final int type) {
|
|
|
+
|
|
|
+ if (isLoading) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ isLoading = true;
|
|
|
+
|
|
|
+ showProgressDialog();
|
|
|
+
|
|
|
+ ApexDriverApplication.sharedApplication().getNetworkQueue().addOperationTask(new OperationQueue.OperationBackgroundCallBack() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object operationDoInBackground() {
|
|
|
+
|
|
|
+ return Network.requestMessage(offset, limit);
|
|
|
+ }
|
|
|
+ }, new OperationQueue.OperationCompletionCallBack() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void operationCompletion(Object object) {
|
|
|
+
|
|
|
+ dismissProgressDialog();
|
|
|
+
|
|
|
+ if (mRefresh.isRefreshing()) {
|
|
|
+ mRefresh.setRefreshing(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject json = (JSONObject) object;
|
|
|
+ if (json != null) {
|
|
|
+
|
|
|
+ int result = json.optInt("result");
|
|
|
+ if (result == com.usai.redant.rautils.utils.Network.RESULT_TRUE) {
|
|
|
+
|
|
|
+ if (type != LoadActionTypeMore) {
|
|
|
+ self.messages.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONArray messages = json.optJSONArray("messages");
|
|
|
+ handleJsonArray(messages);
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ String msg = json.optString("err_msg");
|
|
|
+ if (TextUtils.isEmpty(msg)) {
|
|
|
+ msg = "Sorry,something is wrong";
|
|
|
+ }
|
|
|
+ showWarningMsg(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ showWarningMsg("Sorry,something is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ isLoading = false;
|
|
|
+ }
|
|
|
+ }, new OperationQueue.OperationCancelCallBack() {
|
|
|
+ @Override
|
|
|
+ public void operationCancelled() {
|
|
|
+
|
|
|
+ dismissProgressDialog();
|
|
|
+
|
|
|
+ isLoading = false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONArray prepareMessages2Save() {
|
|
|
+ if (messages.size() > 0) {
|
|
|
+
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
+ for (int i = 0; i < messages.size(); i++) {
|
|
|
+
|
|
|
+ MessageModel model = messages.get(i);
|
|
|
+ JSONObject json = model.toJson();
|
|
|
+ if (json != null) {
|
|
|
+ array.put(json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleJsonArray(JSONArray messages) {
|
|
|
+ if (messages == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (messages != null) {
|
|
|
+
|
|
|
+ for (int i = 0; i < messages.length(); i++) {
|
|
|
+ JSONObject msgItem = messages.optJSONObject(i);
|
|
|
+ if (msgItem != null) {
|
|
|
+
|
|
|
+ MessageModel model = new MessageModel();
|
|
|
+ model.setValuesForKeysWithJSON(msgItem);
|
|
|
+
|
|
|
+ self.messages.add(model);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ private class MessageAdapter extends BaseAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getCount() {
|
|
|
+ return self.messages.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getItem(int position) {
|
|
|
+ return self.messages.get(position);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getItemId(int position) {
|
|
|
+ return position;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
+
|
|
|
+
|
|
|
+ Holder holder;
|
|
|
+ if (convertView == null) {
|
|
|
+
|
|
|
+ convertView = LayoutInflater.from(mCtx).inflate(R.layout.message_item_cell,null);
|
|
|
+ holder = new Holder(convertView);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ holder = (Holder)convertView.getTag();
|
|
|
+ }
|
|
|
+
|
|
|
+ MessageModel model = self.messages.get(position);
|
|
|
+ holder.setModel(model);
|
|
|
+
|
|
|
+ return convertView;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private class Holder implements MessageModel.MessageModelDelegate{
|
|
|
+
|
|
|
+ TextView titleTv,msgTv,dateTv;
|
|
|
+ ImageView newMark;
|
|
|
+
|
|
|
+ Holder(View v) {
|
|
|
+ v.setTag(this);
|
|
|
+
|
|
|
+ titleTv = v.findViewById(R.id.message_title_tv);
|
|
|
+ msgTv = v.findViewById(R.id.message_msg_tv);
|
|
|
+ dateTv = v.findViewById(R.id.message_date_tv);
|
|
|
+ newMark = v.findViewById(R.id.message_new_mark);
|
|
|
+ }
|
|
|
+
|
|
|
+ public MessageModel model;
|
|
|
+ public void setModel(MessageModel model) {
|
|
|
+ if (this.model != null) {
|
|
|
+ this.model.delegate = null;
|
|
|
+ }
|
|
|
+ this.model = model;
|
|
|
+ if (this.model != null) {
|
|
|
+ this.model.delegate = this;
|
|
|
+ }
|
|
|
+
|
|
|
+ refreshUI();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshUI() {
|
|
|
+
|
|
|
+ if (model != null) {
|
|
|
+
|
|
|
+ titleTv.setText(model.title);
|
|
|
+ msgTv.setText(model.message);
|
|
|
+ dateTv.setText(model.date);
|
|
|
+ newMark.setVisibility(model.isNew ? View.VISIBLE : View.INVISIBLE);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ titleTv.setText(null);
|
|
|
+ msgTv.setText(null);
|
|
|
+ dateTv.setText(null);
|
|
|
+ newMark.setVisibility(View.INVISIBLE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|