Search in sources :

Example 11 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method loadMoreCard.

public void loadMoreCard() {
    CardLoadSupport loadSupport = getService(CardLoadSupport.class);
    if (loadSupport == null)
        return;
    List<Card> groups = findGroups(new Predicate<Card>() {

        @Override
        public boolean isMatch(Card data) {
            return data.loadMore && data.hasMore && !data.loading && !TextUtils.isEmpty(data.load);
        }
    });
    if (groups.size() != 0) {
        loadSupport.loadMore(groups.get(groups.size() - 1));
    }
}
Also used : CardLoadSupport(com.tmall.wireless.tangram.support.async.CardLoadSupport) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 12 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method removeBy.

/**
 * NOTE new API
 * Remove target cell. TODO handle nested card, cell in staggered, cell in onePlusN
 * @param data
 */
protected void removeBy(BaseCell data) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (data != null && mGroupBasicAdapter != null && layoutManager != null) {
        int removePosition = mGroupBasicAdapter.getPositionByItem(data);
        if (removePosition >= 0) {
            int cardIdx = mGroupBasicAdapter.findCardIdxFor(removePosition);
            Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
            card.removeCellSilently(data);
            List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
            LayoutHelper emptyLayoutHelper = null;
            if (layoutHelpers != null && cardIdx >= 0 && cardIdx < layoutHelpers.size()) {
                for (int i = 0, size = layoutHelpers.size(); i < size; i++) {
                    LayoutHelper layoutHelper = layoutHelpers.get(i);
                    int start = layoutHelper.getRange().getLower();
                    int end = layoutHelper.getRange().getUpper();
                    if (end < removePosition) {
                    // do nothing
                    } else if (start <= removePosition && removePosition <= end) {
                        int itemCount = layoutHelper.getItemCount() - 1;
                        if (itemCount > 0) {
                            layoutHelper.setItemCount(itemCount);
                            layoutHelper.setRange(start, end - 1);
                        } else {
                            emptyLayoutHelper = layoutHelper;
                        }
                    } else if (removePosition < start) {
                        layoutHelper.setRange(start - 1, end - 1);
                    }
                }
                if (emptyLayoutHelper != null) {
                    final List<LayoutHelper> newLayoutHelpers = new LinkedList<>(layoutHelpers);
                    newLayoutHelpers.remove(emptyLayoutHelper);
                    layoutManager.setLayoutHelpers(newLayoutHelpers);
                }
                mGroupBasicAdapter.removeComponent(data);
            }
        }
    }
}
Also used : LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) LinkedList(java.util.LinkedList) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 13 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class FusionCard method getCards.

