Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Collection setter in RVRendererAdapter #33

Merged
merged 9 commits into from Aug 29, 2016
Merged

Added Collection setter in RVRendererAdapter #33

merged 9 commits into from Aug 29, 2016

Conversation

tonilopezmr
Copy link
Contributor

In some occasions when it creates a AdapteeCollection for RVRendererAdapter needs a RecyclerView.Adapter.

  • Problem example using package android.support.v7.util.SortedList:
public MyAdapteeCollectionConstructor(RecyclerView.Adapter ANY_ADAPTER) {

    this.messageList = new SortedList<Message>(Message.class, new SortedListAdapterCallback<Message>(ANY_ADAPTER) {
            @Override
            public int compare(Message o1, Message o2) {
                return o1.compareTo(o2);
            }

            @Override
            public boolean areContentsTheSame(Message oldItem, Message newItem) {
                return oldItem.equals(newItem);
            }

            @Override
            public boolean areItemsTheSame(Message item1, Message item2) {
                return item1.getId().equals(item2.getId());
            }
        });
}

RVRendererAdapter creation:

adapter = new RVRendererAdapter<Message>(new MessageRenderBuilder(), new MyAdapteeCollection(WHAT ADAPTER?));

  • Solution with actual Renderers version for this example:
public MyAdapteeCollectionConstructor() {
    //init default messageList
}

...

 public void initCollection(RecyclerView.Adapter adapter) {
        messageList = new SortedList<Message>(Message.class, new SortedListAdapterCallback<Message>(adapter) {
            @Override
            public int compare(Message o1, Message o2) {
                return o1.compareTo(o2);
            }

            @Override
            public boolean areContentsTheSame(Message oldItem, Message newItem) {
                return oldItem.equals(newItem);
            }

            @Override
            public boolean areItemsTheSame(Message item1, Message item2) {
                return item1.getId().equals(item2.getId());
            }
        });
    }

RVRendererAdapter creation:

adapteeCollection = new MyAdapteeCollection();
adapter = new RVRendererAdapter<Message>(new MessageRenderBuilder(), adapteeCollection);
adapteeCollection.initCollection(adapter);

  • Solution with for this example (better solution?):
public MyAdapteeCollectionConstructor(RecyclerView.Adapter ANY_ADAPTER) { ... }

RVRendererAdapter creation:

adapter = new RVRendererAdapter<Message>(new MessageRenderBuilder());
adapter.setCollection(new MyAdapteeCollection(adapter));

@Test public void shouldBeAdapteeCollectionNotNullWhenCreateOnlyWithRendererBuilder() {
RVRendererAdapter<Object> adapter = new RVRendererAdapter<Object>(mockedRendererBuilder);

assertNotNull(adapter.getCollection());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking that the collection is not null, we should check that the number of items is 0. If the collection inside the RVRendererAdapeter is null is an implementation detail. We could implement the same behaviour using a null check so my recommendation is to update this test so something like:

@Test public void shouldBeEmptyWhenItsCreatedWithJustARendererBuilder() {
    RVRendererAdapter<Object> adapter = new RVRendererAdapter<Object>(mockedRendererBuilder);

    assertEquals(0, adapter.getCount());
}

@pedrovgs
Copy link
Owner

Hi @tonilopezmr thank you so much for your PR :) I've added some comments, please review them and let me know once the PR be ready.

Thanks

@tonilopezmr
Copy link
Contributor Author

Thanks you for all comments @pedrovgs

Maybe I would send more PR 👍

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know how to avoid that with Optimize imports? 😿

@pedrovgs
Copy link
Owner

Thank you so much @tonilopezmr. I'm merging this PR and publishing a new version :) If you want to add something to the library remember you can create an issue or send me an email.

@pedrovgs pedrovgs merged commit c7b42de into pedrovgs:master Aug 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants