AbsGroup.java 6.17 KB
/*
 * Copyright (c) People Technologies Co., Ltd. 2019-2022. All rights reserved.
 */

package com.wd.display.comp.layoutdata;

import java.util.ArrayList;
import java.util.List;

import com.people.entity.custom.comp.CompBean;
import com.people.entity.custom.comp.GroupBean;

/**
 * Group基类<BR>
 *
 * @author zhangbo
 * @version [V1.0.0, 2021/12/8]
 * @since V1.0.0
 */
public abstract class AbsGroup extends AbsLayout {
    private AbsPage parent;

    /**
     * group在页面里的下标
     */
    private int position;

    /**
     * group在recyclerView里的下标
     */
    private int startPosition;

    // 每个楼层页码
    private int pageNum = 1;

    // 是否推荐策略;2:否,1:是
    private String groupStrategy;


    private List<AbsSection> sections;

    /**
     * 楼层所在页面所有楼层中的位置
     */
    private int layerIndex = 0;

    /**
     *  是否是页面的最后一个楼层
     */
    private boolean isLastLayer = false;


    // 记录AbsGroup所在GroupBean对象
    private GroupBean groupBean;

    //记录的组件支持加载更多数据 (改组件对象必然在页面的最后一个楼层的最后一个组件上)
    private CompBean  supportLoadMoreDataComp;


    /**
     * gzq
     *
     * @param dataBean
     * @param parent
     */
    public AbsGroup(GroupBean dataBean, AbsPage parent) {
        this(parent);
        this.setId(dataBean.getId());
        parseData(dataBean);
    }

    public int getLayerIndex() {
        return layerIndex;
    }

    public void setLayerIndex(int layerIndex) {
        this.layerIndex = layerIndex;
    }

    /**
     * 构造器
     *
     * @param groupId 楼层id
     * @param parent  page对象
     */
    public AbsGroup(String groupId, AbsPage parent) {
        this(parent);
        this.setId(groupId);
    }

    /**
     * gzq
     *
     * @param parent
     */
    public AbsGroup(AbsPage parent) {
        this.parent = parent;
    }

    /**
     * getParent
     *
     * @param <T>
     * @return
     */
    public <T extends AbsPage> T getParent() {
        return (T) this.parent;
    }

    public void setParent(AbsPage parent) {
        this.parent = parent;
    }

    /**
     * getSection
     *
     * @return AbsSection
     */
    public List<AbsSection> getSections() {
        if (sections == null) {
            sections = new ArrayList<>();
        }
        return sections;
    }

    public void setSections(List<AbsSection> sections) {
        this.sections = sections;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public int getStartPosition() {
        return startPosition;
    }

    public void setStartPosition(int startPosition) {
        this.startPosition = startPosition;
    }


    @Override
    public ILayoutPresenter getLayoutPresenter() {
        return getParent().getLayoutPresenter();
    }

    /**
     * 解析楼层,组装section
     *
     * @param groupBean 楼层数据
     */
    public void parseData(GroupBean groupBean) {
        getSections().clear();
        if (groupBean.getComps() == null) {
            return;
        }
        addParseData(groupBean.getComps());
    }

    /**
     * 楼层添加新的组件信息以供解析成section
     *
     * @param compList
     */
    public void addParseData(List<CompBean> compList) {

        if (compList == null) {
            return;
        }
        for (CompBean compBean : compList) {
            if (compBean == null) {
                continue;
            }
            if (getParent() != null && getParent().getSectionParser() != null) {
                AbsSection<?, ?> section = getParent().getSectionParser().parseSection(this, compBean);
                if (section != null) {
                    section.setCompBean(compBean);
                    // 添加section
                    getSections().add(section);
                }
            }

        }

    }

    /**
     * 解析Group数据
     *
     * @param groupBean 楼层对象,携带组件和稿件数据
     * @param loadMore  true:楼层增加数据 ;false:楼层第一次数据
     */
    public void parseGroupData(GroupBean groupBean, boolean loadMore) {
        if (groupBean == null) {
            return;
        }

        if (loadMore) {
            // 记录已经存在的数据量
            setDisplayItemCount(this.getSections().size());
            // 解析group数据,生成section列表
            addParseData(groupBean.getComps());
        } else {
            setDisplayItemCount(0);
            // 解析group数据,生成section列表
            parseData(groupBean);
        }


        for (AbsSection<?, ?> section : this.getSections()) {
            if (section == null) {
                continue;
            }
            // 解析结果
            section.parsePartialData();
        }
    }

    public void removeSection(int index) {
        sections.remove(index);
        getLayoutPresenter().onRefreshPageSuccess();
    }

    public void removeSectionsByType(String type) {
        for (AbsSection section : sections) {
            if (type.equals(section.getCompType())) {
                sections.remove(section);
            }
        }
        getLayoutPresenter().onRefreshPageSuccess();
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public boolean isLastLayer() {
        return isLastLayer;
    }

    public void setLastLayer(boolean lastLayer) {
        isLastLayer = lastLayer;
    }

    public String getGroupStrategy() {
        return groupStrategy;
    }

    public void setGroupStrategy(String groupStrategy) {
        this.groupStrategy = groupStrategy;
    }

    public GroupBean getGroupBean() {
        return groupBean;
    }

    public void setGroupBean(GroupBean groupBean) {
        this.groupBean = groupBean;
    }

    public CompBean getSupportLoadMoreDataComp() {
        return supportLoadMoreDataComp;
    }

    public void setSupportLoadMoreDataComp(CompBean supportLoadMoreDataComp) {
        this.supportLoadMoreDataComp = supportLoadMoreDataComp;
    }
}