Skip to content

Commit 5fb0fc4

Browse files
Updated ObservableListSource to include indexes from list changes
1 parent 8825241 commit 5fb0fc4

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

src/main/java/io/reactivex/rxjavafx/sources/ObservableListSource.java

+51-17
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,20 @@ public static <T> Observable<ListChange<T>> fromObservableListChanges(final Obse
103103
ListChangeListener<T> listener = c -> {
104104
while (c.next()) {
105105
if (c.wasAdded()) {
106-
c.getAddedSubList().forEach(v -> subscriber.onNext(ListChange.of(v,Flag.ADDED)));
106+
for (int i = c.getFrom(); i < c.getTo(); i++) {
107+
subscriber.onNext(ListChange.of(c.getList().get(i), Flag.ADDED, i));
108+
}
107109
}
108110
if (c.wasRemoved()) {
109-
c.getRemoved().forEach(v -> subscriber.onNext(ListChange.of(v,Flag.REMOVED)));
111+
int removedIdx = 0;
112+
for (int i = c.getFrom(); i < c.getTo() + 1; i++) {
113+
subscriber.onNext(ListChange.of(c.getRemoved().get(removedIdx), Flag.REMOVED, i));
114+
removedIdx++;
115+
}
110116
}
111117
if (c.wasUpdated()) {
112118
for (int i = c.getFrom(); i < c.getTo(); i++) {
113-
subscriber.onNext(ListChange.of(c.getList().get(i),Flag.UPDATED));
119+
subscriber.onNext(ListChange.of(c.getList().get(i), Flag.UPDATED, i));
114120
}
115121
}
116122
}
@@ -132,12 +138,22 @@ public static <T> Observable<ListChange<T>> fromObservableListDistinctChanges(fi
132138

133139
while (c.next()) {
134140
if (c.wasAdded()) {
135-
c.getAddedSubList().stream().filter(v -> dupeCounter.add(v) == 1)
136-
.forEach(v -> subscriber.onNext(ListChange.of(v,Flag.ADDED)));
141+
for (int i = c.getFrom(); i < c.getTo(); i++) {
142+
var item = c.getList().get(i);
143+
if (dupeCounter.add(item) == 1) {
144+
subscriber.onNext(ListChange.of(item, Flag.ADDED, i));
145+
}
146+
}
137147
}
138148
if (c.wasRemoved()) {
139-
c.getRemoved().stream().filter(v -> dupeCounter.remove(v) == 0)
140-
.forEach(v -> subscriber.onNext(ListChange.of(v,Flag.REMOVED)));
149+
int removedIdx = 0;
150+
for (int i = c.getFrom(); i < c.getTo() + 1; i++) {
151+
var item = c.getRemoved().get(removedIdx);
152+
if (dupeCounter.remove(item) == 0) {
153+
subscriber.onNext(ListChange.of(item, Flag.REMOVED, i));
154+
}
155+
removedIdx++;
156+
}
141157
}
142158
}
143159
};
@@ -157,12 +173,22 @@ public static <T,R> Observable<ListChange<T>> fromObservableListDistinctChanges(
157173

158174
while (c.next()) {
159175
if (c.wasAdded()) {
160-
c.getAddedSubList().stream().filter(v -> dupeCounter.add(mapper.apply(v)) == 1)
161-
.forEach(v -> subscriber.onNext(ListChange.of(v,Flag.ADDED)));
176+
for (int i = c.getFrom(); i < c.getTo(); i++) {
177+
var item = c.getList().get(i);
178+
if (dupeCounter.add(mapper.apply(item)) == 1) {
179+
subscriber.onNext(ListChange.of(item, Flag.ADDED, i));
180+
}
181+
}
162182
}
163183
if (c.wasRemoved()) {
164-
c.getRemoved().stream().filter(v -> dupeCounter.remove(mapper.apply(v)) == 0)
165-
.forEach(v -> subscriber.onNext(ListChange.of(v,Flag.REMOVED)));
184+
int removedIdx = 0;
185+
for (int i = c.getFrom(); i < c.getTo() + 1; i++) {
186+
var item = c.getRemoved().get(removedIdx);
187+
if (dupeCounter.remove(mapper.apply(item)) == 0) {
188+
subscriber.onNext(ListChange.of(item, Flag.REMOVED, i));
189+
}
190+
removedIdx++;
191+
}
166192
}
167193
}
168194
};
@@ -181,14 +207,22 @@ public static <T,R> Observable<ListChange<R>> fromObservableListDistinctMappings
181207

182208
while (c.next()) {
183209
if (c.wasAdded()) {
184-
c.getAddedSubList().stream().map(mapper)
185-
.filter(v -> dupeCounter.add(v) == 1)
186-
.forEach(v -> subscriber.onNext(ListChange.of(v,Flag.ADDED)));
210+
for (int i = c.getFrom(); i < c.getTo(); i++) {
211+
var item = mapper.apply(c.getList().get(i));
212+
if (dupeCounter.add(item) == 1) {
213+
subscriber.onNext(ListChange.of(item, Flag.ADDED, i));
214+
}
215+
}
187216
}
188217
if (c.wasRemoved()) {
189-
c.getRemoved().stream().map(mapper)
190-
.filter(v -> dupeCounter.remove(v) == 0)
191-
.forEach(v -> subscriber.onNext(ListChange.of(v,Flag.REMOVED)));
218+
int removedIdx = 0;
219+
for (int i = c.getFrom(); i < c.getTo() + 1; i++) {
220+
var item = mapper.apply(c.getRemoved().get(removedIdx));
221+
if (dupeCounter.remove(item) == 0) {
222+
subscriber.onNext(ListChange.of(item, Flag.REMOVED, i));
223+
}
224+
removedIdx++;
225+
}
192226
}
193227
}
194228
};

0 commit comments

Comments
 (0)