package com.zulip.android.viewholders;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

import com.zulip.android.activities.RecyclerMessageAdapter;

public class HeaderSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int verticalMargin;

    private int toolbarHeight;

    public HeaderSpaceItemDecoration(int verticalMargin, Context context) {
        this.verticalMargin = verticalMargin;
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            toolbarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {

        int position = parent.getChildAdapterPosition(view);

        int viewType = parent.getAdapter().getItemViewType(position);
        if (viewType == RecyclerMessageAdapter.VIEWTYPE_MESSAGE_HEADER && position != 0) {
            outRect.top = verticalMargin;
        } else if (viewType == RecyclerMessageAdapter.VIEWTYPE_HEADER) {
            outRect.top = toolbarHeight;
        }
    }
}

@@ -1016,16 +1016,70 @@
 (view);%0A
+        int size = parent.getAdapter().getItemCount();
 %0A       
@@ -1394,20 +1394,90 @@
 ;%0A        %7D%0A
+        outRect.bottom = (position == size - 2) ? verticalMargin : 0;%0A
     %7D%0A%7D%0A

Add margin to the bottommost message