package me.zsj.interessant;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import com.jakewharton.rxbinding.support.v7.widget.RxRecyclerView;

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

import me.zsj.interessant.api.InterestingApi;
import me.zsj.interessant.base.ToolbarActivity;
import me.zsj.interessant.interesting.InterestingAdapter;
import me.zsj.interessant.model.ItemList;
import me.zsj.interessant.rx.ErrorAction;
import me.zsj.interessant.rx.RxScroller;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author zsj
 */

public class VideoListActivity extends ToolbarActivity {

    private RecyclerView list;

    private InterestingAdapter adapter;
    private InterestingApi api;

    private List<ItemList> items = new ArrayList<>();

    private int start;
    private int id;
    private boolean trending;
    private boolean newest;

    @Override
    public int providerLayoutId() {
        return R.layout.video_list_activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        list = (RecyclerView) findViewById(R.id.list);

        api = InteressantFactory.getRetrofit().createApi(InterestingApi.class);

        id = getIntent().getExtras().getInt("id");
        trending = getIntent().getBooleanExtra("trending", false);
        newest = getIntent().getBooleanExtra("newest", false);

        if (trending) ab.setTitle("Trending");
        else if (newest) ab.setTitle("Newest");

        adapter = new InterestingAdapter(this, items);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);

        list.setAdapter(adapter);

        RxRecyclerView.scrollStateChanges(list)
                .compose(bindToLifecycle())
                .compose(RxScroller.scrollTransformer(layoutManager,
                        adapter, items))
                .subscribe(newState -> {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        start += 10;
                        loadVideos();
                    }
                });

        loadVideos(true);
    }

    @Override
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
        list.scheduleLayoutAnimation();
    }

    private void loadVideos() {
        loadVideos(false);
    }

    private void loadVideos(boolean clean) {
        String strategy = null;
        if (trending) strategy = "mostPopular";
        else if (newest) strategy = "date";

        api.videoList(id, start, strategy)
                .compose(bindToLifecycle())
                .filter(interesting -> interesting != null)
                .filter(interesting -> interesting.itemList != null)
                .map(interesting -> interesting.itemList)
                .doOnNext(itemLists -> { if (clean) items.clear(); })
                .doOnNext(itemLists -> items.addAll(itemLists))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(itemLists ->
                        adapter.notifyItemRangeInserted(start, items.size()),
                        ErrorAction.errorAction(this));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -1045,16 +1045,52 @@
  newest;
+%0A    private boolean firstIn = true;
 %0A%0A    @O
@@ -2500,24 +2500,51 @@
 Complete();%0A
+        if (firstIn) %7B%0A    
         list
@@ -2567,24 +2567,63 @@
 nimation();%0A
+            firstIn = false;%0A        %7D%0A
     %7D%0A%0A    p

Fix item layout animation