From 279c67bb70b54b0ee39faafa69c306e254cdefac Mon Sep 17 00:00:00 2001 From: viktoria Date: Sat, 11 Feb 2017 00:16:25 +0100 Subject: [PATCH] use validation lib in cpp - add snackbar --- app/build.gradle | 1 + .../CircadianPercentageProfileFragment.java | 178 ++++++++++++------ .../circadianpercentageprofile_fragment.xml | 21 ++- 3 files changed, 138 insertions(+), 62 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ec7e25f692..ca49fa5e1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -126,6 +126,7 @@ dependencies { compile 'com.android.support:cardview-v7:23.4.0' compile 'com.android.support:recyclerview-v7:23.4.0' compile 'com.android.support:gridlayout-v7:23.4.0' + compile 'com.android.support:design:23.4.0' compile 'com.wdullaer:materialdatetimepicker:2.3.0' compile 'com.squareup:otto:1.3.7' compile 'com.j256.ormlite:ormlite-core:4.46' diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java index 0bed2495d9..0c7856f8c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java @@ -4,18 +4,20 @@ package info.nightscout.androidaps.plugins.CircadianPercentageProfile; import android.app.Activity; import android.content.Context; import android.os.Bundle; -import android.os.CountDownTimer; +import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; +import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; @@ -36,7 +38,6 @@ import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialo import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; -import info.nightscout.utils.ToastUtils; public class CircadianPercentageProfileFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfileFragment.class); @@ -47,11 +48,11 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag return circadianPercentageProfilePlugin; } - EditText diaView; + FormEditText diaView; RadioButton mgdlView; RadioButton mmolView; - EditText targetlowView; - EditText targethighView; + FormEditText targetlowView; + FormEditText targethighView; FormEditText percentageView; FormEditText timeshiftView; TextView profileView; @@ -68,17 +69,20 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag BasalEditDialog basalEditDialog; LinearLayout ll; + Boolean percentageViewHint = true; + Boolean timeshiftViewHint = true; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.circadianpercentageprofile_fragment, container, false); ll = (LinearLayout) layout.findViewById(R.id.circadianpercentageprofile_linearlayout); - diaView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_dia); + ll.requestFocusFromTouch(); + diaView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_dia); mgdlView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mgdl); mmolView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mmol); - targetlowView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow); - targethighView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh); + targetlowView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow); + targethighView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh); percentageView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_percentage); timeshiftView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_timeshift); profileView = (TextView) layout.findViewById(R.id.circadianpercentageprofile_profileview); @@ -141,54 +145,6 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag } }); - timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() { - boolean counter = false; - @Override - public void onFocusChange(View view, boolean hasFocus) { - if (!hasFocus) { - timeshiftView.clearFocus(); - ll.requestFocusFromTouch(); - } - else { - if (!counter) { - new CountDownTimer(5000, 1000) { //timer for 5sec - public void onTick(long millisUntilFinished) { - timeshiftView.setError(getString(R.string.timeshift_hint), null); - } - public void onFinish() { - timeshiftView.setError(null); - } - }.start(); - counter = true; - } - } - } - }); - - percentageView.setOnFocusChangeListener(new View.OnFocusChangeListener() { - boolean counter = false; - @Override - public void onFocusChange(View view, boolean hasFocus) { - if (!hasFocus) { - percentageView.clearFocus(); - ll.requestFocusFromTouch(); - } - else { - if (!counter) { - new CountDownTimer(5000, 1000) { //timer for 5sec - public void onTick(long millisUntilFinished) { - percentageView.setError(getString(R.string.percentagefactor_hint), null); - } - public void onFinish() { - percentageView.setError(null); - } - }.start(); - counter = true; - } - } - } - }); - timeIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -236,6 +192,71 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag } }); + timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + timeshiftView.clearFocus(); + ll.requestFocusFromTouch(); + } + else { + if (timeshiftViewHint) { + customSnackbar(view, getString(R.string.timeshift_hint), "timeshiftViewHint"); + } + } + } + }); + + percentageView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + percentageView.clearFocus(); + ll.requestFocusFromTouch(); + } + else { + if (percentageViewHint) { + customSnackbar(view, getString(R.string.percentagefactor_hint), "percentageViewHint"); + } + } + } + }); + + diaView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + diaView.clearFocus(); + ll.requestFocusFromTouch(); + } + } + }); + + targethighView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + targethighView.clearFocus(); + ll.requestFocusFromTouch(); + } + } + }); + + targetlowView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + targetlowView.clearFocus(); + ll.requestFocusFromTouch(); + } + } + }); + TextWatcher textWatch = new TextWatcher() { @Override @@ -263,9 +284,18 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString()); updateProfileInfo(); } - circadianPercentageProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); - circadianPercentageProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); - circadianPercentageProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); + if (diaView.testValidity()) { + circadianPercentageProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); + updateProfileInfo(); + } + if (targethighView.testValidity()) { + circadianPercentageProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); + updateProfileInfo(); + } + if (targetlowView.testValidity()) { + circadianPercentageProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); + updateProfileInfo(); + } circadianPercentageProfilePlugin.storeSettings(); updateProfileInfo(); } @@ -282,6 +312,34 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag return layout; } + private void customSnackbar(View view, String Msg, final String Hint) { + if (timeshiftViewHint || percentageViewHint) { + Snackbar mSnackBar = Snackbar.make(view, + Msg, + Snackbar.LENGTH_LONG) + .setDuration(7000) + .setActionTextColor(getResources().getColor(R.color.notificationInfo)) + .setAction(getString(R.string.dismiss), new View.OnClickListener() { + @Override + public void onClick(View v) { + if (Hint.equals("percentageViewHint")) { + percentageViewHint = false; + } else if (Hint.equals("timeshiftViewHint")) { + timeshiftViewHint = false; + } + } + }); + view = mSnackBar.getView(); + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams(); + params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; + view.setLayoutParams(params); + view.setBackgroundColor(getResources().getColor(R.color.cardview_dark_background)); + TextView mainTextView = (TextView) (view).findViewById(android.support.design.R.id.snackbar_text); + mainTextView.setTextColor(getResources().getColor(R.color.mdtp_white)); + mSnackBar.show(); + } + } + private void updateProfileInfo() { StringBuilder sb = new StringBuilder(); sb.append("

"); @@ -314,6 +372,7 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag basalEditDialog.dismiss(); } basalEditDialog = null; + ll.requestFocusFromTouch(); } public static class BasalEditDialog extends DialogFragment { @@ -394,6 +453,7 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag basalEditDialog = null; MainApp.bus().unregister(this); + ll.requestFocusFromTouch(); } @Override diff --git a/app/src/main/res/layout/circadianpercentageprofile_fragment.xml b/app/src/main/res/layout/circadianpercentageprofile_fragment.xml index 6d9b21fd35..99bf9f0235 100644 --- a/app/src/main/res/layout/circadianpercentageprofile_fragment.xml +++ b/app/src/main/res/layout/circadianpercentageprofile_fragment.xml @@ -253,7 +253,12 @@ android:text="@string/dia" android:textAppearance="?android:attr/textAppearanceMedium" /> - - -