package net.squanchy.search.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import net.squanchy.schedule.view.ScheduleViewPagerAdapter;
import net.squanchy.search.SearchResults;
import net.squanchy.speaker.domain.view.Speaker;

public class SearchRecyclerView extends RecyclerView {

    private SearchAdapter adapter;

    private static final int COLUMNS_COUNT = 4;

    public SearchRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        setItemAnimator(null);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), COLUMNS_COUNT);
        setLayoutManager(gridLayoutManager);
        adapter = new SearchAdapter(getContext());
        setAdapter(adapter);
        setClipToPadding(false);
    }

    public void updateWith(SearchResults searchResults, OnSearchResultClickListener listener) {
        if (getAdapter() == null) {
            super.setAdapter(adapter);
        }

        adapter.updateWith(searchResults, listener);

        GridLayoutManager layoutManager = (GridLayoutManager) getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = adapter.createSpanSizeLookup(COLUMNS_COUNT);
        layoutManager.setSpanSizeLookup(spanSizeLookup);
    }

    public interface OnSearchResultClickListener extends ScheduleViewPagerAdapter.OnEventClickedListener {

        void onSpeakerClicked(Speaker speaker);
    }
}

@@ -79,35 +79,103 @@
 oid.
-support.annotation.Nullable
+graphics.Rect;%0Aimport android.support.annotation.Nullable;%0Aimport android.support.annotation.Px
 ;%0Aim
@@ -305,16 +305,65 @@
 uteSet;%0A
+import android.view.View;%0A%0Aimport net.squanchy.R;
 %0Aimport 
@@ -505,16 +505,63 @@
 Speaker;
+%0Aimport net.squanchy.support.widget.CardLayout;
 %0A%0Apublic
@@ -1069,24 +1069,25 @@
 hInflate();%0A
+%0A
         Grid
@@ -1213,24 +1213,323 @@
 utManager);%0A
+%0A        int horizontalSpacing = getResources().getDimensionPixelSize(R.dimen.card_horizontal_margin);%0A        int verticalSpacing = getResources().getDimensionPixelSize(R.dimen.card_vertical_margin);%0A        addItemDecoration(new CardOnlySpacingItemDecorator(horizontalSpacing, verticalSpacing));%0A%0A
         adap
@@ -2282,11 +2282,1128 @@
 );%0A    %7D
+%0A%0A    private static class CardOnlySpacingItemDecorator extends ItemDecoration %7B%0A%0A        @Px%0A        private final int horizontalSpacing;%0A        @Px%0A        private final int verticalSpacing;%0A%0A        private CardOnlySpacingItemDecorator(@Px int horizontalSpacing, @Px int verticalSpacing) %7B%0A            this.horizontalSpacing = horizontalSpacing;%0A            this.verticalSpacing = verticalSpacing;%0A        %7D%0A%0A        @Override%0A        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) %7B%0A            if (!(view instanceof CardLayout)) %7B%0A                return;%0A            %7D%0A%0A            int position = parent.getChildAdapterPosition(view);%0A            int count = state.getItemCount();%0A%0A            int topSpacing = verticalSpacing / 2;%0A            int bottomSpacing = topSpacing;%0A            if (position == 0) %7B%0A                topSpacing = verticalSpacing;%0A            %7D else if (position == count - 1) %7B%0A                bottomSpacing = verticalSpacing;%0A            %7D%0A%0A            outRect.set(horizontalSpacing, topSpacing, horizontalSpacing, bottomSpacing);%0A        %7D%0A    %7D
 %0A%7D%0A

Apply correct item spacing to cards in results