|
|
@@ -0,0 +1,337 @@
|
|
|
+package com.usai.redant.apexdrivers.Filter;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.support.v7.app.ActionBar;
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.ExpandableListView;
|
|
|
+
|
|
|
+import com.usai.redant.apexdrivers.R;
|
|
|
+
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+public class OrderFilterActivity extends AppCompatActivity {
|
|
|
+
|
|
|
+ private static final String OrderFilterKey = "OrderFilterKey";
|
|
|
+ private static final String OrderFilterSavedKey = "OrderFilterSavedKey";
|
|
|
+
|
|
|
+ public static Intent filterActivityIntent(Context ctx, JSONObject filter) {
|
|
|
+
|
|
|
+ if (ctx == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (filter.length() == 0) {
|
|
|
+ new AlertDialog.Builder(ctx)
|
|
|
+ .setTitle("Warning")
|
|
|
+ .setMessage("There is no filter data")
|
|
|
+ .setPositiveButton("Ok",null)
|
|
|
+ .show();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Intent intent = new Intent(ctx,OrderFilterActivity.class);
|
|
|
+ intent.putExtra(OrderFilterKey,filter.toString());
|
|
|
+
|
|
|
+ return intent;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Context mCtx = this;
|
|
|
+ private ExpandableListView mFilterListView;
|
|
|
+ private JSONObject mFilter;
|
|
|
+ private OrderFilterAdapter mAdapter;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_order_filter);
|
|
|
+
|
|
|
+ ActionBar actionBar = getSupportActionBar();
|
|
|
+ if (actionBar != null) {
|
|
|
+ actionBar.setHomeButtonEnabled(true);
|
|
|
+ actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ mFilterListView = findViewById(R.id.order_filter_listView);
|
|
|
+ mFilterListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
|
|
|
+ @Override
|
|
|
+ public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ mFilter = null;
|
|
|
+ if (getIntent() != null) {
|
|
|
+ String filterStr = getIntent().getStringExtra(OrderFilterKey);
|
|
|
+ if (filterStr != null && filterStr.length() > 0) {
|
|
|
+ try {
|
|
|
+ mFilter = new JSONObject(filterStr);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ mFilter = null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ mFilter = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (savedInstanceState != null) {
|
|
|
+ String filterStr = savedInstanceState.getString(OrderFilterSavedKey);
|
|
|
+ if (filterStr != null && filterStr.length() > 0) {
|
|
|
+ try {
|
|
|
+ mFilter = new JSONObject(filterStr);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ mFilter = null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ mFilter = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ prepareData();
|
|
|
+
|
|
|
+ mFilterListView.setAdapter(mAdapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onSaveInstanceState(Bundle outState) {
|
|
|
+ super.onSaveInstanceState(outState);
|
|
|
+
|
|
|
+ if (mFilter != null) {
|
|
|
+ outState.putString(OrderFilterSavedKey,mFilter.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
+ switch (item.getItemId()) {
|
|
|
+ case android.R.id.home: {
|
|
|
+ finish();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return super.onOptionsItemSelected(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Data
|
|
|
+ * */
|
|
|
+ private void prepareData() {
|
|
|
+
|
|
|
+ ArrayList<SectionModel> sectionModels = new ArrayList<>();
|
|
|
+
|
|
|
+ if (mFilter != null) {
|
|
|
+ JSONArray sections = mFilter.optJSONArray("sections");
|
|
|
+ for (int i = 0; i < sections.length(); i++) {
|
|
|
+
|
|
|
+ JSONObject section = sections.optJSONObject(i);
|
|
|
+ if (section != null) {
|
|
|
+
|
|
|
+ SectionModel model = new SectionModel();
|
|
|
+ model.setValuesWithJsonObject(section);
|
|
|
+
|
|
|
+ sectionModels.add(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mAdapter = new OrderFilterAdapter(mCtx,sectionModels);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Model
|
|
|
+ * */
|
|
|
+
|
|
|
+ public static class SectionModel {
|
|
|
+ public String title;
|
|
|
+ public String key;
|
|
|
+ public ArrayList<BaseModel> values = new ArrayList<>();
|
|
|
+
|
|
|
+ public void setValuesWithJsonObject(JSONObject jsonObject) {
|
|
|
+ if (jsonObject != null) {
|
|
|
+
|
|
|
+ title = jsonObject.optString("title");
|
|
|
+ key = jsonObject.optString("key");
|
|
|
+ JSONArray values = jsonObject.optJSONArray("values");
|
|
|
+ setValues(values);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setValues(JSONArray values) {
|
|
|
+
|
|
|
+ this.values.clear();
|
|
|
+
|
|
|
+ if (values == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < values.length(); i++) {
|
|
|
+ JSONObject value = values.optJSONObject(i);
|
|
|
+ if (value != null) {
|
|
|
+ int type = value.optInt("type");
|
|
|
+ switch (type) {
|
|
|
+ case ModelTypeStatus: {
|
|
|
+ StatusModel model = new StatusModel();
|
|
|
+ model.setValuesWithJsonObject(value);
|
|
|
+
|
|
|
+ this.values.add(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case ModelTypeDate: {
|
|
|
+ DateModel model = new DateModel();
|
|
|
+ model.setValuesWithJsonObject(value);
|
|
|
+
|
|
|
+ this.values.add(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case ModelTypeInput: {
|
|
|
+ InputModel model = new InputModel();
|
|
|
+ model.setValuesWithJsonObject(value);
|
|
|
+
|
|
|
+ this.values.add(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public BaseModel modelForRow(int row) {
|
|
|
+ return values.get(row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final int ModelTypeStatus = 0;
|
|
|
+ public static final int ModelTypeInput = 1;
|
|
|
+ public static final int ModelTypeDate = 2;
|
|
|
+
|
|
|
+ public interface ModelDelegate {
|
|
|
+ void refreshUI();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class BaseModel {
|
|
|
+
|
|
|
+ public int type;
|
|
|
+ public ModelDelegate delegate;
|
|
|
+
|
|
|
+ public void setValuesWithJsonObject(JSONObject object) {
|
|
|
+ if (object != null) {
|
|
|
+ type = object.optInt("type");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class DateModel extends BaseModel {
|
|
|
+
|
|
|
+ public String from,to;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setValuesWithJsonObject(JSONObject object) {
|
|
|
+ super.setValuesWithJsonObject(object);
|
|
|
+
|
|
|
+ if (object != null) {
|
|
|
+ setFrom(object.optString("from"));
|
|
|
+ setTo(object.optString("to"));
|
|
|
+ } else {
|
|
|
+ setFrom(null);
|
|
|
+ setTo(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFrom(String from) {
|
|
|
+ this.from = from;
|
|
|
+
|
|
|
+ if (delegate != null) {
|
|
|
+ delegate.refreshUI();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTo(String to) {
|
|
|
+ this.to = to;
|
|
|
+
|
|
|
+ if (delegate != null) {
|
|
|
+ delegate.refreshUI();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class InputModel extends BaseModel {
|
|
|
+
|
|
|
+ public String value;
|
|
|
+ @Override
|
|
|
+ public void setValuesWithJsonObject(JSONObject object) {
|
|
|
+ super.setValuesWithJsonObject(object);
|
|
|
+
|
|
|
+ if (object != null) {
|
|
|
+ setValue(object.optString("value"));
|
|
|
+ } else {
|
|
|
+ setValue(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setValue(String value) {
|
|
|
+ this.value = value;
|
|
|
+
|
|
|
+ if (delegate != null) {
|
|
|
+ delegate.refreshUI();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateValue(String value) {
|
|
|
+ this.value = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class StatusModel extends BaseModel {
|
|
|
+
|
|
|
+ public String value;
|
|
|
+ public boolean selected;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setValuesWithJsonObject(JSONObject object) {
|
|
|
+ super.setValuesWithJsonObject(object);
|
|
|
+
|
|
|
+ if (object != null) {
|
|
|
+ setValue(object.optString("value"));
|
|
|
+ } else {
|
|
|
+ setValue(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setValue(String value) {
|
|
|
+ this.value = value;
|
|
|
+
|
|
|
+ if (delegate != null) {
|
|
|
+ delegate.refreshUI();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSelected(boolean selected) {
|
|
|
+ this.selected = selected;
|
|
|
+
|
|
|
+ if (delegate != null) {
|
|
|
+ delegate.refreshUI();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Adapter
|
|
|
+ * */
|
|
|
+
|
|
|
+}
|