lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.beloo.widget.chipslayoutmanager.layouter;
 
import android.graphics.Rect;
import android.view.View;
 
import androidx.recyclerview.widget.RecyclerView;
 
import com.beloo.widget.chipslayoutmanager.ChildViewsIterable;
import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
 
abstract class Square implements ICanvas {
 
    RecyclerView.LayoutManager lm;
    private ChildViewsIterable childViews;
 
    /**
     * highest view in layout. Have always actual value, because it set in {@link ChipsLayoutManager#onLayoutChildren}
     */
    private View topView;
    /**
     * lowest view in layout. Have always actual value, because it set in {@link ChipsLayoutManager#onLayoutChildren}
     */
    private View bottomView;
 
    /**
     * The view have placed in the closest to the left border. Have always actual value, because it set in {@link ChipsLayoutManager#onLayoutChildren}
     */
    private View leftView;
 
    /** The view have placed in the closest to the right border. Have always actual value, because it set in {@link ChipsLayoutManager#onLayoutChildren} */
    private View rightView;
 
    /** minimal position visible on screen*/
    private Integer minPositionOnScreen;
    private Integer maxPositionOnScreen;
 
    private boolean isFirstItemAdded;
 
    Square(RecyclerView.LayoutManager lm) {
        this.lm = lm;
        childViews = new ChildViewsIterable(lm);
    }
 
    @Override
    public Rect getCanvasRect() {
        return new Rect(getCanvasLeftBorder(), getCanvasTopBorder(), getCanvasRightBorder(), getCanvasBottomBorder());
    }
 
    @Override
    public Rect getViewRect(View view) {
        int left = lm.getDecoratedLeft(view);
        int top = lm.getDecoratedTop(view);
        int right = lm.getDecoratedRight(view);
        int bottom = lm.getDecoratedBottom(view);
        return new Rect(left, top, right, bottom);
    }
 
    @Override
    public boolean isInside(Rect rectCandidate) {
        //intersection changes rect!!!
        Rect intersect = new Rect(rectCandidate);
        return getCanvasRect().intersect(intersect);
    }
 
    @Override
    public boolean isInside(View viewCandidate) {
        return isInside(getViewRect(viewCandidate));
    }
 
    @Override
    public boolean isFullyVisible(View view) {
        Rect rect = getViewRect(view);
        return isFullyVisible(rect);
    }
 
    @Override
    public boolean isFullyVisible(Rect rect) {
        return rect.top >= getCanvasTopBorder()
                && rect.bottom <= getCanvasBottomBorder()
                && rect.left >= getCanvasLeftBorder()
                && rect.right <= getCanvasRightBorder();
    }
 
    /**
     * find highest & lowest views among visible attached views
     */
    @Override
    public void findBorderViews() {
        topView = null;
        bottomView = null;
        leftView = null;
        rightView = null;
        minPositionOnScreen = RecyclerView.NO_POSITION;
        maxPositionOnScreen = RecyclerView.NO_POSITION;
 
        isFirstItemAdded = false;
 
        if (lm.getChildCount() > 0) {
            View initView = lm.getChildAt(0);
            topView = initView;
            bottomView = initView;
            leftView = initView;
            rightView = initView;
 
            for (View view : childViews) {
                int position = lm.getPosition(view);
 
                if (!isInside(view)) continue;
 
                if (lm.getDecoratedTop(view) < lm.getDecoratedTop(topView)) {
                    topView = view;
                }
 
                if (lm.getDecoratedBottom(view) > lm.getDecoratedBottom(bottomView)) {
                    bottomView = view;
                }
 
                if (lm.getDecoratedLeft(view) < lm.getDecoratedLeft(leftView)) {
                    leftView = view;
                }
 
                if (lm.getDecoratedRight(view) > lm.getDecoratedRight(rightView)) {
                    rightView = view;
                }
 
                if (minPositionOnScreen == RecyclerView.NO_POSITION || position < minPositionOnScreen) {
                    minPositionOnScreen = position;
                }
 
                if (maxPositionOnScreen == RecyclerView.NO_POSITION || position > maxPositionOnScreen) {
                    maxPositionOnScreen = position;
                }
 
                if (position == 0) {
                    isFirstItemAdded = true;
                }
            }
        }
    }
 
    @Override
    public View getTopView() {
        return topView;
    }
 
    @Override
    public View getBottomView() {
        return bottomView;
    }
 
    @Override
    public View getLeftView() {
        return leftView;
    }
 
    @Override
    public View getRightView() {
        return rightView;
    }
 
    @Override
    public Integer getMinPositionOnScreen() {
        return minPositionOnScreen;
    }
 
    @Override
    public Integer getMaxPositionOnScreen() {
        return maxPositionOnScreen;
    }
 
    @Override
    public boolean isFirstItemAdded() {
        return isFirstItemAdded;
    }
}