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 1293d5cf64..979a03d331 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java @@ -27,6 +27,7 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragme import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfileFragment; import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfilePlugin; import info.nightscout.androidaps.plugins.ProfileNS.NSProfileFragment; @@ -100,14 +101,9 @@ public class SWDefinition { ) .add(new SWScreen(R.string.nsclientinternal_title) .skippable(true) - .add(new SWEditUrl() - .preferenceId(R.string.key_nsclientinternal_url) - .label(R.string.nsclientinternal_url_title) - .comment(R.string.nsclientinternal_url_dialogmessage)) - .add(new SWEditString() - .preferenceId(R.string.key_nsclientinternal_api_secret) - .label(R.string.nsclientinternal_secret_dialogtitle) - .comment(R.string.nsclientinternal_secret_dialogmessage)) + .add(new SWInfotext() + .label(R.string.nsclientinfotext)) + .add(new SWBreak()) .add(new SWButton() .text(R.string.enable_nsclient) .action(() -> { @@ -119,7 +115,29 @@ public class SWDefinition { MainApp.bus().post(new EventSWUpdate(true)); }) .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) + .add(new SWEditUrl() + .preferenceId(R.string.key_nsclientinternal_url) + .label(R.string.nsclientinternal_url_title) + .comment(R.string.nsclientinternal_url_dialogmessage)) + .add(new SWEditString() + .validator(text -> text.length() >= 12) + .preferenceId(R.string.key_nsclientinternal_api_secret) + .label(R.string.nsclientinternal_secret_dialogtitle) + .comment(R.string.nsclientinternal_secret_dialogmessage)) + .add(new SWBreak()) + .add(new SWEventListener(this) + .label(R.string.status) + .initialStatus(NSClientPlugin.getPlugin().status) + .listener(new Object() { + @Subscribe + public void onEventNSClientStatus(EventNSClientStatus event) { + MainApp.bus().post(new EventSWLabel(event.status)); + } + }) + ) + .add(new SWBreak()) .validator(() -> NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth) + .visibility(() -> !(NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth)) ) .add(new SWScreen(R.string.patientage) .skippable(false) diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java index 30dceac55c..a837325447 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java @@ -18,6 +18,8 @@ import info.nightscout.androidaps.startupwizard.events.EventSWLabel; public class SWEventListener extends SWItem { private static Logger log = LoggerFactory.getLogger(SWEventListener.class); + private int textLabel = 0; + private String status = ""; TextView textView; Object listener; SWDefinition definition; @@ -28,6 +30,16 @@ public class SWEventListener extends SWItem { MainApp.bus().register(this); } + public SWEventListener label(int newLabel) { + this.textLabel = newLabel; + return this; + } + + public SWEventListener initialStatus(String status) { + this.status = status; + return this; + } + public SWEventListener listener(Object listener) { this.listener = listener; return this; @@ -39,17 +51,21 @@ public class SWEventListener extends SWItem { textView = new TextView(context); textView.setId(view.generateViewId()); + textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status); layout.addView(textView); if (listener != null) - MainApp.bus().register(listener); + try { + MainApp.bus().register(listener); + } catch (Exception ignored) {} } @Subscribe public void onEventSWLabel(final EventSWLabel l) { + status = l.label; if (definition != null && definition.getActivity() != null) definition.getActivity().runOnUiThread(() -> { if (textView != null) - textView.setText(l.label); + textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status); }); } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWTextValidator.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWTextValidator.java new file mode 100644 index 0000000000..6c91967574 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWTextValidator.java @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.startupwizard; + +public interface SWTextValidator { + boolean isValid(String text); +} diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditString.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditString.java index 0490e93363..a77bc3d1c8 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditString.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditString.java @@ -13,10 +13,15 @@ import android.widget.TextView; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.startupwizard.SWTextValidator; +import info.nightscout.utils.SP; + public class SWEditString extends SWItem { private static Logger log = LoggerFactory.getLogger(SWEditString.class); + private SWTextValidator validator = null; + public SWEditString() { super(Type.STRING); } @@ -41,6 +46,7 @@ public class SWEditString extends SWItem { editText.setId(view.generateViewId()); editText.setInputType(InputType.TYPE_CLASS_TEXT); editText.setMaxLines(1); + editText.setText(SP.getString(preferenceId, "")); layout.addView(editText); super.generateDialog(view, layout); @@ -51,7 +57,8 @@ public class SWEditString extends SWItem { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - save(s.toString()); + if (validator != null && validator.isValid(s.toString())) + save(s.toString()); } @Override @@ -60,4 +67,13 @@ public class SWEditString extends SWItem { }); } + public SWEditString preferenceId(int preferenceId) { + this.preferenceId = preferenceId; + return this; + } + + public SWEditString validator(SWTextValidator validator) { + this.validator = validator; + return this; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditUrl.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditUrl.java index ec03887949..96b0322299 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditUrl.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWEditUrl.java @@ -5,6 +5,7 @@ import android.graphics.Typeface; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; +import android.util.Patterns; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; @@ -13,6 +14,11 @@ import android.widget.TextView; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.startupwizard.events.EventSWLabel; +import info.nightscout.utils.SP; + public class SWEditUrl extends SWItem { private static Logger log = LoggerFactory.getLogger(SWEditUrl.class); @@ -40,6 +46,7 @@ public class SWEditUrl extends SWItem { editText.setId(View.generateViewId()); editText.setInputType(InputType.TYPE_CLASS_TEXT); editText.setMaxLines(1); + editText.setText(SP.getString(preferenceId, "")); layout.addView(editText); super.generateDialog(view, layout); @@ -50,7 +57,10 @@ public class SWEditUrl extends SWItem { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - save(s.toString()); + if (Patterns.WEB_URL.matcher(s).matches()) + save(s.toString()); + else + MainApp.bus().post(new EventSWLabel(MainApp.gs(R.string.error_url_not_valid))); } @Override @@ -58,4 +68,10 @@ public class SWEditUrl extends SWItem { } }); } + + public SWEditUrl preferenceId(int preferenceId) { + this.preferenceId = preferenceId; + return this; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWItem.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWItem.java index 861a9b1916..2d6095c6e1 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWItem.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWItem.java @@ -66,12 +66,7 @@ public class SWItem { return this; } - public SWItem preferenceId(int preferenceId) { - this.preferenceId = preferenceId; - return this; - } - - public void save(String value) { + public void save(String value) { SP.putString(preferenceId, value); MainApp.bus().post(new EventPreferenceChange(preferenceId)); MainApp.bus().post(new EventSWUpdate()); diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWRadioButton.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWRadioButton.java index 5b01b1d05d..9ff7ac5f9c 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWRadioButton.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/elements/SWRadioButton.java @@ -64,4 +64,10 @@ public class SWRadioButton extends SWItem { layout.addView(radioGroup); super.generateDialog(view, layout); } + + public SWRadioButton preferenceId(int preferenceId) { + this.preferenceId = preferenceId; + return this; + } + } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 372b4e658b..2b7d5b1bb2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1074,4 +1074,5 @@ Configure Sensitivity plugin Sensitivity plugin is used for sensitivity detection and COB calculation. For more info visit: https://github.com/MilosKozak/AndroidAPS/wiki/Sensitivity-detection-and-COB + NSClient handles connection to Nightscout. You can skip this part now but you will not be able to pass objectives.