1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 11:26:47 +01:00

Improvements to charts

- backward and forward buttons work better
- buttons only refresh the current fragment now
- activity shows fragment-specific titles
This commit is contained in:
cpfeiffer 2015-08-25 00:33:37 +02:00
parent 070876db06
commit e47ebb8f09
10 changed files with 315 additions and 100 deletions

View File

@ -0,0 +1,51 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import java.util.HashSet;
import java.util.Set;
public abstract class AbstractFragmentPagerAdapter extends FragmentStatePagerAdapter {
private Set<AbstractGBFragment> fragments = new HashSet<>();
private Object primaryFragment;
public AbstractFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Object fragment = super.instantiateItem(container, position);
if (fragment instanceof AbstractGBFragment) {
fragments.add((AbstractGBFragment) fragment);
}
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
fragments.remove(object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (object != primaryFragment) {
primaryFragment = object;
setCurrentFragment(primaryFragment);
}
}
private void setCurrentFragment(Object newCurrentFragment) {
for (AbstractGBFragment frag : fragments) {
if (frag != newCurrentFragment) {
frag.onMadeInvisibleInActivity();
} else {
frag.onMadeVisibleInActivityInternal();
}
}
}
}

View File

@ -0,0 +1,65 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
/**
* Abstract base class for fragments. Provides hooks that are called when
* the fragment is made visible and invisible in the activity. also allows
* the fragment to define the title to be shown in the activity.
* @see AbstractGBFragmentActivity
*/
public abstract class AbstractGBFragment extends Fragment {
private boolean mVisibleInactivity;
/**
* Called when this fragment has been fully scrolled into the activity.
*
* @see #isVisibleInActivity()
* @see #onMadeInvisibleInActivity()
*/
protected void onMadeVisibleInActivity() {
updateActivityTitle();
}
/**
* Called when this fragment has been scrolled out of the activity.
* @see #isVisibleInActivity()
* @see #onMadeVisibleInActivity()
*/
protected void onMadeInvisibleInActivity() {
mVisibleInactivity = false;
}
/**
* Returns true if this fragment is currently visible in the hosting
* activity, not taking into account whether the screen is enabled at all.
*/
public boolean isVisibleInActivity() {
return mVisibleInactivity;
}
protected void updateActivityTitle() {
FragmentActivity activity = getActivity();
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
if (getTitle() != null) {
activity.setTitle(getTitle());
}
}
}
@Nullable
protected abstract CharSequence getTitle();
/**
* Internal
* @hide
*/
public void onMadeVisibleInActivityInternal() {
mVisibleInactivity = true;
if (isVisible()) {
onMadeVisibleInActivity();
}
}
}

View File

@ -0,0 +1,57 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.View;
/**
* A base activity that supports paging through fragments by swiping.
* Subclasses will have to add a ViewPager to their layout and add something
* like this to hook it to the fragments:
*
* <pre>
* // Set up the ViewPager with the sections adapter.
* ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
* viewPager.setAdapter(getPagerAdapter());
* </pre>
*
* @see AbstractGBFragment
*/
public abstract class AbstractGBFragmentActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private AbstractFragmentPagerAdapter mSectionsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = createFragmentPagerAdapter(getSupportFragmentManager());
}
public AbstractFragmentPagerAdapter getPagerAdapter() {
return mSectionsPagerAdapter;
}
/**
* Creates a PagerAdapter that will create the fragments to be used with this
* activity. The fragments should typically extend AbstractGBFragment
* @param fragmentManager
* @return
*/
protected abstract AbstractFragmentPagerAdapter createFragmentPagerAdapter(FragmentManager fragmentManager);
}

View File

