Only replace updated cards after loading from db.

There are currently 2 ways a list of contextual cards can be updated:
1. through loader onFinishLoading
2. onContextualCardUpdated

We need to make the data handling logic consistent between the 2 paths.

Also changed some loops to stream for simplicity.

Change-Id: I242732e180a14092f5745271e5f63c18a6e482e0
Fixes: 115572494
Test: robotests
This commit is contained in:
Fan Zhang
2018-09-13 12:55:07 -07:00
parent 0f8536b034
commit 29aaf62410
5 changed files with 37 additions and 36 deletions

View File

@@ -33,10 +33,10 @@ public class ContextualCard {
/** /**
* Flags indicating the type of the ContextualCard. * Flags indicating the type of the ContextualCard.
*/ */
@IntDef({CardType.INVALID, CardType.SLICE, CardType.SUGGESTION, CardType.CONDITIONAL}) @IntDef({CardType.DEFAULT, CardType.SLICE, CardType.SUGGESTION, CardType.CONDITIONAL})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface CardType { public @interface CardType {
int INVALID = -1; int DEFAULT = 0;
int SLICE = 1; int SLICE = 1;
int SUGGESTION = 2; int SUGGESTION = 2;
int CONDITIONAL = 3; int CONDITIONAL = 3;

View File

@@ -33,6 +33,8 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/** /**
* This is a centralized manager of multiple {@link ContextualCardController}. * This is a centralized manager of multiple {@link ContextualCardController}.
@@ -58,13 +60,15 @@ public class ContextualCardManager implements CardContentLoader.CardContentLoade
private final ControllerRendererPool mControllerRendererPool; private final ControllerRendererPool mControllerRendererPool;
private final Lifecycle mLifecycle; private final Lifecycle mLifecycle;
private final List<ContextualCard> mContextualCards; private final List<ContextualCard> mContextualCards;
private final List<LifecycleObserver> mLifecycleObservers;
private ContextualCardUpdateListener mListener; private ContextualCardUpdateListener mListener;
public ContextualCardManager(Context context, Lifecycle lifecycle) { public ContextualCardManager(Context context, @NonNull Lifecycle lifecycle) {
mContext = context; mContext = context;
mLifecycle = lifecycle; mLifecycle = lifecycle;
mContextualCards = new ArrayList<>(); mContextualCards = new ArrayList<>();
mLifecycleObservers = new ArrayList<>();
mControllerRendererPool = new ControllerRendererPool(); mControllerRendererPool = new ControllerRendererPool();
//for data provided by Settings //for data provided by Settings
for (int cardType : SETTINGS_CARDS) { for (int cardType : SETTINGS_CARDS) {
@@ -81,10 +85,8 @@ public class ContextualCardManager implements CardContentLoader.CardContentLoade
} }
private void loadCardControllers() { private void loadCardControllers() {
if (mContextualCards != null) { for (ContextualCard card : mContextualCards) {
for (ContextualCard card : mContextualCards) { setupController(card.getCardType());
setupController(card.getCardType());
}
} }
} }
@@ -96,53 +98,52 @@ public class ContextualCardManager implements CardContentLoader.CardContentLoade
return; return;
} }
controller.setCardUpdateListener(this); controller.setCardUpdateListener(this);
if (controller instanceof LifecycleObserver) { if (controller instanceof LifecycleObserver && !mLifecycleObservers.contains(controller)) {
if (mLifecycle != null) { mLifecycleObservers.add((LifecycleObserver) controller);
mLifecycle.addObserver((LifecycleObserver) controller); mLifecycle.addObserver((LifecycleObserver) controller);
}
} }
} }
//TODO(b/111822376): implement sorting mechanism. //TODO(b/111822376): implement sorting mechanism.
private void sortCards() { private void sortCards(List<ContextualCard> cards) {
//take mContextualCards as the source and do the ranking based on the rule. //take mContextualCards as the source and do the ranking based on the rule.
} }
@Override @Override
public void onContextualCardUpdated(int cardType, List<ContextualCard> updateList) { public void onContextualCardUpdated(List<ContextualCard> updateList) {
//TODO(b/112245748): Should implement a DiffCallback. //TODO(b/112245748): Should implement a DiffCallback.
//Keep the old list for comparison. //Keep the old list for comparison.
final List<ContextualCard> prevCards = mContextualCards; final List<ContextualCard> prevCards = mContextualCards;
//Remove the existing data that matches the certain cardType so as to insert the new data. final Set<Integer> cardTypes = updateList
for (int i = mContextualCards.size() - 1; i >= 0; i--) { .stream()
if (mContextualCards.get(i).getCardType() == cardType) { .map(card -> card.getCardType())
mContextualCards.remove(i); .collect(Collectors.toSet());
} //Remove the existing data that matches the certain cardType before inserting new data.
} final List<ContextualCard> cardsToKeep = mContextualCards
.stream()
.filter(card -> !cardTypes.contains(card.getCardType()))
.collect(Collectors.toList());
final List<ContextualCard> allCards = new ArrayList<>();
allCards.addAll(cardsToKeep);
allCards.addAll(updateList);
//Append the new data sortCards(allCards);
mContextualCards.addAll(updateList);
sortCards(); //replace with the new data
mContextualCards.clear();
mContextualCards.addAll(allCards);
loadCardControllers();
if (mListener != null) { if (mListener != null) {
mListener.onContextualCardUpdated(ContextualCard.CardType.INVALID, mContextualCards); mListener.onContextualCardUpdated(mContextualCards);
} }
} }
@Override @Override
public void onFinishCardLoading(List<ContextualCard> contextualCards) { public void onFinishCardLoading(List<ContextualCard> contextualCards) {
mContextualCards.clear(); onContextualCardUpdated(contextualCards);
if (contextualCards != null) {
mContextualCards.addAll(contextualCards);
}
//Force card sorting here in case CardControllers of custom view have nothing to update
// for the first launch.
sortCards();
loadCardControllers();
} }
void setListener(ContextualCardUpdateListener listener) { void setListener(ContextualCardUpdateListener listener) {

View File

@@ -27,5 +27,5 @@ import java.util.List;
* ContextualCardsAdapter} in this case. * ContextualCardsAdapter} in this case.
*/ */
public interface ContextualCardUpdateListener { public interface ContextualCardUpdateListener {
void onContextualCardUpdated(int cardType, List<ContextualCard> updateList); void onContextualCardUpdated(List<ContextualCard> updateList);
} }

View File

@@ -101,7 +101,7 @@ public class ContextualCardsAdapter extends RecyclerView.Adapter<RecyclerView.Vi
} }
@Override @Override
public void onContextualCardUpdated(int cardType, List<ContextualCard> contextualCards) { public void onContextualCardUpdated(List<ContextualCard> contextualCards) {
//TODO(b/112245748): Should implement a DiffCallback so we can use notifyItemChanged() //TODO(b/112245748): Should implement a DiffCallback so we can use notifyItemChanged()
// instead. // instead.
if (contextualCards == null) { if (contextualCards == null) {

View File

@@ -58,7 +58,7 @@ public class ConditionContextualCardController implements ContextualCardControll
@Override @Override
public void onDataUpdated(List<ContextualCard> cardList) { public void onDataUpdated(List<ContextualCard> cardList) {
mListener.onContextualCardUpdated(getCardType(), cardList); mListener.onContextualCardUpdated(cardList);
} }
@Override @Override