observe changes bugs fixed

This commit is contained in:
Gavriil Sitnikov 2016-06-15 16:53:05 +03:00
parent 644baccbdd
commit 0a4aabd09c
1 changed files with 12 additions and 18 deletions

View File

@ -54,9 +54,7 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
private static final int PRE_LOADING_COUNT = 10; private static final int PRE_LOADING_COUNT = 10;
private static final int LOADED_ITEM_TYPE = R.id.LOADED_ITEM_TYPE; private static final int LOADED_ITEM_TYPE = R.id.LOADED_ITEM_TYPE;
private static final int UNKNOWN_UPDATE = -1;
//TODO: replace with wrapper
@NonNull @NonNull
private final BehaviorSubject<ObservableCollection<TItem>> observableCollectionSubject private final BehaviorSubject<ObservableCollection<TItem>> observableCollectionSubject
= BehaviorSubject.create((ObservableCollection<TItem>) null); = BehaviorSubject.create((ObservableCollection<TItem>) null);
@ -64,7 +62,7 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
private final UiBindable uiBindable; private final UiBindable uiBindable;
@Nullable @Nullable
private OnItemClickListener<TItem> onItemClickListener; private OnItemClickListener<TItem> onItemClickListener;
private int lastUpdatedChangeNumber = UNKNOWN_UPDATE; private int lastUpdatedChangeNumber = -1;
@NonNull @NonNull
private final Observable<?> newItemsUpdatingObservable; private final Observable<?> newItemsUpdatingObservable;
@NonNull @NonNull
@ -102,6 +100,7 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
break; break;
case REMOVED: case REMOVED:
int k = 0; int k = 0;
Lc.d("removed " + change.getCount());
for (TItem item : change.getChangedItems()) { for (TItem item : change.getChangedItems()) {
Lc.d("removed " + item + " at " + (change.getStart() + k)); Lc.d("removed " + item + " at " + (change.getStart() + k));
k++; k++;
@ -154,12 +153,8 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
private void refreshUpdate() { private void refreshUpdate() {
notifyDataSetChanged(); notifyDataSetChanged();
if (observableCollectionSubject.getValue() != null) { lastUpdatedChangeNumber = innerCollection.getChangesCount();
lastUpdatedChangeNumber = innerCollection.getChangesCount(); Lc.d("refresh update " + lastUpdatedChangeNumber);
Lc.d("refresh update " + lastUpdatedChangeNumber);
} else {
lastUpdatedChangeNumber = UNKNOWN_UPDATE;
}
} }
public void setObservableCollection(@Nullable final ObservableCollection<TItem> observableCollection) { public void setObservableCollection(@Nullable final ObservableCollection<TItem> observableCollection) {
@ -168,9 +163,6 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
} }
protected void onItemsChanged(@NonNull final ObservableCollection.CollectionChange<TItem> collectionChange) { protected void onItemsChanged(@NonNull final ObservableCollection.CollectionChange<TItem> collectionChange) {
if (observableCollectionSubject.getValue() == null) {
return;
}
if (Looper.myLooper() != Looper.getMainLooper()) { if (Looper.myLooper() != Looper.getMainLooper()) {
Lc.assertion("Items changes called on not main thread"); Lc.assertion("Items changes called on not main thread");
return; return;
@ -199,7 +191,12 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
notifyItemRangeChanged(change.getStart() + itemsOffset(), change.getCount()); notifyItemRangeChanged(change.getStart() + itemsOffset(), change.getCount());
break; break;
case REMOVED: case REMOVED:
notifyItemRangeRemoved(change.getStart() + itemsOffset(), change.getCount()); if (getItemCount() == 0) {
//TODO: bug of recyclerview?
notifyDataSetChanged();
} else {
notifyItemRangeRemoved(change.getStart() + itemsOffset(), change.getCount());
}
break; break;
default: default:
Lc.assertion("Not supported " + change.getType()); Lc.assertion("Not supported " + change.getType());
@ -248,15 +245,12 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Obs
@NonNull @NonNull
public TItem getItem(final int position) { public TItem getItem(final int position) {
if (observableCollectionSubject.getValue() == null) { return innerCollection.get(position);
throw new ShouldNotHappenException();
}
return observableCollectionSubject.getValue().get(position);
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
return observableCollectionSubject.getValue() != null ? observableCollectionSubject.getValue().size() : 0; return innerCollection.size();
} }
public boolean isOnClickListenerDisabled(@NonNull final TItem item) { public boolean isOnClickListenerDisabled(@NonNull final TItem item) {