SWRadioButton cleanup

This commit is contained in:
Milos Kozak 2018-05-03 20:13:27 +02:00
parent 94db444acb
commit cc532b9107

View file

@ -11,17 +11,15 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.startupwizard.events.EventSWUpdate; import info.nightscout.androidaps.startupwizard.events.EventSWUpdate;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
public class SWRadioButton extends SWItem { public class SWRadioButton extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWRadioButton.class); private static Logger log = LoggerFactory.getLogger(SWRadioButton.class);
int labelsArray; int labelsArray;
int valuesArray; int valuesArray;
private RadioGroup radioGroup; private RadioGroup radioGroup;
public boolean somethingChecked = false;
public SWRadioButton() { public SWRadioButton() {
super(Type.RADIOBUTTON); super(Type.RADIOBUTTON);
@ -44,56 +42,33 @@ public class SWRadioButton extends SWItem {
@Override @Override
public void generateDialog(View view, LinearLayout layout) { public void generateDialog(View view, LinearLayout layout) {
Context context = view.getContext(); Context context = view.getContext();
String[] labels = context.getResources().getStringArray(labelsArray);
String[] values = context.getResources().getStringArray(valuesArray);
// Get if there is already value in SP // Get if there is already value in SP
String previousValue = SP.getString(preferenceId, "none"); String previousValue = SP.getString(preferenceId, "none");
// log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue);
radioGroup = new RadioGroup(context); radioGroup = new RadioGroup(context);
radioGroup.clearCheck(); radioGroup.clearCheck();
for (int row = 0; row < 1; row++) {
radioGroup.setOrientation(LinearLayout.VERTICAL); radioGroup.setOrientation(LinearLayout.VERTICAL);
radioGroup.setVisibility(View.VISIBLE); radioGroup.setVisibility(View.VISIBLE);
for (int i = 0; i < labels.length; i++) { for (int i = 0; i < labels().length; i++) {
RadioButton rdbtn = new RadioButton(context); RadioButton rdbtn = new RadioButton(context);
rdbtn.setId((row * 2) + i); rdbtn.setId(View.generateViewId());
rdbtn.setText(labels[i]); rdbtn.setText(labels()[i]);
if (previousValue.equals(values[i])) if (previousValue.equals(values()[i]))
rdbtn.setChecked(true); rdbtn.setChecked(true);
rdbtn.setTag(i);
radioGroup.addView(rdbtn); radioGroup.addView(rdbtn);
} }
}
radioGroup.setOnCheckedChangeListener((group, checkedId) -> { radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
save(); int i = (int) group.findViewById(checkedId).getTag();
save(i);
}); });
layout.addView(radioGroup); layout.addView(radioGroup);
} }
public RadioGroup getRadioGroup() { public void save(int i) {
return this.radioGroup; SP.putString(preferenceId, values()[i]);
}
public String getCheckedValue() {
if (radioGroup != null && radioGroup.getCheckedRadioButtonId() > -1) {
Context context = radioGroup.getRootView().getContext();
String[] values = context.getResources().getStringArray(valuesArray);
return values[radioGroup.getCheckedRadioButtonId()];
} else {
return "none";
}
}
public void save() {
if (!getCheckedValue().equals("none")) {
SP.putString(preferenceId, getCheckedValue());
MainApp.bus().post(new EventPreferenceChange(preferenceId)); MainApp.bus().post(new EventPreferenceChange(preferenceId));
MainApp.bus().post(new EventSWUpdate()); MainApp.bus().post(new EventSWUpdate());
} }
}
} }