From bb38d7644b8148762272ed8d2fa62a28c58f941e Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Fri, 27 Apr 2018 13:30:45 +0300 Subject: [PATCH] move onClick in SWRadioButton --- .../startupwizard/SWDefinition.java | 2 +- .../androidaps/startupwizard/SWItem.java | 22 +++++++- .../startupwizard/SWRadioButton.java | 13 +++-- .../androidaps/startupwizard/SWString.java | 33 +++++++----- .../androidaps/startupwizard/SWUrl.java | 30 +++++------ .../startupwizard/SetupWizardActivity.java | 51 ++++--------------- .../main/res/layout/activity_setupwizard.xml | 1 + 7 files changed, 77 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java index 9f2ae113a7..44bf9ccea9 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java @@ -32,7 +32,7 @@ public class SWDefinition { // List all the screens here // todo: SWValidator ?!? add(new SWScreen(R.string.nsclientinternal_title) - .skippable(false) + .skippable(true) .add(new SWUrl().preferenceId(R.string.key_nsclientinternal_url).label(R.string.nsclientinternal_url_title).comment(R.string.nsclientinternal_url_dialogmessage)) .add(new SWString().preferenceId(R.string.key_nsclientinternal_api_secret).label(R.string.nsclientinternal_secret_dialogtitle).comment(R.string.nsclientinternal_secret_dialogmessage)) .validator(() -> NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth) diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java index 43ea2c153b..d0bccd11e0 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java @@ -1,12 +1,20 @@ package info.nightscout.androidaps.startupwizard; +import android.content.Context; import android.view.View; +import android.widget.LinearLayout; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.utils.SP; public class SWItem { + private static Logger log = LoggerFactory.getLogger(SWItem.class); enum Type { NONE, URL, @@ -21,6 +29,8 @@ public class SWItem { Integer label; Integer comment; int preferenceId; + private List labels; + private List values; public SWItem(Type type) { @@ -62,7 +72,17 @@ public class SWItem { MainApp.bus().post(new EventPreferenceChange(preferenceId)); } - public void generateDialog(View view){ + public void setOptions(List labels, List values){ + this.labels = labels; + this.values = values; + } + public static LinearLayout generateLayout(View view) { + LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); + layout.removeAllViews(); + return layout; + } + + public void generateDialog(View view, LinearLayout layout){ } } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java index 1928093df9..75da0aa26f 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java @@ -41,10 +41,10 @@ public class SWRadioButton extends SWItem { } @Override - public void generateDialog(View view){ + public void generateDialog(View view, LinearLayout layout){ Context context = view.getContext(); - LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); - layout.removeAllViews(); +// LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); +// layout.removeAllViews(); String[] labels = context.getResources().getStringArray(labelsArray); String[] values = context.getResources().getStringArray(valuesArray); // Get if there is already value in SP @@ -64,12 +64,17 @@ public class SWRadioButton extends SWItem { rdbtn.setText(labels[i]); if(previousValue.equals(values[i])) rdbtn.setChecked(true); -// log.debug("Button ["+labels[i]+"]="+rdbtn.getId()+" value is "+values[rdbtn.getId()]); radioGroup.addView(rdbtn); } } + radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + save(getCheckedValue()); + } + }); layout.addView(radioGroup); } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java index 01e327e3a5..17a113c1b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java @@ -7,10 +7,14 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.List; public class SWString extends SWItem { + private static Logger log = LoggerFactory.getLogger(SWString.class); private List labels; private List values; private String groupName; @@ -23,28 +27,29 @@ public class SWString extends SWItem { this.groupName = name; } + public void setOptions(List labels, List values){ + this.labels = labels; + this.values = values; + } + @Override - public void generateDialog(View view) { + public void generateDialog(View view, LinearLayout layout) { Context context = view.getContext(); - LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); - layout.removeAllViews(); +// LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); +// layout.removeAllViews(); TextView textlabel = new TextView(context); textlabel.setText(groupName); layout.addView(textlabel); - for (int row = 0; row < 1; row++) { - for (int i = 0; i < labels.size(); i++) { - if(values.get(i) != "" && values.get(i) != null) { - EditText editText = new EditText(context); - editText.setId((row * 2) + i); - editText.setText(values.get(i)); - editText.setInputType(InputType.TYPE_CLASS_TEXT); - editText.setMaxLines(1); - layout.addView(editText); - } - } + if(values.get(values.size()-1) != "" && values.get(values.size()-1) != null) { + EditText editText = new EditText(context); + editText.setId(3); + editText.setText(values.get(values.size()-1)); + editText.setInputType(InputType.TYPE_CLASS_TEXT); + editText.setMaxLines(1); + layout.addView(editText); } } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java index b082a62ec8..9ebd4c03b2 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java @@ -6,10 +6,13 @@ import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.List; public class SWUrl extends SWItem { - + private static Logger log = LoggerFactory.getLogger(SWUrl.class); private List labels; private List values; private String groupName; @@ -18,6 +21,7 @@ public class SWUrl extends SWItem { } public void setOptions(List labels, List values){ +// log.debug("Setting options - labels "+labels.size()+" values - "+values.size()); this.labels = labels; this.values = values; } @@ -27,22 +31,18 @@ public class SWUrl extends SWItem { } @Override - public void generateDialog(View view) { + public void generateDialog(View view, LinearLayout layout) { Context context = view.getContext(); - LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); - layout.removeAllViews(); - for (int row = 0; row < 1; row++) { - for (int i = 0; i < labels.size(); i++) { - if(values.get(i) != "" && values.get(i) != null) { - EditText editText = new EditText(context); - editText.setId((row * 2) + i); - editText.setText(values.get(i)); - editText.setInputType(InputType.TYPE_CLASS_TEXT); - editText.setMaxLines(1); - layout.addView(editText); - } - } + + if(values.get(values.size()-1) != "" && values.get(values.size()-1) != null) { + EditText editText = new EditText(context); + editText.setId(1); + // get the last value in list + editText.setText(values.get(values.size()-1)); + editText.setInputType(InputType.TYPE_CLASS_TEXT); + editText.setMaxLines(1); + layout.addView(editText); } } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java index a582e933a0..7efe752116 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java @@ -160,49 +160,20 @@ public class SetupWizardActivity extends AppCompatActivity { skipButton = (Button) findViewById(R.id.skip_button); } + //Generate layout first + LinearLayout layout = info.nightscout.androidaps.startupwizard.SWItem.generateLayout(this.findViewById(R.id.fullscreen_content_fields)); for(int i = 0; i < currentScreen.items.size(); i++){ SWItem currentItem = currentScreen.items.get(i); - if(currentItem.type == URL || currentItem.type == STRING){ - labels.add(currentItem.getLabel()); - comments.add(currentItem.getComment()); - - } else if(currentItem.type == RADIOBUTTON){ - // generate layout dynamically - SWRadioButton radioGroupItems = (SWRadioButton) currentItem; - radioGroupItems.generateDialog(this.findViewById(R.id.fullscreen_content_fields)); - //allow next button if we have something saved in preferences - if(!radioGroupItems.preferenceSet().equals("none")){ - showNextButton(showPage, screens.size()-1); - } - - - radioGroupItems.getRadioGroup().setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(RadioGroup group, int checkedId) { - radioGroupItems.save(radioGroupItems.getCheckedValue()); - if(currentScreen.validator.isValid()) { - showNextButton(showPage, screens.size() - 1); - show(); - } - } - - }); - - } - if(labels.size() > 0){ - // we have some labels lets display them - SWUrl swUrl = new SWUrl(); - swUrl.setOptions(labels, comments); - swUrl.generateDialog(this.findViewById(R.id.fullscreen_content_fields)); - log.debug("Valid input:" +currentScreen.validator.isValid()); -/* if(currentScreen.validator.isValid()) { - showNextButton(showPage, screens.size() - 1); - show(); - }*/ - showNextButton(showPage, screens.size()-1); - } + labels.add(i,currentItem.getLabel()); + comments.add(i,currentItem.getComment()); + currentItem.setOptions(labels, comments); + currentItem.generateDialog(this.findViewById(R.id.fullscreen_content_fields), layout); + } + // Check if input isValid or screen is sckippable + if(currentScreen.validator.isValid() || currentScreen.skippable) { + showNextButton(showPage, screens.size() - 1); + show(); } - } diff --git a/app/src/main/res/layout/activity_setupwizard.xml b/app/src/main/res/layout/activity_setupwizard.xml index 6a7b3ff56d..b2ba03a37f 100644 --- a/app/src/main/res/layout/activity_setupwizard.xml +++ b/app/src/main/res/layout/activity_setupwizard.xml @@ -61,6 +61,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" + android:onClick="showNextPage" android:text="@string/setupwizard_skip" android:visibility="invisible" />