diff --git a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/FormElementController.java b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/FormElementController.java index fe71054..87f0fe2 100644 --- a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/FormElementController.java +++ b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/FormElementController.java @@ -12,6 +12,8 @@ public abstract class FormElementController { private final String name; private FormModel model; private View view; + private boolean visible; + private boolean enabled; /** * Constructs a new instance with the specified name. @@ -20,8 +22,32 @@ public abstract class FormElementController { * @param name the name of this instance */ protected FormElementController(Context ctx, String name) { + this(ctx, name, true); + } + + /** + * Constructs a new instance with the specified name. + * + * @param ctx the Android context + * @param name the name of this instance + * @param visible visibility flag + */ + protected FormElementController(Context ctx, String name, boolean visible) { + this(ctx, name, visible, true); + } + + /** + * Constructs a new instance with the specified name. + * + * @param ctx the Android context + * @param name the name of this instance + * @param visible visibility flag + */ + protected FormElementController(Context ctx, String name, boolean visible, boolean enabled) { this.context = ctx; this.name = name; + this.visible = visible; + this.enabled = enabled; } /** @@ -87,4 +113,31 @@ public boolean isViewCreated() { * Refreshes the view of this element to reflect current model. */ public abstract void refresh(); + + public boolean isVisible() { + return visible; + } + + /** + * Sets the element visibility + * + * @param visible + */ + public void setVisible(boolean visible) { + this.visible = visible; + if(view != null) { + view.setVisibility(visible ? View.VISIBLE : View.GONE); + } + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + refreshUIEnabled(enabled); + } + + protected abstract void refreshUIEnabled(boolean enabled); } \ No newline at end of file diff --git a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/EditTextController.java b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/EditTextController.java index 0c41319..2727f3a 100644 --- a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/EditTextController.java +++ b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/EditTextController.java @@ -176,4 +176,5 @@ private void refresh(EditText editText) { public void refresh() { refresh(getEditText()); } + } \ No newline at end of file diff --git a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/FormSectionController.java b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/FormSectionController.java index f7b17b1..65146f2 100644 --- a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/FormSectionController.java +++ b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/FormSectionController.java @@ -33,12 +33,24 @@ public class FormSectionController extends FormElementController { * @param ctx the Android context * @param name the name of the section * @param title the title of the section to display + * @param visible the section visibility */ - public FormSectionController(Context ctx, String name, String title) { - super(ctx, name); + public FormSectionController(Context ctx, String name, String title, boolean visible) { + super(ctx, name, visible); this.title = title; } + /** + * Creates a new instance of a form section with a specified name and title. + * + * @param ctx the Android context + * @param name the name of the section + * @param title the title of the section to display + */ + public FormSectionController(Context ctx, String name, String title) { + this(ctx, name, title, true); + } + /** * Creates a new instance of a form section with a specified title. The name of the section is generated randomly. * This method can be used when you do not need to refer to a section by name. @@ -79,6 +91,7 @@ public FormElementController addElement(FormElementController element) { if (elements.containsKey(element.getName())) { throw new IllegalArgumentException("Element with that name already exists"); } else { + element.setVisible(isVisible()); elements.put(element.getName(), element); orderedElements.add(element); return element; @@ -174,6 +187,8 @@ protected View createView() { view = layoutInflater.inflate(R.layout.separator, null); } + view.setVisibility(isVisible() ? View.VISIBLE : View.GONE); + return view; } @@ -183,4 +198,27 @@ public void refresh() { element.refresh(); } } + + @Override + public void setVisible(boolean visible) { + for (FormElementController element : orderedElements) { + element.setVisible(visible); + } + super.setVisible(visible); + } + + @Override + public void setEnabled(boolean enabled) { + for (FormElementController element : orderedElements) { + element.setEnabled(enabled); + } + super.setEnabled(enabled); + } + + @Override + protected void refreshUIEnabled(boolean enabled) { + for (FormElementController element : orderedElements) { + element.refresh(); + } + } } diff --git a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/LabeledFieldController.java b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/LabeledFieldController.java index cfc6169..f7d0306 100644 --- a/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/LabeledFieldController.java +++ b/nexusdialog/src/main/java/com/github/dkharrat/nexusdialog/controllers/LabeledFieldController.java @@ -128,6 +128,13 @@ protected View createView() { FrameLayout container = (FrameLayout)view.findViewById(R.id.field_container); container.addView(getFieldView()); + view.setVisibility(this.isVisible() ? View.VISIBLE : View.GONE); + refreshUIEnabled(this.isEnabled()); + return view; } + + protected void refreshUIEnabled(boolean enabled) { + fieldView.setEnabled(enabled); + } } \ No newline at end of file