lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
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
package com.beloo.widget.chipslayoutmanager.cache;
 
import android.graphics.Rect;
import android.os.Parcelable;
import android.util.Log;
import android.util.Pair;
import android.view.View;
 
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
 
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
 
class ViewCacheStorage implements IViewCacheStorage {
    private static final String TAG = ViewCacheStorage.class.getSimpleName();
    private static final int SIZE_MAX_CACHE = 1000;
 
    private RecyclerView.LayoutManager layoutManager;
    private NavigableSet<Integer> startsRow = new TreeSet<>();
    private NavigableSet<Integer> endsRow = new TreeSet<>();
    private int maxCacheSize = SIZE_MAX_CACHE;
    private boolean isCachingEnabled;
 
    ViewCacheStorage(RecyclerView.LayoutManager layoutManager) {
        this.layoutManager = layoutManager;
        isCachingEnabled = true;
    }
 
    public void setMaxCacheSize(int maxCacheSize) {
        this.maxCacheSize = maxCacheSize;
    }
 
    @Override
    public boolean isCachingEnabled() {
        return isCachingEnabled;
    }
 
    @Override
    public int getStartOfRow(int positionInRow) {
        Integer integer = startsRow.floor(positionInRow);
        if (integer == null) {
            integer = positionInRow;
        }
        return integer;
    }
 
    @Override
    public boolean isPositionEndsRow(int position) {
        return endsRow.contains(position);
    }
 
    @Override
    public boolean isPositionStartsRow(int position) {
        return startsRow.contains(position);
    }
 
    @Override
    public void setCachingEnabled(boolean isEnabled) {
        if (isCachingEnabled == isEnabled) return;
        Log.i(TAG, isEnabled ? "caching enabled" : "caching disabled");
        isCachingEnabled = isEnabled;
    }
 
    //todo test max size cache reached
    private void checkCacheSizeReached() {
        if (startsRow.size() > maxCacheSize) {
            startsRow.remove(startsRow.first());
        }
        if (endsRow.size() > maxCacheSize) {
            endsRow.remove(endsRow.first());
        }
    }
 
    @Override
    public void storeRow(List<Pair<Rect, View>> row) {
        if (isCachingEnabled && !row.isEmpty()) {
 
            Pair<Rect, View> firstPair = row.get(0);
            Pair<Rect, View> secondPair = row.get(row.size()-1);
 
            int startPosition = layoutManager.getPosition(firstPair.second);
            int endPosition = layoutManager.getPosition(secondPair.second);
 
            checkCacheSizeReached();
 
            startsRow.add(startPosition);
            endsRow.add(endPosition);
        }
    }
 
    @Override
    public boolean isInCache(int position) {
        return startsRow.ceiling(position) != null || endsRow.ceiling(position) != null;
    }
 
    @Override
    public void purge() {
        startsRow.clear();
        endsRow.clear();
    }
 
 
    @Override
    public void purgeCacheToPosition(int position) {
        if (isCacheEmpty()) return;
        Log.d(TAG, "cache purged to position " + position);
        Iterator<Integer> removeIterator = startsRow.headSet(position).iterator();
        while (removeIterator.hasNext()) {
            removeIterator.next();
            removeIterator.remove();
        }
 
        removeIterator = endsRow.headSet(position).iterator();
        while (removeIterator.hasNext()) {
            removeIterator.next();
            removeIterator.remove();
        }
    }
 
    @Override
 
    public Integer getLastCachePosition() {
        if (isCacheEmpty()) return null;
        return endsRow.last();
    }
 
    @Override
    public boolean isCacheEmpty() {
        return endsRow.isEmpty();
    }
 
 
    @Override
    public void purgeCacheFromPosition(int position) {
        if (isCacheEmpty()) return;
 
        Iterator<Integer> removeIterator = startsRow.tailSet(position, true).iterator();
        while (removeIterator.hasNext()) {
            removeIterator.next();
            removeIterator.remove();
        }
        Integer previous = startsRow.lower(position);
        previous = previous == null? position : previous;
 
        //we should also remove previous end row cache to guarantee consistency
        removeIterator = endsRow.tailSet(previous, true).iterator();
        while (removeIterator.hasNext()) {
            removeIterator.next();
            removeIterator.remove();
        }
    }
 
    @Override
    public Parcelable onSaveInstanceState() {
        return new CacheParcelableContainer(startsRow, endsRow);
    }
 
    public void onRestoreInstanceState(@Nullable Parcelable parcelable) {
        if (parcelable == null) return;
        if (!(parcelable instanceof CacheParcelableContainer)) throw new IllegalStateException("wrong parcelable passed");
        CacheParcelableContainer container = (CacheParcelableContainer) parcelable;
        startsRow = container.getStartsRow();
        endsRow = container.getEndsRow();
    }
}