Compare commits
3 Commits
222d67ca79
...
6aece7bb99
| Author | SHA1 | Date | |
|---|---|---|---|
| 6aece7bb99 | |||
| 7aadcbe8f7 | |||
| df958f61f9 |
@@ -2,10 +2,16 @@ package ru.penkrat.stbf.templates;
|
||||
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface ScreenResolver {
|
||||
|
||||
Screen getScreen(String name);
|
||||
|
||||
Screen getScreen(String name, Object context);
|
||||
|
||||
default <T> Function<T, Screen> getScreenFactory(String name) {
|
||||
return context -> getScreen(name, context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class ReflectionUtils {
|
||||
Method method = context.getClass().getMethod(methodName);
|
||||
method.setAccessible(true);
|
||||
Object result = method.invoke(context);
|
||||
if (clazz.isAssignableFrom(result.getClass())) {
|
||||
if (result != null && clazz.isAssignableFrom(result.getClass())) {
|
||||
return (T) result;
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.penkrat.stbf.templates.xml;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
|
||||
@@ -20,6 +21,18 @@ class Button {
|
||||
@JacksonXmlProperty(isAttribute = true, localName = "if")
|
||||
private String ifCondition;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private boolean requestContact;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private boolean requestLocation;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private String callbackData;
|
||||
|
||||
@JacksonXmlProperty(isAttribute = true)
|
||||
private String url;
|
||||
|
||||
public Button(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ import ru.penkrat.stbf.templates.utils.ReflectionUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -45,7 +43,7 @@ public class XmlScreenResolver implements ScreenResolver {
|
||||
public Screen getScreen(String name) {
|
||||
ScreenItem item = get(name);
|
||||
|
||||
return new TextScreen(item.getText(), buildKeyboard(item.getKeyboard()));
|
||||
return new TextScreen(item.getText(), buildKeyboard(item.getKeyboard(), null));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,18 +66,21 @@ public class XmlScreenResolver implements ScreenResolver {
|
||||
return keyboard;
|
||||
}
|
||||
|
||||
return buildKeyboard(wrapper);
|
||||
return buildKeyboard(wrapper, context);
|
||||
}
|
||||
|
||||
private Keyboard buildKeyboard(KeyboardWrapper wrapper) {
|
||||
private Keyboard buildKeyboard(KeyboardWrapper wrapper, Object context) {
|
||||
Keyboard keyboard = null;
|
||||
if (wrapper.isNotEmpty()) {
|
||||
KeyboardBuilder builder = KeyboardBuilder.newKeyboard();
|
||||
|
||||
for (ButtonsRow row : wrapper.getRows()) {
|
||||
List<Action> buttons = row.getButtons().stream()
|
||||
.filter(btn -> checkIfCondition(context, btn.getIfCondition()))
|
||||
.map(btn -> Action.builder()
|
||||
.text(btn.getText())
|
||||
.requestContact(btn.isRequestContact())
|
||||
.requestLocation(btn.isRequestLocation())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
@@ -91,6 +92,24 @@ public class XmlScreenResolver implements ScreenResolver {
|
||||
return keyboard;
|
||||
}
|
||||
|
||||
private boolean checkIfCondition(Object context, String ifCondition) {
|
||||
if (ifCondition == null || ifCondition.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
if (context == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("true".equalsIgnoreCase(ifCondition)) {
|
||||
return true;
|
||||
}
|
||||
if ("false".equalsIgnoreCase(ifCondition)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ReflectionUtils.getMethodResult(context, ifCondition, Boolean.class);
|
||||
}
|
||||
|
||||
private ScreenItem get(String key) {
|
||||
List<ScreenItem> list = byId.get(key);
|
||||
if (list != null) {
|
||||
|
||||
@@ -32,6 +32,7 @@ public class XmlWriterTest {
|
||||
Button btn1 = new Button("Btn1");
|
||||
btn1.setIfCondition("false");
|
||||
Button btn2 = new Button("Btn2");
|
||||
btn2.setRequestContact(true);
|
||||
ButtonsRow row1 = new ButtonsRow();
|
||||
row1.getButtons().add(btn1);
|
||||
row1.getButtons().add(btn2);
|
||||
|
||||
Reference in New Issue
Block a user