/
SPAdapter.java
189 lines (159 loc) · 6.88 KB
/
SPAdapter.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.sudoplz.rnsynchronouslistmanager.Views.List;
//import android.support.v4.widget.NestedScrollView;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
//import android.widget.TextView;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import com.sudoplz.rnsynchronouslistmanager.Views.SyncRootView;
import com.sudoplz.rnsynchronouslistmanager.Utils.SPGlobals;
import java.util.ArrayList;
/**
* Created by SudoPlz on 15/02/2018.
*/
public class SPAdapter extends RecyclerView.Adapter <SPAdapter.SPViewHolder> {
ArrayList<Object> data = new ArrayList<Object>();
public SPAdapter() {
super();
}
public SPAdapter(ArrayList initialData) {
super();
if (initialData != null && initialData.size() > 0) {
data = initialData;
}
}
@Override
public SPViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// SPGlobals g = SPGlobals.getInstance();
// SyncRootView rowView = new SyncRootView(
// g.getMainViewTemplateName(),
// g.getRcContext(),
// g.getRcHost()
// );
// ;
// TextView text = new TextView(parent.getContext());
// text.setText("YOO");
// SPViewHolder holder = new SPViewHolder(text);
SyncRootView child = new SyncRootView(SPGlobals.getInstance().getMainViewTemplateName(), null, (ReactContext) parent.getContext());
SPViewHolder holder = new SPViewHolder(child);
return holder;
}
@Override
public void onBindViewHolder(SPViewHolder holder, int position) {
ReadableMap dataForChild = (ReadableMap) data.get(position);
holder.setItemData(dataForChild, position);
}
public void setAdapterData(ArrayList initialData) {
if (initialData != null && initialData.size() > 0) {
this.data = initialData;
notifyDataSetChanged();
}
}
public void prependData(ArrayList dataToPrepend) {
if (dataToPrepend != null && dataToPrepend.size() > 0) {
if (this.data != null && this.data.size() > 0) {
for (int i = 0; i < dataToPrepend.size(); i++) {
ReadableMap dataForChild = (ReadableMap) dataToPrepend.get(i);
this.data.add(0, dataForChild);
notifyItemRangeInserted(0, dataToPrepend.size());
}
} else {
this.data = dataToPrepend;
notifyDataSetChanged();
}
}
}
public void appendData(ArrayList dataToPrepend) {
if (dataToPrepend != null && dataToPrepend.size() > 0) {
if (this.data != null && this.data.size() > 0) {
int initialDataEndPosition = this.data.size();
for (int i = 0; i < dataToPrepend.size(); i++) {
ReadableMap dataForChild = (ReadableMap) dataToPrepend.get(i);
this.data.add(dataForChild);
}
notifyItemRangeInserted(initialDataEndPosition - 1, dataToPrepend.size());
} else {
this.data = dataToPrepend;
notifyDataSetChanged();
}
}
}
public void updateDataAtIndex(final int indexToUpdate, ReadableMap updatedChild) {
if (updatedChild != null) {
if (this.data != null && this.data.size() > indexToUpdate) {
this.data.set(indexToUpdate, updatedChild);
final SPAdapter self = this;
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
System.out.println("\n@@@@@@@@@@@@@@@@@ Updating: "+indexToUpdate);
self.notifyItemChanged(indexToUpdate);
}
});
}
}
}
public void clearData() {
if (this.data != null) {
int dataCnt = this.data.size();
if (dataCnt > 0) {
this.data.clear();
notifyItemRangeRemoved(0, dataCnt);
}
}
}
@Override
public int getItemCount() {
if (this.data != null) {
return this.data.size();
}
return 0;
}
/////////////////////////////////////////////////////////////
//////////////////////// VIEW HOLDER ////////////////////////
/////////////////////////////////////////////////////////////
protected class SPViewHolder extends RecyclerView.ViewHolder {
protected SyncRootView curView;
public SPViewHolder(SyncRootView itemView) {
super(itemView);
curView = itemView;
}
public void setItemData(ReadableMap newProps, int newPosition) {
if (curView != null) { // if we have a view
if (curView.hasInitialised()) {
// if that view has been initialised in the past
if (curView.hasLastPosition()) {
// if it has a last position set
// if (curView.getLastPosition() != newPosition) {
// // and if that last position is different from the new position
// System.out.println("\n@@@@@@@@@@@@@@@@@ we want the view in: "+curView.getLastPosition()+" to become: "+newPosition);
// curView.terminate();
// curView.setLastPosition(newPosition);
// curView.drawOnScreen(newProps);
// } else { // if it's the same position we assume we have new props, and update'em
// System.out.println("\n@@@@@@@@@@@@@@@@@ we want the view in: "+curView.getLastPosition()+" to receive new props");
// curView.updateProps(newProps);
// }
// System.out.println("\n@@@@@@@@@@@@@@@@@ we want the view in: "+curView.getLastPosition()+" to become: "+newPosition);
curView.setLastPosition(newPosition);
curView.updateProps(newProps);
} else {
// System.out.println("\n@@@@@@@@@@@@@@@@@ we want a view to become: "+newPosition);
// but if we don't have a last position
curView.setLastPosition(newPosition);
curView.drawOnScreen(newProps);
}
} else {
// System.out.println("\n@@@@@@@@@@@@@@@@@ we want an uninitialised view to become: "+newPosition);
// if the view has not been initialised
curView.setLastPosition(newPosition);
curView.drawOnScreen(newProps);
}
}
}
public Boolean viewHasInitialised() {
return curView.hasInitialised();
}
}
}