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 2d44389180..98b6d7a180 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java @@ -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 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 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 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 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) ) ; } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWPlugin.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWPlugin.java index 50ecfa5197..dc6ce4e71a 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWPlugin.java @@ -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); } 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 3329d0bbbe..882f361a32 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java @@ -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"); - } - - } 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 202a3ccc06..b9dde66688 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java @@ -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 labels; - private List values; - private String groupName; public SWString() { super(Type.STRING); } - public void setName(String name){ - this.groupName = name; - } - - public void setOptions(List labels, List 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) { + } + }); } } 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 02b6f1bd79..a9485bb1f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java @@ -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 labels; - private List values; - private String groupName; + public SWUrl() { super(Type.URL); } - public void setName(String name){ - this.groupName = name; - } - - public void setOptions(List labels, List 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) { + } + }); } - - } 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 09fe839aa0..958eea28b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java @@ -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 labels = new ArrayList<>(); - private List comments = new ArrayList<>(); - - private TextView screenName; - //logging private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class); + private TextView screenName; + + SWDefinition swDefinition = SWDefinition.getInstance(); + List 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 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); diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWUpdate.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWUpdate.java new file mode 100644 index 0000000000..0c20cdde78 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWUpdate.java @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.startupwizard.events; + +import info.nightscout.androidaps.events.Event; + +public class EventSWUpdate extends Event { +} diff --git a/app/src/main/res/layout/activity_setupwizard.xml b/app/src/main/res/layout/activity_setupwizard.xml index 025d85f335..9422f76d76 100644 --- a/app/src/main/res/layout/activity_setupwizard.xml +++ b/app/src/main/res/layout/activity_setupwizard.xml @@ -6,9 +6,6 @@ android:background="#0099cc" tools:context=".startupwizard.SetupWizardActivity"> - -