@SuppressWarnings("unchecked")
@Override
public List<Card> getCards(final CardResolver cardResolver) {
    if (serviceManager == null)
        return Collections.emptyList();
    final CardLoadSupport loadSupport = serviceManager.getService(CardLoadSupport.class);
    final MVHelper cellResolver = serviceManager.getService(MVHelper.class);
    final ViewFactory viewFactory = serviceManager.getService(ViewFactory.class);
    if (loadSupport == null || cellResolver == null || cardResolver == null)
        return Collections.emptyList();
    if (style instanceof DelegateStyle) {
        DelegateStyle dStyle = (DelegateStyle) this.style;
        final Card anchorCard = cardResolver.create(String.valueOf(TangramBuilder.TYPE_SINGLE_COLUMN));
        final BaseCell emptyCell = new BaseCell(TangramBuilder.TYPE_EMPTY_VIEW);
        if (emptyCell.style != null)
            emptyCell.style.height = 0;
        anchorCard.addCell(emptyCell);
        final Card headerCard = cardResolver.create(String.valueOf(TangramBuilder.TYPE_STICKY_START));
        final BaseCell tabCell = mCells.get(0);
        mCells.remove(0);
        // asign header id
        headerCard.id = id + "-tabheader";
        headerCard.addCell(tabCell);
        final DelegateStyle.CardInfo info = dStyle.cardInfos.get(0);
        final String cardType = info.type;
        final Card originalCard = cardResolver.create(String.valueOf(info.type));
        // no cell is needed
        originalCard.setStringType(info.type);
        // assign id;
        originalCard.id = id;
        originalCard.parseWith(info.data, cellResolver);
        final Card contentCard = new FusionContentCard(originalCard, (tabCell instanceof SwitchTabHeaderCell) ? (SwitchTabHeaderCell) tabCell : null, 0);
        if (viewFactory != null) {
            View view = viewFactory.create();
            int defaultHeight = viewFactory.getDefaultHeight();
            if (view != null && defaultHeight > 0)
                contentCard.enablePlaceholderView(view, defaultHeight);
        }
        contentCard.loadMore = true;
        contentCard.hasMore = true;
        if (TextUtils.isEmpty(contentCard.load))
            contentCard.load = this.load;
        if (TextUtils.isEmpty(contentCard.load))
            return Collections.emptyList();
        if (mCells.size() > 0) {
            contentCard.addCells(mCells);
        }
        if (mPendingCells.size() > 0) {
            contentCard.addCells(mPendingCells);
        }
        if ((tabCell instanceof SwitchTabHeaderCell)) {
            final SwitchTabHeaderCell switchHeader = (SwitchTabHeaderCell) tabCell;
            switchHeader.setSwitchTabTrigger(new SwitchTabTrigger() {

                private Map<Integer, IndexCache> mCacheMap = new HashMap<>();

                private int currentIndex = 0;

                private Card currentCard = contentCard;

                @Override
                public void invalidate(int index) {
                    mCacheMap.remove(index);
                    if (currentIndex == index) {
                        currentIndex = -1;
                    }
                }

                @Override
                public void switchTo(int index, @NonNull Cell cell, Map<String, Object> params) {
                    switchTo(index, FusionCard.this.id, cell, params);
                }

                @Override
                public void switchTo(int index, String id, @NonNull Cell cell, @Nullable Map<String, Object> params) {
                    if (currentIndex == index) {
                        // reassign id
                        currentCard.id = id;
                        currentCard.setParams(params);
                        return;
                    }
                    if (currentIndex >= 0)
                        mCacheMap.put(currentIndex, new IndexCache(currentIndex, currentCard));
                    IndexCache indexCache = mCacheMap.get(index);
                    if (indexCache == null) {
                        Card newCard = cardResolver.create(cardType);
                        // no cell is needed
                        newCard.setStringType(cardType);
                        // assign id
                        newCard.id = id;
                        newCard.parseWith(info.data, cellResolver);
                        newCard = new FusionContentCard(newCard, switchHeader, index);
                        newCard.loadMore = true;
                        newCard.hasMore = true;
                        if (viewFactory != null) {
                            View view = viewFactory.create();
                            int defaultHeight = viewFactory.getDefaultHeight();
                            if (view != null && defaultHeight > 0)
                                newCard.enablePlaceholderView(view, defaultHeight);
                        }
                        indexCache = new IndexCache(index, newCard);
                    }
                    indexCache.card.setParams(params);
                    // TODO: not generic!!!
                    Engine engine = (Engine) FusionCard.this.serviceManager;
                    if (engine != null) {
                        // first stopScrolling!!!
                        engine.scrollToPosition(headerCard);
                        engine.replaceCard(currentCard, indexCache.card);
                        currentCard = indexCache.card;
                        if (!currentCard.loaded)
                            loadSupport.loadMore(currentCard);
                    }
                    currentIndex = index;
                }
            });
        } else {
            return Collections.emptyList();
        }
        return Arrays.asList(anchorCard, headerCard, contentCard);
    }
    return Collections.emptyList();
}
Also used : DelegateStyle(com.tmall.wireless.tangram.structure.style.DelegateStyle) BaseCell(com.tmall.wireless.tangram.structure.BaseCell) HashMap(java.util.HashMap) CardLoadSupport(com.tmall.wireless.tangram.support.async.CardLoadSupport) MVHelper(com.tmall.wireless.tangram.MVHelper) View(android.view.View) WrapperCard(com.tmall.wireless.tangram.dataparser.concrete.WrapperCard) Card(com.tmall.wireless.tangram.dataparser.concrete.Card) IDelegateCard(com.tmall.wireless.tangram.dataparser.concrete.IDelegateCard) JSONObject(org.json.JSONObject) Cell(com.tmall.wireless.tangram.dataparser.concrete.Cell) BaseCell(com.tmall.wireless.tangram.structure.BaseCell) Engine(com.tmall.wireless.tangram.Engine)

Example 14 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class GridCard method convertChildLayoutHelper.

