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