|
|
@@ -19,6 +19,7 @@ import android.support.v7.app.AlertDialog;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
+import android.widget.AbsListView;
|
|
|
import android.widget.BaseExpandableListAdapter;
|
|
|
import android.widget.Button;
|
|
|
import android.widget.ExpandableListView;
|
|
|
@@ -28,6 +29,7 @@ import android.widget.TextView;
|
|
|
import com.usai.redant.apexdrivers.ApexDriverApplication;
|
|
|
import com.usai.redant.apexdrivers.MainActivity;
|
|
|
import com.usai.redant.apexdrivers.R;
|
|
|
+import com.usai.redant.apexdrivers.SaveInstanceHelper;
|
|
|
import com.usai.redant.apexdrivers.badgeview.BadgeView;
|
|
|
import com.usai.redant.apexdrivers.detail.DetailActivity;
|
|
|
import com.usai.redant.apexdrivers.message.MessageActivity;
|
|
|
@@ -46,27 +48,28 @@ import java.util.ArrayList;
|
|
|
public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderDelegate {
|
|
|
|
|
|
public final static String HomeReloadBroadcastAction = "com.usai.redant.apexdriver.home_refresh";
|
|
|
- private final static String HomeSaveDataKey = "HomeSaveDataKey";
|
|
|
|
|
|
private final static int REQUEST_MORE_CODE = 0;
|
|
|
|
|
|
+ private final static String SavedFirstVisible = "SavedFirstVisible";
|
|
|
+
|
|
|
private Context mCtx;
|
|
|
private HomeFragment self = this;
|
|
|
private ExpandableListView mListView;
|
|
|
private SwipeRefreshLayout mRefresh;
|
|
|
private HomeListAdapter mAdapter;
|
|
|
- private ArrayList<HomeSectionModel> mSectionArray = new ArrayList<>();
|
|
|
private HomeHandler mHandler = new HomeHandler(this);
|
|
|
private HomeBroadCastReceiver mReceiver;
|
|
|
- private JSONObject mJson;
|
|
|
- private HomeOrderModel mSelectedModel;
|
|
|
+
|
|
|
|
|
|
private TextView mEmptyView;
|
|
|
private RelativeLayout mEmptyContainer;
|
|
|
-
|
|
|
private HomeHeaderView mHeaderView;
|
|
|
|
|
|
- private int mMessageCount = 0;
|
|
|
+ private HomeHeaderModel mHeaderModel = new HomeHeaderModel();
|
|
|
+ private HomeOrderModel mSelectedModel;
|
|
|
+ private ArrayList<HomeSectionModel> mSectionArray = new ArrayList<>();
|
|
|
+ private int mFirstVisible = 0;
|
|
|
|
|
|
@Nullable
|
|
|
@Override
|
|
|
@@ -90,6 +93,8 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
|
|
+ mCtx = getActivity();
|
|
|
+
|
|
|
mListView = view.findViewById(R.id.home_list_view);
|
|
|
|
|
|
mAdapter = new HomeListAdapter();
|
|
|
@@ -139,6 +144,7 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
// header
|
|
|
mHeaderView = HomeHeaderView.headerView(mCtx);
|
|
|
mHeaderView.setDelegate(this);
|
|
|
+ mHeaderView.init();
|
|
|
mListView.addHeaderView(mHeaderView);
|
|
|
|
|
|
mEmptyContainer = view.findViewById(R.id.home_empty_view);
|
|
|
@@ -152,45 +158,56 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
});
|
|
|
// mListView.setEmptyView(view.findViewById(R.id.home_empty_view));
|
|
|
|
|
|
- }
|
|
|
+ mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
|
|
|
+ @Override
|
|
|
+ public void onScrollStateChanged(AbsListView view, int scrollState) {
|
|
|
|
|
|
- @Override
|
|
|
- public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
- super.onCreate(savedInstanceState);
|
|
|
- mCtx = getActivity();
|
|
|
+ }
|
|
|
|
|
|
- registBroadcastReceiver();
|
|
|
+ @Override
|
|
|
+ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
|
|
+ mFirstVisible = firstVisibleItem;
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- if (savedInstanceState != null) {
|
|
|
+ {
|
|
|
|
|
|
- String jsonStr = savedInstanceState.getString(HomeSaveDataKey);
|
|
|
- if (jsonStr != null) {
|
|
|
- try {
|
|
|
+ registBroadcastReceiver();
|
|
|
|
|
|
- JSONObject json = new JSONObject(jsonStr);
|
|
|
- handleJson(json);
|
|
|
+ if (savedInstanceState != null) {
|
|
|
|
|
|
- } catch (JSONException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+ recovery();
|
|
|
|
|
|
- } else {
|
|
|
- loadData();
|
|
|
- }
|
|
|
- }
|
|
|
+ final int tmpVisible = savedInstanceState.getInt(SavedFirstVisible);
|
|
|
+ mListView.post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ mListView.setSelection(tmpVisible);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- @Override
|
|
|
- public void onResume() {
|
|
|
- super.onResume();
|
|
|
+ } else {
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
+ } // initial
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
|
- if (mJson != null) {
|
|
|
- outState.putString(HomeSaveDataKey,mJson.toString());
|
|
|
+ outState.putInt(SavedFirstVisible,mFirstVisible);
|
|
|
+
|
|
|
+ SaveInstanceHelper.HomeSaveHelper helper = SaveInstanceHelper.getInstance().getHomeSaveHelper();
|
|
|
+ helper.saved = true;
|
|
|
+ helper.mHeaderModel = mHeaderModel;
|
|
|
+ helper.mSelectedModel = mSelectedModel;
|
|
|
+ helper.mSectionArray = mSectionArray;
|
|
|
+
|
|
|
+ if (mSectionArray != null && mSectionArray.size() > 0) {
|
|
|
+ for (HomeSectionModel sectionModel : mSectionArray) {
|
|
|
+ sectionModel.clearDelegate();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -208,8 +225,6 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -220,6 +235,41 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
dismissProgressDialog();
|
|
|
}
|
|
|
|
|
|
+ private void recovery() {
|
|
|
+
|
|
|
+ final SaveInstanceHelper.HomeSaveHelper helper = SaveInstanceHelper.getInstance().getHomeSaveHelper();
|
|
|
+ if (helper.saved) {
|
|
|
+
|
|
|
+ mHeaderModel = helper.mHeaderModel;
|
|
|
+ mSelectedModel = helper.mSelectedModel;
|
|
|
+ mSectionArray = helper.mSectionArray;
|
|
|
+
|
|
|
+ changeData();
|
|
|
+
|
|
|
+ final Intent intent = helper.homeBroadcastIntent;
|
|
|
+ if (intent != null) {
|
|
|
+
|
|
|
+ new Handler().post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ mReceiver.onReceive(ApexDriverApplication.sharedApplication(), intent);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ helper.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateHomeHeader() {
|
|
|
+
|
|
|
+ mHeaderView.setAvailable(mHeaderModel.driver_available);
|
|
|
+ mHeaderView.setNewCount(mHeaderModel.new_count);
|
|
|
+ mHeaderView.setProcessingCount(mHeaderModel.processing_count);
|
|
|
+ mHeaderView.setFinishCount(mHeaderModel.finish_count);
|
|
|
+ mHeaderView.setExistNewMessage(mHeaderModel.message_count > 0);
|
|
|
+ }
|
|
|
+
|
|
|
private void registBroadcastReceiver() {
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
|
intentFilter.addAction(HomeReloadBroadcastAction);
|
|
|
@@ -284,6 +334,20 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
.show();
|
|
|
}
|
|
|
|
|
|
+ private void changeData() {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手动调用打开/关闭 Group,否则默认关闭
|
|
|
+ * */
|
|
|
+ for (int i = 0; i < mSectionArray.size(); i++) {
|
|
|
+ HomeSectionModel sectionModel = mSectionArray.get(i);
|
|
|
+ mListView.expandGroup(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
+ updateHomeHeader();
|
|
|
+ }
|
|
|
+
|
|
|
private void handleJson(JSONObject json) {
|
|
|
|
|
|
if (json != null) {
|
|
|
@@ -291,7 +355,6 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
try {
|
|
|
JSONArray sectionArr = json.optJSONArray("sections");
|
|
|
if (sectionArr != null) {
|
|
|
- mJson = json;
|
|
|
mSectionArray.clear();
|
|
|
|
|
|
for (int i = 0; i < sectionArr.length(); i++) {
|
|
|
@@ -308,6 +371,8 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
|
|
|
mSectionArray.add(model);
|
|
|
}
|
|
|
+
|
|
|
+ mHeaderModel.setValuesForKeysWithJSON(json);
|
|
|
}
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -316,7 +381,6 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
|
|
|
Message msg = new Message();
|
|
|
msg.what = HomeHandler.HomeActionReloadData;
|
|
|
- msg.obj = json;
|
|
|
mHandler.sendMessage(msg);
|
|
|
}
|
|
|
|
|
|
@@ -514,36 +578,7 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
switch (msg.what) {
|
|
|
case HomeActionReloadData: {
|
|
|
|
|
|
-// fragment.updateEmptyViewState(false);
|
|
|
-
|
|
|
- /**
|
|
|
- * 手动调用打开/关闭 Group,否则默认关闭
|
|
|
- * */
|
|
|
- for (int i = 0; i < fragment.mSectionArray.size(); i++) {
|
|
|
- HomeSectionModel sectionModel = fragment.mSectionArray.get(i);
|
|
|
- fragment.mListView.expandGroup(i);
|
|
|
- }
|
|
|
-
|
|
|
- fragment.mAdapter.notifyDataSetChanged();
|
|
|
-
|
|
|
- JSONObject json = (JSONObject) msg.obj;
|
|
|
- if (json != null) {
|
|
|
- boolean driver_available = json.optBoolean("driver_available");
|
|
|
-
|
|
|
- int new_count = json.optInt("new_count");
|
|
|
- int processing_count = json.optInt("processing_count");
|
|
|
- int finish_count = json.optInt("finish_count");
|
|
|
- int message_count = json.optInt("message_count");
|
|
|
- fragment.mMessageCount = message_count;
|
|
|
-
|
|
|
- if (fragment.mHeaderView != null) {
|
|
|
- fragment.mHeaderView.setAvailable(driver_available);
|
|
|
- fragment.mHeaderView.setNewCount(new_count);
|
|
|
- fragment.mHeaderView.setProcessingCount(processing_count);
|
|
|
- fragment.mHeaderView.setFinishCount(finish_count);
|
|
|
- fragment.mHeaderView.setExistNewMessage(message_count > 0);
|
|
|
- }
|
|
|
- }
|
|
|
+ fragment.changeData();
|
|
|
}
|
|
|
break;
|
|
|
case HomeActionError: {
|
|
|
@@ -781,14 +816,12 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static class HomeSectionModel {
|
|
|
+ public static class HomeSectionModel {
|
|
|
|
|
|
public interface SectionModelListener {
|
|
|
void refresh();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
int type; // OrderStatus
|
|
|
ArrayList<HomeOrderModel> orders;
|
|
|
String title;
|
|
|
@@ -802,6 +835,15 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
mContext = context;
|
|
|
}
|
|
|
|
|
|
+ public void clearDelegate() {
|
|
|
+ listener = null;
|
|
|
+ if (orders != null && orders.size() > 0) {
|
|
|
+ for (HomeOrderModel model : orders) {
|
|
|
+ model.clearDelegate();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void setBackendFlagCount(int count) {
|
|
|
backendFlagCount = count;
|
|
|
|
|
|
@@ -911,12 +953,12 @@ public class HomeFragment extends Fragment implements HomeHeaderView.HomeHeaderD
|
|
|
fsection.setBackendFlagCount(backendCount);
|
|
|
|
|
|
// 刷新Message
|
|
|
- int message_count = mMessageCount;
|
|
|
+ int message_count = mHeaderModel.message_count;
|
|
|
if (message_count > 0) {
|
|
|
message_count--;
|
|
|
- mMessageCount = message_count;
|
|
|
+ mHeaderModel.message_count = message_count;
|
|
|
}
|
|
|
- mHeaderView.setExistNewMessage(message_count > 0);
|
|
|
+ updateHomeHeader();
|
|
|
|
|
|
}
|
|
|
});
|