/** * Creates a new item. * * @param model the instance of the {@code MODEL} class. * @param adapterViewTypeDelegate the {@link AdapterViewTypeDelegate} associated with this item. */ public ViewItem(MODEL model, AdapterViewTypeDelegate adapterViewTypeDelegate) { this.model = model; this.viewType = adapterViewTypeDelegate.getViewType(); } }
public void registerFeatureDecoration(FeatureItemDecoration decoration, AdapterViewTypeDelegate viewTypeDelegate) { viewTypeDecorationMap.put(viewTypeDelegate.getViewType(), decoration); }
public void registerFeatureAnimatorListener(FeatureAnimatorListener featureAnimator, AdapterViewTypeDelegate viewTypeDelegate) { viewTypeFeatureAnimatorMap.put(viewTypeDelegate.getViewType(), featureAnimator); }
/** * Returns the position of the first view item for a given view type * * @param adapterViewTypeDelegate of the feature views. * @return the first position of the view item defined by the given adapterViewTypeDelegate */ @SuppressWarnings("unused") public int getFirstItemPositionForAdapterViewTypeDelegate( AdapterViewTypeDelegate adapterViewTypeDelegate) { int indexViewItem = 0; for (Iterator iterator = this.featureItems.iterator(); iterator.hasNext(); ++indexViewItem) { ViewItem recyclerViewItem = (ViewItem) iterator.next(); if (recyclerViewItem.viewType == adapterViewTypeDelegate.getViewType()) { return indexViewItem; } } return -1; }
private void registerAdapterViewTypeDelegates(List<FeatureController<MODEL>> featureControllers) { for (FeatureController<MODEL> featureController : featureControllers) { for (AdapterViewTypeDelegate delegate : featureController.getAdapterViewTypeDelegates()) { // assign unique view type delegate.setViewType(mapViewTypeToAdapterViewTypeDelegate.size()); // register delegate mapViewTypeToAdapterViewTypeDelegate.put(delegate.getViewType(), delegate); // register item comparator mapViewTypeToItemComparator.put( delegate.getViewType(), delegate.createDiffUtilComparator()); } } }
public GroupAdapterViewTypeDelegate(List<AdapterViewTypeDelegate> delegates) { childAdapterViewTypeDelegates.addAll(delegates); int childViewType = 0; for (AdapterViewTypeDelegate delegate : childAdapterViewTypeDelegates) { if (delegate.getViewType() != RecyclerView.INVALID_TYPE) { throw new IllegalStateException("Do not reuse AdapterViewTypeDelegate instances"); } delegate.setViewType(childViewType); childViewType++; delegate.addFeatureEventListener(this::fireEvent); childDiffUtilComparators.add(delegate.createDiffUtilComparator()); childViewHolderCache.add(new ArrayDeque<>()); } }
@Test(expected = IllegalStateException.class) public void constructor_shouldThrowException_whenSharingDelegates() { // GIVEN AdapterViewTypeDelegate delegate = createMock(AdapterViewTypeDelegate.class); expect(delegate.getViewType()).andReturn(0); replay(delegate); // WHEN new TargetGroupAdapterViewTypeDelegate(singletonList(delegate)); // THEN // expected exception }
private AdapterViewTypeDelegate createValidAdapterViewTypeDelegate(int expectedViewType) { AdapterViewTypeDelegate delegate = createMock(AdapterViewTypeDelegate.class); expect(delegate.getViewType()).andStubReturn(RecyclerView.INVALID_TYPE); delegate.setViewType(expectedViewType); delegate.addFeatureEventListener(anyObject()); // we build a real DiffUtilComparator so we can generate realistic DiffUtil.DiffResult expect(delegate.createDiffUtilComparator()).andStubReturn(new DiffUtilComparator<ChildModel>() { @Override public boolean areItemsTheSame(ChildModel oldModel, ChildModel newModel) { return oldModel.key == newModel.key; } @Override public boolean areContentsTheSame(ChildModel oldModel, ChildModel newModel) { return oldModel.value.equals(newModel.value); } @Override public Object getChangePayload(ChildModel oldModel, ChildModel newModel) { return null; } }); return delegate; }