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

Add CheckBoxController #10

Merged
merged 3 commits into from
Nov 1, 2015
Merged
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
@@ -0,0 +1,141 @@
package com.github.dkharrat.nexusdialog.controllers;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.github.dkharrat.nexusdialog.R;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Represents a field that allows a user to select multiple items from checkboxes.
* <p/>
* For the field value, the associated FormModel must return either a Set<String> or a Set<0-based index>, representing the
* currently selected items. Which representation to use is specified by the constructor. In either representation, no
* selection can be represented by returning {@code null} for the value of the field.
*/
public class CheckBoxController extends LabeledFieldController {
private final static int CHECKBOX_ID = 101010;
private final List<String> items;
private final List<?> values;

/**
* Constructs a new instance of a checkboxes field.
*
* @param ctx the Android context
* @param name the name of the field
* @param labelText the label to display beside the field. Set to {@code null} to not show a label
* @param isRequired indicates if the field is required or not
* @param items a list of Strings defining the selection items to show
* @param useItemsAsValues if true, {@code CheckBoxController} expects the associated form model to use
* the same string of the selected item when getting or setting the field; otherwise,
* {@code CheckBoxController} expects the form model to use index (as an Integer) to
* represent the selected item
*/
public CheckBoxController(Context ctx, String name, String labelText, boolean isRequired, List<String> items, boolean useItemsAsValues) {
this(ctx, name, labelText, isRequired, items, useItemsAsValues ? items : null);
}

/**
* Constructs a new instance of a checkboxes field.
*
* @param ctx the Android context
* @param name the name of the field
* @param labelText the label to display beside the field
* @param isRequired indicates if the field is required or not
* @param items a list of Strings defining the selection items to show
* @param values a list of Objects representing the values to set the form model on a selection (in
* the same order as the {@code items}.
*/
public CheckBoxController(Context ctx, String name, String labelText, boolean isRequired, List<String> items, List<?> values) {
super(ctx, name, labelText, isRequired);
this.items = items;
this.values = values;
}

@Override
protected View createFieldView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup checkboxContainer = (ViewGroup) inflater.inflate(R.layout.form_checkbox_container, null);

CheckBox checkBox;
int nbItem = items.size();
for (int index = 0; index < nbItem; index++) {
checkBox = new CheckBox(getContext());
checkBox.setText(items.get(index));
checkBox.setId(CHECKBOX_ID + index);
checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = buttonView.getId() - CHECKBOX_ID;
Object value = areValuesDefined() ? values.get(position): position;
Set<Object> modelValues = retrieveModelValues();
if (isChecked) {
modelValues.add(value);
} else {
modelValues.remove(value);
}
getModel().setValue(getName(), modelValues);
}
});

checkboxContainer.addView(checkBox);
}
return checkboxContainer;
}

@Override
public void refresh() {
Set<Object> modelValues = retrieveModelValues();
ViewGroup layout = getContainer();

CheckBox checkbox;
int nbItem = items.size();
for (int index = 0; index < nbItem; index++) {
checkbox = (CheckBox) layout.findViewById(CHECKBOX_ID + index);
checkbox.setChecked(
modelValues.contains(
areValuesDefined() ? checkbox.getText() : index
)
);
}
}

/**
* Returns the status of the values entry.
*
* @return true if values entry can be used. false otherwise.
*/
private boolean areValuesDefined() {
return values != null;
}

/**
* Returns the values hold in the model.
*
* @return The values from the model.
*/
private Set<Object> retrieveModelValues() {
Set<Object> modelValues = (Set<Object>) getModel().getValue(getName());
if (modelValues == null) {
modelValues = new HashSet<>();
}
return modelValues;
}

/**
* Returns the View containing the checkboxes.
*
* @return The View containing the checkboxes.
*/
private ViewGroup getContainer() {
return (ViewGroup) getView().findViewById(R.id.form_checkbox_container);
}
}
8 changes: 8 additions & 0 deletions nexusdialog/src/main/res/layout/form_checkbox_container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/form_checkbox_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dkharrat.nexusdialog.sample;

import com.github.dkharrat.nexusdialog.FormWithAppCompatActivity;
import com.github.dkharrat.nexusdialog.controllers.CheckBoxController;
import com.github.dkharrat.nexusdialog.controllers.EditTextController;
import com.github.dkharrat.nexusdialog.controllers.FormSectionController;
import com.github.dkharrat.nexusdialog.controllers.SelectionController;
Expand All @@ -19,7 +20,7 @@ public class SimpleExample extends FormWithAppCompatActivity {
section.addElement(new EditTextController(this, "firstName", "First name"));
section.addElement(new EditTextController(this, "lastName", "Last name"));
section.addElement(new SelectionController(this, "gender", "Gender", true, "Select", Arrays.asList("Male", "Female"), true));

section.addElement(new CheckBoxController(this, "hobbies", "You like", true, Arrays.asList("sport", "gaming", "relaxation", "development"), true));
getFormController().addSection(section);
}
}