private void convertChildLayoutHelper(@Nullable RangeGridLayoutHelper gridHelper, GridCard parentCard) {
    for (int i = 0, size = parentCard.getChildren().size(); i < size; i++) {
        Range<Integer> range = parentCard.getChildren().keyAt(i);
        Card child = parentCard.getChildren().valueAt(i);
        Style style = child.style;
        if (style instanceof GridStyle && child instanceof GridCard) {
            final GridStyle gridStyle = (GridStyle) style;
            final GridCard gridCard = (GridCard) child;
            if (!gridCard.getChildren().isEmpty()) {
                convertChildLayoutHelper(gridHelper, gridCard);
            }
            GridRangeStyle rangeStyle = new GridRangeStyle();
            int totalColumn = gridCard.mColumn;
            if (gridStyle.column > 0) {
                totalColumn = gridStyle.column;
                rangeStyle.setSpanCount(gridStyle.column);
            } else {
                rangeStyle.setSpanCount(totalColumn);
            }
            rangeStyle.setSpanSizeLookup(new CellSpanSizeLookup(gridCard.getCells(), totalColumn));
            rangeStyle.setVGap(gridStyle.vGap);
            rangeStyle.setHGap(gridStyle.hGap);
            rangeStyle.setAutoExpand(gridStyle.autoExpand);
            if (gridStyle.cols != null && gridStyle.cols.length > 0) {
                rangeStyle.setWeights(gridStyle.cols);
            }
            if (!Float.isNaN(gridStyle.aspectRatio)) {
                rangeStyle.setAspectRatio(gridStyle.aspectRatio);
            }
            rangeStyle.setBgColor(style.bgColor);
            rangeStyle.setMargin(style.margin[Style.MARGIN_LEFT_INDEX], style.margin[Style.MARGIN_TOP_INDEX], style.margin[Style.MARGIN_RIGHT_INDEX], style.margin[Style.MARGIN_BOTTOM_INDEX]);
            rangeStyle.setPadding(style.padding[Style.MARGIN_LEFT_INDEX], style.padding[Style.MARGIN_TOP_INDEX], style.padding[Style.MARGIN_RIGHT_INDEX], style.padding[Style.MARGIN_BOTTOM_INDEX]);
            if (!TextUtils.isEmpty(style.bgImgUrl)) {
                if (serviceManager != null && serviceManager.getService(CardSupport.class) != null) {
                    final CardSupport support = serviceManager.getService(CardSupport.class);
                    rangeStyle.setLayoutViewBindListener(new BindListener(style) {

                        @Override
                        public void onBind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
                            support.onBindBackgroundView(layoutView, gridCard);
                        }
                    });
                    rangeStyle.setLayoutViewUnBindListener(new UnbindListener(style) {

                        @Override
                        public void onUnbind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
                            support.onUnbindBackgroundView(layoutView, gridCard);
                        }
                    });
                } else {
                    rangeStyle.setLayoutViewBindListener(new BindListener(style));
                    rangeStyle.setLayoutViewUnBindListener(new UnbindListener(style));
                }
            } else {
                rangeStyle.setLayoutViewBindListener(null);
                rangeStyle.setLayoutViewUnBindListener(null);
            }
            gridHelper.addRangeStyle(range.getLower().intValue(), range.getUpper().intValue(), rangeStyle);
        }
    }
}
Also used : View(android.view.View) Card(com.tmall.wireless.tangram.dataparser.concrete.Card) CardSupport(com.tmall.wireless.tangram.support.CardSupport) Style(com.tmall.wireless.tangram.dataparser.concrete.Style) GridRangeStyle(com.alibaba.android.vlayout.layout.RangeGridLayoutHelper.GridRangeStyle) GridRangeStyle(com.alibaba.android.vlayout.layout.RangeGridLayoutHelper.GridRangeStyle) BaseLayoutHelper(com.alibaba.android.vlayout.layout.BaseLayoutHelper)

Example 15 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class GridCard method offsetChildCard.

@Override
public void offsetChildCard(Card anchorCard, int offset) {
    if (anchorCard == null) {
        return;
    }
    ArrayMap<Range<Integer>, Card> newChildren = new ArrayMap<>();
    boolean startOffset = false;
    for (int i = 0, size = mChildren.size(); i < size; i++) {
        Range<Integer> key = mChildren.keyAt(i);
        Card child = mChildren.valueAt(i);
        if (child == anchorCard) {
            Range<Integer> newKey = Range.create(key.getLower().intValue(), key.getUpper().intValue() + offset);
            newChildren.put(newKey, child);
            startOffset = true;
            continue;
        }
        if (startOffset) {
            Range<Integer> newKey = Range.create(key.getLower().intValue() + offset, key.getUpper().intValue() + offset);
            newChildren.put(newKey, child);
        } else {
            newChildren.put(key, child);
        }
    }
    mChildren.clear();
    mChildren.putAll((SimpleArrayMap<? extends Range<Integer>, ? extends Card>) newChildren);
}
Also used : SimpleArrayMap(android.support.v4.util.SimpleArrayMap) ArrayMap(android.support.v4.util.ArrayMap) Range(com.alibaba.android.vlayout.Range) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Aggregations

Card (com.tmall.wireless.tangram.dataparser.concrete.Card)15 CardLoadSupport (com.tmall.wireless.tangram.support.async.CardLoadSupport)6 View (android.view.View)5 Range (com.alibaba.android.vlayout.Range)4 JSONObject (org.json.JSONObject)4 RecyclerView (android.support.v7.widget.RecyclerView)3 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)3 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)3 ArrayList (java.util.ArrayList)3 TargetApi (android.annotation.TargetApi)2 Handler (android.os.Handler)2 NonNull (android.support.annotation.NonNull)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 Picasso (com.squareup.picasso.Picasso)2 MVHelper (com.tmall.wireless.tangram.MVHelper)2 GroupBasicAdapter (com.tmall.wireless.tangram.core.adapter.GroupBasicAdapter)2 RatioTextView (com.tmall.wireless.tangram.example.data.RatioTextView)2 SimpleImgView (com.tmall.wireless.tangram.example.data.SimpleImgView)2 SingleImageView (com.tmall.wireless.tangram.example.data.SingleImageView)2