<commit_before>/*
 * Copyright (C) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.taneza.ronald.carbs.main;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.taneza.ronald.carbs.R;
import com.gmail.taneza.ronald.carbs.common.CarbsApp;
import com.gmail.taneza.ronald.carbs.common.FoodDbAdapter;
import com.gmail.taneza.ronald.carbs.common.FoodItem;
import com.gmail.taneza.ronald.carbs.common.FoodItemInfo;

public class FoodDetailsActivity extends ActionBarActivity {

	public enum Mode {
		NewFood,
		RecentFood,
		EditFoodInMeal
	}

	public final static int FOOD_DETAILS_RESULT_OK = RESULT_OK;
	public final static int FOOD_DETAILS_RESULT_CANCELED = RESULT_CANCELED;
	public final static int FOOD_DETAILS_RESULT_REPLACE = RESULT_FIRST_USER;
	public final static int FOOD_DETAILS_RESULT_REMOVE = RESULT_FIRST_USER + 1;
	
	public final static String FOOD_ITEM_MESSAGE = "com.gmail.taneza.ronald.carbs.FOOD_ITEM_MESSAGE";
	public final static String FOOD_ITEM_RESULT = "com.gmail.taneza.ronald.carbs.FOOD_ITEM_RESULT";
	public final static String ACTIVITY_MODE_MESSAGE = "com.gmail.taneza.ronald.carbs.ACTIVITY_MODE_MESSAGE";
	
	private FoodItemInfo mFoodItemInfo;
	private EditText mWeightEditText;
	private TextView mNumCarbsTextView;
	private Mode mMode;
	private int mRemoveItemConfirmationStringId;
	private int mItemRemovedMessageId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_food_details);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		// Get the message from the intent
		Intent intent = getIntent();

		FoodItem foodItem = intent.getParcelableExtra(FOOD_ITEM_MESSAGE);
		Mode mode = Mode.values()[intent.getIntExtra(ACTIVITY_MODE_MESSAGE, Mode.NewFood.ordinal())];

		FoodDbAdapter foodDbAdapter = ((CarbsApp)getApplication()).getFoodDbAdapter();
		mFoodItemInfo = foodDbAdapter.getFoodItemInfo(foodItem);
		
		TextView foodNameTextView = (TextView) findViewById(R.id.food_details_name);
		foodNameTextView.setText(mFoodItemInfo.getName());
		
		TextView foodNameExtraTextView = (TextView) findViewById(R.id.food_details_name_extra);
		foodNameExtraTextView.setText(String.format("(%.1f g carbs per %d %s)", 
				mFoodItemInfo.getNumCarbsInGramsPerUnit(), mFoodItemInfo.getWeightPerUnit(), mFoodItemInfo.getUnitText()));
		
		mWeightEditText = (EditText) findViewById(R.id.food_details_weight_edit);
		mWeightEditText.setText(Integer.toString(mFoodItemInfo.getWeight()));
		// Request focus and show soft keyboard automatically
		mWeightEditText.requestFocus();
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

		TextView weightUnitTextView = (TextView) findViewById(R.id.food_details_weight_unit);
		weightUnitTextView.setText(mFoodItemInfo.getUnitText());
        
		mNumCarbsTextView = (TextView) findViewById(R.id.food_details_carbs_text);
		updateCarbsText();
		
		mMode = mode;
		if (mode == Mode.EditFoodInMeal) {			
			setTitle(R.string.title_activity_food_details_edit);
			Button okButton = (Button) findViewById(R.id.food_details_ok_button);
			okButton.setText(R.string.save_food_details);
			mRemoveItemConfirmationStringId = R.string.remove_item_from_meal_confirmation;<commit_msg>Fixed bug that food item info (weight) is changed even when pressing "cancel".<commit_after>/*
 * Copyright (C) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.taneza.ronald.carbs.main;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.taneza.ronald.carbs.R;
import com.gmail.taneza.ronald.carbs.common.CarbsApp;
import com.gmail.taneza.ronald.carbs.common.FoodDbAdapter;
import com.gmail.taneza.ronald.carbs.common.FoodItem;
import com.gmail.taneza.ronald.carbs.common.FoodItemInfo;

public class FoodDetailsActivity extends ActionBarActivity {

	public enum Mode {
		NewFood,
		RecentFood,
		EditFoodInMeal
	}

	public final static int FOOD_DETAILS_RESULT_OK = RESULT_OK;
	public final static int FOOD_DETAILS_RESULT_CANCELED = RESULT_CANCELED;
	public final static int FOOD_DETAILS_RESULT_REPLACE = RESULT_FIRST_USER;
	public final static int FOOD_DETAILS_RESULT_REMOVE = RESULT_FIRST_USER + 1;
	
	public final static String FOOD_ITEM_MESSAGE = "com.gmail.taneza.ronald.carbs.FOOD_ITEM_MESSAGE";
	public final static String FOOD_ITEM_RESULT = "com.gmail.taneza.ronald.carbs.FOOD_ITEM_RESULT";
	public final static String ACTIVITY_MODE_MESSAGE = "com.gmail.taneza.ronald.carbs.ACTIVITY_MODE_MESSAGE";
	
	private FoodItemInfo mFoodItemInfo;
	private EditText mWeightEditText;
	private TextView mNumCarbsTextView;
	private Mode mMode;
	private int mRemoveItemConfirmationStringId;
	private int mItemRemovedMessageId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_food_details);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		// Get the message from the intent
		Intent intent = getIntent();

		FoodItem foodItem = intent.getParcelableExtra(FOOD_ITEM_MESSAGE);
		Mode mode = Mode.values()[intent.getIntExtra(ACTIVITY_MODE_MESSAGE, Mode.NewFood.ordinal())];

		// Create a copy of the foodItemInfo returned by foodDbAdapter,
		// because the user may modify the weight value, and then cancel his changes.
		FoodDbAdapter foodDbAdapter = ((CarbsApp)getApplication()).getFoodDbAdapter();
		FoodItemInfo foodItemInfo = foodDbAdapter.getFoodItemInfo(foodItem);
		mFoodItemInfo = new FoodItemInfo(foodItem, foodItemInfo.getName(), foodItemInfo.getWeightPerUnit(), foodItemInfo.getNumCarbsInGramsPerUnit(), foodItemInfo.getUnitText());
		
		TextView foodNameTextView = (TextView) findViewById(R.id.food_details_name);
		foodNameTextView.setText(mFoodItemInfo.getName());
		
		TextView foodNameExtraTextView = (TextView) findViewById(R.id.food_details_name_extra);
		foodNameExtraTextView.setText(String.format("(%.1f g carbs per %d %s)", 
				mFoodItemInfo.getNumCarbsInGramsPerUnit(), mFoodItemInfo.getWeightPerUnit(), mFoodItemInfo.getUnitText()));
		
		mWeightEditText = (EditText) findViewById(R.id.food_details_weight_edit);
		mWeightEditText.setText(Integer.toString(mFoodItemInfo.getWeight()));
		// Request focus and show soft keyboard automatically
		mWeightEditText.requestFocus();
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

		TextView weightUnitTextView = (TextView) findViewById(R.id.food_details_weight_unit);
		weightUnitTextView.setText(mFoodItemInfo.getUnitText());
        
		mNumCarbsTextView = (TextView) findViewById(R.id.food_details_carbs_text);
		updateCarbsText();
		
		mMode = mode;
		if (mode == Mode.EditFoodInMeal) {			
			setTitle(R.string.title_activity_food_details_edit);
			Button okButton = (Button) findViewById(R.id.food_details_ok_button);
			okButton.setText(R.string.save_food_details);
			mRemoveItemConfirmationStringId = R.string.remove_item_from_meal_confirmation;
{'fraction_non_alphanumeric': 0.08983863933711295, 'fraction_numerical': 0.0026166593981683385, 'mean_word_length': 5.825148809523809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4626399', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2772, 'n_words': 615}