SW validator, language, cleanup

This commit is contained in:
Milos Kozak 2018-05-03 20:06:13 +02:00
parent 36265ab1c6
commit 94db444acb
8 changed files with 159 additions and 165 deletions

View file

@ -1,9 +1,5 @@
package info.nightscout.androidaps.startupwizard;
import android.content.Context;
import android.content.Intent;
import android.widget.RadioButton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,25 +8,20 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
import info.nightscout.androidaps.plugins.Source.SourceNSClientPlugin;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.SP;
public class SWDefinition {
private static Logger log = LoggerFactory.getLogger(SWDefinition.class);
private static SWDefinition swDefinition = null;
public static SWDefinition getInstance() {
if (swDefinition == null)
swDefinition = new SWDefinition();
return swDefinition;
}
android.content.Context context = MainApp.instance().getApplicationContext();
static List<SWScreen> screens = new ArrayList<>();
@ -44,60 +35,42 @@ public class SWDefinition {
}
SWDefinition() {
// List all the screens here
// todo: SWValidator ?!?
add(new SWScreen(R.string.language)
.skippable(false)
.add(new SWRadioButton().option(R.array.languagesArray, R.array.languagesValues).preferenceId(R.string.key_language).label(R.string.language).comment(R.string.setupwizard_language_prompt))
.validator(() -> {
LocaleHelper.setLocale(context, SP.getString(R.string.key_language, "en"));
MainApp.bus().post(new EventRefreshGui(true));
if(!SP.getString(R.string.key_language, "en").equals(LocaleHelper.getLanguage(context))) {
Intent intent = new Intent(MainApp.instance().getApplicationContext(), SetupWizardActivity.class);
intent.putExtra("WIZZARDPAGE", 0);
}
return SP.contains(R.string.key_language);}
))
.skippable(false)
.add(new SWRadioButton().option(R.array.languagesArray, R.array.languagesValues).preferenceId(R.string.key_language).label(R.string.language).comment(R.string.setupwizard_language_prompt))
.validator(() -> {
String lang = SP.getString("language", "en");
LocaleHelper.setLocale(MainApp.instance().getApplicationContext(), lang);
return SP.contains(R.string.key_language);
})
)
.add(new SWScreen(R.string.nsclientinternal_title)
.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(() -> {
return NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth;
})
.validator(() -> NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth)
)
.add(new SWScreen(R.string.patientage)
.skippable(false)
.add(new SWRadioButton().option(R.array.ageArray, R.array.ageValues).preferenceId(R.string.key_age).label(R.string.patientage).comment(R.string.patientage_summary))
.validator(() -> {
return SP.contains(R.string.key_age);
})
.validator(() -> SP.contains(R.string.key_age))
)
.add(new SWScreen(R.string.configbuilder_insulin)
.skippable(false)
.add(new SWPlugin().option(PluginType.INSULIN).label(R.string.configbuilder_insulin).comment(R.string.configbuilder_insulin))
.validator(() -> {
ArrayList<PluginBase> pluginsInCategory = MainApp.getSpecificPluginsList(PluginType.INSULIN);
return pluginsInCategory != null;
})
)
.validator(() -> MainApp.getSpecificPluginsList(PluginType.INSULIN) != null)
)
.add(new SWScreen(R.string.configbuilder_bgsource)
.skippable(false)
.add(new SWPlugin().option(PluginType.BGSOURCE).label(R.string.configbuilder_bgsource).comment(R.string.configbuilder_bgsource))
.validator(() -> {
ArrayList<PluginBase> pluginsInCategory = MainApp.getSpecificPluginsList(PluginType.BGSOURCE);
return pluginsInCategory != null;
})
)
.skippable(false)
.add(new SWPlugin().option(PluginType.BGSOURCE).label(R.string.configbuilder_bgsource).comment(R.string.configbuilder_bgsource))
.validator(() -> MainApp.getSpecificPluginsList(PluginType.BGSOURCE) != null)
)
.add(new SWScreen(R.string.configbuilder_aps)
.skippable(false)
.add(new SWPlugin().option(PluginType.APS).label(R.string.configbuilder_aps).comment(R.string.configbuilder_aps))
.validator(() -> {
ArrayList<PluginBase> pluginsInCategory = MainApp.getSpecificPluginsList(PluginType.APS);
return pluginsInCategory != null;
})
.skippable(false)
.add(new SWPlugin().option(PluginType.APS).label(R.string.configbuilder_aps).comment(R.string.configbuilder_aps))
.validator(() -> MainApp.getSpecificPluginsList(PluginType.APS) != null)
)
;
}

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.startupwizard.events.EventSWUpdate;
public class SWPlugin extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWPlugin.class);
@ -65,6 +66,7 @@ public class SWPlugin extends SWItem {
ConfigBuilderFragment.processOnEnabledCategoryChanged(plugin1, pType);
ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard");
MainApp.bus().post(new EventConfigBuilderChange());
MainApp.bus().post(new EventSWUpdate());
});
layout.addView(radioGroup);
}

