/*
 *  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.support.v7.widget.StaggeredGridLayoutManager;
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);
    }

    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
    //ConstructorCallsOverridableMethod: it's OK
    public CalendarRecyclerView(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        setupCacheForMonthsOnScreenCount(3);
        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        setItemAnimator(null);
    }

    /**
     * Setups recycler cache for smooth scroll without lagging based on month that could be displayed on screen.
     *
     * @param maxMonthOnScreen Maximum months count on screen.
     */
    public void setupCacheForMonthsOnScreenCount(final int maxMonthOnScreen) {
        getRecycledViewPool().setMaxRecycledViews(CalendarAdapter.HEADER_ITEM_TYPE, HEADER_MAX_ELEMENTS_IN_A_ROW * (maxMonthOnScreen + 1));
        getRecycledViewPool().setMaxRecycledViews(CalendarAdapter.EMPTY_ITEM_TYPE, EMPTY_MAX_ELEMENTS_IN_A_ROW * (maxMonthOnScreen * 2 + 1));
        // we need such much views to prevent cache/gap animations of StaggeredGridLayoutManager
        getRecycledViewPool().setMaxRecycledViews(CalendarAdapter.DAY_ITEM_TYPE, DAY_MAX_ELEMENTS_IN_A_ROW * (maxMonthOnScreen * 5 + 1));
        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.");
    }

}
@@ -1675,120 +1675,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22PMD.ConstructorCallsOverridableMethod%22)%0A    //ConstructorCallsOverridableMethod: it's OK%0A
     
@@ -1834,16 +1834,189 @@
 Style);%0A
+        initialize();%0A    %7D%0A%0A    @SuppressWarnings(%22PMD.ConstructorCallsOverridableMethod%22)%0A    //ConstructorCallsOverridableMethod: it's OK%0A    private void initialize() %7B%0A
         
@@ -2265,16 +2265,57 @@
 _NONE);%0A
+        setLayoutManager(layoutManager);%0A
         

fix of layout manager