From 03b45d10719bb118f1bb884845a5f707dbda6ce5 Mon Sep 17 00:00:00 2001 From: vanous Date: Tue, 18 Aug 2020 11:16:36 +0200 Subject: [PATCH] Add better Sport Activities Filering --- app/src/main/AndroidManifest.xml | 5 + .../activities/AbstractListActivity.java | 8 + .../activities/ActivitySummariesActivity.java | 68 ++--- .../activities/ActivitySummariesFilter.java | 235 ++++++++++++++++++ .../activities/ActivitySummaryDetail.java | 8 +- .../adapter/AbstractItemAdapter.java | 8 + .../adapter/ActivitySummariesAdapter.java | 34 ++- .../model/ActivitySummaryItems.java | 10 +- app/src/main/res/drawable/ic_filter.xml | 10 + app/src/main/res/layout/activity_list.xml | 11 +- .../res/layout/activity_summary_details.xml | 1 + .../main/res/layout/sport_activity_filter.xml | 117 +++++++++ app/src/main/res/menu/activity_list_menu.xml | 7 + app/src/main/res/values/strings.xml | 6 + 14 files changed, 479 insertions(+), 49 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesFilter.java create mode 100644 app/src/main/res/drawable/ic_filter.xml create mode 100644 app/src/main/res/layout/sport_activity_filter.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 40d40d411..bfd36b8ff 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -425,6 +425,11 @@ android:parentActivityName=".activities.ActivitySummariesActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden" /> + extends AbstractGBActivity { this.itemAdapter.setActivityKindFilter(activityKind); } + public void setDateFromFilter(long date){ + this.itemAdapter.setDateFromFilter(date); + } + + public void setDateToFilter(long date){ + this.itemAdapter.setDateToFilter(date); + } + public AbstractItemAdapter getItemAdapter() { return itemAdapter; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesActivity.java index caceb102a..dc7d0ef67 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesActivity.java @@ -17,6 +17,7 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.activities; +import android.app.Activity; import android.app.DatePickerDialog; import android.content.BroadcastReceiver; import android.content.Context; @@ -45,22 +46,22 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Objects; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.adapter.ActivitySummariesAdapter; import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummary; import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes; -import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,8 +72,10 @@ public class ActivitySummariesActivity extends AbstractListActivity activityKindMap = new LinkedHashMap<>(1); + HashMap activityKindMap = new HashMap<>(1); int activityFilter=0; + long dateFromFilter=0; + long dateToFilter=0; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override @@ -113,10 +116,33 @@ public class ActivitySummariesActivity extends AbstractListActivity parent, View view, int pos,long id) { - activityFilter=activityKindMap.get(parent.getItemAtPosition(pos)); - setActivityKindFilter(activityFilter); - refresh(); - } - - @Override - public void onNothingSelected(AdapterView arg0) { - // TODO Auto-generated method stub - } - - } - - public void addItemsOnSpinner() { - Spinner spinner = (Spinner) findViewById(R.id.select_kind); - ArrayList spinnerArray = new ArrayList<>(activityKindMap.keySet()); - ArrayAdapter dataAdapter = new ArrayAdapter(this, - android.R.layout.simple_spinner_dropdown_item, spinnerArray); - spinner.setAdapter(dataAdapter); - } - public void resetFetchTimestampToChosenDate() { final Calendar currentDate = Calendar.getInstance(); new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @@ -323,6 +320,9 @@ public class ActivitySummariesActivity extends AbstractListActivity. */ +package nodomain.freeyourgadget.gadgetbridge.activities; + +import android.app.Activity; +import android.app.DatePickerDialog; +import android.content.Context; +import android.content.Intent; +import android.content.res.Resources; +import android.graphics.Color; +import android.os.Bundle; +import android.util.TypedValue; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.DatePicker; +import android.widget.LinearLayout; +import android.widget.Spinner; +import android.widget.TextView; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; + + +public class ActivitySummariesFilter extends Activity implements View.OnClickListener { + private static final Logger LOG = LoggerFactory.getLogger(ActivitySummariesActivity.class); + private static final String DATE_FILTER_FROM = "dateFromFilter"; + private static final String DATE_FILTER_TO = "dateToFilter"; + int activityFilter = 0; + long dateFromFilter = 0; + long dateToFilter = 0; + HashMap activityKindMap = new HashMap<>(1); + + public static int getAlternateColor(Context context) { + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = context.getTheme(); + theme.resolveAttribute(R.attr.alternate_row_background, typedValue, true); + return typedValue.data; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Bundle bundle = this.getIntent().getExtras(); + + activityKindMap = (HashMap) bundle.getSerializable("activityKindMap"); + activityFilter = bundle.getInt("activityFilter", 0); + dateFromFilter = bundle.getLong("dateFromFilter", 0); + dateToFilter = bundle.getLong("dateToFilter", 0); + + Context appContext = this.getApplicationContext(); + if (appContext instanceof GBApplication) { + setContentView(R.layout.sport_activity_filter); + } + + //get spinner ready - assign data, set selected item... + final Spinner filterKindSpinner = findViewById(R.id.select_kind); + ArrayList spinnerArray = new ArrayList<>(activityKindMap.keySet()); + ArrayAdapter dataAdapter = new ArrayAdapter(this, + android.R.layout.simple_spinner_dropdown_item, spinnerArray); + filterKindSpinner.setAdapter(dataAdapter); + filterKindSpinner.setSelection(dataAdapter.getPosition(getKeyByValue(activityFilter))); + addListenerOnSpinnerItemSelection(); + + final LinearLayout filterfrom = findViewById(R.id.filterfrom); + final TextView filterfromlabel = findViewById(R.id.textViewFromData); + final LinearLayout filterto = findViewById(R.id.filterto); + final TextView filtertolabel = findViewById(R.id.textViewToData); + + + final Button reset_filter_button = findViewById(R.id.reset_filter_button); + final Button apply_filter_button = findViewById(R.id.apply_filter_button); + apply_filter_button.setBackgroundColor(this.getResources().getColor(R.color.accent)); + + + reset_filter_button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + activityFilter = 0; + dateFromFilter = 0; + dateToFilter = 0; + filterKindSpinner.setSelection(0); + update_filter_fields(); + } + }); + + apply_filter_button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(); + intent.putExtra("activityFilter", activityFilter); + intent.putExtra("dateFromFilter", dateFromFilter); + intent.putExtra("dateToFilter", dateToFilter); + setResult(1, intent); + finish(); + } + }); + + //set current values coming from parent + update_filter_fields(); + + filterfrom.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getDate(DATE_FILTER_FROM, dateFromFilter); + + } + }); + + filterto.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getDate(DATE_FILTER_TO, dateToFilter); + } + }); + + } + + public String getKeyByValue(Integer value) { + for (Map.Entry entry : activityKindMap.entrySet()) { + if (Objects.equals(value, entry.getValue())) { + return entry.getKey(); + } + } + return null; + } + + @Override + public void onClick(View view) { + } + + public void addListenerOnSpinnerItemSelection() { + Spinner spinner = findViewById(R.id.select_kind); + spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener()); + } + + public void update_filter_fields() { + TextView filterDateFromDataView = findViewById(R.id.textViewFromData); + TextView filterDateToDataView = findViewById(R.id.textViewToData); + Button reset_filter_button = findViewById(R.id.reset_filter_button); + + if (dateFromFilter > 0) { + filterDateFromDataView.setText(DateTimeUtils.formatDate(new Date(dateFromFilter))); + } else { + filterDateFromDataView.setText(""); + } + + if (dateToFilter > 0) { + filterDateToDataView.setText(DateTimeUtils.formatDate(new Date(dateToFilter))); + } else { + filterDateToDataView.setText(""); + } + + if (dateToFilter < dateFromFilter && dateToFilter > 0) { + filterDateFromDataView.setBackgroundColor(Color.RED); + filterDateToDataView.setBackgroundColor(Color.RED); + } else { + filterDateFromDataView.setBackgroundColor(Color.WHITE); + filterDateToDataView.setBackgroundColor(Color.WHITE); + } + + if (dateToFilter != 0 || dateFromFilter != 0 || activityFilter != 0) { + reset_filter_button.setBackgroundColor(this.getResources().getColor(R.color.accent)); + + } else { + reset_filter_button.setBackgroundColor(this.getResources().getColor(R.color.secondarytext)); + } + } + + public void getDate(final String filter, long currentDatemillis) { + Calendar currentDate = Calendar.getInstance(); + if (currentDatemillis > 0) { + currentDate = GregorianCalendar.getInstance(); + currentDate.setTimeInMillis(currentDatemillis); + } + + new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { + @Override + public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { + Calendar date = Calendar.getInstance(); + + if (filter == DATE_FILTER_FROM) { + date.set(year, monthOfYear, dayOfMonth, 0, 0); + dateFromFilter = date.getTimeInMillis(); + } else { + date.set(year, monthOfYear, dayOfMonth, 23, 59); + dateToFilter = date.getTimeInMillis(); + } + update_filter_fields(); + } + }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show(); + } + + public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { + + public void onItemSelected(AdapterView parent, View view, int pos, long id) { + activityFilter = activityKindMap.get(parent.getItemAtPosition(pos)); + update_filter_fields(); + } + + @Override + public void onNothingSelected(AdapterView arg0) { + // TODO Auto-generated method stub + } + + } + +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java index 42131f3a8..dce2de53d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java @@ -37,6 +37,8 @@ import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; +import com.google.android.material.bottomsheet.BottomSheetDialog; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -67,6 +69,7 @@ public class ActivitySummaryDetail extends AbstractGBActivity { private boolean show_raw_data = false; BaseActivitySummary currentItem = null; private int alternateColor; + //private Object BottomSheetBehavior; @SuppressLint("ClickableViewAccessibility") @Override @@ -77,7 +80,10 @@ public class ActivitySummaryDetail extends AbstractGBActivity { GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); final int filter = intent.getIntExtra("filter", 0); final int position = intent.getIntExtra("position", 0); - final ActivitySummaryItems items = new ActivitySummaryItems(this, gbDevice, filter); + final long dateFromFilter = intent.getLongExtra("dateFromFilter", 0); + final long dateToFilter = intent.getLongExtra("dateToFilter", 0); + + final ActivitySummaryItems items = new ActivitySummaryItems(this, gbDevice, filter, dateFromFilter, dateToFilter); final RelativeLayout layout = findViewById(R.id.activity_summary_detail_relative_layout); alternateColor = getAlternateColor(this); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AbstractItemAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AbstractItemAdapter.java index 950428c8a..4fc1b6342 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AbstractItemAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/AbstractItemAdapter.java @@ -63,6 +63,14 @@ public abstract class AbstractItemAdapter extends ArrayAdapter { this.setActivityKindFilter(activityKind); } + public void setDateFromFilter(long date){ + this.setDateFromFilter(date); + } + + public void setDateToFilter(long date){ + this.setDateToFilter(date); + } + @Override public View getView(int position, View view, ViewGroup parent) { T item = getItem(position); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java index ff4daa13d..7dedab03d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java @@ -38,11 +38,15 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; public class ActivitySummariesAdapter extends AbstractItemAdapter { private final GBDevice device; private int activityKindFilter; + long dateFromFilter=0; + long dateToFilter=0; - public ActivitySummariesAdapter(Context context, GBDevice device, int activityKindFilter) { + public ActivitySummariesAdapter(Context context, GBDevice device, int activityKindFilter, long dateFromFilter, long dateToFilter) { super(context); this.device = device; this.activityKindFilter = activityKindFilter; + this.dateFromFilter=dateFromFilter; + this.dateToFilter=dateToFilter; loadItems(); } @@ -53,15 +57,22 @@ public class ActivitySummariesAdapter extends AbstractItemAdapter qb = summaryDao.queryBuilder(); - if (activityKindFilter !=0){ + qb.where( + BaseActivitySummaryDao.Properties.DeviceId.eq( + dbDevice.getId())).orderDesc(BaseActivitySummaryDao.Properties.StartTime); + + if (activityKindFilter !=0) { qb.where( - BaseActivitySummaryDao.Properties.DeviceId.eq(dbDevice.getId()), - BaseActivitySummaryDao.Properties.ActivityKind.eq(activityKindFilter)) - .orderDesc(BaseActivitySummaryDao.Properties.StartTime); - }else{ + BaseActivitySummaryDao.Properties.ActivityKind.eq(activityKindFilter)); + } + + if (dateFromFilter !=0) { qb.where( - BaseActivitySummaryDao.Properties.DeviceId.eq( - dbDevice.getId())).orderDesc(BaseActivitySummaryDao.Properties.StartTime); + BaseActivitySummaryDao.Properties.StartTime.gt(new Date(dateFromFilter))); + } + if (dateToFilter !=0) { + qb.where( + BaseActivitySummaryDao.Properties.EndTime.lt(new Date(dateToFilter))); } List allSummaries = qb.build().list(); @@ -74,6 +85,13 @@ public class ActivitySummariesAdapter extends AbstractItemAdapter allItems; ActivitySummariesAdapter itemsAdapter; private int current_position = 0; + long dateFromFilter=0; + long dateToFilter=0; - public ActivitySummaryItems(Context context, GBDevice device, int activityKindFilter) { + + public ActivitySummaryItems(Context context, GBDevice device, int activityKindFilter, long dateFromFilter, long dateToFilter) { this.device = device; this.activityKindFilter = activityKindFilter; - this.itemsAdapter = new ActivitySummariesAdapter(context, device, activityKindFilter); + this.dateFromFilter=dateFromFilter; + this.dateToFilter=dateToFilter; + this.itemsAdapter = new ActivitySummariesAdapter(context, device, activityKindFilter, dateFromFilter, dateToFilter); + } public BaseActivitySummary getItem(int position){ diff --git a/app/src/main/res/drawable/ic_filter.xml b/app/src/main/res/drawable/ic_filter.xml new file mode 100644 index 000000000..454bd7deb --- /dev/null +++ b/app/src/main/res/drawable/ic_filter.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_list.xml b/app/src/main/res/layout/activity_list.xml index 4b4e7b8ea..9d3b605cb 100644 --- a/app/src/main/res/layout/activity_list.xml +++ b/app/src/main/res/layout/activity_list.xml @@ -1,5 +1,6 @@ @@ -9,11 +10,13 @@ android:layout_height="match_parent" android:orientation="vertical"> - + android:layout_height="match_parent" + android:orientation="horizontal"> + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/sport_activity_filter.xml b/app/src/main/res/layout/sport_activity_filter.xml new file mode 100644 index 000000000..d5a8f7930 --- /dev/null +++ b/app/src/main/res/layout/sport_activity_filter.xml @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +