1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package ml.docilealligator.infinityforreddit.customviews;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import ml.docilealligator.infinityforreddit.utils.Utils;
public class CommentIndentationView extends LinearLayout {
private final Paint paint;
private int level;
private int[] colors;
private ArrayList<Integer> startXs;
private final int spacing;
private int pathWidth;
private boolean showOnlyOneDivider = false;
public CommentIndentationView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setSaveEnabled(true);
setWillNotDraw(false);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
pathWidth = (int) Utils.convertDpToPixel(2, context);
spacing = pathWidth * 6;
paint.setStrokeWidth(pathWidth);
paint.setStyle(Paint.Style.STROKE);
startXs = new ArrayList<>();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
startXs.clear();
for (int i = 0; i < level; i++) {
startXs.add(spacing * (i + 1) + pathWidth);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (showOnlyOneDivider) {
if (startXs.size() > 0) {
paint.setColor(colors[(startXs.size() - 1) % 7]);
canvas.drawLine(level * pathWidth, 0, level * pathWidth, getHeight(), paint);
}
} else {
for (int i = 0; i < startXs.size(); i++) {
paint.setColor(colors[i % 7]);
canvas.drawLine(startXs.get(i), 0, startXs.get(i), getHeight(), paint);
}
}
}
@Nullable
@Override
protected Parcelable onSaveInstanceState() {
Parcelable parcelable = super.onSaveInstanceState();
SavedState myState = new SavedState(parcelable);
myState.startXs = this.startXs;
myState.colors = this.colors;
return parcelable;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
this.startXs = savedState.startXs;
this.colors = savedState.colors;
invalidate();
}
public void setLevelAndColors(int level, int[] colors) {
this.colors = colors;
this.level = level;
if (level > 0) {
int indentationSpacing = showOnlyOneDivider ? pathWidth * level : level * spacing + pathWidth;
setPaddingRelative(indentationSpacing, 0, pathWidth, 0);
} else {
setPaddingRelative(0, 0, 0, 0);
}
invalidate();
}
public void setShowOnlyOneDivider(boolean showOnlyOneDivider) {
this.showOnlyOneDivider = showOnlyOneDivider;
if (showOnlyOneDivider) {
pathWidth = (int) Utils.convertDpToPixel(4, getContext());
}
}
private static class SavedState extends BaseSavedState {
ArrayList<Integer> startXs;
int[] colors;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
startXs = new ArrayList<>();
in.readList(startXs, Integer.class.getClassLoader());
colors = in.createIntArray();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeList(startXs);
out.writeIntArray(colors);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
|