View file

@ -12,6 +12,7 @@ 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 {
@ -46,7 +47,7 @@ public class SWRadioButton extends SWItem {
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, "unset");
String previousValue = SP.getString(preferenceId, "none");
// log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue);
radioGroup = new RadioGroup(context);
radioGroup.clearCheck();
@ -66,13 +67,8 @@ public class SWRadioButton extends SWItem {
}
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
save();
MainApp.bus().post(new EventRefreshGui(true));
}
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
save();
});
layout.addView(radioGroup);
@ -92,20 +88,12 @@ public class SWRadioButton extends SWItem {
}
}
public boolean isSomethingChecked() {
return this.somethingChecked;
}
public void save() {
if (!getCheckedValue().equals("none")) {
SP.putString(preferenceId, getCheckedValue());
MainApp.bus().post(new EventPreferenceChange(preferenceId));
MainApp.bus().post(new EventSWUpdate());
}
}
public String preferenceSet() {
return SP.getString(preferenceId, "none");
}
}

View file

@ -1,7 +1,9 @@
package info.nightscout.androidaps.startupwizard;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
@ -10,47 +12,55 @@ import android.widget.TextView;
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.androidaps.startupwizard.events.EventSWUpdate;
import info.nightscout.utils.SP;
public class SWString extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWString.class);
private List<String> labels;
private List<String> values;
private String groupName;
public SWString() {
super(Type.STRING);
}
public void setName(String name){
this.groupName = name;
}
public void setOptions(List<String> labels, List<String> values){
this.labels = labels;
this.values = values;
}
@Override
public void generateDialog(View view, LinearLayout layout) {
Context context = view.getContext();
// LinearLayout layout = (LinearLayout) view.findViewById(view.getId());
// layout.removeAllViews();
TextView textlabel = new TextView(context);
textlabel.setText(groupName);
TextView l = new TextView(context);
l.setId(view.generateViewId());
l.setText(label);
layout.addView(l);
layout.addView(textlabel);
TextView c = new TextView(context);
c.setId(view.generateViewId());
c.setText(label);
layout.addView(c);
if(values.get(values.size()-1) != "" && values.get(values.size()-1) != null) {
EditText editText = new EditText(context);
editText.setId(view.generateViewId());
editText.setText(values.get(values.size()-1));
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setMaxLines(1);
layout.addView(editText);
}
EditText editText = new EditText(context);
editText.setId(view.generateViewId());
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setMaxLines(1);
layout.addView(editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
SP.putString(preferenceId, s.toString());
MainApp.bus().post(new EventPreferenceChange(preferenceId));
MainApp.bus().post(new EventSWUpdate());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}

View file

@ -1,49 +1,64 @@
package info.nightscout.androidaps.startupwizard;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
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.androidaps.startupwizard.events.EventSWUpdate;
import info.nightscout.utils.SP;
public class SWUrl extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWUrl.class);
private List<String> labels;
private List<String> values;
private String groupName;
public SWUrl() {
super(Type.URL);
}
public void setName(String name){
this.groupName = name;
}
public void setOptions(List<String> labels, List<String> values){
this.labels = labels;
this.values = values;
}
@Override
public void generateDialog(View view, LinearLayout layout) {
Context context = view.getContext();
TextView l = new TextView(context);
l.setId(View.generateViewId());
l.setText(label);
layout.addView(l);
if(values.get(values.size()-1) != "" && values.get(values.size()-1) != null) {
EditText editText = new EditText(context);
editText.setId(View.generateViewId());
// 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);
}
TextView c = new TextView(context);
c.setId(View.generateViewId());
c.setText(comment);
layout.addView(c);
EditText editText = new EditText(context);
editText.setId(View.generateViewId());
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setMaxLines(1);
layout.addView(editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
SP.putString(preferenceId, s.toString());
MainApp.bus().post(new EventPreferenceChange(preferenceId));
MainApp.bus().post(new EventSWUpdate());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}

View file

@ -4,106 +4,111 @@ import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus;
import info.nightscout.androidaps.startupwizard.events.EventSWUpdate;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.SP;
public class SetupWizardActivity extends AppCompatActivity {
private List<String> labels = new ArrayList<>();
private List<String> comments = new ArrayList<>();
private TextView screenName;
//logging
private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class);
private TextView screenName;
SWDefinition swDefinition = SWDefinition.getInstance();
List<SWScreen> screens = swDefinition.getScreens();
private int currentWizardPage = 0;
public static final String INTENT_MESSAGE = "WIZZARDPAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocaleHelper.onCreate(this, "en");
setContentView(R.layout.activity_setupwizard);
Intent intent = getIntent();
int showPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
SWDefinition swDefinition = SWDefinition.getInstance();
List<SWScreen> screens = swDefinition.getScreens();
if (screens.size() > 0 && showPage < screens.size()) {
SWScreen currentScreen = screens.get(showPage);
currentWizardPage = showPage;
// show/hide prev/next buttons if we are at the beninning/end
//showNextButton(showPage, screens.size()-1);
currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
if (screens.size() > 0 && currentWizardPage < screens.size()) {
SWScreen currentScreen = screens.get(currentWizardPage);
if (showPage == 0)
((Button) findViewById(R.id.previous_button)).setVisibility(View.GONE);
//Set screen name
screenName = (TextView) findViewById(R.id.fullscreen_content);
screenName.setText(currentScreen.getHeader());
//Generate layout first
LinearLayout layout = info.nightscout.androidaps.startupwizard.SWItem.generateLayout(this.findViewById(R.id.fullscreen_content_fields));
LinearLayout layout = SWItem.generateLayout(this.findViewById(R.id.fullscreen_content_fields));
for (int i = 0; i < currentScreen.items.size(); i++) {
SWItem currentItem = currentScreen.items.get(i);
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);
}
updateButtons();
}
}
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
@Override
protected void onResume() {
super.onResume();
// check is current locale is different from the one in preferences
// log.debug("Current: "+LocaleHelper.getLanguage(this)+" preferences: "+SP.getString("language", "en"));
if (!LocaleHelper.getLanguage(this).equals(SP.getString("language", "en"))) {
// it is so change it in locale and restart SetupWizard
// log.debug("Setting locale to: "+SP.getString("language", "en")+" and restarting");
LocaleHelper.setLocale(this, SP.getString(R.string.key_language, "en"));
MainApp.bus().post(new EventRefreshGui(true));
Intent intent = getIntent();
this.finish();
startActivity(intent);
}
MainApp.bus().register(this);
}
private void showNextButton(int currentPage, int maxPages) {
if (currentPage == maxPages) {
((Button) findViewById(R.id.finish_button)).setVisibility(View.VISIBLE);
((Button) findViewById(R.id.next_button)).setVisibility(View.GONE);
} else
((Button) findViewById(R.id.next_button)).setVisibility(View.VISIBLE);
@Subscribe
public void onContentUpdate(EventSWUpdate ev) {
updateButtons();
}
@Subscribe
public void onContentUpdate(EventNSClientStatus ev) {
updateButtons();
}
private void updateButtons() {
SWScreen currentScreen = screens.get(currentWizardPage);
if (currentScreen.validator.isValid() || currentScreen.skippable) {
if (currentWizardPage == screens.size() - 1) {
findViewById(R.id.finish_button).setVisibility(View.VISIBLE);
findViewById(R.id.next_button).setVisibility(View.GONE);
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
findViewById(R.id.next_button).setVisibility(View.VISIBLE);
}
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
findViewById(R.id.next_button).setVisibility(View.GONE);
}
if (currentWizardPage == 0)
findViewById(R.id.previous_button).setVisibility(View.GONE);
else
findViewById(R.id.previous_button).setVisibility(View.VISIBLE);
}
public void showNextPage(View view) {
this.finish();
Intent intent = new Intent(this, SetupWizardActivity.class);
intent.putExtra(INTENT_MESSAGE, currentWizardPage + 1);
startActivity(intent);
}
public void showPreviousPage(View view) {
this.finish();
Intent intent = new Intent(this, SetupWizardActivity.class);
if (currentWizardPage > 0)
intent.putExtra(INTENT_MESSAGE, currentWizardPage - 1);

View file

@ -0,0 +1,6 @@
package info.nightscout.androidaps.startupwizard.events;
import info.nightscout.androidaps.events.Event;
public class EventSWUpdate extends Event {
}

View file

@ -6,9 +6,6 @@
android:background="#0099cc"
tools:context=".startupwizard.SetupWizardActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
@ -16,11 +13,9 @@
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textSize="20dp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">