Files
packages_apps_Settings/src/com/android/settings/homepage/ContextualCardsAdapter.java
Fan Zhang 29aaf62410 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
2018-09-13 14:04:07 -07:00

116 lines
4.1 KiB
Java

/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.homepage;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ContextualCardsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements ContextualCardUpdateListener {
static final int SPAN_COUNT = 2;
private static final String TAG = "ContextualCardsAdapter";
private static final int HALF_WIDTH = 1;
private static final int FULL_WIDTH = 2;
private final Context mContext;
private final ControllerRendererPool mControllerRendererPool;
private final List<ContextualCard> mContextualCards;
public ContextualCardsAdapter(Context context, ContextualCardManager manager) {
mContext = context;
mContextualCards = new ArrayList<>();
mControllerRendererPool = manager.getControllerRendererPool();
setHasStableIds(true);
}
@Override
public long getItemId(int position) {
return mContextualCards.get(position).hashCode();
}
@Override
public int getItemViewType(int position) {
return mContextualCards.get(position).getCardType();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int cardType) {
final ContextualCardRenderer renderer = mControllerRendererPool.getRenderer(mContext,
cardType);
final int viewType = renderer.getViewType();
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return renderer.createViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int cardType = mContextualCards.get(position).getCardType();
final ContextualCardRenderer renderer = mControllerRendererPool.getRenderer(mContext,
cardType);
renderer.bindView(holder, mContextualCards.get(position));
}
@Override
public int getItemCount() {
return mContextualCards.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
final ContextualCard card = mContextualCards.get(position);
//TODO(b/114009676): may use another field to make decision. still under review.
if (card.isHalfWidth()) {
return HALF_WIDTH;
}
return FULL_WIDTH;
}
});
}
}
@Override
public void onContextualCardUpdated(List<ContextualCard> contextualCards) {
//TODO(b/112245748): Should implement a DiffCallback so we can use notifyItemChanged()
// instead.
if (contextualCards == null) {
mContextualCards.clear();
} else {
mContextualCards.clear();
mContextualCards.addAll(contextualCards);
}
notifyDataSetChanged();
}
}