Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow form elements visibility #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,5 @@ private void refresh(EditText editText) {
public void refresh() {
refresh(getEditText());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -174,6 +187,8 @@ protected View createView() {
view = layoutInflater.inflate(R.layout.separator, null);
}

view.setVisibility(isVisible() ? View.VISIBLE : View.GONE);

return view;
}

Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}