Fix issue #459 - Remove offset for compose view

This commit is contained in:
Thomas 2022-11-16 14:36:20 +01:00
parent 23cecbf3c4
commit 7500f1cd7b

View file

@ -15,17 +15,19 @@ package app.fedilab.android.helper;
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Arrays;
import java.util.List;
import app.fedilab.android.R;
@ -36,23 +38,44 @@ public class DividerDecorationSimple extends RecyclerView.ItemDecoration {
private final Context _mContext;
private final List<Status> statusList;
private final float fontScale;
private final int indentationMax;
private final List<Integer> colorList = Arrays.asList(
R.color.decoration_1,
R.color.decoration_2,
R.color.decoration_3,
R.color.decoration_4,
R.color.decoration_5,
R.color.decoration_6,
R.color.decoration_7,
R.color.decoration_8,
R.color.decoration_9,
R.color.decoration_10,
R.color.decoration_11,
R.color.decoration_12
);
public DividerDecorationSimple(Context context, List<Status> statuses) {
_mContext = context;
statusList = statuses;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_mContext);
fontScale = prefs.getFloat(_mContext.getString(R.string.SET_FONT_SCALE), 1.1f);
indentationMax = prefs.getInt(_mContext.getString(R.string.SET_MAX_INDENTATION), 5);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
ComposeAdapter composeAdapter = ((ComposeAdapter) parent.getAdapter());
if (composeAdapter != null && composeAdapter.getItemCount() > position && position >= 0) {
if (composeAdapter != null && composeAdapter.getItemCount() > position && position > 0) {
Status status = composeAdapter.getItem(position);
if (status != null) {
int start = (int) Helper.convertDpToPixel(
4 * CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, 15),
_mContext);
int start = (int) Helper.convertDpToPixel(
6 * fontScale * CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, indentationMax),
_mContext);
if (status.id == null || status.id.startsWith("@fedilab_compose_")) {
start = 0;
}
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
outRect.set(start, 0, 0, 0);
} else {
@ -60,62 +83,77 @@ public class DividerDecorationSimple extends RecyclerView.ItemDecoration {
}
}
}
}
@Override
public void onDraw(@NonNull Canvas c, RecyclerView parent, @NonNull RecyclerView.State state) {
int margin = (int) Helper.convertDpToPixel(12, _mContext);
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(view);
ComposeAdapter composeAdapter = ((ComposeAdapter) parent.getAdapter());
if (composeAdapter != null && composeAdapter.getItemCount() > position && position >= 0) {
if (composeAdapter != null && position >= 0) {
Status status = composeAdapter.getItem(position);
if (status != null) {
int indentation = CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, 15);
int indentation = Math.min(
CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, indentationMax),
indentationMax);
if (indentation > 0) {
Paint paint = new Paint();
paint.setDither(false);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(Helper.convertDpToPixel(2F, _mContext));
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setColor(ResourcesCompat.getColor(_mContext.getResources(), R.color.cyanea_accent, _mContext.getTheme()));
if (indentation == 15) {
paint.setStrokeWidth(Helper.convertDpToPixel(1.5F, _mContext));
paint.setStrokeCap(Paint.Cap.BUTT);
paint.setStrokeJoin(Paint.Join.MITER);
for (int j = 0; j < indentation; j++) {
float startPx = Helper.convertDpToPixel(6 * fontScale + 6 * fontScale * j, _mContext);
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
startPx = c.getWidth() - startPx;
float bottomPx = view.getBottom();
int color;
if (j >= colorList.size()) {
color = colorList.get(j - colorList.size());
} else {
color = colorList.get(j);
}
paint.setColor(ResourcesCompat.getColor(_mContext.getResources(), color, _mContext.getTheme()));
if (j == indentationMax - 1) {
paint.setPathEffect(new DashPathEffect(
new float[]{Helper.convertDpToPixel(3, _mContext), Helper.convertDpToPixel(3, _mContext)},
0));
bottomPx = bottomPx - view.getHeight() / 2F;
}
float startDp = 12 + 4 * (indentation - 1);
if (i > 0) startDp = startDp - 6;
c.drawLine(startPx, view.getTop() - margin, startPx, bottomPx, paint);
}
int color;
if (indentation - 1 >= colorList.size()) {
color = colorList.get(indentation - 1 - colorList.size());
} else {
color = colorList.get(indentation - 1);
}
paint.setColor(ResourcesCompat.getColor(_mContext.getResources(), color, _mContext.getTheme()));
float endDp = startDp + 4;
if (i > 0) endDp = endDp + 4;
float startDp = 6 * fontScale * (indentation - 1) + 6 * fontScale;
float centerPx = view.getBottom() - view.getHeight() / 2F;
float endDp = startDp + 12 * fontScale;
float endPx = Helper.convertDpToPixel(endDp, _mContext);
float startPx = Helper.convertDpToPixel(startDp, _mContext);
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
startPx = c.getWidth() - startPx;
endPx = c.getWidth() - endPx;
}
float topPx = view.getTop() - Helper.convertDpToPixel(12, _mContext);
if (i > 0) {
View aboveView = parent.getChildAt(i - 1);
topPx = topPx - (aboveView.getHeight() / 2F);
float aboveViewLineTopPx = (aboveView.getTop() + aboveView.getHeight() / 2F) + Helper.convertDpToPixel(0.75F, _mContext);
float aboveViewLineBottomPx = parent.getChildAt(i - 1).getBottom();
c.drawLine(startPx, aboveViewLineTopPx, startPx, aboveViewLineBottomPx, paint);
}
float bottomPx = view.getBottom() - view.getHeight() / 2F;
float endPx = Helper.convertDpToPixel(endDp, _mContext);
Path path = new Path();
path.moveTo(startPx, topPx);
path.lineTo(startPx, bottomPx);
path.lineTo(endPx, bottomPx);
c.drawPath(path, paint);
}
c.drawLine(startPx, centerPx, endPx, centerPx, paint);
}
}
}