Fix issue #459 - Remove offset for compose view

pull/483/head
Thomas 2 years ago
parent 23cecbf3c4
commit 7500f1cd7b

@ -15,17 +15,19 @@ package app.fedilab.android.helper;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.DashPathEffect; import android.graphics.DashPathEffect;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect; import android.graphics.Rect;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.Arrays;
import java.util.List; import java.util.List;
import app.fedilab.android.R; import app.fedilab.android.R;
@ -36,86 +38,122 @@ public class DividerDecorationSimple extends RecyclerView.ItemDecoration {
private final Context _mContext; private final Context _mContext;
private final List<Status> statusList; 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) { public DividerDecorationSimple(Context context, List<Status> statuses) {
_mContext = context; _mContext = context;
statusList = statuses; 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 @Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition(); int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
ComposeAdapter composeAdapter = ((ComposeAdapter) parent.getAdapter()); 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); Status status = composeAdapter.getItem(position);
if (status != null) {
int start = (int) Helper.convertDpToPixel( int start = (int) Helper.convertDpToPixel(
4 * CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, 15), 6 * fontScale * CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, indentationMax),
_mContext); _mContext);
if (status.id == null || status.id.startsWith("@fedilab_compose_")) {
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) { start = 0;
outRect.set(start, 0, 0, 0); }
} else { if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
outRect.set(0, 0, start, 0); outRect.set(start, 0, 0, 0);
} } else {
outRect.set(0, 0, start, 0);
} }
} }
} }
@Override @Override
public void onDraw(@NonNull Canvas c, RecyclerView parent, @NonNull RecyclerView.State state) { 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++) { for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i); View view = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(view); int position = parent.getChildAdapterPosition(view);
ComposeAdapter composeAdapter = ((ComposeAdapter) parent.getAdapter()); ComposeAdapter composeAdapter = ((ComposeAdapter) parent.getAdapter());
if (composeAdapter != null && composeAdapter.getItemCount() > position && position >= 0) { if (composeAdapter != null && position >= 0) {
Status status = composeAdapter.getItem(position); Status status = composeAdapter.getItem(position);
if (status != null) { int indentation = Math.min(
int indentation = CommentDecorationHelper.getIndentation(status.in_reply_to_id, statusList, 15); 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.setPathEffect(new DashPathEffect(
new float[]{Helper.convertDpToPixel(3, _mContext), Helper.convertDpToPixel(3, _mContext)},
0));
}
float startDp = 12 + 4 * (indentation - 1);
if (i > 0) startDp = startDp - 6;
float endDp = startDp + 4; if (indentation > 0) {
if (i > 0) endDp = endDp + 4; Paint paint = new Paint();
paint.setDither(false);
paint.setStrokeWidth(Helper.convertDpToPixel(1.5F, _mContext));
paint.setStrokeCap(Paint.Cap.BUTT);
paint.setStrokeJoin(Paint.Join.MITER);
float startPx = Helper.convertDpToPixel(startDp, _mContext); for (int j = 0; j < indentation; j++) {
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { float startPx = Helper.convertDpToPixel(6 * fontScale + 6 * fontScale * j, _mContext);
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
startPx = c.getWidth() - startPx; startPx = c.getWidth() - startPx;
}
float topPx = view.getTop() - Helper.convertDpToPixel(12, _mContext);
if (i > 0) { float bottomPx = view.getBottom();
View aboveView = parent.getChildAt(i - 1); int color;
topPx = topPx - (aboveView.getHeight() / 2F); 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 bottomPx = view.getBottom() - view.getHeight() / 2F; c.drawLine(startPx, view.getTop() - margin, startPx, bottomPx, paint);
float endPx = Helper.convertDpToPixel(endDp, _mContext); }
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 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);
Path path = new Path(); float startPx = Helper.convertDpToPixel(startDp, _mContext);
path.moveTo(startPx, topPx); if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
path.lineTo(startPx, bottomPx); startPx = c.getWidth() - startPx;
path.lineTo(endPx, bottomPx); endPx = c.getWidth() - endPx;
}
c.drawPath(path, paint); if (i > 0) {
View aboveView = parent.getChildAt(i - 1);
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);
} }
c.drawLine(startPx, centerPx, endPx, centerPx, paint);
} }
} }
} }

Loading…
Cancel
Save