Merge pull request #919 from PoweRGbg/dev

WIP: Basic navigation and display of wizzard pages
This commit is contained in:
Milos Kozak 2018-05-02 15:04:24 +02:00 committed by GitHub
commit e856306267
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 395 additions and 9 deletions

View file

@ -3,8 +3,11 @@ package info.nightscout.androidaps.startupwizard;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.SP;
public class SWDefinition {
@ -16,6 +19,7 @@ public class SWDefinition {
swDefinition = new SWDefinition();
return swDefinition;
}
android.content.Context context = MainApp.instance().getApplicationContext();
static List<SWScreen> screens = new ArrayList<>();
@ -29,8 +33,19 @@ public class SWDefinition {
}
SWDefinition() {
add(new SWScreen(R.string.nsclientinternal_title)
.skippable(false)
// 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(() -> {
context = MainApp.instance().getApplicationContext();
LocaleHelper.setLocale(context, SP.getString(R.string.key_language, "en"));
MainApp.bus().post(new EventRefreshGui(true));
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(() -> NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth)
@ -40,6 +55,7 @@ public class SWDefinition {
.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(() -> SP.contains(R.string.key_age))
)
;
}

View file

@ -1,10 +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,
@ -19,6 +29,8 @@ public class SWItem {
Integer label;
Integer comment;
int preferenceId;
private List<String> labels;
private List<String> values;
public SWItem(Type type) {
@ -59,4 +71,18 @@ public class SWItem {
SP.putString(preferenceId, value);
MainApp.bus().post(new EventPreferenceChange(preferenceId));
}
public void setOptions(List<String> labels, List<String> 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){
}
}

View file

@ -1,11 +1,25 @@
package info.nightscout.androidaps.startupwizard;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.utils.SP;
public class SWRadioButton extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWRadioButton.class);
int labelsArray;
int valuesArray;
private RadioGroup radioGroup;
public boolean somethingChecked = false;
public SWRadioButton() {
super(Type.RADIOBUTTON);
@ -25,4 +39,73 @@ public class SWRadioButton extends SWItem {
return MainApp.sResources.getStringArray(valuesArray);
}
@Override
public void generateDialog(View view, LinearLayout layout){
Context context = view.getContext();
// 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
String previousValue = SP.getString(preferenceId, "unset");
// log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue);
radioGroup = new RadioGroup(context);
radioGroup.clearCheck();
for (int row = 0; row < 1; row++) {
radioGroup.setOrientation(LinearLayout.VERTICAL);
radioGroup.setVisibility(View.VISIBLE);
for (int i = 0; i < labels.length; i++) {
RadioButton rdbtn = new RadioButton(context);
rdbtn.setId((row * 2) + i);
rdbtn.setText(labels[i]);
if(previousValue.equals(values[i]))
rdbtn.setChecked(true);
radioGroup.addView(rdbtn);
}
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
save();
}
});
layout.addView(radioGroup);
}
public RadioGroup getRadioGroup(){
return this.radioGroup;
}
public String getCheckedValue(){
if(radioGroup != null && radioGroup.getCheckedRadioButtonId() > -1){
Context context = radioGroup.getRootView().getContext();
String[] values = context.getResources().getStringArray(valuesArray);
return values[radioGroup.getCheckedRadioButtonId()];
} else {
return "none";
}
}
public boolean isSomethingChecked(){
return this.somethingChecked;
}
public void save(){
if(!getCheckedValue().equals("none")) {
SP.putString(preferenceId, getCheckedValue());
MainApp.bus().post(new EventPreferenceChange(preferenceId));
}
}
public String preferenceSet(){
return SP.getString(preferenceId, "none");
}
}

View file

@ -1,8 +1,56 @@
package info.nightscout.androidaps.startupwizard;
import android.content.Context;
import android.text.InputType;
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;
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);
layout.addView(textlabel);
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);
}
}
}

View file

@ -1,8 +1,49 @@
package info.nightscout.androidaps.startupwizard;
public class SWUrl extends SWItem {
import android.content.Context;
import android.text.InputType;
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<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();
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);
}
}
}

View file

