Search in sources :

Example 6 with MVHelper

use of com.tmall.wireless.tangram.MVHelper in project Tangram-Android by alibaba.

the class Card method addCellInternal.

private boolean addCellInternal(@Nullable BaseCell cell, boolean silent) {
    if (cell != null) {
        cell.parentId = id;
        cell.parent = this;
        cell.serviceManager = serviceManager;
        MVHelper mvHelper = getMVHelper();
        if (mvHelper != null) {
            if (mvHelper.isValid(cell, serviceManager)) {
                if (cell.position >= 0 && !TextUtils.isEmpty(load)) {
                    cell.pos = cell.position;
                    mPendingCells.add(cell);
                    return true;
                } else {
                    cell.pos = mHeader != null ? this.mCells.size() + 1 : this.mCells.size();
                }
                if (!silent && mIsActivated) {
                    // do cell added
                    cell.added();
                }
                this.mCells.add(cell);
                if (mFooter != null) {
                    mFooter.pos = cell.pos + 1;
                }
                return true;
            }
        }
    }
    return false;
}
Also used : MVHelper(com.tmall.wireless.tangram.MVHelper)

Example 7 with MVHelper

use of com.tmall.wireless.tangram.MVHelper in project Tangram-Android by alibaba.

the class PojoDataParser method parseGroup.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public List<Card> parseGroup(@NonNull JSONArray data, @NonNull final ServiceManager serviceManager) {
    final CardResolver cardResolver = serviceManager.getService(CardResolver.class);
    Preconditions.checkState(cardResolver != null, "Must register CardResolver into ServiceManager first");
    final MVHelper cellResolver = serviceManager.getService(MVHelper.class);
    Preconditions.checkState(cellResolver != null, "Must register CellResolver into ServiceManager first");
    final int size = data.length();
    final List<Card> result = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        JSONObject cardData = data.optJSONObject(i);
        final Card card = parseSingleGroup(cardData, serviceManager);
        if (card != null) {
            if (card instanceof IDelegateCard) {
                List<Card> cards = ((IDelegateCard) card).getCards(new CardResolver() {

                    @Override
                    public Card create(String type) {
                        Card c = cardResolver.create(type);
                        c.serviceManager = serviceManager;
                        c.id = card.id;
                        c.setStringType(type);
                        c.rowId = card.rowId;
                        return c;
                    }
                });
                for (Card c : cards) {
                    if (c.isValid()) {
                        result.add(c);
                    }
                }
            } else {
                result.add(card);
            }
        }
    }
    cellResolver.resolver().setCards(result);
    return result;
}
Also used : JSONObject(org.json.JSONObject) MVHelper(com.tmall.wireless.tangram.MVHelper) ArrayList(java.util.ArrayList) WrapCellCard(com.tmall.wireless.tangram.structure.card.WrapCellCard) SlideCard(com.tmall.wireless.tangram.structure.card.SlideCard) NonNull(android.support.annotation.NonNull)

Example 8 with MVHelper

use of com.tmall.wireless.tangram.MVHelper 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);
                            loadSupport.reactiveDoLoadMore(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

MVHelper (com.tmall.wireless.tangram.MVHelper)8 NonNull (android.support.annotation.NonNull)3 JSONObject (org.json.JSONObject)3 Card (com.tmall.wireless.tangram.dataparser.concrete.Card)2 BaseCell (com.tmall.wireless.tangram.structure.BaseCell)2 SlideCard (com.tmall.wireless.tangram.structure.card.SlideCard)2 WrapCellCard (com.tmall.wireless.tangram.structure.card.WrapCellCard)2 ArrayList (java.util.ArrayList)2 Nullable (android.support.annotation.Nullable)1 View (android.view.View)1 Engine (com.tmall.wireless.tangram.Engine)1 CardResolver (com.tmall.wireless.tangram.dataparser.concrete.CardResolver)1 Cell (com.tmall.wireless.tangram.dataparser.concrete.Cell)1 IDelegateCard (com.tmall.wireless.tangram.dataparser.concrete.IDelegateCard)1 WrapperCard (com.tmall.wireless.tangram.dataparser.concrete.WrapperCard)1 BannerCard (com.tmall.wireless.tangram.structure.card.BannerCard)1 GridCard (com.tmall.wireless.tangram.structure.card.GridCard)1 DelegateStyle (com.tmall.wireless.tangram.structure.style.DelegateStyle)1 CardLoadSupport (com.tmall.wireless.tangram.support.async.CardLoadSupport)1 ViewManager (com.tmall.wireless.vaf.framework.ViewManager)1