Refactor.

This commit is contained in:
Denis 2014-10-28 20:07:20 +02:00
parent cf7eb8e4ab
commit 101c13a4a1

View file

@ -58,8 +58,16 @@ public class AppModeDialog {
getVisibleModes(values, selected, visible);
LinearLayout ll = (LinearLayout) a.getLayoutInflater().inflate(R.layout.mode_toggles, null);
final ToggleButton[] buttons = createDrawerToggles(visible, values, a, ll, onDialogOk);
final boolean[] selectionChangeLoop = new boolean[] {false};
for (int i = 0; i < buttons.length - 1; i++) {
setButtonListener(visible, selected, onClickListener, buttons, i, true);
}
return ll;
}
private static void setButtonListener(final List<ApplicationMode> visible, final Set<ApplicationMode> selected,
final View.OnClickListener onClickListener, final ToggleButton[] buttons,
int i,final boolean singleChoise) {
final boolean[] selectionChangeLoop = new boolean[] {false};
if (buttons[i] != null) {
final int ind = i;
ToggleButton b = buttons[i];
@ -73,7 +81,7 @@ public class AppModeDialog {
}
selectionChangeLoop[0] = true;
try {
handleSelection(visible, selected, true, buttons, ind, buttonAppMode, isChecked);
handleSelection(visible, selected, singleChoise, buttons, ind, buttonAppMode, isChecked);
if (onClickListener != null) {
onClickListener.onClick(null);
}
@ -81,13 +89,9 @@ public class AppModeDialog {
selectionChangeLoop[0] = false;
}
}
});
}
}
return ll;
}
private static ToggleButton[] createDrawerToggles(final List<ApplicationMode> visible, final List<ApplicationMode> modes,
final Activity a, LinearLayout ll, final DialogInterface.OnClickListener onDialogOk) {
@ -207,33 +211,8 @@ public class AppModeDialog {
ViewGroup parent, final boolean singleSelection, final View.OnClickListener onClickListener) {
LinearLayout ll = (LinearLayout) a.getLayoutInflater().inflate(R.layout.mode_toggles, parent);
final ToggleButton[] buttons = createToggles(values, ll, a);
final boolean[] selectionChangeLoop = new boolean[] {false};
for (int i = 0; i < buttons.length; i++) {
if (buttons[i] != null) {
final int ind = i;
ToggleButton b = buttons[i];
final ApplicationMode buttonAppMode = values.get(i);
b.setChecked(selected.contains(buttonAppMode));
b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (selectionChangeLoop[0]) {
return;
}
selectionChangeLoop[0] = true;
try {
handleSelection(values, selected, singleSelection, buttons, ind, buttonAppMode, isChecked);
if (onClickListener != null) {
onClickListener.onClick(null);
}
} finally {
selectionChangeLoop[0] = false;
}
}
});
}
setButtonListener(values, selected, onClickListener, buttons, i, singleSelection);
}
return ll;
}