use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.
the class PojoDataParser method createCell.
protected BaseCell createCell(@NonNull MVHelper resolver, @Nullable JSONObject cellData, ServiceManager serviceManager) {
if (cellData != null) {
BaseCell cell = null;
String cellType = cellData.optString(Card.KEY_TYPE);
if ((resolver != null && resolver.resolver().getViewClass(cellType) != null) || Utils.isCard(cellData)) {
if (resolver.resolver().isCompatibleType(cellType)) {
cell = Utils.newInstance(resolver.resolver().getCellClass(cellType));
// do not display when newInstance failed
if (cell == null) {
return null;
}
cell.serviceManager = serviceManager;
} else {
if (Utils.isCard(cellData)) {
switch(cellType) {
// TODO support parse inline flow card
case TangramBuilder.TYPE_CONTAINER_BANNER:
cell = new BannerEntityCard();
break;
case TangramBuilder.TYPE_CONTAINER_SCROLL:
cell = new LinearScrollEntityCard();
break;
}
if (cell != null) {
cell.serviceManager = serviceManager;
}
} else {
cell = new BaseCell(cellType);
cell.serviceManager = serviceManager;
}
}
if (cell != null) {
resolver.parseCell(resolver, cell, cellData);
cell.setStringType(cellType);
}
return cell;
} else {
// support virtual view at layout
BaseCellBinderResolver componentBinderResolver = serviceManager.getService(BaseCellBinderResolver.class);
if (componentBinderResolver.has(cellType)) {
cell = new BaseCell(cellType);
cell.serviceManager = serviceManager;
resolver.parseCell(resolver, cell, cellData);
cell.setStringType(cellType);
return cell;
} else {
return null;
}
}
}
return null;
}
use of com.tmall.wireless.tangram.structure.BaseCell 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();
}
use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.
the class OnePlusNCard method convertLayoutHelper.
@Nullable
@Override
public LayoutHelper convertLayoutHelper(@Nullable LayoutHelper oldHelper) {
OnePlusNLayoutHelper layoutHelper;
if (oldHelper instanceof OnePlusNLayoutHelper) {
layoutHelper = (OnePlusNLayoutHelper) oldHelper;
} else {
layoutHelper = new OnePlusNLayoutHelper();
}
layoutHelper.setItemCount(mCells.size());
if (mCells.size() == 1) {
BaseCell isHeader = mCells.get(0);
layoutHelper.setHasHeader(Style.DISPLAY_BLOCK.equalsIgnoreCase(isHeader.optStringParam(Style.KEY_DISPLAY)));
layoutHelper.setHasFooter(false);
} else if (mCells.size() >= 2) {
BaseCell isHeader = mCells.get(0);
layoutHelper.setHasHeader(Style.DISPLAY_BLOCK.equalsIgnoreCase(isHeader.optStringParam(Style.KEY_DISPLAY)));
BaseCell isFooter = mCells.get(mCells.size() - 1);
layoutHelper.setHasFooter(Style.DISPLAY_BLOCK.equalsIgnoreCase(isFooter.optStringParam(Style.KEY_DISPLAY)));
}
if (style instanceof ColumnStyle) {
ColumnStyle columnStyle = (ColumnStyle) style;
if (columnStyle.cols != null && columnStyle.cols.length > 0)
layoutHelper.setColWeights(columnStyle.cols);
else
layoutHelper.setColWeights(EMPTY_WEIGHTS);
if (!Float.isNaN(style.aspectRatio)) {
layoutHelper.setAspectRatio(style.aspectRatio);
}
if (columnStyle.rows != null && columnStyle.rows.length > 0) {
layoutHelper.setRowWeight(columnStyle.rows[0]);
}
layoutHelper.setBgColor(columnStyle.bgColor);
layoutHelper.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]);
layoutHelper.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]);
}
return layoutHelper;
}
use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.
the class OnePlusNCard method parseHeaderCell.
@Override
protected void parseHeaderCell(@NonNull MVHelper resolver, @Nullable JSONObject header) {
BaseCell mHeader = createCell(resolver, header, true);
ensureBlock(mHeader);
}
use of com.tmall.wireless.tangram.structure.BaseCell in project Tangram-Android by alibaba.
the class BannerEntityCard method parseWith.
@Override
public void parseWith(@NonNull JSONObject data, @NonNull MVHelper resolver) {
this.mCells.clear();
id = data.optString(Card.KEY_ID, id == null ? "" : id);
this.cardType = data.optString(Card.KEY_TYPE);
// parsing header
JSONObject header = data.optJSONObject(Card.KEY_HEADER);
parseHeaderCell(resolver, header);
// parsing body
JSONArray componentArray = data.optJSONArray(Card.KEY_ITEMS);
if (componentArray != null) {
final int cellLength = componentArray.length();
for (int i = 0; i < cellLength; i++) {
final JSONObject cellData = componentArray.optJSONObject(i);
BaseCell cell = createCell(resolver, cellData, true);
try {
if (cell != null) {
cell.extras.put(MVResolver.KEY_INDEX, cell.pos);
}
} catch (JSONException e) {
}
}
}
// parsing footer
JSONObject footer = data.optJSONObject(Card.KEY_FOOTER);
parseFooterCell(resolver, footer);
JSONObject styleJson = data.optJSONObject(Card.KEY_STYLE);
parseStyle(styleJson);
}
Aggregations