AbsSectionParser.java
3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* 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);
}
}
}