package com.sinyuk.yuk.ui.feeds;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sinyuk.yuk.R;
import com.sinyuk.yuk.data.shot.Shot;
import com.sinyuk.yuk.utils.glide.CropCircleTransformation;

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

import rx.functions.Action1;
import timber.log.Timber;

/**
 * Created by Sinyuk on 16/7/6.
 * 尽量减少这里的逻辑
 */
public class FeedsAdapter extends RecyclerView.Adapter<FeedsAdapter.FeedItemViewHolder> implements Action1<List<Shot>> {
    private final DrawableRequestBuilder<String> avatarBuilder;
    private final DrawableRequestBuilder<String> GIFBuilder;
    private final DrawableRequestBuilder<String> PNGBuilder;

    private List<Shot> mDataSet = new ArrayList<>();
    private boolean isAutoPlayGif = false;

    public FeedsAdapter(Context context, RequestManager requestManager, ArrayList<Shot> dataSet) {
        Timber.tag("FeedsAdapter");
        this.GIFBuilder = requestManager
                .fromString()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .centerCrop();

        this.PNGBuilder = requestManager
                .fromString()
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .dontAnimate()
                .centerCrop();

        this.mDataSet = dataSet;

        avatarBuilder = requestManager.fromString()
                .priority(Priority.NORMAL)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .centerCrop()
                .dontAnimate()
                .bitmapTransform(new CropCircleTransformation(context));
    }


    @Override
    public FeedItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FeedItemViewHolder((FeedItemView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.feed_list_item, parent, false));
    }

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

    @Override
    public void onBindViewHolder(FeedItemViewHolder holder, int position) {
        final Shot data = mDataSet.get(position);
        if (data.isAnimated()) {
            holder.bindTo(data, GIFBuilder, isAutoPlayGif, avatarBuilder);
        } else {
            holder.bindTo(data, PNGBuilder, isAutoPlayGif, avatarBuilder);
        }
    }


    public void setAutoPlayGif(boolean autoPlayGif) {
        this.isAutoPlayGif = autoPlayGif;
    }

    @Override
    public void call(List<Shot> shots) {
        mDataSet = shots;
        notifyDataSetChanged();
        Timber.d("Data in adapter %s", shots.toString());
    }


    public class FeedItemViewHolder extends RecyclerView.ViewHolder {
        private final FeedItemView itemView;

        public FeedItemViewHolder(FeedItemView itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public void bindTo(Shot data,
                           DrawableRequestBuilder<String> shotBuilder,
                           boolean isAutoPlayGif,
                           DrawableRequestBuilder<String> avatarBuilder) {
            itemView.bindTo(data, shotBuilder, isAutoPlayGif, avatarBuilder);
        }

    }
}

@@ -228,44 +228,8 @@
 er;%0A
-import com.bumptech.glide.Priority;%0A
 impo
@@ -348,16 +348,58 @@
 .yuk.R;%0A
+import com.sinyuk.yuk.data.BaseRVAdapter;%0A
 import c
@@ -688,60 +688,26 @@
 nds 
-RecyclerView.Adapter%3CFeedsAdapter.FeedItemViewHolder
+BaseRVAdapter%3CShot
 %3E im
@@ -926,62 +926,8 @@
 der;
-%0A%0A    private List%3CShot%3E mDataSet = new ArrayList%3C%3E();
 %0A   
@@ -1062,24 +1062,48 @@
  dataSet) %7B%0A
+        super(dataSet);%0A
         Timb
@@ -1134,21 +1134,16 @@
         
-this.
 GIFBuild
@@ -1165,147 +1165,65 @@
 ager
-%0A                .fromString()%0A                .diskCacheStrategy(DiskCacheStrategy.SOURCE)%0A                .dontAnimate()%0A                
+.fromString().diskCacheStrategy(DiskCacheStrategy.SOURCE)
 .cen
@@ -1229,33 +1229,32 @@
 nterCrop();%0A
-%0A
         
 this.PNGBuil
@@ -1241,21 +1241,16 @@
         
-this.
 PNGBuild
@@ -1272,493 +1272,315 @@
 ager
-%0A                .fromString()%0A                .diskCacheStrategy(DiskCacheStrategy.RESULT)%0A                .dontAnimate()%0A                .centerCrop();%0A%0A        this.mDataSet = dataSet;%0A%0A        avatarBuilder = requestManager.fromString()%0A                .priority(Priority.NORMAL)%0A                .diskCacheStrategy(DiskCacheStrategy.RESULT)%0A                .centerCrop()%0A                .dontAnimate()%0A                .bitmapTransform(new CropCircleTransformation(context));%0A    %7D%0A
+.fromString().diskCacheStrategy(DiskCacheStrategy.RESULT).centerCrop();%0A        avatarBuilder = requestManager.fromString().diskCacheStrategy(DiskCacheStrategy.RESULT).centerCrop().bitmapTransform(new CropCircleTransformation(context));%0A    %7D%0A%0A    @Override%0A    protected void footerOnVisibleItem() %7B%0A%0A    %7D
 %0A%0A  
@@ -1598,32 +1598,37 @@
 %0A    public 
-FeedItem
+RecyclerView.
 ViewHolder o
@@ -1634,16 +1634,22 @@
 onCreate
+MyItem
 ViewHold
@@ -1857,93 +1857,8 @@
  %7D%0A%0A
-    @Override%0A    public int getItemCount() %7B%0A        return mDataSet.size();%0A    %7D%0A%0A
     
@@ -1889,16 +1889,22 @@
 d onBind
+MyItem
 ViewHold
@@ -1902,32 +1902,37 @@
 mViewHolder(
-FeedItem
+RecyclerView.
 ViewHolder h
@@ -1980,24 +1980,24 @@
  data = 
-mDataSet
+getAll()
 .get(pos
@@ -2040,39 +2040,62 @@
 )) %7B%0A           
+ ((FeedItemViewHolder)
  holder
+)
 .bindTo(data, GI
@@ -2163,23 +2163,46 @@
         
+ ((FeedItemViewHolder)
  holder
+)
 .bindTo(
@@ -2438,55 +2438,19 @@
     
-mDataSet = shots;%0A        notifyDataSetChanged(
+reset(shots
 );%0A 
@@ -2615,25 +2615,29 @@
 eedItemView 
-i
+feedI
 temView;%0A%0A  
@@ -2681,17 +2681,21 @@
 temView 
-i
+feedI
 temView)
@@ -2715,17 +2715,21 @@
   super(
-i
+feedI
 temView)
@@ -2747,17 +2747,21 @@
    this.
-i
+feedI
 temView 
@@ -2762,17 +2762,21 @@
 mView = 
-i
+feedI
 temView;
@@ -3033,17 +3033,21 @@
         
-i
+feedI
 temView.

Implement the base recycler view adapter