forked from Jasonette/JASONETTE-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJasonComponent.java
More file actions
189 lines (172 loc) · 8.57 KB
/
Copy pathJasonComponent.java
File metadata and controls
189 lines (172 loc) · 8.57 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.jasonette.seed.Component;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.jasonette.seed.Core.JasonViewActivity;
import com.jasonette.seed.Helper.JasonHelper;
import com.jasonette.seed.Section.JasonLayout;
import org.json.JSONObject;
import timber.log.Timber;
public class JasonComponent {
public static View build(View view, final JSONObject component, final JSONObject parent, final Context root_context) {
float width = 0;
float height = 0;
int corner_radius = 0;
view.setTag(component);
JSONObject style = JasonHelper.style(component, root_context);
try{
if(parent == null) {
// Layer type
width = RelativeLayout.LayoutParams.WRAP_CONTENT;
height = RelativeLayout.LayoutParams.WRAP_CONTENT;
if (style.has("height")) {
try {
height = (int) JasonHelper.pixels(root_context, style.getString("height"), "vertical");
if (style.has("ratio")) {
Float ratio = JasonHelper.ratio(style.getString("ratio"));
width = height * ratio;
}
} catch (Exception e) {
}
}
if (style.has("width")) {
try {
width = (int) JasonHelper.pixels(root_context, style.getString("width"), "horizontal");
if (style.has("ratio")) {
Float ratio = JasonHelper.ratio(style.getString("ratio"));
height = width / ratio;
}
} catch (Exception e) {
}
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)width, (int)height);
view.setLayoutParams(layoutParams);
} else {
// Section item type
LinearLayout.LayoutParams layoutParams = JasonLayout.autolayout(null, parent, component, root_context);
view.setLayoutParams(layoutParams);
}
if (style.has("background")) {
int color = JasonHelper.parse_color(style.getString("background"));
view.setBackgroundColor(color);
}
if(style.has("opacity"))
{
try {
float opacity = (float) style.getDouble("opacity");
view.setAlpha(opacity);
}
catch (Exception ex) {
}
}
// padding
int padding_left = (int)JasonHelper.pixels(root_context, "0", "horizontal");
int padding_right = (int)JasonHelper.pixels(root_context, "0", "horizontal");
int padding_top = (int)JasonHelper.pixels(root_context, "0", "horizontal");
int padding_bottom = (int)JasonHelper.pixels(root_context, "0", "horizontal");
if (style.has("padding")) {
padding_left = (int)JasonHelper.pixels(root_context, style.getString("padding"), "horizontal");
padding_right = padding_left;
padding_top = padding_left;
padding_bottom = padding_left;
}
// overwrite if more specific values exist
if (style.has("padding_left")) {
padding_left = (int)JasonHelper.pixels(root_context, style.getString("padding_left"), "horizontal");
}
if (style.has("padding_right")) {
padding_right = (int)JasonHelper.pixels(root_context, style.getString("padding_right"), "horizontal");
}
if (style.has("padding_top")) {
padding_top = (int)JasonHelper.pixels(root_context, style.getString("padding_top"), "vertical");
}
if (style.has("padding_bottom")) {
padding_bottom = (int)JasonHelper.pixels(root_context, style.getString("padding_bottom"), "vertical");
}
if (style.has("corner_radius")) {
float corner = JasonHelper.pixels(root_context, style.getString("corner_radius"), "horizontal");
int color = ContextCompat.getColor(root_context, android.R.color.transparent);
GradientDrawable cornerShape = new GradientDrawable();
cornerShape.setShape(GradientDrawable.RECTANGLE);
if (style.has("background")) {
color = JasonHelper.parse_color(style.getString("background"));
}
cornerShape.setColor(color);
cornerShape.setCornerRadius(corner);
// border + corner_radius handling
if (style.has("border_width")){
int border_width = (int)JasonHelper.pixels(root_context, style.getString("border_width"), "horizontal");
if(border_width > 0){
int border_color;
if (style.has("border_color")){
border_color = JasonHelper.parse_color(style.getString("border_color"));
} else {
border_color = JasonHelper.parse_color("#000000");
}
cornerShape.setStroke(border_width, border_color);
}
}
cornerShape.invalidateSelf();
view.setBackground(cornerShape);
} else {
// border handling (no corner radius)
if (style.has("border_width")){
int border_width = (int)JasonHelper.pixels(root_context, style.getString("border_width"), "horizontal");
if(border_width > 0){
int border_color;
if (style.has("border_color")){
border_color = JasonHelper.parse_color(style.getString("border_color"));
} else {
border_color = JasonHelper.parse_color("#000000");
}
GradientDrawable cornerShape = new GradientDrawable();
cornerShape.setStroke(border_width, border_color);
cornerShape.invalidateSelf();
view.setBackground(cornerShape);
}
}
}
view.setPadding(padding_left, padding_top, padding_right, padding_bottom);
return view;
} catch (Exception e){
Timber.w(e);
return new View(root_context);
}
}
public static void addListener(final View view, final Context root_context){
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
JSONObject component = (JSONObject)v.getTag();
try {
if (component.has("action")) {
JSONObject action = component.getJSONObject("action");
((JasonViewActivity) root_context).call(action.toString(), new JSONObject().toString(), "{}", v.getContext());
} else if (component.has("href")) {
JSONObject href = component.getJSONObject("href");
JSONObject action = new JSONObject().put("type", "$href").put("options", href);
((JasonViewActivity) root_context).call(action.toString(), new JSONObject().toString(), "{}", v.getContext());
} else {
// NONE Explicitly stated.
// Need to bubble up all the way to the root viewholder.
View cursor = view;
while(cursor.getParent() != null) {
JSONObject item = (JSONObject)(((View)cursor.getParent()).getTag());
if (item!=null && (item.has("action") || item.has("href"))) {
((View)cursor.getParent()).performClick();
break;
} else {
cursor = (View) cursor.getParent();
}
}
}
} catch (Exception e) {
Timber.w(e);
}
}
};
view.setOnClickListener(clickListener);
}
}