<commit_before>package com.github.st1hy.countthemcalories.activities.addmeal.fragment.presenter;

import android.support.annotation.NonNull;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.st1hy.countthemcalories.R;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.model.IngredientAction;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.model.MealIngredientsListModel;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.view.AddMealView;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.viewholder.IngredientItemViewHolder;
import com.github.st1hy.countthemcalories.activities.addmeal.model.PhysicalQuantitiesModel;
import com.github.st1hy.countthemcalories.core.permissions.PermissionsHelper;
import com.github.st1hy.countthemcalories.core.rx.SimpleSubscriber;
import com.github.st1hy.countthemcalories.core.state.Visibility;
import com.github.st1hy.countthemcalories.core.withpicture.imageholder.ImageHolderDelegate;
import com.github.st1hy.countthemcalories.database.Ingredient;
import com.github.st1hy.countthemcalories.database.IngredientTemplate;
import com.github.st1hy.countthemcalories.database.parcel.IngredientTypeParcel;
import com.github.st1hy.countthemcalories.database.unit.AmountUnit;
import com.github.st1hy.countthemcalories.database.unit.AmountUnitType;
import com.github.st1hy.countthemcalories.database.unit.EnergyDensity;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import dagger.internal.Preconditions;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

public class IngredientsAdapter extends RecyclerView.Adapter<IngredientItemViewHolder> implements
        IngredientItemViewHolder.Callback {
    private final AddMealView view;
    private final MealIngredientsListModel model;
    private final PhysicalQuantitiesModel quantityModel;
    private final Picasso picasso;
    private final PermissionsHelper permissionsHelper;

    private final CompositeSubscription subscriptions = new CompositeSubscription();

    public IngredientsAdapter(@NonNull AddMealView view, @NonNull MealIngredientsListModel model,
                              @NonNull PhysicalQuantitiesModel quantityModel,
                              @NonNull Picasso picasso,
                              @NonNull PermissionsHelper permissionsHelper) {
        this.view = view;
        this.model = model;
        this.quantityModel = quantityModel;
        this.picasso = picasso;
        this.permissionsHelper = permissionsHelper;
    }

    public void onStart() {
        subscribe(model.getItemsLoadedObservable()
                .subscribe(onInitialLoading()));
    }

    public void onStop() {
        subscriptions.clear();
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.add_meal_ingredient_item;
    }

    @Override
    public int getItemCount() {
        return model.getItemsCount();
    }

    @Override
    public void onIngredientClicked(@NonNull Ingredient ingredient, @NonNull final IngredientItemViewHolder viewHolder) {
        IngredientTypeParcel typeParcel = new IngredientTypeParcel(ingredient.getIngredientType());
        BigDecimal amount = ingredient.getAmount();
        int position = model.indexOf(ingredient);
        List<Pair<View, String>> sharedViews = ImmutableList.of(
                pairOf(viewHolder.getImage(), "ingredient-shared-view-image")
//                pairOf(viewHolder.getRoot(), "ingredient-shared-view")
//                pairOf(viewHolder.getName(), "ingredient-shared-view-name")
//                pairOf(viewHolder.getCalories(), "ingredient-shared-view-calories"),
//                pairOf(viewHolder.getDensity(), "ingredient-shared-view-density")
        );
        viewHolder.setEnabled(false);
        enableOnNextIngredientAction(viewHolder);
        view.showIngredientDetails(position, typeParcel, amount, sharedViews);
    }

    @Override
    public IngredientItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        Preconditions.checkNotNull(view);
        return new IngredientItemViewHolder(view, this, picasso, permissionsHelper);
    }

    @Override
    public void onBindViewHolder(IngredientItemViewHolder holder, int position) {
        final Ingredient ingredient = model.getItemAt(position);
        holder.setIngredient(ingredient);
        IngredientTemplate type = ingredient.getIngredientType();
        holder.setName(type.getName());
        final EnergyDensity energyDensity = quantityModel.convertToPreferred(EnergyDensity.from(type));
        holder.setEnergyDensity(quantityModel.format(energyDensity));
        final AmountUnit amountUnit = energyDensity.getAmountUnit().getBaseUnit();
        final BigDecimal amount = quantityModel.convertAmountFromDatabase(ingredient.getAmount(), amountUnit);
        BigDecimal displayedAmount = amount.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros();
        holder.setAmount(quantityModel.format(displayedAmount, amountUnit));
        holder.setCalorieCount(quantityModel.formatEnergyCount(amount, amountUnit, energyDensity));
        onBindImage(type, holder);
    }

    @Override
    public void onViewAttachedToWindow(IngredientItemViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.onAttached();
    }

    @Override
    public void onViewDetachedFromWindow(IngredientItemViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.onDetached();
    }

    void onBindImage(@NonNull IngredientTemplate ingredient,
                     @NonNull IngredientItemViewHolder holder) {
        holder.setImagePlaceholder(ingredient.getAmountType() == AmountUnitType.VOLUME ?
                R.drawable.ic_fizzy_drink : R.drawable.ic_fork_and_knife_wide);
        holder.setImageUri(ImageHolderDelegate.from(ingredient.getImageUri()));
    }

    private void enableOnNextIngredientAction(@NonNull final IngredientItemViewHolder viewHolder) {
        subscribe(view.getIngredientActionObservable()
                .first()
                .subscribe(new Action1<IngredientAction>() {
                    @Override
                    public void call(IngredientAction ingredientAction) {
                        viewHolder.setEnabled(true);
                    }
                }));
    }

    private void subscribe(Subscription subscribe) {
        subscriptions.add(subscribe);
    }

    @NonNull
    private Subscriber<IngredientAction> onIngredientAction() {
        return new SimpleSubscriber<IngredientAction>() {
            @Override
            public void onNext(IngredientAction ingredientAction) {
                IngredientAction.EditData editData;
                switch (ingredientAction.getType()) {
                    case NEW:
                        editData = ingredientAction.getDataOptional().get();
                        onIngredientReceived(editData.getParcel());
                        break;
                    case EDIT:
                        editData = ingredientAction.getDataOptional().get();
                        onIngredientEditFinished(ingredientAction.getId(), editData.getParcel(),
                                editData.getValue());
                        break;
                    case REMOVE:
                        onIngredientRemoved(ingredientAction.getId());
                        break;
                    case CANCELED:
                        break;
                }
            }
        };
    }

    void onIngredientRemoved(long requestId) {
        if (requestId == -1L) return;
        model.removeIngredient((int) requestId);
        notifyDataSetChanged();<commit_msg>Fixed: after ingredient in meal has been removed; add ingredient button stayed invisible and total calories showed old value.<commit_after>package com.github.st1hy.countthemcalories.activities.addmeal.fragment.presenter;

import android.support.annotation.NonNull;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.st1hy.countthemcalories.R;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.model.IngredientAction;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.model.MealIngredientsListModel;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.view.AddMealView;
import com.github.st1hy.countthemcalories.activities.addmeal.fragment.viewholder.IngredientItemViewHolder;
import com.github.st1hy.countthemcalories.activities.addmeal.model.PhysicalQuantitiesModel;
import com.github.st1hy.countthemcalories.core.permissions.PermissionsHelper;
import com.github.st1hy.countthemcalories.core.rx.SimpleSubscriber;
import com.github.st1hy.countthemcalories.core.state.Visibility;
import com.github.st1hy.countthemcalories.core.withpicture.imageholder.ImageHolderDelegate;
import com.github.st1hy.countthemcalories.database.Ingredient;
import com.github.st1hy.countthemcalories.database.IngredientTemplate;
import com.github.st1hy.countthemcalories.database.parcel.IngredientTypeParcel;
import com.github.st1hy.countthemcalories.database.unit.AmountUnit;
import com.github.st1hy.countthemcalories.database.unit.AmountUnitType;
import com.github.st1hy.countthemcalories.database.unit.EnergyDensity;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import dagger.internal.Preconditions;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

public class IngredientsAdapter extends RecyclerView.Adapter<IngredientItemViewHolder> implements
        IngredientItemViewHolder.Callback {
    private final AddMealView view;
    private final MealIngredientsListModel model;
    private final PhysicalQuantitiesModel quantityModel;
    private final Picasso picasso;
    private final PermissionsHelper permissionsHelper;

    private final CompositeSubscription subscriptions = new CompositeSubscription();

    public IngredientsAdapter(@NonNull AddMealView view, @NonNull MealIngredientsListModel model,
                              @NonNull PhysicalQuantitiesModel quantityModel,
                              @NonNull Picasso picasso,
                              @NonNull PermissionsHelper permissionsHelper) {
        this.view = view;
        this.model = model;
        this.quantityModel = quantityModel;
        this.picasso = picasso;
        this.permissionsHelper = permissionsHelper;
    }

    public void onStart() {
        subscribe(model.getItemsLoadedObservable()
                .subscribe(onInitialLoading()));
    }

    public void onStop() {
        subscriptions.clear();
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.add_meal_ingredient_item;
    }

    @Override
    public int getItemCount() {
        return model.getItemsCount();
    }

    @Override
    public void onIngredientClicked(@NonNull Ingredient ingredient, @NonNull final IngredientItemViewHolder viewHolder) {
        IngredientTypeParcel typeParcel = new IngredientTypeParcel(ingredient.getIngredientType());
        BigDecimal amount = ingredient.getAmount();
        int position = model.indexOf(ingredient);
        List<Pair<View, String>> sharedViews = ImmutableList.of(
                pairOf(viewHolder.getImage(), "ingredient-shared-view-image")
//                pairOf(viewHolder.getRoot(), "ingredient-shared-view")
//                pairOf(viewHolder.getName(), "ingredient-shared-view-name")
//                pairOf(viewHolder.getCalories(), "ingredient-shared-view-calories"),
//                pairOf(viewHolder.getDensity(), "ingredient-shared-view-density")
        );
        viewHolder.setEnabled(false);
        enableOnNextIngredientAction(viewHolder);
        view.showIngredientDetails(position, typeParcel, amount, sharedViews);
    }

    @Override
    public IngredientItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        Preconditions.checkNotNull(view);
        return new IngredientItemViewHolder(view, this, picasso, permissionsHelper);
    }

    @Override
    public void onBindViewHolder(IngredientItemViewHolder holder, int position) {
        final Ingredient ingredient = model.getItemAt(position);
        holder.setIngredient(ingredient);
        IngredientTemplate type = ingredient.getIngredientType();
        holder.setName(type.getName());
        final EnergyDensity energyDensity = quantityModel.convertToPreferred(EnergyDensity.from(type));
        holder.setEnergyDensity(quantityModel.format(energyDensity));
        final AmountUnit amountUnit = energyDensity.getAmountUnit().getBaseUnit();
        final BigDecimal amount = quantityModel.convertAmountFromDatabase(ingredient.getAmount(), amountUnit);
        BigDecimal displayedAmount = amount.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros();
        holder.setAmount(quantityModel.format(displayedAmount, amountUnit));
        holder.setCalorieCount(quantityModel.formatEnergyCount(amount, amountUnit, energyDensity));
        onBindImage(type, holder);
    }

    @Override
    public void onViewAttachedToWindow(IngredientItemViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.onAttached();
    }

    @Override
    public void onViewDetachedFromWindow(IngredientItemViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.onDetached();
    }

    void onBindImage(@NonNull IngredientTemplate ingredient,
                     @NonNull IngredientItemViewHolder holder) {
        holder.setImagePlaceholder(ingredient.getAmountType() == AmountUnitType.VOLUME ?
                R.drawable.ic_fizzy_drink : R.drawable.ic_fork_and_knife_wide);
        holder.setImageUri(ImageHolderDelegate.from(ingredient.getImageUri()));
    }

    private void enableOnNextIngredientAction(@NonNull final IngredientItemViewHolder viewHolder) {
        subscribe(view.getIngredientActionObservable()
                .first()
                .subscribe(new Action1<IngredientAction>() {
                    @Override
                    public void call(IngredientAction ingredientAction) {
                        viewHolder.setEnabled(true);
                    }
                }));
    }

    private void subscribe(Subscription subscribe) {
        subscriptions.add(subscribe);
    }

    @NonNull
    private Subscriber<IngredientAction> onIngredientAction() {
        return new SimpleSubscriber<IngredientAction>() {
            @Override
            public void onNext(IngredientAction ingredientAction) {
                IngredientAction.EditData editData;
                switch (ingredientAction.getType()) {
                    case NEW:
                        editData = ingredientAction.getDataOptional().get();
                        onIngredientReceived(editData.getParcel());
                        break;
                    case EDIT:
                        editData = ingredientAction.getDataOptional().get();
                        onIngredientEditFinished(ingredientAction.getId(), editData.getParcel(),
                                editData.getValue());
                        break;
                    case REMOVE:
                        onIngredientRemoved(ingredientAction.getId());
                        break;
                    case CANCELED:
                        break;
                }
            }
        };
    }

    void onIngredientRemoved(long requestId) {
        if (requestId == -1L) return;
        model.removeIngredient((int) requestId);
        notifyDataSetChanged();
        onDataSetChanged();
{'fraction_non_alphanumeric': 0.0849285195056942, 'fraction_numerical': 0.002786527744124061, 'mean_word_length': 3.1179845347967072, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2684889', 'n_tokens_mistral': 4589, 'n_tokens_neox': 4429, 'n_words': 708}