package com.stolets.rxdiffutil;

import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;

import java.lang.ref.WeakReference;
import java.util.List;

import static com.stolets.rxdiffutil.internal.Preconditions.checkNotNull;

/**
 * Default implementation of the methods required by {@link DiffUtil.Callback}.
 * Feel free to create our own subclass overriding only the required method.
 *
 * @param <I> Type of the data id.
 * @param <D> Comparable data type.
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class DefaultDiffCallback<I, D extends Identifiable<I>, A extends RecyclerView.Adapter & Updatable<D>> extends DiffUtil.Callback {
    @NonNull
    private WeakReference<A> mAdapterWeakRef;
    @NonNull
    private final List<? extends D> mOldData;
    @NonNull
    private final List<? extends D> mNewData;

    /**
     * Constructs an instance of {@link DefaultDiffCallback}.
     *
     * @param oldData The current list with the data.
     * @param newData The updated list with the data which is compared with the oldData.
     * @param adapter {@link RecyclerView.Adapter} that will be automatically updated and notified about the data changes. Note: the given adapter must implement {@link Updatable} interface.
     */
    public DefaultDiffCallback(@NonNull final List<? extends D> oldData,
                               @NonNull final List<? extends D> newData,
                               @NonNull final A adapter) {
        checkNotNull(oldData, "oldData must not be null!");
        checkNotNull(newData, "newData must not be null!");
        checkNotNull(newData, "adapter must not be null!");

        this.mOldData = oldData;
        this.mNewData = newData;
        this.mAdapterWeakRef = new WeakReference<>(adapter);
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        return mNewData.get(newItemPosition).equals(mOldData.get(oldItemPosition));
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        return mNewData.get(newItemPosition).getId().equals(mOldData.get(oldItemPosition).getId());
    }

    @Override
    public int getNewListSize() {
        return mNewData.size();
    }

    @Override
    public int getOldListSize() {
        return mOldData.size();
    }
}

@@ -69,16 +69,60 @@
 onNull;%0A
+import android.support.annotation.UiThread;%0A
 import a
@@ -154,16 +154,16 @@
 ffUtil;%0A
-
 import a
@@ -200,16 +200,41 @@
 lerView;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -793,61 +793,63 @@
     
-@NonNull%0A    private WeakReference%3CA%3E mAdapterWeakRef
+private static final String TAG = %22DefaultDiffCallback%22
 ;%0A  
@@ -963,16 +963,75 @@
 NewData;
+%0A    @NonNull%0A    private WeakReference%3CA%3E mAdapterWeakRef;
 %0A%0A    /*
@@ -2477,32 +2477,32 @@
 OldListSize() %7B%0A
-
         return m
@@ -2522,11 +2522,672 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Updates data and visual representation of the adapter.%0A     * @param diffResult The result of the difference calculation.%0A     */%0A    @UiThread%0A    public void update(@NonNull final DiffUtil.DiffResult diffResult) %7B%0A        checkNotNull(diffResult, %22diffResult must not be null!%22);%0A%0A        final A adapter = mAdapterWeakRef.get();%0A        if (adapter != null) %7B%0A            // Update data%0A            adapter.update(mNewData);%0A%0A            // Update visual representation%0A            diffResult.dispatchUpdatesTo(adapter);%0A        %7D else %7B%0A            Log.w(TAG, %22The adapter is null, diff result updates won't be dispatched%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Add update method