From ecd16c26b8171b4208cbac6a690e00bc3279d570 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 31 Jan 2018 00:04:15 +0100 Subject: [PATCH] ComboFragment: disable buttons when running. --- .../plugins/PumpCombo/ComboFragment.java | 286 ++++++++++-------- 1 file changed, 167 insertions(+), 119 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java index 3929dbcacd..54a88cd703 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java @@ -21,6 +21,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI; +import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.events.EventQueueChanged; import info.nightscout.utils.DateUtil; @@ -71,11 +72,24 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis return view; } + private void runOnUiThread(Runnable action) { + Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread(action); + } + } + @Override public void onClick(View view) { switch (view.getId()) { case R.id.combo_refresh_button: - ConfigBuilderPlugin.getCommandQueue().readStatus("User request", null); + refreshButton.setEnabled(false); + ConfigBuilderPlugin.getCommandQueue().readStatus("User request", new Callback() { + @Override + public void run() { + runOnUiThread(() -> refreshButton.setEnabled(true)); + } + }); break; case R.id.combo_alerts_button: ComboAlertHistoryDialog ehd = new ComboAlertHistoryDialog(); @@ -87,24 +101,60 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis break; case R.id.combo_full_history_button: AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(R.string.combo_warning); builder.setMessage(R.string.combo_read_full_history_info); builder.show(); break; } } + // TODO clean up when when queuing @Override public boolean onLongClick(View view) { switch (view.getId()) { case R.id.combo_alerts_button: - new Thread(() -> ComboPlugin.getPlugin().readAlertData()).start(); + alertsButton.setEnabled(false); + tddsButton.setEnabled(false); + fullHistoryButton.setEnabled(false); + new Thread(() -> ComboPlugin.getPlugin().readAlertData(new Callback() { + @Override + public void run() { + runOnUiThread(() -> { + alertsButton.setEnabled(true); + tddsButton.setEnabled(true); + fullHistoryButton.setEnabled(true); + }); + } + })).start(); return true; case R.id.combo_tdds_button: - new Thread(() -> ComboPlugin.getPlugin().readTddData()).start(); + alertsButton.setEnabled(false); + tddsButton.setEnabled(false); + fullHistoryButton.setEnabled(false); + new Thread(() -> ComboPlugin.getPlugin().readTddData(new Callback() { + @Override + public void run() { + runOnUiThread(() -> { + alertsButton.setEnabled(true); + tddsButton.setEnabled(true); + fullHistoryButton.setEnabled(true); + }); + } + })).start(); return true; case R.id.combo_full_history_button: - new Thread(() -> ComboPlugin.getPlugin().readAllPumpData()).start(); + alertsButton.setEnabled(false); + tddsButton.setEnabled(false); + fullHistoryButton.setEnabled(false); + new Thread(() -> ComboPlugin.getPlugin().readAllPumpData(new Callback() { + @Override + public void run() { + runOnUiThread(() -> { + alertsButton.setEnabled(true); + tddsButton.setEnabled(true); + fullHistoryButton.setEnabled(true); + }); + } + })).start(); return true; } return false; @@ -122,123 +172,121 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis public void updateGUI() { - Activity fragmentActivity = getActivity(); - if (fragmentActivity != null) - fragmentActivity.runOnUiThread(() -> { - ComboPlugin plugin = ComboPlugin.getPlugin(); + runOnUiThread(() -> { + ComboPlugin plugin = ComboPlugin.getPlugin(); - // state - stateView.setText(plugin.getStateSummary()); - PumpState ps = plugin.getPump().state; - if (ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY - || ps.activeAlert != null && ps.activeAlert.errorCode != null) { - stateView.setTextColor(Color.RED); - stateView.setTypeface(null, Typeface.BOLD); - } else if (plugin.getPump().state.suspended - || ps.activeAlert != null && ps.activeAlert.warningCode != null) { - stateView.setTextColor(Color.YELLOW); - stateView.setTypeface(null, Typeface.BOLD); + // state + stateView.setText(plugin.getStateSummary()); + PumpState ps = plugin.getPump().state; + if (ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY + || ps.activeAlert != null && ps.activeAlert.errorCode != null) { + stateView.setTextColor(Color.RED); + stateView.setTypeface(null, Typeface.BOLD); + } else if (plugin.getPump().state.suspended + || ps.activeAlert != null && ps.activeAlert.warningCode != null) { + stateView.setTextColor(Color.YELLOW); + stateView.setTypeface(null, Typeface.BOLD); + } else { + stateView.setTextColor(Color.WHITE); + stateView.setTypeface(null, Typeface.NORMAL); + } + + // activity + String activity = plugin.getPump().activity; + activityView.setText(activity != null ? activity : ""); + + if (plugin.isInitialized()) { + refreshButton.setVisibility(View.VISIBLE); + alertsButton.setVisibility(View.VISIBLE); + tddsButton.setVisibility(View.VISIBLE); + fullHistoryButton.setVisibility(View.VISIBLE); + + // battery + batteryView.setTextSize(20); + if (ps.batteryState == PumpState.EMPTY) { + batteryView.setText("{fa-battery-empty}"); + batteryView.setTextColor(Color.RED); + } else if (ps.batteryState == PumpState.LOW) { + batteryView.setText("{fa-battery-quarter}"); + batteryView.setTextColor(Color.YELLOW); } else { - stateView.setTextColor(Color.WHITE); - stateView.setTypeface(null, Typeface.NORMAL); + batteryView.setText("{fa-battery-full}"); + batteryView.setTextColor(Color.WHITE); } - // activity - String activity = plugin.getPump().activity; - activityView.setText(activity != null ? activity : ""); - - if (plugin.isInitialized()) { - refreshButton.setVisibility(View.VISIBLE); - alertsButton.setVisibility(View.VISIBLE); - tddsButton.setVisibility(View.VISIBLE); - fullHistoryButton.setVisibility(View.VISIBLE); - - // battery - batteryView.setTextSize(20); - if (ps.batteryState == PumpState.EMPTY) { - batteryView.setText("{fa-battery-empty}"); - batteryView.setTextColor(Color.RED); - } else if (ps.batteryState == PumpState.LOW) { - batteryView.setText("{fa-battery-quarter}"); - batteryView.setTextColor(Color.YELLOW); - } else { - batteryView.setText("{fa-battery-full}"); - batteryView.setTextColor(Color.WHITE); - } - - // reservoir - int reservoirLevel = plugin.getPump().reservoirLevel; - if (reservoirLevel != -1) { - reservoirView.setText(reservoirLevel + " " + MainApp.sResources.getString(R.string.treatments_wizard_unit_label)); - } else if (ps.insulinState == PumpState.LOW) { - reservoirView.setText(MainApp.gs(R.string.combo_reservoir_low)); - } else if (ps.insulinState == PumpState.EMPTY) { - reservoirView.setText(MainApp.gs(R.string.combo_reservoir_empty)); - } else { - reservoirView.setText(MainApp.gs(R.string.combo_reservoir_normal)); - } - - if (ps.insulinState == PumpState.UNKNOWN) { - reservoirView.setTextColor(Color.WHITE); - reservoirView.setTypeface(null, Typeface.NORMAL); - } else if (ps.insulinState == PumpState.LOW) { - reservoirView.setTextColor(Color.YELLOW); - reservoirView.setTypeface(null, Typeface.BOLD); - } else if (ps.insulinState == PumpState.EMPTY) { - reservoirView.setTextColor(Color.RED); - reservoirView.setTypeface(null, Typeface.BOLD); - } else { - reservoirView.setTextColor(Color.WHITE); - reservoirView.setTypeface(null, Typeface.NORMAL); - } - - // last connection - String minAgo = DateUtil.minAgo(plugin.getPump().lastSuccessfulCmdTime); - long min = (System.currentTimeMillis() - plugin.getPump().lastSuccessfulCmdTime) / 1000 / 60; - if (plugin.getPump().lastSuccessfulCmdTime + 60 * 1000 > System.currentTimeMillis()) { - lastConnectionView.setText(R.string.combo_pump_connected_now); - lastConnectionView.setTextColor(Color.WHITE); - } else if (plugin.getPump().lastSuccessfulCmdTime + 30 * 60 * 1000 < System.currentTimeMillis()) { - lastConnectionView.setText(MainApp.gs(R.string.combo_no_pump_connection, min)); - lastConnectionView.setTextColor(Color.RED); - } else { - lastConnectionView.setText(minAgo); - lastConnectionView.setTextColor(Color.WHITE); - } - - // last bolus - Bolus bolus = plugin.getPump().lastBolus; - if (bolus != null && bolus.timestamp + 6 * 60 * 60 * 1000 >= System.currentTimeMillis()) { - long agoMsc = System.currentTimeMillis() - bolus.timestamp; - double bolusMinAgo = agoMsc / 60d / 1000d; - String unit = MainApp.gs(R.string.treatments_wizard_unit_label); - String ago; - if ((agoMsc < 60 * 1000)) { - ago = MainApp.gs(R.string.combo_pump_connected_now); - } else if (bolusMinAgo < 60) { - ago = DateUtil.minAgo(bolus.timestamp); - } else { - ago = DateUtil.hourAgo(bolus.timestamp); - } - lastBolusView.setText(MainApp.gs(R.string.combo_last_bolus, bolus.amount, unit, ago)); - } else { - lastBolusView.setText(""); - } - - // base basal rate - baseBasalRate.setText(MainApp.gs(R.string.pump_basebasalrate, plugin.getBaseBasalRate())); - - // TBR - String tbrStr = ""; - if (ps.tbrPercent != -1 && ps.tbrPercent != 100) { - long minSinceRead = (System.currentTimeMillis() - plugin.getPump().state.timestamp) / 1000 / 60; - long remaining = ps.tbrRemainingDuration - minSinceRead; - if (remaining >= 0) { - tbrStr = MainApp.gs(R.string.combo_tbr_remaining, ps.tbrPercent, remaining); - } - } - tempBasalText.setText(tbrStr); + // reservoir + int reservoirLevel = plugin.getPump().reservoirLevel; + if (reservoirLevel != -1) { + reservoirView.setText(reservoirLevel + " " + MainApp.sResources.getString(R.string.treatments_wizard_unit_label)); + } else if (ps.insulinState == PumpState.LOW) { + reservoirView.setText(MainApp.gs(R.string.combo_reservoir_low)); + } else if (ps.insulinState == PumpState.EMPTY) { + reservoirView.setText(MainApp.gs(R.string.combo_reservoir_empty)); + } else { + reservoirView.setText(MainApp.gs(R.string.combo_reservoir_normal)); } - }); + + if (ps.insulinState == PumpState.UNKNOWN) { + reservoirView.setTextColor(Color.WHITE); + reservoirView.setTypeface(null, Typeface.NORMAL); + } else if (ps.insulinState == PumpState.LOW) { + reservoirView.setTextColor(Color.YELLOW); + reservoirView.setTypeface(null, Typeface.BOLD); + } else if (ps.insulinState == PumpState.EMPTY) { + reservoirView.setTextColor(Color.RED); + reservoirView.setTypeface(null, Typeface.BOLD); + } else { + reservoirView.setTextColor(Color.WHITE); + reservoirView.setTypeface(null, Typeface.NORMAL); + } + + // last connection + String minAgo = DateUtil.minAgo(plugin.getPump().lastSuccessfulCmdTime); + long min = (System.currentTimeMillis() - plugin.getPump().lastSuccessfulCmdTime) / 1000 / 60; + if (plugin.getPump().lastSuccessfulCmdTime + 60 * 1000 > System.currentTimeMillis()) { + lastConnectionView.setText(R.string.combo_pump_connected_now); + lastConnectionView.setTextColor(Color.WHITE); + } else if (plugin.getPump().lastSuccessfulCmdTime + 30 * 60 * 1000 < System.currentTimeMillis()) { + lastConnectionView.setText(MainApp.gs(R.string.combo_no_pump_connection, min)); + lastConnectionView.setTextColor(Color.RED); + } else { + lastConnectionView.setText(minAgo); + lastConnectionView.setTextColor(Color.WHITE); + } + + // last bolus + Bolus bolus = plugin.getPump().lastBolus; + if (bolus != null && bolus.timestamp + 6 * 60 * 60 * 1000 >= System.currentTimeMillis()) { + long agoMsc = System.currentTimeMillis() - bolus.timestamp; + double bolusMinAgo = agoMsc / 60d / 1000d; + String unit = MainApp.gs(R.string.treatments_wizard_unit_label); + String ago; + if ((agoMsc < 60 * 1000)) { + ago = MainApp.gs(R.string.combo_pump_connected_now); + } else if (bolusMinAgo < 60) { + ago = DateUtil.minAgo(bolus.timestamp); + } else { + ago = DateUtil.hourAgo(bolus.timestamp); + } + lastBolusView.setText(MainApp.gs(R.string.combo_last_bolus, bolus.amount, unit, ago)); + } else { + lastBolusView.setText(""); + } + + // base basal rate + baseBasalRate.setText(MainApp.gs(R.string.pump_basebasalrate, plugin.getBaseBasalRate())); + + // TBR + String tbrStr = ""; + if (ps.tbrPercent != -1 && ps.tbrPercent != 100) { + long minSinceRead = (System.currentTimeMillis() - plugin.getPump().state.timestamp) / 1000 / 60; + long remaining = ps.tbrRemainingDuration - minSinceRead; + if (remaining >= 0) { + tbrStr = MainApp.gs(R.string.combo_tbr_remaining, ps.tbrPercent, remaining); + } + } + tempBasalText.setText(tbrStr); + } + }); } -} +} \ No newline at end of file