@ -6,7 +6,6 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -33,6 +32,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBFragment;
import nodomain.freeyourgadget.gadgetbridge.database.DBAccess; import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
@ -43,7 +43,7 @@ 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;
public abstract class AbstractChartFragment extends Fragment { public abstract class AbstractChartFragment extends AbstractGBFragment {
private static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class); private static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
private final Set<String> mIntentFilterActions; private final Set<String> mIntentFilterActions;
@ -53,6 +53,13 @@ public abstract class AbstractChartFragment extends Fragment {
AbstractChartFragment.this.onReceive(context, intent); AbstractChartFragment.this.onReceive(context, intent);
} }
}; };
private boolean mChartDirty = true;
public boolean isChartDirty() {
return mChartDirty;
}
public abstract String getTitle();
protected static final class ActivityConfig { protected static final class ActivityConfig {
public final int type; public final int type;
@ -102,12 +109,42 @@ public abstract class AbstractChartFragment extends Fragment {
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public void onHiddenChanged(boolean hidden) {
View view = super.onCreateView(inflater, container, savedInstanceState); super.onHiddenChanged(hidden);
updateDateInfo(mStartDate, mEndDate);
return view;
} }
@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;
// }
public void setStartDate(Date date) { public void setStartDate(Date date) {
mStartDate = date; mStartDate = date;
} }
@ -121,6 +158,20 @@ public abstract class AbstractChartFragment extends Fragment {
setStartDate(DateTimeUtils.shiftByDays(mEndDate, -1)); setStartDate(DateTimeUtils.shiftByDays(mEndDate, -1));
} }
/**
* Called when this fragment has been fully scrolled into the activity.
*
* @see #isVisibleInActivity()
* @see #onMadeInvisibleInActivity()
*/
@Override
protected void onMadeVisibleInActivity() {
super.onMadeVisibleInActivity();
if (isChartDirty()) {
refresh();
}
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -140,10 +191,20 @@ public abstract class AbstractChartFragment extends Fragment {
protected void handleDatePrev(Date startDate, Date endDate) { protected void handleDatePrev(Date startDate, Date endDate) {
shiftDates(startDate, endDate, -1); shiftDates(startDate, endDate, -1);
refreshIfVisible();
} }
protected void handleDateNext(Date startDate, Date endDate) { protected void handleDateNext(Date startDate, Date endDate) {
shiftDates(startDate, endDate, +1); shiftDates(startDate, endDate, +1);
refreshIfVisible();
}
protected void refreshIfVisible() {
if (isVisibleInActivity()) {
refresh();
} else {
mChartDirty = true;
}
} }
protected void shiftDates(Date startDate, Date endDate, int offset) { protected void shiftDates(Date startDate, Date endDate, int offset) {
@ -151,7 +212,6 @@ public abstract class AbstractChartFragment extends Fragment {
Date newEnd = DateTimeUtils.shiftByDays(endDate, offset); Date newEnd = DateTimeUtils.shiftByDays(endDate, offset);
setDateRange(newStart, newEnd); setDateRange(newStart, newEnd);
refresh();
} }
protected Integer getColorFor(int activityKind) { protected Integer getColorFor(int activityKind) {
@ -250,15 +310,19 @@ public abstract class AbstractChartFragment extends Fragment {
* #renderCharts * #renderCharts
*/ */
protected void refresh() { protected void refresh() {
if (getHost().getDevice() != null) {
mChartDirty = false;
updateDateInfo(mStartDate, mEndDate);
createRefreshTask("Visualizing data", getActivity()).execute(); createRefreshTask("Visualizing data", getActivity()).execute();
} }
}
/** /**
* This method reads the data from the database, analyzes and prepares it for * This method reads the data from the database, analyzes and prepares it for
* the charts. This will be called from a background task, so there must not be * the charts. This will be called from a background task, so there must not be
* any UI access. #renderCharts will be automatically called after this method. * any UI access. #renderCharts will be automatically called after this method.
*/ */
protected abstract void refreshInBackground(DBHandler db); protected abstract void refreshInBackground(DBHandler db, GBDevice device);
/** /**
* Performs a re-rendering of the chart. * Performs a re-rendering of the chart.
@ -267,10 +331,6 @@ public abstract class AbstractChartFragment extends Fragment {
protected abstract void renderCharts(); protected abstract void renderCharts();
protected void refresh(GBDevice gbDevice, BarLineChartBase chart, List<ActivitySample> samples) { protected void refresh(GBDevice gbDevice, BarLineChartBase chart, List<ActivitySample> samples) {
if (gbDevice == null) {
return;
}
Calendar cal = GregorianCalendar.getInstance(); Calendar cal = GregorianCalendar.getInstance();
cal.clear(); cal.clear();
Date date; Date date;
@ -450,7 +510,7 @@ public abstract class AbstractChartFragment extends Fragment {
@Override @Override
protected void doInBackground(DBHandler db) { protected void doInBackground(DBHandler db) {
refreshInBackground(db); refreshInBackground(db, getHost().getDevice());
} }
@Override @Override
@ -471,7 +531,6 @@ public abstract class AbstractChartFragment extends Fragment {
} }
mStartDate = from; mStartDate = from;
mEndDate = to; mEndDate = to;
updateDateInfo(mStartDate, mEndDate);
} }
protected void updateDateInfo(Date from, Date to) { protected void updateDateInfo(Date from, Date to) {

View File

@ -34,19 +34,12 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
private int mSmartAlarmTo = -1; private int mSmartAlarmTo = -1;
private int mTimestampFrom = -1; private int mTimestampFrom = -1;
private int mSmartAlarmGoneOff = -1; private int mSmartAlarmGoneOff = -1;
private GBDevice mGBDevice = null;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_charts, container, false); View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
}
mChart = (BarLineChartBase) rootView.findViewById(R.id.activitysleepchart); mChart = (BarLineChartBase) rootView.findViewById(R.id.activitysleepchart);
setupChart(); setupChart();
@ -54,6 +47,11 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
return rootView; return rootView;
} }
@Override
public String getTitle() {
return getString(R.string.activity_sleepchart_activity_and_sleep);
}
private void setupChart() { private void setupChart() {
mChart.setBackgroundColor(BACKGROUND_COLOR); mChart.setBackgroundColor(BACKGROUND_COLOR);
mChart.setDescriptionColor(DESCRIPTION_COLOR); mChart.setDescriptionColor(DESCRIPTION_COLOR);
@ -85,6 +83,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
yAxisRight.setDrawTopYLabelEntry(false); yAxisRight.setDrawTopYLabelEntry(false);
yAxisRight.setTextColor(CHART_TEXT_COLOR); yAxisRight.setTextColor(CHART_TEXT_COLOR);
// refreshIfVisible();
refresh(); refresh();
} }
@ -104,9 +103,9 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
} }
@Override @Override
protected void refreshInBackground(DBHandler db) { protected void refreshInBackground(DBHandler db, GBDevice device) {
List<ActivitySample> samples = getSamples(db, mGBDevice); List<ActivitySample> samples = getSamples(db, device);
refresh(mGBDevice, mChart, samples); refresh(device, mChart, samples);
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR); mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
} }

View File

@ -6,11 +6,10 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -22,32 +21,17 @@ import android.widget.TextView;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; 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.activities.ControlCenter;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class ChartsActivity extends FragmentActivity implements ChartsHost { public class ChartsActivity extends AbstractGBFragmentActivity implements ChartsHost {
private static final Logger LOG = LoggerFactory.getLogger(ChartsActivity.class); private static final Logger LOG = LoggerFactory.getLogger(ChartsActivity.class);
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private ProgressBar mProgressBar; private ProgressBar mProgressBar;
private Button mPrevButton; private Button mPrevButton;
private Button mNextButton; private Button mNextButton;
@ -61,8 +45,6 @@ public class ChartsActivity extends FragmentActivity implements ChartsHost {
case ControlCenter.ACTION_QUIT: case ControlCenter.ACTION_QUIT:
finish(); finish();
break; break;
case ControlCenter.ACTION_REFRESH_DEVICELIST:
break;
case GBDevice.ACTION_DEVICE_CHANGED: case GBDevice.ACTION_DEVICE_CHANGED:
GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
refreshBusyState(dev); refreshBusyState(dev);
@ -70,6 +52,7 @@ public class ChartsActivity extends FragmentActivity implements ChartsHost {
} }
} }
}; };
private GBDevice mGBDevice;
private void refreshBusyState(GBDevice dev) { private void refreshBusyState(GBDevice dev) {
if (dev.isBusy()) { if (dev.isBusy()) {
@ -90,18 +73,19 @@ public class ChartsActivity extends FragmentActivity implements ChartsHost {
IntentFilter filterLocal = new IntentFilter(); IntentFilter filterLocal = new IntentFilter();
filterLocal.addAction(ControlCenter.ACTION_QUIT); filterLocal.addAction(ControlCenter.ACTION_QUIT);
filterLocal.addAction(ControlCenter.ACTION_REFRESH_DEVICELIST);
filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED); filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
Bundle extras = getIntent().getExtras();
// Create the adapter that will return a fragment for each of the three if (extras != null) {
// primary sections of the activity. mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); } else {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}
// Set up the ViewPager with the sections adapter. // Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager); ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter); viewPager.setAdapter(getPagerAdapter());
mProgressBar = (ProgressBar) findViewById(R.id.charts_progress); mProgressBar = (ProgressBar) findViewById(R.id.charts_progress);
mPrevButton = (Button) findViewById(R.id.charts_previous); mPrevButton = (Button) findViewById(R.id.charts_previous);
@ -122,6 +106,11 @@ public class ChartsActivity extends FragmentActivity implements ChartsHost {
mDateControl = (TextView) findViewById(R.id.charts_text_date); mDateControl = (TextView) findViewById(R.id.charts_text_date);
} }
@Override
public GBDevice getDevice() {
return mGBDevice;
}
private void handleNextButtonClicked() { private void handleNextButtonClicked() {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DATE_NEXT)); LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DATE_NEXT));
} }
@ -161,11 +150,16 @@ public class ChartsActivity extends FragmentActivity implements ChartsHost {
mDateControl.setText(dateInfo); mDateControl.setText(dateInfo);
} }
@Override
protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(FragmentManager fragmentManager) {
return new SectionsPagerAdapter(fragmentManager);
}
/** /**
* A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages. * one of the sections/tabs/pages.
*/ */
public static class SectionsPagerAdapter extends FragmentStatePagerAdapter { public static class SectionsPagerAdapter extends AbstractFragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) { public SectionsPagerAdapter(FragmentManager fm) {
super(fm); super(fm);
@ -191,19 +185,5 @@ public class ChartsActivity extends FragmentActivity implements ChartsHost {
// Show 3 total pages. // Show 3 total pages.
return 3; return 3;
} }
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Today".toUpperCase(l);
case 1:
return "Week".toUpperCase(l);
case 2:
return "Month".toUpperCase(l);
}
return null;
}
} }
} }

View File

@ -1,9 +1,13 @@
package nodomain.freeyourgadget.gadgetbridge.activities.charts; package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public interface ChartsHost { public interface ChartsHost {
public static final String DATE_PREV = ChartsActivity.class.getName().concat(".date_prev"); 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 DATE_NEXT = ChartsActivity.class.getName().concat(".date_next");
public static final String REFRESH = ChartsActivity.class.getName().concat(".refresh"); public static final String REFRESH = ChartsActivity.class.getName().concat(".refresh");
GBDevice getDevice();
void setDateInfo(String dateInfo); void setDateInfo(String dateInfo);
} }

View File

@ -44,17 +44,13 @@ public class SleepChartFragment extends AbstractChartFragment {
private int mSmartAlarmTo = -1; private int mSmartAlarmTo = -1;
private int mTimestampFrom = -1; private int mTimestampFrom = -1;
private int mSmartAlarmGoneOff = -1; private int mSmartAlarmGoneOff = -1;
private GBDevice mGBDevice = null;
@Override @Override
protected void refreshInBackground(DBHandler db) { protected void refreshInBackground(DBHandler db, GBDevice device) {
List<ActivitySample> samples = getSamples(db); List<ActivitySample> samples = getSamples(db, device);
refresh(mGBDevice, mActivityChart, samples);
refreshSleepAmounts(mGBDevice, mSleepAmountChart, samples);
}
private List<ActivitySample> getSamples(DBHandler db) { refresh(device, mActivityChart, samples);
return getSamples(db, mGBDevice); refreshSleepAmounts(device, mSleepAmountChart, samples);
} }
private void refreshSleepAmounts(GBDevice mGBDevice, PieChart pieChart, List<ActivitySample> samples) { private void refreshSleepAmounts(GBDevice mGBDevice, PieChart pieChart, List<ActivitySample> samples) {
@ -87,22 +83,23 @@ public class SleepChartFragment extends AbstractChartFragment {
//setupLegend(pieChart); //setupLegend(pieChart);
} }
@Override
public String getTitle() {
return getString(R.string.sleepchart_your_sleep);
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sleepchart, container, false); View rootView = inflater.inflate(R.layout.fragment_sleepchart, container, false);
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
}
mActivityChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart); mActivityChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart);
mSleepAmountChart = (PieChart) rootView.findViewById(R.id.sleepchart_pie_light_deep); mSleepAmountChart = (PieChart) rootView.findViewById(R.id.sleepchart_pie_light_deep);
setupActivityChart(); setupActivityChart();
setupSleepAmountChart(); setupSleepAmountChart();
// refreshIfVisible();
refresh(); refresh();
return rootView; return rootView;

View File

@ -44,14 +44,12 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
private BarLineChartBase mWeekStepsChart; private BarLineChartBase mWeekStepsChart;
private PieChart mTodayStepsChart; private PieChart mTodayStepsChart;
private GBDevice mGBDevice = null;
@Override @Override
protected void refreshInBackground(DBHandler db) { protected void refreshInBackground(DBHandler db, GBDevice device) {
Calendar day = Calendar.getInstance(); Calendar day = Calendar.getInstance();
//NB: we could have omitted the day, but this way we can move things to the past easily //NB: we could have omitted the day, but this way we can move things to the past easily
refreshDaySteps(db, mTodayStepsChart, day); refreshDaySteps(db, mTodayStepsChart, day, device);
refreshWeekBeforeSteps(db, mWeekStepsChart, day); refreshWeekBeforeSteps(db, mWeekStepsChart, day, device);
} }
@Override @Override
@ -60,7 +58,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
mTodayStepsChart.invalidate(); mTodayStepsChart.invalidate();
} }
private void refreshWeekBeforeSteps(DBHandler db, BarLineChartBase barChart, Calendar day) { private void refreshWeekBeforeSteps(DBHandler db, BarLineChartBase barChart, Calendar day, GBDevice device) {
ActivityAnalysis analysis = new ActivityAnalysis(); ActivityAnalysis analysis = new ActivityAnalysis();
@ -69,7 +67,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
List<String> labels = new ArrayList<>(); List<String> labels = new ArrayList<>();
for (int counter = 0; counter < 7; counter++) { for (int counter = 0; counter < 7; counter++) {
entries.add(new BarEntry(analysis.calculateTotalSteps(getSamplesOfDay(db, day)), counter)); entries.add(new BarEntry(analysis.calculateTotalSteps(getSamplesOfDay(db, day, device)), counter));
labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale)); labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale));
day.add(Calendar.DATE, 1); day.add(Calendar.DATE, 1);
} }
@ -89,10 +87,10 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
barChart.getLegend().setEnabled(false); barChart.getLegend().setEnabled(false);
} }
private void refreshDaySteps(DBHandler db, PieChart pieChart, Calendar day) { private void refreshDaySteps(DBHandler db, PieChart pieChart, Calendar day, GBDevice device) {
ActivityAnalysis analysis = new ActivityAnalysis(); ActivityAnalysis analysis = new ActivityAnalysis();
int totalSteps = analysis.calculateTotalSteps(getSamplesOfDay(db, day)); int totalSteps = analysis.calculateTotalSteps(getSamplesOfDay(db, day, device));
pieChart.setCenterText(NumberFormat.getNumberInstance(mLocale).format(totalSteps)); pieChart.setCenterText(NumberFormat.getNumberInstance(mLocale).format(totalSteps));
PieData data = new PieData(); PieData data = new PieData();
@ -128,13 +126,9 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
View rootView = inflater.inflate(R.layout.fragment_sleepchart, container, false); View rootView = inflater.inflate(R.layout.fragment_sleepchart, container, false);
Bundle extras = getActivity().getIntent().getExtras(); GBDevice device = getHost().getDevice();
if (extras != null) { if(device != null) {
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE); mTargetSteps = MiBandCoordinator.getFitnessGoal(device.getAddress());
}
if(mGBDevice != null) {
mTargetSteps = MiBandCoordinator.getFitnessGoal(mGBDevice.getAddress());
} }
mWeekStepsChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart); mWeekStepsChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart);
@ -143,11 +137,17 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
setupWeekStepsChart(); setupWeekStepsChart();
setupTodayStepsChart(); setupTodayStepsChart();
// refreshIfVisible();
refresh(); refresh();
return rootView; return rootView;
} }
@Override
public String getTitle() {
return getString(R.string.weekstepschart_steps_a_week);
}
private void setupTodayStepsChart() { private void setupTodayStepsChart() {
mTodayStepsChart.setBackgroundColor(BACKGROUND_COLOR); mTodayStepsChart.setBackgroundColor(BACKGROUND_COLOR);
mTodayStepsChart.setDescriptionColor(DESCRIPTION_COLOR); mTodayStepsChart.setDescriptionColor(DESCRIPTION_COLOR);
@ -196,7 +196,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
chart.getLegend().setTextColor(LEGEND_TEXT_COLOR); chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
} }
private List<ActivitySample> getSamplesOfDay(DBHandler db, Calendar day) { private List<ActivitySample> getSamplesOfDay(DBHandler db, Calendar day, GBDevice device) {
int startTs; int startTs;
int endTs; int endTs;
@ -210,7 +210,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
day.set(Calendar.SECOND, 59); day.set(Calendar.SECOND, 59);
endTs = (int) (day.getTimeInMillis() / 1000); endTs = (int) (day.getTimeInMillis() / 1000);
return getSamples(db, mGBDevice, startTs, endTs); return getSamples(db, device, startTs, endTs);
} }
@Override @Override

View File

@ -166,4 +166,7 @@
<string name="pbw_install_handler_unable_to_install">Unable to install the given file: $1%s</string> <string name="pbw_install_handler_unable_to_install">Unable to install the given file: $1%s</string>
<string name="pbw_install_handler_hw_revision_mismatch">Unable to install the given firmware: it doesn\'t match your Pebble\'s hardware revision.</string> <string name="pbw_install_handler_hw_revision_mismatch">Unable to install the given firmware: it doesn\'t match your Pebble\'s hardware revision.</string>
<string name="installer_activity_wait_while_determining_status">Please wait while determining the installation status...</string> <string name="installer_activity_wait_while_determining_status">Please wait while determining the installation status...</string>
<string name="sleepchart_your_sleep">Your Sleep</string>
<string name="weekstepschart_steps_a_week">Steps a week</string>
<string name="activity_sleepchart_activity_and_sleep">Your Activity and Sleep</string>
</resources> </resources>