/*
 *  Copyright (c) 2016 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
 *
 *  This file is part of RoboSwag library.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package ru.touchin.templates.calendar;

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

import ru.touchin.roboswag.core.log.Lc;

/**
 * Created by Ilia Kurtov on 17/03/2016.
 * Specific {@link RecyclerView} that works with {@link CalendarAdapter}. It optimizes speed of the calendar.
 */
public class CalendarRecyclerView extends RecyclerView {

    private static final int HEADER_MAX_ELEMENTS_IN_A_ROW = 1;
    private static final int EMPTY_MAX_ELEMENTS_IN_A_ROW = 6;
    private static final int DAY_MAX_ELEMENTS_IN_A_ROW = 7;

    public CalendarRecyclerView(@NonNull final Context context) {
        this(context, null);
    }

    public CalendarRecyclerView(@NonNull final Context context, @Nullable final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CalendarRecyclerView(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        getRecycledViewPool().setMaxRecycledViews(CalendarAdapter.HEADER_ITEM_TYPE, HEADER_MAX_ELEMENTS_IN_A_ROW * 3);
        getRecycledViewPool().setMaxRecycledViews(CalendarAdapter.EMPTY_ITEM_TYPE, EMPTY_MAX_ELEMENTS_IN_A_ROW * 3);
        getRecycledViewPool().setMaxRecycledViews(CalendarAdapter.DAY_ITEM_TYPE, DAY_MAX_ELEMENTS_IN_A_ROW * 3);
        setItemViewCacheSize(0);
    }

    /**
     * Used to set adapter that extends from {@link CalendarAdapter}.
     *
     * @param calendarAdapter Adapter that extends from {@link CalendarAdapter}.
     */
    // This suppress needed for using only specific CalendarAdapter}
    @SuppressWarnings("PMD.UselessOverridingMethod")
    public void setAdapter(@NonNull final CalendarAdapter calendarAdapter) {
        super.setAdapter(calendarAdapter);
    }

    @Override
    @Deprecated
    public void setAdapter(@NonNull final Adapter adapter) {
        Lc.assertion("Unsupported adapter class. Use CalendarAdapter instead.");
    }

}
@@ -887,16 +887,77 @@
 erView;%0A
+import android.support.v7.widget.StaggeredGridLayoutManager;%0A
 import a
@@ -982,16 +982,16 @@
 uteSet;%0A
-
 %0Aimport 
@@ -1834,16 +1834,590 @@
 Style);%0A
+        setupCacheForMonthsOnScreenCount(3);%0A        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL);%0A        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);%0A        setItemAnimator(null);%0A    %7D%0A%0A    /**%0A     * Setups recycler cache for smooth scroll without lagging based on month that could be displayed on screen.%0A     *%0A     * @param maxMonthOnScreen Maximum months count on screen.%0A     */%0A    public void setupCacheForMonthsOnScreenCount(final int maxMonthOnScreen) %7B%0A
         
@@ -2515,33 +2515,54 @@
 ENTS_IN_A_ROW * 
-3
+(maxMonthOnScreen + 1)
 );%0A        getRe
@@ -2653,35 +2653,157 @@
 ENTS_IN_A_ROW * 
-3);
+(maxMonthOnScreen * 2 + 1));%0A        // we need such much views to prevent cache/gap animations of StaggeredGridLayoutManager
 %0A        getRecy
@@ -2896,17 +2896,42 @@
 A_ROW * 
-3
+(maxMonthOnScreen * 5 + 1)
 );%0A     

fix of unnecessary moves of adapter on it's items changes