mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2024-12-24 17:50:04 +02:00
Fix issue #459 - Remove offset for compose view
This commit is contained in:
parent
23cecbf3c4
commit
7500f1cd7b
1 changed files with 88 additions and 50 deletions
|
@ -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,23 +38,44 @@ 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(
|
|
||||||
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) {
|
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
|
||||||
outRect.set(start, 0, 0, 0);
|
outRect.set(start, 0, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,62 +83,77 @@ public class DividerDecorationSimple extends RecyclerView.ItemDecoration {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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) {
|
if (indentation > 0) {
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setDither(false);
|
paint.setDither(false);
|
||||||
paint.setStyle(Paint.Style.STROKE);
|
paint.setStrokeWidth(Helper.convertDpToPixel(1.5F, _mContext));
|
||||||
paint.setStrokeWidth(Helper.convertDpToPixel(2F, _mContext));
|
paint.setStrokeCap(Paint.Cap.BUTT);
|
||||||
paint.setStrokeCap(Paint.Cap.ROUND);
|
paint.setStrokeJoin(Paint.Join.MITER);
|
||||||
paint.setStrokeJoin(Paint.Join.ROUND);
|
|
||||||
paint.setColor(ResourcesCompat.getColor(_mContext.getResources(), R.color.cyanea_accent, _mContext.getTheme()));
|
for (int j = 0; j < indentation; j++) {
|
||||||
if (indentation == 15) {
|
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(
|
paint.setPathEffect(new DashPathEffect(
|
||||||
new float[]{Helper.convertDpToPixel(3, _mContext), Helper.convertDpToPixel(3, _mContext)},
|
new float[]{Helper.convertDpToPixel(3, _mContext), Helper.convertDpToPixel(3, _mContext)},
|
||||||
0));
|
0));
|
||||||
|
bottomPx = bottomPx - view.getHeight() / 2F;
|
||||||
}
|
}
|
||||||
|
|
||||||
float startDp = 12 + 4 * (indentation - 1);
|
c.drawLine(startPx, view.getTop() - margin, startPx, bottomPx, paint);
|
||||||
if (i > 0) startDp = startDp - 6;
|
}
|
||||||
|
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;
|
float startDp = 6 * fontScale * (indentation - 1) + 6 * fontScale;
|
||||||
if (i > 0) endDp = endDp + 4;
|
float centerPx = view.getBottom() - view.getHeight() / 2F;
|
||||||
|
float endDp = startDp + 12 * fontScale;
|
||||||
|
float endPx = Helper.convertDpToPixel(endDp, _mContext);
|
||||||
|
|
||||||
float startPx = Helper.convertDpToPixel(startDp, _mContext);
|
float startPx = Helper.convertDpToPixel(startDp, _mContext);
|
||||||
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
|
if (parent.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
|
||||||
startPx = c.getWidth() - startPx;
|
startPx = c.getWidth() - startPx;
|
||||||
|
endPx = c.getWidth() - endPx;
|
||||||
}
|
}
|
||||||
|
|
||||||
float topPx = view.getTop() - Helper.convertDpToPixel(12, _mContext);
|
|
||||||
|
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
View aboveView = parent.getChildAt(i - 1);
|
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;
|
c.drawLine(startPx, centerPx, endPx, centerPx, paint);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue