Start and end dates now work properly across charts
They are now managed by the Activity, because the fragments may be created and destroyed at any time and hece cannot synchronize their date-state all the time. Open issue: moving across the borders (first day with data, current day)
This commit is contained in:
parent
e47ebb8f09
commit
0bb3188bc8
|
@ -8,9 +8,6 @@ import android.graphics.Color;
|
|||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.github.mikephil.charting.charts.BarLineChartBase;
|
||||
import com.github.mikephil.charting.charts.Chart;
|
||||
|
@ -73,9 +70,6 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
}
|
||||
}
|
||||
|
||||
private Date mStartDate;
|
||||
private Date mEndDate;
|
||||
|
||||
protected ActivityConfig akActivity = new ActivityConfig(ActivityKind.TYPE_ACTIVITY, "Activity", Color.rgb(89, 178, 44));
|
||||
protected ActivityConfig akLightSleep = new ActivityConfig(ActivityKind.TYPE_LIGHT_SLEEP, "Light Sleep", Color.rgb(182, 191, 255));
|
||||
protected ActivityConfig akDeepSleep = new ActivityConfig(ActivityKind.TYPE_DEEP_SLEEP, "Deep Sleep", Color.rgb(76, 90, 255));
|
||||
|
@ -99,8 +93,6 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
initDates();
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
for (String action : mIntentFilterActions) {
|
||||
filter.addAction(action);
|
||||
|
@ -145,17 +137,20 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
// return view;
|
||||
// }
|
||||
|
||||
public void setStartDate(Date date) {
|
||||
mStartDate = date;
|
||||
private void setStartDate(Date date) {
|
||||
getHost().setStartDate(date);
|
||||
}
|
||||
|
||||
public void setEndDate(Date endDate) {
|
||||
mEndDate = endDate;
|
||||
private void setEndDate(Date date) {
|
||||
getHost().setEndDate(date);
|
||||
}
|
||||
|
||||
protected void initDates() {
|
||||
setEndDate(new Date());
|
||||
setStartDate(DateTimeUtils.shiftByDays(mEndDate, -1));
|
||||
public Date getStartDate() {
|
||||
return getHost().getStartDate();
|
||||
}
|
||||
|
||||
public Date getEndDate() {
|
||||
return getHost().getEndDate();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -183,19 +178,23 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
if (ChartsHost.REFRESH.equals(action)) {
|
||||
refresh();
|
||||
} else if (ChartsHost.DATE_NEXT.equals(action)) {
|
||||
handleDateNext(mStartDate, mEndDate);
|
||||
handleDateNext(getStartDate(), getEndDate());
|
||||
} else if (ChartsHost.DATE_PREV.equals(action)) {
|
||||
handleDatePrev(mStartDate, mEndDate);
|
||||
handleDatePrev(getStartDate(), getEndDate());
|
||||
}
|
||||
}
|
||||
|
||||
protected void handleDatePrev(Date startDate, Date endDate) {
|
||||
shiftDates(startDate, endDate, -1);
|
||||
if (isVisibleInActivity()) {
|
||||
shiftDates(startDate, endDate, -1);
|
||||
}
|
||||
refreshIfVisible();
|
||||
}
|
||||
|
||||
protected void handleDateNext(Date startDate, Date endDate) {
|
||||
shiftDates(startDate, endDate, +1);
|
||||
if (isVisibleInActivity()) {
|
||||
shiftDates(startDate, endDate, +1);
|
||||
}
|
||||
refreshIfVisible();
|
||||
}
|
||||
|
||||
|
@ -312,7 +311,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
protected void refresh() {
|
||||
if (getHost().getDevice() != null) {
|
||||
mChartDirty = false;
|
||||
updateDateInfo(mStartDate, mEndDate);
|
||||
updateDateInfo(getStartDate(), getEndDate());
|
||||
createRefreshTask("Visualizing data", getActivity()).execute();
|
||||
}
|
||||
}
|
||||
|
@ -529,8 +528,8 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
if (from.compareTo(to) > 0) {
|
||||
throw new IllegalArgumentException("Bad date range: " +from + ".." + to);
|
||||
}
|
||||
mStartDate = from;
|
||||
mEndDate = to;
|
||||
setStartDate(from);
|
||||
setEndDate(to);
|
||||
}
|
||||
|
||||
protected void updateDateInfo(Date from, Date to) {
|
||||
|
@ -546,11 +545,11 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
|
|||
}
|
||||
|
||||
private int getTSEnd() {
|
||||
return toTimestamp(mEndDate);
|
||||
return toTimestamp(getEndDate());
|
||||
}
|
||||
|
||||
private int getTSStart() {
|
||||
return toTimestamp(mStartDate);
|
||||
return toTimestamp(getStartDate());
|
||||
}
|
||||
|
||||
private int toTimestamp(Date date) {
|
||||
|
|
|
@ -21,12 +21,15 @@ import android.widget.TextView;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdapter;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBFragmentActivity;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||
|
||||
public class ChartsActivity extends AbstractGBFragmentActivity implements ChartsHost {
|
||||
|
||||
|
@ -37,6 +40,10 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
|
|||
private Button mNextButton;
|
||||
private TextView mDateControl;
|
||||
|
||||
private Date mStartDate;
|
||||
private Date mEndDate;
|
||||
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
@ -71,6 +78,8 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_charts);
|
||||
|
||||
initDates();
|
||||
|
||||
IntentFilter filterLocal = new IntentFilter();
|
||||
filterLocal.addAction(ControlCenter.ACTION_QUIT);
|
||||
filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED);
|
||||
|
@ -106,11 +115,36 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
|
|||
mDateControl = (TextView) findViewById(R.id.charts_text_date);
|
||||
}
|
||||
|
||||
protected void initDates() {
|
||||
setEndDate(new Date());
|
||||
setStartDate(DateTimeUtils.shiftByDays(getEndDate(), -1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GBDevice getDevice() {
|
||||
return mGBDevice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStartDate(Date startDate) {
|
||||
mStartDate = startDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEndDate(Date endDate) {
|
||||
mEndDate = endDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getStartDate() {
|
||||
return mStartDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getEndDate() {
|
||||
return mEndDate;
|
||||
}
|
||||
|
||||
private void handleNextButtonClicked() {
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DATE_NEXT));
|
||||
}
|
||||
|
|
|
@ -1,13 +1,20 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
|
||||
public interface ChartsHost {
|
||||
public static final String DATE_PREV = ChartsActivity.class.getName().concat(".date_prev");
|
||||
public static final String DATE_NEXT = ChartsActivity.class.getName().concat(".date_next");
|
||||
public static final String REFRESH = ChartsActivity.class.getName().concat(".refresh");
|
||||
static final String DATE_PREV = ChartsActivity.class.getName().concat(".date_prev");
|
||||
static final String DATE_NEXT = ChartsActivity.class.getName().concat(".date_next");
|
||||
static final String REFRESH = ChartsActivity.class.getName().concat(".refresh");
|
||||
|
||||
GBDevice getDevice();
|
||||
|
||||
void setStartDate(Date startDate);
|
||||
void setEndDate(Date endDate);
|
||||
|
||||
Date getStartDate();
|
||||
Date getEndDate();
|
||||
void setDateInfo(String dateInfo);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue