package com.alexstyl.specialdates.upcoming;

import android.support.v7.util.DiffUtil;
import android.support.v7.util.ListUpdateCallback;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.alexstyl.specialdates.upcoming.view.OnUpcomingEventClickedListener;
import com.novoda.notils.logger.simple.Log;

import java.util.ArrayList;
import java.util.List;

class UpcomingEventsAdapter extends RecyclerView.Adapter<UpcomingRowViewHolder> {

    private final UpcomingViewHolderFactory viewHolderFactory;

    private final List<UpcomingRowViewModel> viewModels = new ArrayList<>();
    private final OnUpcomingEventClickedListener listener;

    UpcomingEventsAdapter(UpcomingViewHolderFactory viewHolderFactory, OnUpcomingEventClickedListener listener) {
        this.viewHolderFactory = viewHolderFactory;
        this.listener = listener;
    }

    @Override
    public UpcomingRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return viewHolderFactory.createFor(viewType, parent);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void onBindViewHolder(UpcomingRowViewHolder holder, int position) {
        holder.bind(viewModels.get(position), listener);
    }

    @Override
    @UpcomingRowViewType
    public int getItemViewType(int position) {
        UpcomingRowViewModel viewModel = viewModels.get(position);
        return viewModel.getViewType();
    }

    @Override
    public int getItemCount() {
        return viewModels.size();
    }

    void displayUpcomingEvents(List<UpcomingRowViewModel> upcomingEventRows) {
        final UpcomingEventsDiffCallback diffCallback = new UpcomingEventsDiffCallback(viewModels, upcomingEventRows);
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        this.viewModels.clear();
        this.viewModels.addAll(upcomingEventRows);
        diffResult.dispatchUpdatesTo(new ListUpdateCallback() {
            @Override
            public void onInserted(int position, int count) {
                Log.d("onInserted");
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                Log.d("onRemoved");
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                Log.d("onMoved");
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count, Object payload) {
                Log.d("onChanged");
                notifyItemRangeChanged(position, count, payload);
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return viewModels.get(position).getId();
    }
}

@@ -83,59 +83,8 @@
 il;%0A
-import android.support.v7.util.ListUpdateCallback;%0A
 impo
@@ -240,52 +240,8 @@
 ner;
-%0Aimport com.novoda.notils.logger.simple.Log;
 %0A%0Aim
@@ -1838,842 +1838,12 @@
 sTo(
-new ListUpdateCallback() %7B%0A            @Override%0A            public void onInserted(int position, int count) %7B%0A                Log.d(%22onInserted%22);%0A                notifyItemRangeInserted(position, count);%0A            %7D%0A%0A            @Override%0A            public void onRemoved(int position, int count) %7B%0A                Log.d(%22onRemoved%22);%0A                notifyItemRangeRemoved(position, count);%0A            %7D%0A%0A            @Override%0A            public void onMoved(int fromPosition, int toPosition) %7B%0A                Log.d(%22onMoved%22);%0A                notifyItemMoved(fromPosition, toPosition);%0A            %7D%0A%0A            @Override%0A            public void onChanged(int position, int count, Object payload) %7B%0A                Log.d(%22onChanged%22);%0A                notifyItemRangeChanged(position, count, payload);%0A            %7D%0A        %7D
+this
 );%0A 

Remove non needed ListUpdateCallback