<commit_before>package com.example.dntf.dntf;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

/**
 * Created by franblas on 28/11/16.
 */
public class FoodDetailsActivity extends AppCompatActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private String noDetailsFound = "No details found...";
    private int expiredFoodStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_details);
        
        this.setupCustomActionBar();

        Bundle bExtras = getIntent().getExtras();

        String productName = "";
        JSONObject product = null;
        try {
            product = new JSONObject(bExtras.getString(FoodList.EXTRA_PRODUCT_KEY));
            productName = FoodApi.getProductName(product);
            expiredFoodStatus = bExtras.getInt(FoodList.EXTRA_EXPIRED_FOOD_STATUS_KEY);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ImageView imageView = (ImageView) this.findViewById(R.id.foodDetailIcon);
        imageView.setColorFilter(expiredFoodStatus);
        imageView.setImageResource(R.drawable.dntf_logo_dark);

        TextView foodDetailProductName = (TextView) this.findViewById(R.id.foodDetailProductName);
        foodDetailProductName.setText(productName);

        TextView foodDetailTxt = (TextView) this.findViewById(R.id.foodDetailTxt);
        foodDetailTxt.setText(Html.fromHtml(buildDetails(product)));

    }

    private String buildDetails(JSONObject product) {
        String res = "";
        if (product != null) {
            try {
                List<String> keys = Arrays.asList("brands", "countries", "quantity", "nutrition_score_debug", "nutriments", "ingredients_text_debug");
                for (String key : keys) {
                    String formatedKey = key.replace("_", " ");
                    res += "<b>" + formatedKey + "</b>: " + product.get(key).toString() + "<br/><br/>";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            res += noDetailsFound;
        }
        return res;
    }

    private void setupCustomActionBar() {<commit_msg>Enhance view for food details<commit_after>package com.example.dntf.dntf;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by franblas on 28/11/16.
 */
public class FoodDetailsActivity extends AppCompatActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private String noDetailsFound = "No details found...";
    private int expiredFoodStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_details);
        
        this.setupCustomActionBar();

        Bundle bExtras = getIntent().getExtras();

        String productName = "";
        JSONObject product = null;
        try {
            product = new JSONObject(bExtras.getString(FoodList.EXTRA_PRODUCT_KEY));
            productName = FoodApi.getProductName(product);
            expiredFoodStatus = bExtras.getInt(FoodList.EXTRA_EXPIRED_FOOD_STATUS_KEY);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ImageView imageView = (ImageView) this.findViewById(R.id.foodDetailIcon);
        imageView.setColorFilter(expiredFoodStatus);
        imageView.setImageResource(R.drawable.dntf_logo_dark);

        TextView foodDetailProductName = (TextView) this.findViewById(R.id.foodDetailProductName);
        foodDetailProductName.setText(productName);

        TextView foodDetailTxt = (TextView) this.findViewById(R.id.foodDetailTxt);
        foodDetailTxt.setText(Html.fromHtml(buildDetails(product)));

    }

    private String buildDetails(JSONObject product) {
        String res = "";
        if (product != null) {
            try {
                List<String> keys = Arrays.asList("brands", "quantity", "countries", "ingredients_text_debug", "nutriments", "nutrition_score_debug");
                List<String> formattedKeys = Arrays.asList("Brand", "Quantity", "Country", "Ingredients", "Nutriments", "Nutrition score");
                for (int i=0; i<keys.size(); i++) {
                    String key = keys.get(i);
                    String formattedKey = formattedKeys.get(i);
                    String addToRes = "<b>" + formattedKey + "</b>: &nbsp;";
                    switch (formattedKey) {
                        case "Ingredients":
                            addToRes += ingredientsFormatter(product.get(key).toString());
                            break;
                        case "Nutriments":
                            addToRes += "<br/>" + nutrimentsFormatter(product.get(key).toString());
                            break;
                        default:
                            addToRes += product.get(key).toString();
                            break;

                    }
                    res += addToRes + "<br/><br/>";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            res += noDetailsFound;
        }
        return res;
    }

    private String ingredientsFormatter(String ingredients) {
        return ingredients.trim().replace("_", "").replace(":", "");
    }

    private String nutrimentsFormatter(String nutriments) {
        String firstStep = nutriments.replace("{nameValuePairs={", "").replace("}}", "");
        String[] keyValuePairs = firstStep.split(",");
        Map<String, String> units = new HashMap<>();
        for (String k : keyValuePairs) {
            if (k.contains("_unit")) {
                String[] kv = k.split("=");
                units.put(kv[0].trim().replace("_unit", ""), kv[1]);
            }
        }

        String res = "";
        for (String k : keyValuePairs) {
            if (!k.contains("_unit")) {
                String[] kv = k.split("=");
                if (kv.length < 2) {
                    continue;
                }
                String key = kv[0].trim();
                String value = kv[1];
                String unit = units.get(key.split("_")[0]);
                if (unit == null) {
                    unit = "";
                }
                res += "&nbsp;&nbsp; &bull; &nbsp;<i>" + key.replace("_", " ") + "</i>: &nbsp;" + value + unit + "<br/>";
            }
        }
        return res;
    }

    private void setupCustomActionBar() {
{'fraction_non_alphanumeric': 0.11826961238259029, 'fraction_numerical': 0.003307315782510914, 'mean_word_length': 2.2769830949284784, 'pattern_counts': {'":': 3, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2891726', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2008, 'n_words': 395}