19
19
import java .util .Set ;
20
20
21
21
/**
22
- *
23
22
* @author SilenceDut
24
23
* @date 2018/12/19
25
24
*/
26
25
class AsyncListUpdateDiffer <T extends BaseMutableData > {
27
- private static final String TAG ="AsyncListUpdateDiffer" ;
26
+ private static final String TAG = "AsyncListUpdateDiffer" ;
28
27
private final ListUpdateCallback mUpdateCallback ;
29
28
private final AsyncDifferConfig <T > mConfig ;
30
29
private final ListChangedCallback <T > mListChangedCallback ;
@@ -36,42 +35,48 @@ class AsyncListUpdateDiffer<T extends BaseMutableData> {
36
35
static final int DELAY_STEP = 5 ;
37
36
private Handler mDiffHandler ;
38
37
39
- AsyncListUpdateDiffer (@ NonNull DiffAdapter adapter , @ NonNull ListChangedCallback <T > listChangedCallback , @ NonNull DiffUtil .ItemCallback <T > diffCallback ) {
38
+ AsyncListUpdateDiffer (@ NonNull DiffAdapter adapter , @ NonNull ListChangedCallback <T > listChangedCallback ,
39
+ @ NonNull DiffUtil .ItemCallback <T > diffCallback ) {
40
40
this .mDiffHandler = adapter .mDiffHandler ;
41
41
this .mUpdateCallback = new AdapterListUpdateCallback (adapter );
42
42
this .mConfig = new AsyncDifferConfig .Builder <>(diffCallback ).build ();
43
43
this .mListChangedCallback = listChangedCallback ;
44
44
updateCurrentList (new ArrayList <T >());
45
45
}
46
46
47
-
48
-
49
47
void submitList (@ Nullable final List <T > newList ) {
50
48
final long runGeneration = ++this .mMaxScheduledGeneration ;
51
49
mGenerations .add (runGeneration );
52
- Log .d (TAG ,"latchList submitList runGeneration add :" + runGeneration + ";;size" + mGenerations .size ());
50
+ Log .d (TAG , "latchList submitList runGeneration add :" + runGeneration + ";;size" + mGenerations .size ());
53
51
if (newList != this .mOldList ) {
54
52
if (newList == null ) {
55
53
int countRemoved = this .mOldList .size ();
56
54
syncOldList (null );
57
55
updateCurrentList (new ArrayList <T >());
58
56
this .mUpdateCallback .onRemoved (0 , countRemoved );
59
57
mGenerations .remove (runGeneration );
60
- Log .d (TAG ,"latchList submitList newList == null runGeneration :" +runGeneration +";;size" +mGenerations .size ());
58
+ Log .d (TAG , "latchList submitList newList == null runGeneration :" + runGeneration + ";;size" +
59
+ mGenerations .size ());
61
60
} else if (this .mOldList == null ) {
62
61
syncOldList (newList );
63
62
updateSyncTime (newList );
64
63
updateCurrentList (new ArrayList <>(newList ));
65
64
this .mUpdateCallback .onInserted (0 , newList .size ());
66
65
mGenerations .remove (runGeneration );
67
- Log .d (TAG ,"latchList submitList mOldList == null runGeneration :" +runGeneration +";;size" +mGenerations .size ());
66
+ Log .d (TAG , "latchList submitList mOldList == null runGeneration :" + runGeneration + ";;size" +
67
+ mGenerations .size ());
68
68
} else {
69
- doDiff (newList ,runGeneration );
69
+ doDiff (newList , runGeneration );
70
70
}
71
71
}
72
72
}
73
73
74
- private void doDiff (@ NonNull final List <T > newList ,final long runGeneration ) {
74
+ private void doDiff (@ NonNull final List <T > newList , final long runGeneration ) {
75
+
76
+ if (this .mOldList == null ) {
77
+ return ;
78
+ }
79
+
75
80
final List <T > oldList = new ArrayList <>(this .mOldList );
76
81
77
82
this .mConfig .getBackgroundThreadExecutor ().execute (new Runnable () {
@@ -82,19 +87,22 @@ public void run() {
82
87
public int getOldListSize () {
83
88
return oldList .size ();
84
89
}
90
+
85
91
@ Override
86
92
public int getNewListSize () {
87
93
return newList .size ();
88
94
}
95
+
89
96
@ Override
90
97
public boolean areItemsTheSame (int oldItemPosition , int newItemPosition ) {
91
98
92
99
T oldItem = oldList .get (oldItemPosition );
93
100
T newItem = newList .get (newItemPosition );
94
- if (oldItem == null || newItem == null ) {
101
+ if (oldItem == null || newItem == null ) {
95
102
return false ;
96
103
}
97
- if (oldItem .getItemViewId ()!=newItem .getItemViewId () || oldItem .getClass () != newItem .getClass ()) {
104
+ if (oldItem .getItemViewId () != newItem .getItemViewId () ||
105
+ oldItem .getClass () != newItem .getClass ()) {
98
106
return false ;
99
107
}
100
108
return AsyncListUpdateDiffer .this .mConfig .getDiffCallback ().areItemsTheSame (oldItem , newItem );
@@ -105,32 +113,37 @@ public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
105
113
106
114
T oldItem = oldList .get (oldItemPosition );
107
115
T newItem = newList .get (newItemPosition );
108
- if (oldItem != null && newItem != null && oldItem .getClass () == newItem .getClass () ) {
109
- return AsyncListUpdateDiffer .this .mConfig .getDiffCallback ().areContentsTheSame (oldItem , newItem );
110
- } else {
116
+ if (oldItem != null && newItem != null && oldItem .getClass () == newItem .getClass ()) {
117
+ return AsyncListUpdateDiffer .this .mConfig .getDiffCallback ()
118
+ .areContentsTheSame (oldItem , newItem );
119
+ } else {
111
120
return oldItem == null && newItem == null ;
112
121
}
113
122
}
123
+
114
124
@ Override
115
125
@ Nullable
116
126
public Object getChangePayload (int oldItemPosition , int newItemPosition ) {
117
127
T oldItem = oldList .get (oldItemPosition );
118
128
T newItem = newList .get (newItemPosition );
119
129
if (oldItem != null && newItem != null && oldItem .getClass () == newItem .getClass ()) {
120
- return AsyncListUpdateDiffer .this .mConfig .getDiffCallback ().getChangePayload (oldItem , newItem );
130
+ return AsyncListUpdateDiffer .this .mConfig .getDiffCallback ()
131
+ .getChangePayload (oldItem , newItem );
121
132
} else {
122
133
return null ;
123
134
}
124
135
}
125
136
});
126
- mDiffHandler .post ( new Runnable () {
137
+ mDiffHandler .post (new Runnable () {
127
138
@ Override
128
139
public void run () {
129
140
if (AsyncListUpdateDiffer .this .mMaxScheduledGeneration == runGeneration ) {
130
- AsyncListUpdateDiffer .this .latchList (newList , result ,runGeneration );
131
- Log .d (TAG ,"latchList doDiff runGeneration :" +runGeneration +";;size" +mGenerations .size ());
141
+ AsyncListUpdateDiffer .this .latchList (newList , result , runGeneration );
142
+ Log .d (TAG , "latchList doDiff runGeneration :" + runGeneration + ";;size" +
143
+ mGenerations .size ());
132
144
} else {
133
- Log .d (TAG ,"latchList doDiff else runGeneration :" +runGeneration +";;size" +mGenerations .size ());
145
+ Log .d (TAG , "latchList doDiff else runGeneration :" + runGeneration + ";;size" +
146
+ mGenerations .size ());
134
147
mGenerations .remove (runGeneration );
135
148
}
136
149
}
@@ -139,17 +152,18 @@ public void run() {
139
152
});
140
153
}
141
154
142
- private void latchList (@ NonNull final List <T > newList , @ NonNull final DiffUtil .DiffResult diffResult ,final long runGeneration ) {
155
+ private void latchList (@ NonNull final List <T > newList , @ NonNull final DiffUtil .DiffResult diffResult ,
156
+ final long runGeneration ) {
143
157
144
- long needDelay = mCanSyncTime - SystemClock .elapsedRealtime () ;
145
- if (needDelay <= 0 ) {
158
+ long needDelay = mCanSyncTime - SystemClock .elapsedRealtime ();
159
+ if (needDelay <= 0 ) {
146
160
147
161
syncOldList (newList );
148
162
updateSyncTime (newList );
149
163
updateCurrentList (new ArrayList <>(newList ));
150
164
diffResult .dispatchUpdatesTo (AsyncListUpdateDiffer .this .mUpdateCallback );
151
165
mGenerations .remove (runGeneration );
152
- Log .d (TAG ,"latchList needDelay <= 0 runGeneration :" + runGeneration + ";;size" + mGenerations .size ());
166
+ Log .d (TAG , "latchList needDelay <= 0 runGeneration :" + runGeneration + ";;size" + mGenerations .size ());
153
167
154
168
} else {
155
169
@@ -165,29 +179,28 @@ public void run() {
165
179
diffResult .dispatchUpdatesTo (AsyncListUpdateDiffer .this .mUpdateCallback );
166
180
167
181
}
168
- Log .d (TAG ,"latchList else runGeneration :" + runGeneration + ";;size" + mGenerations .size ());
182
+ Log .d (TAG , "latchList else runGeneration :" + runGeneration + ";;size" + mGenerations .size ());
169
183
mGenerations .remove (runGeneration );
170
184
}
171
- }, needDelay );
185
+ }, needDelay );
172
186
}
173
187
174
188
}
175
189
176
-
177
- void updateOldListSize (final @ NonNull Runnable listSizeRunnable , final List <T > oldDatas ) {
178
- if (mGenerations .size () > 0 ) {
190
+ void updateOldListSize (final @ NonNull Runnable listSizeRunnable , final List <T > oldDatas ) {
191
+ if (mGenerations .size () > 0 ) {
179
192
return ;
180
193
}
181
194
182
195
long currentTimeMillis = SystemClock .elapsedRealtime ();
183
196
184
- if (currentTimeMillis >= mCanSyncTime ) {
197
+ if (currentTimeMillis >= mCanSyncTime ) {
185
198
186
199
listSizeRunnable .run ();
187
200
syncOldList (oldDatas );
188
201
189
- }else {
190
- final long runGeneration = AsyncListUpdateDiffer .this .mMaxScheduledGeneration ;
202
+ } else {
203
+ final long runGeneration = AsyncListUpdateDiffer .this .mMaxScheduledGeneration ;
191
204
mDiffHandler .postDelayed (new Runnable () {
192
205
@ Override
193
206
public void run () {
@@ -198,20 +211,20 @@ public void run() {
198
211
syncOldList (oldDatas );
199
212
}
200
213
}
201
- }, mCanSyncTime - currentTimeMillis );
214
+ }, mCanSyncTime - currentTimeMillis );
202
215
}
203
216
}
204
217
205
218
private void updateCurrentList (List <T > currentList ) {
206
219
this .mListChangedCallback .onListChanged (currentList );
207
220
}
208
221
209
- private void syncOldList (@ Nullable List <T > oldData ) {
222
+ private void syncOldList (@ Nullable List <T > oldData ) {
210
223
this .mOldList = oldData ;
211
224
}
212
225
213
- private void updateSyncTime (@ Nullable List <T > oldData ) {
214
- mCanSyncTime = SystemClock .elapsedRealtime () + (oldData != null ? oldData .size () * DELAY_STEP : 0 ) ;
226
+ private void updateSyncTime (@ Nullable List <T > oldData ) {
227
+ mCanSyncTime = SystemClock .elapsedRealtime () + (oldData != null ? oldData .size () * DELAY_STEP : 0 ) ;
215
228
}
216
229
217
230
0 commit comments