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

package com.wd.display.comp.parser;

import java.util.HashMap;
import java.util.Map;

import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.people.component.comp.layoutdata.AbsGroup;
import com.people.component.comp.layoutdata.AbsSection;
import com.people.component.comp.layoutmanager.ItemLayoutManager;
import com.people.entity.custom.comp.CompBean;

/**
 * section和layoutManager管理基类<BR>
 *
 * @author zhangbo
 * @version [V1.0.0, 2022/3/25]
 * @since V1.0.0
 */
public abstract class AbsSectionParser {

    protected final Map<String, Class<?>> sections = new HashMap<>();

    protected final Map<String, Class<?>> layoutManagers = new HashMap<>();

    /**
     * 获取样式管理对象
     *
     * @param section section对象
     * @param <T> 泛型
     * @return 样式管理对象
     */
    public <T extends ItemLayoutManager<?>> T parseLayoutManager(AbsSection<?, ?> section) {
        if (section == null) {
            Logger.t(getLogTag()).e( "parseLayoutManager, section is null");
            return null;
        }
        return parseLayoutManager(section.getCompType() + "$" + section.getCompStyle());
    }

    /**
     * 获取样式管理对象
     */
    public <T extends ItemLayoutManager<?>> T parseLayoutManager(String key) {
        // 先获取LABEL$LABEL-01
        Class<?> layoutClazz = layoutManagers.get(key);
        if (layoutClazz == null) {
            Logger.t(getLogTag()).e( "parseLayoutManager, layoutClazz is null");
            return null;
        } else {
            try {
                return (T) layoutClazz.newInstance();
            } catch (Exception e) {
                Logger.t(getLogTag()).e( "parseLayoutManager, failed Exception: " + e.getMessage());
                return null;
            }
        }

    }

    /**
     * 获取section对象
     *
     * @param group group对象
     * @param compBean comp数据
     * @param <T> 泛型
     * @return section对象
     */
    public <T extends AbsSection<?, ?>> T parseSection(AbsGroup group, CompBean compBean) {
        if (compBean == null) {
            Logger.t(getLogTag()).e( "parseLayoutManager, section is null");
            return null;
        }

        String style = compBean.getCompStyle();
        String type = compBean.getCompType();

        if(TextUtils.isEmpty(style) || TextUtils.isEmpty(type)){
            return null;
        }
        // 优先获取 compType$compStyle,获取不到再获取compType
        String key = compBean.getCompType() + "$" + compBean.getCompStyle();
        Class<?> sectionClazz = sections.get(key);
        if (sectionClazz == null) {
            sectionClazz = sections.get(compBean.getCompType());
        }
        if (sectionClazz == null) {
            Logger.t(getLogTag()).e( "parseSection, no matching sectionClazz "+compBean.getCompType() + "$" + compBean.getCompStyle());
            return null;
        }
        T absSection = null;
        try {
            absSection = (T) sectionClazz.getConstructor(AbsGroup.class, CompBean.class).newInstance(group, compBean);
        } catch (Exception e) {
            Logger.t(getLogTag()).e( "parseSection, Exception: " + e.getMessage());
        }
        return absSection;
    }

    /**
     * 获取日志tag
     * 
     * @return 日志tag
     */
    protected abstract String getLogTag();

    public void setLayoutManager(){
        if(layoutManagers.size() == 0){
            layoutManagers.putAll(NewsSectionParserHelper.LAYOUT_MANGER_MAP);
        }
    }

}