Update calorie when weight changes

This commit is contained in:
Jimmy Shih
2013-11-13 17:34:09 -08:00
parent 8e361610bc
commit aef035923a
4 changed files with 27 additions and 14 deletions
@@ -626,11 +626,11 @@ public class TrackDetailActivity extends AbstractSendToGoogleActivity
}
@Override
public void onChooseActivityTypeDone(String iconValue) {
public void onChooseActivityTypeDone(String iconValue, boolean newWeight) {
Track track = myTracksProviderUtils.getTrack(trackId);
String category = getString(TrackIconUtils.getIconActivityType(iconValue));
TrackUtils.updateTrack(
this, track, null, category, null, myTracksProviderUtils, trackRecordingServiceConnection);
this, track, null, category, null, myTracksProviderUtils, trackRecordingServiceConnection, newWeight);
// Add toast if cannot calculate calorie
if (CalorieUtils.getActivityType(this, category) == ActivityType.INVALID) {
@@ -64,6 +64,8 @@ public class TrackEditActivity extends AbstractMyTracksActivity
private AutoCompleteTextView activityType;
private Spinner activityTypeIcon;
private EditText description;
private boolean newWeight = false;
@Override
protected void onCreate(Bundle bundle) {
@@ -153,7 +155,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity
public void onClick(View v) {
TrackUtils.updateTrack(TrackEditActivity.this, track, name.getText().toString(),
activityType.getText().toString(), description.getText().toString(),
myTracksProviderUtils, trackRecordingServiceConnection);
myTracksProviderUtils, trackRecordingServiceConnection, newWeight);
finish();
}
});
@@ -208,7 +210,10 @@ public class TrackEditActivity extends AbstractMyTracksActivity
}
@Override
public void onChooseActivityTypeDone(String value) {
public void onChooseActivityTypeDone(String value, boolean newWeight) {
if (!this.newWeight) {
this.newWeight = newWeight;
}
setActivityTypeIcon(value);
activityType.setText(getString(TrackIconUtils.getIconActivityType(value)));
}
@@ -55,7 +55,7 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
/**
* Called when choose activity type is done.
*/
public void onChooseActivityTypeDone(String iconValue);
public void onChooseActivityTypeDone(String iconValue, boolean newWeight);
}
public static final String CHOOSE_ACTIVITY_TYPE_DIALOG_TAG = "chooseActivityType";
@@ -67,6 +67,7 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
private AlertDialog alertDialog;
private View weightContainer;
private TextView weight;
private String weightValue;
public static ChooseActivityTypeDialogFragment newInstance(String category) {
Bundle bundle = new Bundle();
@@ -127,11 +128,17 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean newWeight = false;
if (weightContainer.getVisibility() == View.VISIBLE) {
PreferencesUtils.storeWeightValue(getActivity(), weight.getText().toString());
String newValue = weight.getText().toString();
if (!newValue.equals(weightValue)) {
newWeight = true;
PreferencesUtils.storeWeightValue(getActivity(), newValue);
}
}
int selected = imageAdapter.getSelected();
caller.onChooseActivityTypeDone(TrackIconUtils.getAllIconValues().get(selected));
caller.onChooseActivityTypeDone(
TrackIconUtils.getAllIconValues().get(selected), newWeight);
}
}).setTitle(R.string.track_edit_activity_type_hint).setView(view).create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@@ -145,8 +152,9 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
imageAdapter.notifyDataSetChanged();
}
updateWeightContainer(position);
double weightValue = PreferencesUtils.getWeightDisplayValue(getActivity());
weight.setText(StringUtils.formatWeight(weightValue));
double weightDisplayValue = PreferencesUtils.getWeightDisplayValue(getActivity());
weightValue = StringUtils.formatWeight(weightDisplayValue);
weight.setText(weightValue);
}
});
return alertDialog;
@@ -34,17 +34,17 @@ public class TrackUtils {
public static void updateTrack(Context context, Track track, String name, String category,
String description, MyTracksProviderUtils myTracksProviderUtils,
TrackRecordingServiceConnection trackRecordingServiceConnection) {
TrackRecordingServiceConnection trackRecordingServiceConnection, boolean newWeight) {
if (name != null) {
track.setName(name);
}
boolean newCategory = false;
boolean updateCalorie = false;
boolean isRecording = false;
if (category != null) {
newCategory = !category.equals(track.getCategory());
updateCalorie = !category.equals(track.getCategory()) || newWeight;
track.setCategory(category);
track.setIcon(TrackIconUtils.getIconValue(context, category));
if (newCategory) {
if (updateCalorie) {
isRecording = track.getId()
== PreferencesUtils.getLong(context, R.string.recording_track_id_key);
if (!isRecording) {
@@ -61,7 +61,7 @@ public class TrackUtils {
track.setModifiedTime(System.currentTimeMillis());
myTracksProviderUtils.updateTrack(track);
if (newCategory && isRecording) {
if (updateCalorie && isRecording) {
// Update calorie through track recording service
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection);
}