Skip to content

Commit

Permalink
Use stream noneMatch API
Browse files Browse the repository at this point in the history
  • Loading branch information
seabornlee committed Oct 28, 2022
1 parent 201f6f3 commit 64768f0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,53 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

import timber.log.Timber;

public class ActivityAdapter extends RecyclerView.Adapter<BinderViewHolder<?>> implements AdapterCallback
{
private final ActivitySortedList<SortedItem<?>> items = new ActivitySortedList<>(SortedItem.class, new ActivitySortedList.Callback<SortedItem<?>>() {
private final ActivitySortedList<SortedItem<?>> items = new ActivitySortedList<>(SortedItem.class, new ActivitySortedList.Callback<SortedItem<?>>()
{
@Override
public int compare(SortedItem left, SortedItem right)
{
return left.compare(right);
}

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

@Override
public boolean areItemsTheSame(SortedItem left, SortedItem right) {
public boolean areItemsTheSame(SortedItem left, SortedItem right)
{
return left.areItemsTheSame(right);
}

@Override
public void onChanged(int position, int count) {
public void onChanged(int position, int count)
{
notifyItemRangeChanged(position, count);
}

@Override
public void onInserted(int position, int count) {
public void onInserted(int position, int count)
{
notifyItemRangeInserted(position, count);
}

@Override
public void onRemoved(int position, int count) {
public void onRemoved(int position, int count)
{
notifyItemRangeRemoved(position, count);
}

@Override
public void onMoved(int fromPosition, int toPosition) {
public void onMoved(int fromPosition, int toPosition)
{
notifyItemMoved(fromPosition, toPosition);
}
});
Expand All @@ -97,7 +105,8 @@ public void onMoved(int fromPosition, int toPosition) {
private boolean pendingReset = false;

public ActivityAdapter(TokensService service, FetchTransactionsInteract fetchTransactionsInteract,
AssetDefinitionService svs, ActivityDataInteract dataInteract) {
AssetDefinitionService svs, ActivityDataInteract dataInteract)
{
this.fetchTransactionsInteract = fetchTransactionsInteract;
this.dataInteract = dataInteract;
this.assetService = svs;
Expand All @@ -113,8 +122,10 @@ public ActivityAdapter(TokensService service, FetchTransactionsInteract fetchTra
}

@Override
public BinderViewHolder<?> onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
public BinderViewHolder<?> onCreateViewHolder(ViewGroup parent, int viewType)
{
switch (viewType)
{
case TransactionHolder.VIEW_TYPE:
return new TransactionHolder(parent, tokensService, fetchTransactionsInteract,
assetService);
Expand Down Expand Up @@ -153,7 +164,7 @@ else if (position > lastItemPos && dataInteract != null && System.currentTimeMil

public void onRViewRecycled(RecyclerView.ViewHolder holder)
{
onViewRecycled((BinderViewHolder<?>)(holder));
onViewRecycled((BinderViewHolder<?>) (holder));
}

@Override
Expand Down Expand Up @@ -185,16 +196,19 @@ private void fetchData(long earliestDate)
};

@Override
public int getItemCount() {
public int getItemCount()
{
return items.size();
}

@Override
public int getItemViewType(int position) {
public int getItemViewType(int position)
{
return items.get(position).viewType;
}

public void setDefaultWallet(Wallet wallet) {
public void setDefaultWallet(Wallet wallet)
{
this.wallet = wallet;
notifyDataSetChanged();
}
Expand All @@ -215,11 +229,11 @@ else if (obj instanceof EventSortedItem)
}
else if (obj instanceof DateSortedItem)
{
return ((DateSortedItem)obj).getUID();
return ((DateSortedItem) obj).getUID();
}
else if (obj instanceof TransferSortedItem)
{
return ((TransferSortedItem)obj).getUID();
return ((TransferSortedItem) obj).getUID();
}
else
{
Expand All @@ -231,7 +245,7 @@ else if (obj instanceof TransferSortedItem)

public void updateActivityItems(ActivityMeta[] activityItems)
{
if (activityItems.length == 0) return ;
if (activityItems.length == 0) return;

items.beginBatchedUpdates();
if (itemLimit != 0)
Expand All @@ -243,17 +257,17 @@ public void updateActivityItems(ActivityMeta[] activityItems)
{
if (item instanceof TransactionMeta)
{
TransactionSortedItem sortedItem = new TransactionSortedItem(TransactionHolder.VIEW_TYPE, (TransactionMeta)item, TimestampSortedItem.DESC);
TransactionSortedItem sortedItem = new TransactionSortedItem(TransactionHolder.VIEW_TYPE, (TransactionMeta) item, TimestampSortedItem.DESC);
items.addTransaction(sortedItem); //event has higher UI priority than an event, don't overwrite
}
else if (item instanceof EventMeta)
{
EventSortedItem sortedItem = new EventSortedItem(EventHolder.VIEW_TYPE, (EventMeta)item, TimestampSortedItem.DESC);
EventSortedItem sortedItem = new EventSortedItem(EventHolder.VIEW_TYPE, (EventMeta) item, TimestampSortedItem.DESC);
items.add(sortedItem);
}
else if (item instanceof TokenTransferData)
{
TransferSortedItem sortedItem = new TransferSortedItem(TransferHolder.VIEW_TYPE, (TokenTransferData)item, TimestampSortedItem.DESC);
TransferSortedItem sortedItem = new TransferSortedItem(TransferHolder.VIEW_TYPE, (TokenTransferData) item, TimestampSortedItem.DESC);
items.add(sortedItem);
}
items.add(DateSortedItem.round(item.getTimeStampSeconds()));
Expand All @@ -264,7 +278,8 @@ else if (item instanceof TokenTransferData)
items.endBatchedUpdates();
}

public void clear() {
public void clear()
{
items.clear();
notifyDataSetChanged();
}
Expand All @@ -289,9 +304,9 @@ private void applyItemLimit()
}
}

if (items.get(items.size()-1) instanceof DateSortedItem)
if (items.get(items.size() - 1) instanceof DateSortedItem)
{
removalObjects.add(items.get(items.size()-1));
removalObjects.add(items.get(items.size() - 1));
}

for (SortedItem sortedItem : removalObjects)
Expand All @@ -306,9 +321,9 @@ public void updateItems(List<ContractLocator> tokenContracts)
for (int i = 0; i < items.size(); i++)
{
if (items.get(i).viewType == TransactionHolder.VIEW_TYPE
&& items.get(i).value instanceof TransactionMeta)
&& items.get(i).value instanceof TransactionMeta)
{
TransactionMeta tm = (TransactionMeta)items.get(i).value;
TransactionMeta tm = (TransactionMeta) items.get(i).value;
if (tm.contractAddress != null && hasMatchingContract(tokenContracts, tm.contractAddress.toLowerCase()))
{
notifyItemChanged(i);
Expand Down Expand Up @@ -352,26 +367,21 @@ public void resetRequired()

public boolean isEmpty()
{
for (int i = 0; i < items.size(); i++)
{
Object item = items.get(i).value;
if (item instanceof ActivityMeta)
{
return false;
}
}

return true;
return IntStream.range(0, items.size())
.noneMatch(i -> items.get(i).value instanceof ActivityMeta);
}

private static class LabelHolder extends BinderViewHolder<Date> {
private static class LabelHolder extends BinderViewHolder<Date>
{

public LabelHolder(int resId, ViewGroup parent) {
public LabelHolder(int resId, ViewGroup parent)
{
super(resId, parent);
}

@Override
public void bind(@Nullable Date data, @NonNull Bundle addition) {
public void bind(@Nullable Date data, @NonNull Bundle addition)
{

}
}
Expand All @@ -387,12 +397,13 @@ public void addTransaction(T item)
{
if (item instanceof TransactionSortedItem)
{
TransactionSortedItem txSortedItem = (TransactionSortedItem)item;
TransactionSortedItem txSortedItem = (TransactionSortedItem) item;
int index = items.indexOf(txSortedItem);
if (index >= 0 && items.get(index).value instanceof EventMeta)
{
EventMeta em = (EventMeta)items.get(index).value;
if (!em.hash.equals(txSortedItem.value.hash)) add(item); //don't replace matching Event
EventMeta em = (EventMeta) items.get(index).value;
if (!em.hash.equals(txSortedItem.value.hash))
add(item); //don't replace matching Event
}
else
{
Expand All @@ -411,7 +422,7 @@ public void onDestroy(RecyclerView recyclerView)
//ensure all holders have their realm listeners cleaned up
for (int childCount = recyclerView.getChildCount(), i = 0; i < childCount; ++i)
{
((BinderViewHolder<?>)recyclerView.getChildViewHolder(recyclerView.getChildAt(i))).onDestroyView();
((BinderViewHolder<?>) recyclerView.getChildViewHolder(recyclerView.getChildAt(i))).onDestroyView();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.alphawallet.app.ui.widget.adapter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.alphawallet.app.entity.ActivityMeta;
import com.alphawallet.app.entity.EventMeta;
import com.alphawallet.shadows.ShadowAnalyticsService;
import com.alphawallet.shadows.ShadowApp;
import com.alphawallet.shadows.ShadowKeyProviderFactory;
import com.alphawallet.shadows.ShadowKeyService;
import com.alphawallet.shadows.ShadowRealmManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPackageManager;

@RunWith(AndroidJUnit4.class)
@Config(shadows = {ShadowApp.class, ShadowKeyProviderFactory.class, ShadowRealmManager.class, ShadowKeyService.class, ShadowAnalyticsService.class, ShadowPackageManager.class})
public class ActivityAdapterTest
{
@Test
public void test_isEmpty()
{
ActivityAdapter activityAdapter = new ActivityAdapter(null, null, null);
activityAdapter.updateActivityItems(new ActivityMeta[]{});
assertThat(activityAdapter.isEmpty(), equalTo(true));
activityAdapter.updateActivityItems(new ActivityMeta[]{ new EventMeta("", "", "", System.currentTimeMillis(), 1L)});
assertThat(activityAdapter.isEmpty(), equalTo(false));
}
}

0 comments on commit 64768f0

Please sign in to comment.