SW nsclient screen finalizing

This commit is contained in:
Milos Kozak 2018-05-20 21:36:17 +02:00
parent 52a4f9af14
commit f24d296836
8 changed files with 91 additions and 18 deletions

View file

@ -27,6 +27,7 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragme
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; 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.LocalProfileFragment;
import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfilePlugin; import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfilePlugin;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfileFragment; import info.nightscout.androidaps.plugins.ProfileNS.NSProfileFragment;
@ -100,14 +101,9 @@ public class SWDefinition {
) )
.add(new SWScreen(R.string.nsclientinternal_title) .add(new SWScreen(R.string.nsclientinternal_title)
.skippable(true) .skippable(true)
.add(new SWEditUrl() .add(new SWInfotext()
.preferenceId(R.string.key_nsclientinternal_url) .label(R.string.nsclientinfotext))
.label(R.string.nsclientinternal_url_title) .add(new SWBreak())
.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 SWButton() .add(new SWButton()
.text(R.string.enable_nsclient) .text(R.string.enable_nsclient)
.action(() -> { .action(() -> {
@ -119,7 +115,29 @@ public class SWDefinition {
MainApp.bus().post(new EventSWUpdate(true)); MainApp.bus().post(new EventSWUpdate(true));
}) })
.visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) .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) .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) .add(new SWScreen(R.string.patientage)
.skippable(false) .skippable(false)

View file

@ -18,6 +18,8 @@ import info.nightscout.androidaps.startupwizard.events.EventSWLabel;
public class SWEventListener extends SWItem { public class SWEventListener extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWEventListener.class); private static Logger log = LoggerFactory.getLogger(SWEventListener.class);
private int textLabel = 0;
private String status = "";
TextView textView; TextView textView;
Object listener; Object listener;
SWDefinition definition; SWDefinition definition;
@ -28,6 +30,16 @@ public class SWEventListener extends SWItem {
MainApp.bus().register(this); 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) { public SWEventListener listener(Object listener) {
this.listener = listener; this.listener = listener;
return this; return this;
@ -39,17 +51,21 @@ public class SWEventListener extends SWItem {
textView = new TextView(context); textView = new TextView(context);
textView.setId(view.generateViewId()); textView.setId(view.generateViewId());
textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status);
layout.addView(textView); layout.addView(textView);
if (listener != null) if (listener != null)
MainApp.bus().register(listener); try {
MainApp.bus().register(listener);
} catch (Exception ignored) {}
} }
@Subscribe @Subscribe
public void onEventSWLabel(final EventSWLabel l) { public void onEventSWLabel(final EventSWLabel l) {
status = l.label;
if (definition != null && definition.getActivity() != null) if (definition != null && definition.getActivity() != null)
definition.getActivity().runOnUiThread(() -> { definition.getActivity().runOnUiThread(() -> {
if (textView != null) if (textView != null)
textView.setText(l.label); textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status);
}); });
} }

View file

@ -0,0 +1,5 @@
package info.nightscout.androidaps.startupwizard;
public interface SWTextValidator {
boolean isValid(String text);
}

View file

@ -13,10 +13,15 @@ import android.widget.TextView;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.startupwizard.SWTextValidator;
import info.nightscout.utils.SP;
public class SWEditString extends SWItem { public class SWEditString extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWEditString.class); private static Logger log = LoggerFactory.getLogger(SWEditString.class);
private SWTextValidator validator = null;
public SWEditString() { public SWEditString() {
super(Type.STRING); super(Type.STRING);
} }
@ -41,6 +46,7 @@ public class SWEditString extends SWItem {
editText.setId(view.generateViewId()); editText.setId(view.generateViewId());
editText.setInputType(InputType.TYPE_CLASS_TEXT); editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setMaxLines(1); editText.setMaxLines(1);
editText.setText(SP.getString(preferenceId, ""));
layout.addView(editText); layout.addView(editText);
super.generateDialog(view, layout); super.generateDialog(view, layout);
@ -51,7 +57,8 @@ public class SWEditString extends SWItem {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { 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 @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;
}
} }

View file

@ -5,6 +5,7 @@ import android.graphics.Typeface;
import android.text.Editable; import android.text.Editable;
import android.text.InputType; import android.text.InputType;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -13,6 +14,11 @@ import android.widget.TextView;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 { public class SWEditUrl extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWEditUrl.class); private static Logger log = LoggerFactory.getLogger(SWEditUrl.class);
@ -40,6 +46,7 @@ public class SWEditUrl extends SWItem {
editText.setId(View.generateViewId()); editText.setId(View.generateViewId());
editText.setInputType(InputType.TYPE_CLASS_TEXT); editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setMaxLines(1); editText.setMaxLines(1);
editText.setText(SP.getString(preferenceId, ""));
layout.addView(editText); layout.addView(editText);
super.generateDialog(view, layout); super.generateDialog(view, layout);
@ -50,7 +57,10 @@ public class SWEditUrl extends SWItem {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { 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 @Override
@ -58,4 +68,10 @@ public class SWEditUrl extends SWItem {
} }
}); });
} }
public SWEditUrl preferenceId(int preferenceId) {
this.preferenceId = preferenceId;
return this;
}
} }

View file

@ -66,12 +66,7 @@ public class SWItem {
return this; return this;
} }
public SWItem preferenceId(int preferenceId) { public void save(String value) {
this.preferenceId = preferenceId;
return this;
}
public void save(String value) {
SP.putString(preferenceId, value); SP.putString(preferenceId, value);
MainApp.bus().post(new EventPreferenceChange(preferenceId)); MainApp.bus().post(new EventPreferenceChange(preferenceId));
MainApp.bus().post(new EventSWUpdate()); MainApp.bus().post(new EventSWUpdate());

View file

@ -64,4 +64,10 @@ public class SWRadioButton extends SWItem {
layout.addView(radioGroup); layout.addView(radioGroup);
super.generateDialog(view, layout); super.generateDialog(view, layout);
} }
public SWRadioButton preferenceId(int preferenceId) {
this.preferenceId = preferenceId;
return this;
}
} }

View file

@ -1074,4 +1074,5 @@
<string name="sensitivitysetup">Configure Sensitivity plugin</string> <string name="sensitivitysetup">Configure Sensitivity plugin</string>
<string name="setupwizard_sensitivity_description">Sensitivity plugin is used for sensitivity detection and COB calculation. For more info visit:</string> <string name="setupwizard_sensitivity_description">Sensitivity plugin is used for sensitivity detection and COB calculation. For more info visit:</string>
<string name="setupwizard_sensitivity_url">https://github.com/MilosKozak/AndroidAPS/wiki/Sensitivity-detection-and-COB</string> <string name="setupwizard_sensitivity_url">https://github.com/MilosKozak/AndroidAPS/wiki/Sensitivity-detection-and-COB</string>
<string name="nsclientinfotext">NSClient handles connection to Nightscout. You can skip this part now but you will not be able to pass objectives.</string>
</resources> </resources>