static fixes

This commit is contained in:
Gavriil Sitnikov 2016-05-24 15:06:10 +03:00
parent d11efcbe50
commit 8a14678b99
3 changed files with 46 additions and 8 deletions

View File

@ -33,6 +33,7 @@ import rx.subjects.PublishSubject;
* Created by Gavriil Sitnikov on 07/12/2015. * Created by Gavriil Sitnikov on 07/12/2015.
* TODO: fill description * TODO: fill description
*/ */
@SuppressWarnings("CPD-START")
public abstract class ItemsProvider<T> { public abstract class ItemsProvider<T> {
private final PublishSubject<List<Change>> listChangesSubject = PublishSubject.create(); private final PublishSubject<List<Change>> listChangesSubject = PublishSubject.create();

View File

@ -42,6 +42,7 @@ import rx.functions.Actions;
* Created by Gavriil Sitnikov on 20/11/2015. * Created by Gavriil Sitnikov on 20/11/2015.
* TODO: fill description * TODO: fill description
*/ */
@SuppressWarnings("CPD-START")
public abstract class AbstractItemsAdapter<TItem, TViewHolder extends RecyclerView.ViewHolder> public abstract class AbstractItemsAdapter<TItem, TViewHolder extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<RecyclerView.ViewHolder> { extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

View File

@ -22,11 +22,13 @@ package ru.touchin.roboswag.components.listing.adapters;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import java.util.List; import java.util.List;
import ru.touchin.roboswag.components.R; import ru.touchin.roboswag.components.R;
import ru.touchin.roboswag.components.navigation.UiBindable;
import ru.touchin.roboswag.components.utils.UiUtils; import ru.touchin.roboswag.components.utils.UiUtils;
import ru.touchin.roboswag.core.log.Lc; import ru.touchin.roboswag.core.log.Lc;
import ru.touchin.roboswag.core.observables.collections.Change; import ru.touchin.roboswag.core.observables.collections.Change;
@ -34,20 +36,21 @@ import ru.touchin.roboswag.core.observables.collections.ObservableCollection;
import ru.touchin.roboswag.core.observables.collections.ObservableList; import ru.touchin.roboswag.core.observables.collections.ObservableList;
import ru.touchin.roboswag.core.utils.ShouldNotHappenException; import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Actions; import rx.functions.Actions;
/** /**
* Created by Gavriil Sitnikov on 20/11/2015. * Created by Gavriil Sitnikov on 20/11/2015.
* TODO: fill description * TODO: fill description
*/ */
public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends RecyclerView.ViewHolder> public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends ObservableCollectionAdapter.ViewHolder>
extends RecyclerView.Adapter<RecyclerView.ViewHolder> { extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
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;
@NonNull
private final UiBindable uiBindable;
@Nullable @Nullable
private OnItemClickListener<TItem> onItemClickListener; private OnItemClickListener<TItem> onItemClickListener;
@Nullable @Nullable
@ -55,6 +58,16 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Rec
@Nullable @Nullable
private Subscription itemsProviderSubscription; private Subscription itemsProviderSubscription;
@NonNull
public UiBindable getUiBindable() {
return uiBindable;
}
public ObservableCollectionAdapter(@NonNull final UiBindable uiBindable) {
super();
this.uiBindable = uiBindable;
}
protected long getItemClickDelay() { protected long getItemClickDelay() {
return UiUtils.RIPPLE_EFFECT_DELAY; return UiUtils.RIPPLE_EFFECT_DELAY;
} }
@ -80,8 +93,7 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Rec
this.observableCollection = observableCollection; this.observableCollection = observableCollection;
notifyDataSetChanged(); notifyDataSetChanged();
if (this.observableCollection != null) { if (this.observableCollection != null) {
itemsProviderSubscription = this.observableCollection.observeChanges() itemsProviderSubscription = uiBindable.bind(this.observableCollection.observeChanges())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onItemsChanged); .subscribe(this::onItemsChanged);
} }
} }
@ -123,14 +135,14 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Rec
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final TItem item = getItem(position);
if (observableCollection == null) { if (observableCollection == null) {
Lc.assertion(new ShouldNotHappenException("Item at" + position + " should not be null")); Lc.assertion(new ShouldNotHappenException());
return; return;
} }
final TItem item = getItem(position - itemsOffset());
onBindItemToViewHolder((TViewHolder) holder, position, item); onBindItemToViewHolder((TViewHolder) holder, position, item);
observableCollection.loadRange(Math.max(0, position - PRE_LOADING_COUNT), position + PRE_LOADING_COUNT).first() ((TViewHolder) holder).bindPosition(uiBindable, observableCollection, position);
.subscribe(Actions.empty(), Actions.empty());
if (onItemClickListener != null && !isOnClickListenerDisabled(item)) { if (onItemClickListener != null && !isOnClickListenerDisabled(item)) {
UiUtils.setOnRippleClickListener(holder.itemView, () -> onItemClickListener.onItemClicked(item, position), getItemClickDelay()); UiUtils.setOnRippleClickListener(holder.itemView, () -> onItemClickListener.onItemClicked(item, position), getItemClickDelay());
} }
@ -161,4 +173,28 @@ public abstract class ObservableCollectionAdapter<TItem, TViewHolder extends Rec
} }
public static class ViewHolder extends RecyclerView.ViewHolder {
@Nullable
private Subscription preLoadingSubscription;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
}
public void bindPosition(@NonNull final UiBindable uiBindable,
@NonNull final ObservableCollection<?> observableCollection,
final int position) {
if (preLoadingSubscription != null) {
preLoadingSubscription.unsubscribe();
}
preLoadingSubscription = uiBindable
.untilStop(observableCollection
.loadRange(Math.max(0, position - PRE_LOADING_COUNT), position + PRE_LOADING_COUNT)
.first())
.subscribe(Actions.empty(), Actions.empty());
}
}
} }