This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SectionAdapterListUpdateCallback. Update example 8 with example o…
…f usage.
- Loading branch information
Showing
8 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../io/github/luizgrp/sectionedrecyclerviewadapter/demo/example8/PersonListDiffCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.luizgrp.sectionedrecyclerviewadapter.demo.example8; | ||
|
||
import java.util.List; | ||
|
||
import androidx.recyclerview.widget.DiffUtil; | ||
|
||
public class PersonListDiffCallback extends DiffUtil.Callback { | ||
|
||
private final List<Person> oldList; | ||
private final List<Person> newList; | ||
|
||
PersonListDiffCallback(final List<Person> oldList, final List<Person> newList) { | ||
this.oldList = oldList; | ||
this.newList = newList; | ||
} | ||
|
||
@Override | ||
public int getOldListSize() { | ||
return oldList != null ? oldList.size() : 0; | ||
} | ||
|
||
@Override | ||
public int getNewListSize() { | ||
return newList != null ? newList.size() : 0; | ||
} | ||
|
||
@Override | ||
public boolean areItemsTheSame(final int oldItemPosition, final int newItemPosition) { | ||
return newList.get(newItemPosition).id.equals(oldList.get(oldItemPosition).id); | ||
} | ||
|
||
@Override | ||
public boolean areContentsTheSame(final int oldItemPosition, final int newItemPosition) { | ||
return newList.get(newItemPosition).equals(oldList.get(oldItemPosition)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...java/io/github/luizgrp/sectionedrecyclerviewadapter/SectionAdapterListUpdateCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.luizgrp.sectionedrecyclerviewadapter; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.recyclerview.widget.DiffUtil; | ||
import androidx.recyclerview.widget.ListUpdateCallback; | ||
|
||
/** | ||
* ListUpdateCallback that dispatches update events to the given {@link SectionAdapter}. | ||
* | ||
* @see DiffUtil.DiffResult#dispatchUpdatesTo(ListUpdateCallback) | ||
*/ | ||
public class SectionAdapterListUpdateCallback implements ListUpdateCallback { | ||
|
||
@SuppressWarnings("PMD.BeanMembersShouldSerialize") | ||
private final SectionAdapter sectionAdapter; | ||
|
||
public SectionAdapterListUpdateCallback(final SectionAdapter sectionAdapter) { | ||
this.sectionAdapter = sectionAdapter; | ||
} | ||
|
||
@Override | ||
public void onInserted(final int position, final int count) { | ||
sectionAdapter.notifyItemRangeInserted(position, count); | ||
} | ||
|
||
@Override | ||
public void onRemoved(final int position, final int count) { | ||
sectionAdapter.notifyItemRangeRemoved(position, count); | ||
} | ||
|
||
@Override | ||
public void onMoved(final int fromPosition, final int toPosition) { | ||
sectionAdapter.notifyItemMoved(fromPosition, toPosition); | ||
} | ||
|
||
@Override | ||
public void onChanged(final int position, final int count, @Nullable final Object payload) { | ||
sectionAdapter.notifyItemRangeChanged(position, count, payload); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
.../io/github/luizgrp/sectionedrecyclerviewadapter/SectionAdapterListUpdateCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.github.luizgrp.sectionedrecyclerviewadapter; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
public class SectionAdapterListUpdateCallbackTest { | ||
|
||
@Mock | ||
private SectionAdapter sectionAdapter; | ||
|
||
private SectionAdapterListUpdateCallback sectionAdapterListUpdateCallback; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
sectionAdapterListUpdateCallback = new SectionAdapterListUpdateCallback(sectionAdapter); | ||
} | ||
|
||
@Test | ||
public void givenSectionAdapter_whenOnInserted_thenNotifyItemRangeInsertedIsCalled() { | ||
// Given | ||
final int position = 3; | ||
final int count = 9; | ||
|
||
// When | ||
sectionAdapterListUpdateCallback.onInserted(position, count); | ||
|
||
// Then | ||
verify(sectionAdapter).notifyItemRangeInserted(position, count); | ||
} | ||
|
||
@Test | ||
public void givenSectionAdapter_whenOnRemoved_thenNotifyItemRangeRemovedIsCalled() { | ||
// Given | ||
final int position = 3; | ||
final int count = 9; | ||
|
||
// When | ||
sectionAdapterListUpdateCallback.onRemoved(position, count); | ||
|
||
// Then | ||
verify(sectionAdapter).notifyItemRangeRemoved(position, count); | ||
} | ||
|
||
@Test | ||
public void givenSectionAdapter_whenOnMoved_thenNotifyItemMovedIsCalled() { | ||
// Given | ||
final int fromPosition = 3; | ||
final int toPosition = 9; | ||
|
||
// When | ||
sectionAdapterListUpdateCallback.onMoved(fromPosition, toPosition); | ||
|
||
// Then | ||
verify(sectionAdapter).notifyItemMoved(fromPosition, toPosition); | ||
} | ||
|
||
@Test | ||
public void givenSectionAdapter_whenOnChanged_thenNotifyItemRangeChangedIsCalled() { | ||
// Given | ||
final int position = 3; | ||
final int count = 9; | ||
final Object payload = new Object(); | ||
|
||
// When | ||
sectionAdapterListUpdateCallback.onChanged(position, count, payload); | ||
|
||
// Then | ||
verify(sectionAdapter).notifyItemRangeChanged(position, count, payload); | ||
} | ||
} |