A program is just a finite state machine.
public interface Mutable<T> {
T getValue();
void setValue(T value);
}
Why bother to wrap to value?
public static void bindCheckBox(CheckBox checkBox, final Mutable<Boolean> value)
{
checkBox.setChecked(value.getValue());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
value.setValue(isChecked);
}
});
}
/**
* Bind the radio buttons to a set of values
* @param radioGroup
* @param model
* @param map a bidirectional map from values to radio button ids, and it must be 1:1 relationship.
*/
public static <T> void bindRadioGroup(final RadioGroup radioGroup, final Mutable<T> model, final BiMap<T, Integer> map)
{
T value = model.getValue();
int id = map.get(value);
radioGroup.check(id);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
T newValue = map.inverse().get(checkedId);
model.setValue(newValue);
}
});
}
The view model acts as an intermediary between the view and the model, and is responsible for handling the view logic.
Activity
Fragment
Adapter
View Model
View
Data
App States