Search in sources :

Example 1 with BannerCard

use of com.tmall.wireless.tangram.structure.card.BannerCard in project Tangram-Android by alibaba.

the class Card method createCell.

public static BaseCell createCell(@Nullable Card parent, @NonNull MVHelper resolver, @NonNull JSONObject cellData, @NonNull ServiceManager serviceManager, boolean appended) {
    if (cellData != null) {
        BaseCell cell = null;
        String cellType = cellData.optString(Card.KEY_TYPE);
        if ((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 BaseCell.NaN;
                }
                // ensure service manager
                cell.serviceManager = serviceManager;
            } else {
                if (Utils.isCard(cellData)) {
                    switch(cellType) {
                        case TangramBuilder.TYPE_CONTAINER_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_1C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_2C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_3C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_4C_FLOW:
                        case TangramBuilder.TYPE_CONTAINER_5C_FLOW:
                            CardResolver cardResolver = serviceManager.getService(CardResolver.class);
                            Card gridCard = cardResolver.create(cellType);
                            gridCard.serviceManager = serviceManager;
                            gridCard.parseWith(cellData, resolver);
                            parent.addChildCard(gridCard);
                            break;
                        case TangramBuilder.TYPE_CONTAINER_BANNER:
                            BannerCard bannerCard = new BannerCard();
                            bannerCard.serviceManager = serviceManager;
                            bannerCard.parseWith(cellData, resolver);
                            List<BaseCell> bannerChildren = bannerCard.getCells();
                            if (bannerChildren.size() > 0) {
                                cell = bannerCard.getCells().get(0);
                            }
                            break;
                        case TangramBuilder.TYPE_CONTAINER_SCROLL:
                            LinearScrollCard linearScrollCard = new LinearScrollCard();
                            linearScrollCard.serviceManager = serviceManager;
                            linearScrollCard.parseWith(cellData, resolver);
                            List<BaseCell> scrollChildren = linearScrollCard.getCells();
                            if (scrollChildren.size() > 0) {
                                cell = linearScrollCard.getCells().get(0);
                            }
                            break;
                    }
                    if (cell != null) {
                        cell.serviceManager = serviceManager;
                        if (parent != null) {
                            cell.parent = parent;
                            cell.parentId = parent.id;
                        }
                    } else {
                        return BaseCell.NaN;
                    }
                } else {
                    cell = new BaseCell(cellType);
                    cell.serviceManager = serviceManager;
                    if (parent != null) {
                        cell.parent = parent;
                        cell.parentId = parent.id;
                    }
                }
            }
            if (parent != null) {
                parent.parseCell(resolver, cellData, cell, appended);
            } else {
                resolver.parseCell(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;
                if (parent != null) {
                    cell.parent = parent;
                    cell.parentId = parent.id;
                    parent.parseCell(resolver, cellData, cell, appended);
                } else {
                    resolver.parseCell(cell, cellData);
                }
                cell.setStringType(cellType);
                return cell;
            } else {
                return BaseCell.NaN;
            }
        }
    }
    return BaseCell.NaN;
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) BannerCard(com.tmall.wireless.tangram.structure.card.BannerCard) LinearScrollCard(com.tmall.wireless.tangram.structure.card.LinearScrollCard) BannerCard(com.tmall.wireless.tangram.structure.card.BannerCard) LinearScrollCard(com.tmall.wireless.tangram.structure.card.LinearScrollCard)

Example 2 with BannerCard

use of com.tmall.wireless.tangram.structure.card.BannerCard in project Tangram-Android by alibaba.

the class SampleDataParser method addCardStyle.

private void addCardStyle(JSONObject cardData, Card card) {
    try {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Float.class, new JsonSerializer<Float>() {

            @Override
            public JsonElement serialize(final Float src, final Type typeOfSrc, final JsonSerializationContext context) {
                try {
                    if (src.isInfinite() || src.isNaN()) {
                        return new JsonPrimitive(0f);
                    }
                    BigDecimal value = BigDecimal.valueOf(src);
                    return new JsonPrimitive(value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return new JsonPrimitive(0f);
            }
        });
        Gson gson = gsonBuilder.create();
        GridCard.GridStyle gridStyle = new GridCard.GridStyle();
        if (card instanceof BannerCard) {
            gridStyle.aspectRatio = 3.223f;
        }
        cardData.put(Card.KEY_STYLE, new JSONObject(gson.toJson(gridStyle)));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonPrimitive(com.google.gson.JsonPrimitive) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) BigDecimal(java.math.BigDecimal) JSONException(org.json.JSONException) GridCard(com.tmall.wireless.tangram.structure.card.GridCard) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) JsonElement(com.google.gson.JsonElement) JsonSerializationContext(com.google.gson.JsonSerializationContext) BannerCard(com.tmall.wireless.tangram.structure.card.BannerCard)

Aggregations

BannerCard (com.tmall.wireless.tangram.structure.card.BannerCard)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSerializationContext (com.google.gson.JsonSerializationContext)1 BaseCell (com.tmall.wireless.tangram.structure.BaseCell)1 GridCard (com.tmall.wireless.tangram.structure.card.GridCard)1 LinearScrollCard (com.tmall.wireless.tangram.structure.card.LinearScrollCard)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1