fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ConversationDecoration.java

87 lines
3.2 KiB
Java
Raw Normal View History

2018-09-08 11:30:58 +02:00
package fr.gouv.etalab.mastodon.drawers;
/* Copyright 2018 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
2018-10-25 14:09:41 +02:00
import android.support.annotation.NonNull;
2018-09-08 11:30:58 +02:00
import android.support.v4.content.ContextCompat;
2018-10-29 14:30:34 +01:00
import android.support.v4.content.res.ResourcesCompat;
2018-09-08 11:30:58 +02:00
import android.support.v7.widget.RecyclerView;
import android.view.View;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 08/09/2018.
* Adapter for thread decoration
*/
public class ConversationDecoration extends RecyclerView.ItemDecoration{
private Drawable divider;
private Context context;
2018-10-26 16:24:07 +02:00
public ConversationDecoration(Context context){
2018-10-30 13:46:24 +01:00
divider = ContextCompat.getDrawable(context,R.drawable.line_divider);
2018-09-08 11:30:58 +02:00
this.context = context;
}
@Override
2018-10-25 14:09:41 +02:00
public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int left = parent.getPaddingLeft() + (int)Helper.convertDpToPixel(12, context);
int right = left + (int)Helper.convertDpToPixel(4, context);
2018-09-08 11:30:58 +02:00
int childCount = parent.getChildCount();
2018-10-25 14:09:41 +02:00
int offSet = (int) Helper.convertDpToPixel(30, context);
2018-09-08 11:30:58 +02:00
for (int i = 0; i < childCount; i++) {
2018-10-25 14:09:41 +02:00
2018-09-08 11:30:58 +02:00
View child = parent.getChildAt(i);
2018-10-25 14:09:41 +02:00
StatusListAdapter adapter = (StatusListAdapter) parent.getAdapter();
2018-09-08 11:30:58 +02:00
int position = parent.getChildAdapterPosition(child);
2018-10-25 14:09:41 +02:00
2018-10-26 17:00:38 +02:00
assert adapter != null;
2018-09-08 11:30:58 +02:00
Status status = adapter.getItem(position);
2018-10-25 14:09:41 +02:00
2018-10-26 17:00:38 +02:00
int top, bottom;
2018-09-08 11:30:58 +02:00
if( status != null){
Status statusBefore = null;
if( position > 0)
statusBefore = adapter.getItem(position - 1);
top = (statusBefore != null && statusBefore.getId().equals(status.getIn_reply_to_id()))?
2018-10-25 14:09:41 +02:00
child.getTop(): (child.getTop() + offSet);
2018-09-08 11:30:58 +02:00
Status statusAfter = null;
if( adapter.getItemCount() > position+1)
statusAfter = adapter.getItem(position + 1);
2018-10-25 14:09:41 +02:00
bottom = (statusAfter != null && status.getId().equals(statusAfter.getIn_reply_to_id()) )?
child.getBottom():child.getTop()+offSet;
2018-10-26 16:24:07 +02:00
if( position == 0 && childCount > 1)
2018-10-25 14:09:41 +02:00
top = bottom - (int)Helper.convertDpToPixel(28, context);
2018-10-26 17:00:38 +02:00
divider.setBounds(left, top, right, bottom);
divider.draw(canvas);
2018-09-08 11:30:58 +02:00
}
2018-10-26 17:00:38 +02:00
2018-09-08 11:30:58 +02:00
}
2018-10-25 14:09:41 +02:00
2018-09-08 11:30:58 +02:00
}
}