@ -1,14 +1,34 @@
package info.nightscout.androidaps.startupwizard;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
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.utils.LocaleHelper;
import info.nightscout.utils.SP;
import static info.nightscout.androidaps.startupwizard.SWItem.Type.RADIOBUTTON;
import static info.nightscout.androidaps.startupwizard.SWItem.Type.STRING;
import static info.nightscout.androidaps.startupwizard.SWItem.Type.URL;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
@ -34,6 +54,21 @@ public class SetupWizardActivity extends AppCompatActivity {
private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler();
private View mContentView;
private LinearLayout linearLayout;
private TextView radioLabel;
private int numberOfButtons = 0;
private List<String> labels = new ArrayList<String>();
private List<String> comments = new ArrayList<String>();
private LinearLayout layout;
private TextView textlabel;
private TextView screenName;
private Button skipButton;
//logiing
private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class);
private int currentWizardPage = 0;
public static final String INTENT_MESSAGE = "WIZZARDPAGE";
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
@ -88,7 +123,6 @@ public class SetupWizardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setupwizard);
mVisible = true;
@ -104,10 +138,51 @@ public class SetupWizardActivity extends AppCompatActivity {
}
});
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);
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());
//Display screen items in the order entered
linearLayout = (LinearLayout) findViewById(R.id.fullscreen_content_controls);
// is it skippable ?
if(currentScreen.skippable) {
//display skip button
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);
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();
}
}
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
findViewById(R.id.next_button).setOnTouchListener(mDelayHideTouchListener);
}
@Override
@ -120,6 +195,23 @@ public class SetupWizardActivity extends AppCompatActivity {
delayedHide(100);
}
@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);
}
}
private void toggle() {
if (mVisible) {
hide();
@ -162,4 +254,35 @@ public class SetupWizardActivity extends AppCompatActivity {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
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);
show();
}
public void showNextPage(View view) {
Intent intent = new Intent(this, SetupWizardActivity.class);
intent.putExtra(INTENT_MESSAGE, currentWizardPage + 1);
startActivity(intent);
}
public void showPreviousPage(View view) {
Intent intent = new Intent(this, SetupWizardActivity.class);
if(currentWizardPage > 0)
intent.putExtra(INTENT_MESSAGE, currentWizardPage - 1);
else
intent.putExtra(INTENT_MESSAGE, 0);
startActivity(intent);
}
// Go back to overview
public void finishSetupWizard(View view){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}

View file

@ -27,6 +27,15 @@
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/fullscreen_content_fields"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
@ -38,12 +47,44 @@
tools:ignore="UselessParent">
<Button
android:id="@+id/dummy_button"
android:id="@+id/previous_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
android:onClick="showPreviousPage"
android:text="@string/setupwizard_previous" />
<Button
android:id="@+id/skip_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showNextPage"
android:text="@string/setupwizard_skip"
android:visibility="invisible" />
<Button
android:id="@+id/next_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showNextPage"
android:text="@string/setupwizard_next"
android:visibility="gone" />
<Button
android:id="@+id/finish_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="finishSetupWizard"
android:text="@string/setupwizard_finish"
android:visibility="gone" />
</LinearLayout>
</FrameLayout>

View file

@ -482,7 +482,7 @@
<string name="restart">Restart</string>
<string name="nsclientinternal_title">NSClient</string>
<string name="nsclientinternal_url_title">Nightscout URL</string>
<string name="nsclientinternal_url_dialogmessage">Enter Nightscout URL</string>
<string name="nsclientinternal_url_dialogmessage">Enter Your Nightscout URL</string>
<string name="nsclientinternal_secret_title">NS API secret</string>
<string name="nsclientinternal_secret_dialogtitle">NS API secret</string>
<string name="nsclientinternal_secret_dialogmessage">Enter NS API secret (min 12 chars)</string>
@ -1002,7 +1002,15 @@
<string name="overview_show_notes_field_in_dialogs_title">Show notes field in treatment dialogs</string>
<string name="title_activity_setup_wizard">SetupWizardActivity</string>
<string name="dummy_button">Dummy Button</string>
<string name="next_button">Next</string>
<string name="previous_button">Prev</string>
<string name="skip_button">Skip</string>
<string name="dummy_content">DUMMY\nCONTENT</string>
<string name="nav_setupwizard">Setup Wizard</string>
<string name="setupwizard_next">Next</string>
<string name="setupwizard_previous">Prev</string>
<string name="setupwizard_skip">Skip</string>
<string name="setupwizard_finish">FINISH</string>
<string name="setupwizard_language_prompt">Select your language</string>
<string name="key_language">language</string>
</resources>