Prevent charts date in the future, documentation and cleanup

This commit is contained in:
cpfeiffer 2015-08-25 22:45:07 +02:00
parent b4e34db1d2
commit e1c02cc373
1 changed files with 67 additions and 44 deletions

View File

@ -40,10 +40,28 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
/**
* A base class fragment to be used with ChartsActivity. The fragment can supply
* a title to be displayed in the activity by returning non-null in #getTitle()
* Broadcast events can be received by overriding #onReceive(Context,Intent).
* The chart can be refreshed by calling #refresh()
* Implement refreshInBackground(DBHandler, GBDevice) to fetch the samples from the DB,
* and add the samples to the chart. The actual rendering, which must be performed in the UI
* thread, must be done in #renderCharts().
* Access functionality of the hosting activity with #getHost()
*
* The hosting ChartsHost activity provides a section for displaying a date or date range
* being the basis for the chart, as well as two buttons for moving backwards and forward
* in time. The date is held by the activity, so that it can be shared by multiple chart
* fragments. It is still the responsibility of the (currently visible) chart fragment
* to set the desired date in the ChartsActivity via #setDateRange(Date,Date).
* The default implementations #handleDatePrev(Date,Date) and #handleDateNext(Date,Date)
* shift the date by one day.
*/
public abstract class AbstractChartFragment extends AbstractGBFragment { public abstract class AbstractChartFragment extends AbstractGBFragment {
protected int ANIM_TIME = 350; protected int ANIM_TIME = 350;
private static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class); private static final Logger LOG = LoggerFactory.getLogger(AbstractChartFragment.class);
private final Set<String> mIntentFilterActions; private final Set<String> mIntentFilterActions;
private BroadcastReceiver mReceiver = new BroadcastReceiver() { private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@ -102,43 +120,6 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter); LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter);
} }
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// View view = super.onCreateView(inflater, container, savedInstanceState);
// updateDateInfo(mStartDate, mEndDate);
// return view;
// }
private void setStartDate(Date date) { private void setStartDate(Date date) {
getHost().setStartDate(date); getHost().setStartDate(date);
} }
@ -186,16 +167,32 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
} }
} }
/**
* Default implementation shifts the dates by one day, if visible
* and calls #refreshIfVisible().
* @param startDate
* @param endDate
*/
protected void handleDatePrev(Date startDate, Date endDate) { protected void handleDatePrev(Date startDate, Date endDate) {
if (isVisibleInActivity()) { if (isVisibleInActivity()) {
shiftDates(startDate, endDate, -1); if (!shiftDates(startDate, endDate, -1)) {
return;
}
} }
refreshIfVisible(); refreshIfVisible();
} }
/**
* Default implementation shifts the dates by one day, if visible
* and calls #refreshIfVisible().
* @param startDate
* @param endDate
*/
protected void handleDateNext(Date startDate, Date endDate) { protected void handleDateNext(Date startDate, Date endDate) {
if (isVisibleInActivity()) { if (isVisibleInActivity()) {
shiftDates(startDate, endDate, +1); if (!shiftDates(startDate, endDate, +1)) {
return;
}
} }
refreshIfVisible(); refreshIfVisible();
} }
@ -208,11 +205,18 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
} }
} }
protected void shiftDates(Date startDate, Date endDate, int offset) { /**
* Shifts the given dates by offset days. offset may be positive or negative.
* @param startDate
* @param endDate
* @param offset a positive or negative number of days to shift the dates
* @return true if the shift was successful and false otherwise
*/
protected boolean shiftDates(Date startDate, Date endDate, int offset) {
Date newStart = DateTimeUtils.shiftByDays(startDate, offset); Date newStart = DateTimeUtils.shiftByDays(startDate, offset);
Date newEnd = DateTimeUtils.shiftByDays(endDate, offset); Date newEnd = DateTimeUtils.shiftByDays(endDate, offset);
setDateRange(newStart, newEnd); return setDateRange(newStart, newEnd);
} }
protected Integer getColorFor(int activityKind) { protected Integer getColorFor(int activityKind) {
@ -326,7 +330,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
protected abstract void refreshInBackground(DBHandler db, GBDevice device); protected abstract void refreshInBackground(DBHandler db, GBDevice device);
/** /**
* Performs a re-rendering of the chart. * Triggers the actual (re-) rendering of the chart.
* Always called from the UI thread. * Always called from the UI thread.
*/ */
protected abstract void renderCharts(); protected abstract void renderCharts();
@ -441,6 +445,14 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
} }
} }
/**
* Implement this to supply the samples to be displayed.
* @param db
* @param device
* @param tsFrom
* @param tsTo
* @return
*/
protected abstract List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo); protected abstract List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo);
protected abstract void setupLegend(Chart chart); protected abstract void setupLegend(Chart chart);
@ -526,12 +538,23 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
} }
} }
public void setDateRange(Date from, Date to) { /**
* Returns true if the date was successfully shifted, and false if the shift
* was ignored, e.g. when the to-value is in the future.
* @param from
* @param to
*/
public boolean setDateRange(Date from, Date to) {
if (from.compareTo(to) > 0) { if (from.compareTo(to) > 0) {
throw new IllegalArgumentException("Bad date range: " +from + ".." + to); throw new IllegalArgumentException("Bad date range: " +from + ".." + to);
} }
Date now = new Date();
if (to.after(now)) {
return false;
}
setStartDate(from); setStartDate(from);
setEndDate(to); setEndDate(to);
return true;
} }
protected void updateDateInfo(Date from, Date to) { protected void updateDateInfo(Date from, Date to) {