From 7fa8ea7224de703a0717d1c10f2a83fe6d7997fc Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 10 Jul 2017 03:55:21 +0200 Subject: [PATCH] cpp-watch working2 --- .../CircadianPercentageProfileFragment.java | 146 +++++++++++------- .../plugins/Wear/ActionStringHandler.java | 12 +- .../wearintegration/WatchUpdaterService.java | 13 +- .../interaction/actions/CPPActivity.java | 2 +- .../interaction/utils/PlusMinusEditText.java | 23 ++- 5 files changed, 129 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java index 112ead21fa..1bfd25acf4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java @@ -33,6 +33,8 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventInitializationChanged; +import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.events.EventProfileSwitchChange; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; @@ -75,6 +77,50 @@ public class CircadianPercentageProfileFragment extends Fragment { static Boolean percentageViewHint = true; static Boolean timeshiftViewHint = true; + TextWatcher textWatch = new TextWatcher() { + + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + + if (percentageView.testValidity()) { + if (SafeParse.stringToInt(percentageView.getText().toString()) == 0) { + circadianPercentageProfilePlugin.percentage = 100; + } else { + circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString()); + } + updateProfileInfo(); + } + if (timeshiftView.testValidity()) { + circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString()); + updateProfileInfo(); + } + 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(); + } + }; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -106,16 +152,6 @@ public class CircadianPercentageProfileFragment extends Fragment { layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal_layout).setVisibility(View.GONE); } - - mgdlView.setChecked(circadianPercentageProfilePlugin.mgdl); - mmolView.setChecked(circadianPercentageProfilePlugin.mmol); - diaView.setText(circadianPercentageProfilePlugin.dia.toString()); - targetlowView.setText(circadianPercentageProfilePlugin.targetLow.toString()); - targethighView.setText(circadianPercentageProfilePlugin.targetHigh.toString()); - percentageView.setText("" + circadianPercentageProfilePlugin.percentage); - timeshiftView.setText("" + circadianPercentageProfilePlugin.timeshift); - updateProfileInfo(); - mgdlView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -266,49 +302,6 @@ public class CircadianPercentageProfileFragment extends Fragment { } }); - TextWatcher textWatch = new TextWatcher() { - - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - - if (percentageView.testValidity()) { - if (SafeParse.stringToInt(percentageView.getText().toString()) == 0) { - circadianPercentageProfilePlugin.percentage = 100; - } else { - circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString()); - } - updateProfileInfo(); - } - if (timeshiftView.testValidity()) { - circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString()); - updateProfileInfo(); - } - 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(); - } - }; diaView.addTextChangedListener(textWatch); targetlowView.addTextChangedListener(textWatch); @@ -316,11 +309,41 @@ public class CircadianPercentageProfileFragment extends Fragment { percentageView.addTextChangedListener(textWatch); timeshiftView.addTextChangedListener(textWatch); - onStatusEvent(null); + updateGui(); + + onStatusEvent(new EventInitializationChanged()); return layout; } + public void updateGui() { + + + updateProfileInfo(); + + diaView.removeTextChangedListener(textWatch); + targetlowView.removeTextChangedListener(textWatch); + targethighView.removeTextChangedListener(textWatch); + percentageView.removeTextChangedListener(textWatch); + timeshiftView.removeTextChangedListener(textWatch); + + mgdlView.setChecked(circadianPercentageProfilePlugin.mgdl); + mmolView.setChecked(circadianPercentageProfilePlugin.mmol); + diaView.setText(circadianPercentageProfilePlugin.dia.toString()); + targetlowView.setText(circadianPercentageProfilePlugin.targetLow.toString()); + targethighView.setText(circadianPercentageProfilePlugin.targetHigh.toString()); + percentageView.setText("" + circadianPercentageProfilePlugin.percentage); + timeshiftView.setText("" + circadianPercentageProfilePlugin.timeshift); + + + diaView.addTextChangedListener(textWatch); + targetlowView.addTextChangedListener(textWatch); + targethighView.addTextChangedListener(textWatch); + percentageView.addTextChangedListener(textWatch); + timeshiftView.addTextChangedListener(textWatch); + + } + private void customSnackbar(View view, final String Msg, Object snackbarCaller) { if(mSnackBar!= null) mSnackBar.dismiss(); @@ -470,7 +493,7 @@ public class CircadianPercentageProfileFragment extends Fragment { public void onResume() { super.onResume(); MainApp.bus().register(this); - onStatusEvent(null); + onStatusEvent(new EventInitializationChanged()); fl.requestFocusFromTouch(); } @@ -489,4 +512,17 @@ public class CircadianPercentageProfileFragment extends Fragment { } }); } + + @Subscribe + public void onStatusEvent(final EventProfileSwitchChange e) { + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + updateGui(); + } + }); + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index bee3b47451..096b5b5cda 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -250,8 +250,8 @@ public class ActionStringHandler { } else { // read CPP values rMessage = "CPP:" + "\n\n"+ - "Percentage: " + act[1] + "%\n" + - "Timeshift: " + act[2]; + "Timeshift: " + act[1] + "\n" + + "Percentage: " + act[2] + "%"; rAction = actionstring; } @@ -428,7 +428,13 @@ public class ActionStringHandler { return; } String msg = cpp.externallySetParameters(timeshift, percentage); - ToastUtils.showToastInUiThread(MainApp.instance(), msg); + if(msg != null && !"".equals(msg)){ + String rTitle = "STATUS"; + String rAction = "statusmessage"; + WearFragment.getPlugin(MainApp.instance()).requestActionConfirmation(rTitle, msg, rAction); + lastSentTimestamp = System.currentTimeMillis(); + lastConfirmActionString = rAction; + } } private static void generateTempTarget(int duration, double low, double high) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java index 3f86a78a02..93a85686b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java @@ -19,6 +19,9 @@ import com.google.android.gms.wearable.PutDataRequest; import com.google.android.gms.wearable.Wearable; import com.google.android.gms.wearable.WearableListenerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -37,6 +40,7 @@ import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; +import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfilePlugin; import info.nightscout.androidaps.plugins.Wear.ActionStringHandler; import info.nightscout.androidaps.plugins.Wear.WearPlugin; import info.nightscout.utils.DecimalFormatter; @@ -72,6 +76,9 @@ public class WatchUpdaterService extends WearableListenerService implements SharedPreferences mPrefs; private static boolean lastLoopStatus; + private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfilePlugin.class); + + @Override public void onCreate() { mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); @@ -169,13 +176,13 @@ public class WatchUpdaterService extends WearableListenerService implements if (event != null && event.getPath().equals(WEARABLE_INITIATE_ACTIONSTRING_PATH)) { String actionstring = new String(event.getData()); - ToastUtils.showToastInUiThread(this, "Wear: " + actionstring); + log.debug("Wear: " + actionstring); ActionStringHandler.handleInitiate(actionstring); } if (event != null && event.getPath().equals(WEARABLE_CONFIRM_ACTIONSTRING_PATH)) { String actionstring = new String(event.getData()); - ToastUtils.showToastInUiThread(this, "Wear Confirm: " + actionstring); + log.debug("Wear Confirm: " + actionstring); ActionStringHandler.handleConfirmation(actionstring); } } @@ -497,7 +504,7 @@ public class WatchUpdaterService extends WearableListenerService implements dataMapRequest.getDataMap().putString("message", message); dataMapRequest.getDataMap().putString("actionstring", actionstring); - ToastUtils.showToastInUiThread(this, "Requesting confirmation from wear: " + actionstring); + log.debug("Requesting confirmation from wear: " + actionstring); PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest(); Wearable.DataApi.putDataItem(googleApiClient, putDataRequest); diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CPPActivity.java b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CPPActivity.java index 723b2b161b..f3ffb06bc0 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CPPActivity.java +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CPPActivity.java @@ -80,7 +80,7 @@ public class CPPActivity extends ViewSelectorActivity { if (editTimeshift != null){ def = SafeParse.stringToDouble(editTimeshift.editText.getText().toString()); } - editTimeshift = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, def, 0d, 24d, 1d, new DecimalFormat("0"), false); + editTimeshift = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, def, 0d, 23d, 1d, new DecimalFormat("0"), true, true); setLabelToPlusMinusView(view, "timeshift"); container.addView(view); return view; diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/PlusMinusEditText.java b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/PlusMinusEditText.java index 0cbae9fecf..5af013c042 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/PlusMinusEditText.java +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/PlusMinusEditText.java @@ -31,6 +31,7 @@ public class PlusMinusEditText implements View.OnKeyListener, Double step = 1d; NumberFormat formater; boolean allowZero = false; + boolean roundRobin; private Handler mHandler; private ScheduledExecutorService mUpdater; @@ -65,6 +66,10 @@ public class PlusMinusEditText implements View.OnKeyListener, private static final int MSG_DEC = 1; public PlusMinusEditText(View view, int editTextID, int plusID, int minusID, Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero) { + this( view, editTextID, plusID, minusID, initValue, minValue, maxValue, step, formater, allowZero, false); + } + + public PlusMinusEditText(View view, int editTextID, int plusID, int minusID, Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, boolean roundRobin) { editText = (TextView) view.findViewById(editTextID); minusImage = (ImageView) view.findViewById(minusID); plusImage = (ImageView) view.findViewById(plusID); @@ -75,6 +80,7 @@ public class PlusMinusEditText implements View.OnKeyListener, this.step = step; this.formater = formater; this.allowZero = allowZero; + this.roundRobin = roundRobin; mHandler = new Handler() { @Override @@ -112,12 +118,15 @@ public class PlusMinusEditText implements View.OnKeyListener, public void setStep(Double step) { this.step = step; } - private void inc(int multiplier) { value += step * multiplier; if (value > maxValue) { - value = maxValue; - stopUpdating(); + if(roundRobin){ + value = minValue; + } else { + value = maxValue; + stopUpdating(); + } } updateEditText(); } @@ -125,8 +134,12 @@ public class PlusMinusEditText implements View.OnKeyListener, private void dec( int multiplier) { value -= step * multiplier; if (value < minValue) { - value = minValue; - stopUpdating(); + if(roundRobin){ + value = maxValue; + } else { + value = minValue; + stopUpdating(); + } } updateEditText(); }