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
package com.beloo.widget.chipslayoutmanager;
 
import android.content.res.Configuration;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;
 
import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
 
import com.beloo.widget.chipslayoutmanager.anchor.AnchorViewState;
import com.beloo.widget.chipslayoutmanager.cache.CacheParcelableContainer;
 
class ParcelableContainer implements Parcelable {
 
    private AnchorViewState anchorViewState;
    private SparseArray<Object> orientationCacheMap = new SparseArray<>();
    private SparseArray<Object> cacheNormalizationPositionMap = new SparseArray<>();
    //store previous orientation
    private int orientation;
 
    ParcelableContainer() {
        //initial values. We should normalize cache when scrolled to zero in case first time of changing layoutOrientation state
        cacheNormalizationPositionMap.put(Configuration.ORIENTATION_PORTRAIT, 0);
        cacheNormalizationPositionMap.put(Configuration.ORIENTATION_LANDSCAPE, 0);
    }
 
    void putAnchorViewState(AnchorViewState anchorViewState) {
        this.anchorViewState = anchorViewState;
    }
 
    AnchorViewState getAnchorViewState() {
        return anchorViewState;
    }
 
    @DeviceOrientation
    int getOrientation() {
        return orientation;
    }
 
    void putOrientation(@DeviceOrientation int orientation) {
        this.orientation = orientation;
    }
 
    @SuppressWarnings("unchecked")
    private ParcelableContainer(Parcel parcel) {
        anchorViewState = AnchorViewState.CREATOR.createFromParcel(parcel);
        orientationCacheMap = parcel.readSparseArray(CacheParcelableContainer.class.getClassLoader());
        cacheNormalizationPositionMap = parcel.readSparseArray(Integer.class.getClassLoader());
        orientation = parcel.readInt();
    }
 
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        anchorViewState.writeToParcel(parcel, i);
        parcel.writeSparseArray(orientationCacheMap);
        parcel.writeSparseArray(cacheNormalizationPositionMap);
        parcel.writeInt(orientation);
    }
 
    void putPositionsCache(@DeviceOrientation int orientation, Parcelable parcelable) {
        orientationCacheMap.put(orientation, parcelable);
    }
 
    void putNormalizationPosition(@DeviceOrientation int orientation, @Nullable Integer normalizationPosition) {
        cacheNormalizationPositionMap.put(orientation, normalizationPosition);
    }
 
    @Nullable
    Parcelable getPositionsCache(@DeviceOrientation int orientation) {
        return (Parcelable) orientationCacheMap.get(orientation);
    }
 
    @IntRange(from = 0)
    @Nullable
    Integer getNormalizationPosition(@DeviceOrientation int orientation) {
        return (Integer) cacheNormalizationPositionMap.get(orientation);
    }
 
    public static final Creator<ParcelableContainer> CREATOR = new Creator<ParcelableContainer>() {
 
        @Override
        public ParcelableContainer createFromParcel(Parcel parcel) {
            return new ParcelableContainer(parcel);
        }
 
        @Override
        public ParcelableContainer[] newArray(int i) {
            return new ParcelableContainer[i];
        }
    };
 
    @Override
    public int describeContents() {
        return 0;
    }
 
}