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