Search in sources :

Example 6 with CardLoadSupport

use of com.tmall.wireless.tangram.support.async.CardLoadSupport 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)

Aggregations

Card (com.tmall.wireless.tangram.dataparser.concrete.Card)6 CardLoadSupport (com.tmall.wireless.tangram.support.async.CardLoadSupport)6 View (android.view.View)3 Range (com.alibaba.android.vlayout.Range)3 JSONObject (org.json.JSONObject)3 TargetApi (android.annotation.TargetApi)2 Handler (android.os.Handler)2 NonNull (android.support.annotation.NonNull)2 RecyclerView (android.support.v7.widget.RecyclerView)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 Picasso (com.squareup.picasso.Picasso)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 TestView (com.tmall.wireless.tangram.example.data.TestView)2 TestViewHolder (com.tmall.wireless.tangram.example.data.TestViewHolder)2 SampleClickSupport (com.tmall.wireless.tangram.example.support.SampleClickSupport)2 AsyncLoader (com.tmall.wireless.tangram.support.async.AsyncLoader)2