Search in sources :

Example 1 with FeedItem

use of com.android.example.devsummit.archdemo.vo.FeedItem in project dev-summit-architecture-demo by yigit.

the class TestUtil method createDummyFeedItem.

public static FeedItem createDummyFeedItem() {
    User user = createDummyUser();
    Post post = createDummyPost(user.getId());
    return new FeedItem(post, user);
}
Also used : User(com.android.example.devsummit.archdemo.vo.User) FeedItem(com.android.example.devsummit.archdemo.vo.FeedItem) Post(com.android.example.devsummit.archdemo.vo.Post)

Example 2 with FeedItem

use of com.android.example.devsummit.archdemo.vo.FeedItem in project dev-summit-architecture-demo by yigit.

the class FeedActivityTest method refreshFeedWithOlderItems.

@Test
public void refreshFeedWithOlderItems() throws Throwable {
    FeedModel mockFeedModel = mock(FeedModel.class);
    when(mComponent.feedModel()).thenReturn(mockFeedModel);
    FeedItem feedItem1 = TestUtil.createDummyFeedItem();
    FeedItem feedItem2 = TestUtil.createDummyFeedItem();
    List<FeedItem> items = Collections.singletonList(feedItem1);
    List<FeedItem> items2 = Arrays.asList(feedItem1, feedItem2);
    Post post1 = feedItem1.getPost();
    final Post post2 = feedItem2.getPost();
    post1.setCreated(10L);
    post2.setCreated(2L);
    //noinspection unchecked
    when(mockFeedModel.loadFeed(0L, null)).thenReturn(items);
    when(mockFeedModel.loadFeed(1L, null)).thenReturn(items2);
    getActivity();
    onView(atAdapterPosition(getActivity().mBinding.list, 0)).check(matches(withChild(withText(post1.getText())))).check(matches(withChild(withText(feedItem1.getUser().getName()))));
    runTestOnUiThread(new Runnable() {

        @Override
        public void run() {
            getActivity().onEventMainThread(new FetchedFeedEvent(true, null, post2));
        }
    });
    onItemAt(0).check(matches(withChild(withText(post1.getText())))).check(matches(withChild(withText(feedItem1.getUser().getName()))));
    onItemAt(1).check(matches(withChild(withText(post2.getText())))).check(matches(withChild(withText(feedItem2.getUser().getName()))));
}
Also used : FeedModel(com.android.example.devsummit.archdemo.model.FeedModel) FeedItem(com.android.example.devsummit.archdemo.vo.FeedItem) Post(com.android.example.devsummit.archdemo.vo.Post) FetchedFeedEvent(com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent) Test(org.junit.Test)

Example 3 with FeedItem

use of com.android.example.devsummit.archdemo.vo.FeedItem in project dev-summit-architecture-demo by yigit.

the class FeedAdapter method updatePost.

public void updatePost(Post post) {
    String key = createKeyFor(post);
    FeedItem existing = mUniqueMapping.get(key);
    if (existing == null) {
        L.d("update post is received but it does not exist, ignoring... %s", key);
        return;
    }
    int pos = mList.indexOf(existing);
    FeedItem newItem = new FeedItem(post, existing.getUser());
    mUniqueMapping.put(key, newItem);
    mList.updateItemAt(pos, newItem);
}
Also used : FeedItem(com.android.example.devsummit.archdemo.vo.FeedItem)

Example 4 with FeedItem

use of com.android.example.devsummit.archdemo.vo.FeedItem in project dev-summit-architecture-demo by yigit.

the class FeedAdapter method swapList.

public void swapList(List<FeedItem> items) {
    Set<String> newListKeys = new HashSet<>();
    for (FeedItem item : items) {
        newListKeys.add(createKeyFor(item.getPost()));
    }
    for (int i = mList.size() - 1; i >= 0; i--) {
        FeedItem item = mList.get(i);
        String key = createKeyFor(item.getPost());
        if (!newListKeys.contains(key)) {
            mUniqueMapping.remove(key);
            mList.removeItemAt(i);
        }
    }
    insertAll(items);
}
Also used : FeedItem(com.android.example.devsummit.archdemo.vo.FeedItem) HashSet(java.util.HashSet)

Example 5 with FeedItem

use of com.android.example.devsummit.archdemo.vo.FeedItem in project dev-summit-architecture-demo by yigit.

the class FeedModel method loadFeed.

public List<FeedItem> loadFeed(long since, @Nullable Long userId) {
    final List<Post> posts;
    if (userId == null) {
        posts = mComponent.postModel().loadPostsSince(since);
    } else {
        posts = mComponent.postModel().loadPostsOfUser(userId, since);
    }
    List<Long> userIds = new ArrayList<>();
    for (Post post : posts) {
        userIds.add(post.getUserId());
    }
    Map<Long, User> users = mComponent.userModel().loadUsersAsMap(userIds);
    List<FeedItem> result = new ArrayList<>();
    for (Post post : posts) {
        User user = users.get(post.getUserId());
        if (user != null) {
            result.add(new FeedItem(post, user));
        }
    }
    return result;
}
Also used : User(com.android.example.devsummit.archdemo.vo.User) FeedItem(com.android.example.devsummit.archdemo.vo.FeedItem) Post(com.android.example.devsummit.archdemo.vo.Post) ArrayList(java.util.ArrayList)

Aggregations

FeedItem (com.android.example.devsummit.archdemo.vo.FeedItem)10 Post (com.android.example.devsummit.archdemo.vo.Post)5 Test (org.junit.Test)4 FeedModel (com.android.example.devsummit.archdemo.model.FeedModel)3 User (com.android.example.devsummit.archdemo.vo.User)3 FetchedFeedEvent (com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent)2 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 BaseTest (com.android.example.devsummit.archdemo.BaseTest)1 NewPostResponse (com.android.example.devsummit.archdemo.api.NewPostResponse)1 FeedItemBinding (com.android.example.devsummit.archdemo.databinding.FeedItemBinding)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1