Nightscout + UI performance improvements
This commit is contained in:
parent
d07d9d309e
commit
7f73c59530
|
@ -25,27 +25,33 @@ import info.nightscout.androidaps.plugins.PumpInsight.utils.ui.StatusItemViewAda
|
|||
|
||||
|
||||
public class InsightPumpFragment extends SubscriberFragment {
|
||||
private static Logger log = LoggerFactory.getLogger(InsightPumpFragment.class);
|
||||
private static Handler sLoopHandler = new Handler();
|
||||
private static Runnable sRefreshLoop = null;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(InsightPumpFragment.class);
|
||||
private static final Handler sLoopHandler = new Handler();
|
||||
private static volatile boolean refresh = false;
|
||||
private static volatile boolean pending = false;
|
||||
StatusItemViewAdapter viewAdapter;
|
||||
LinearLayout holder;
|
||||
private final Runnable sRefreshLoop = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
pending = false;
|
||||
updateGUI();
|
||||
if (refresh) {
|
||||
scheduleRefresh();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private synchronized void scheduleRefresh() {
|
||||
if (!pending) {
|
||||
pending = true;
|
||||
sLoopHandler.postDelayed(sRefreshLoop, 30 * 1000L);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (sRefreshLoop == null) {
|
||||
sRefreshLoop = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateGUI();
|
||||
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
|
||||
}
|
||||
};
|
||||
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,6 +70,22 @@ public class InsightPumpFragment extends SubscriberFragment {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setUserVisibleHint(boolean visible) {
|
||||
super.setUserVisibleHint(visible);
|
||||
if (visible) {
|
||||
refresh = true;
|
||||
pending = false;
|
||||
updateGUI();
|
||||
scheduleRefresh();
|
||||
} else {
|
||||
refresh = false;
|
||||
//sLoopHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Subscribe
|
||||
public void onStatusEvent(final EventInsightPumpUpdateGui ev) {
|
||||
updateGUI();
|
||||
|
@ -77,7 +99,7 @@ public class InsightPumpFragment extends SubscriberFragment {
|
|||
@Override
|
||||
public void run() {
|
||||
final InsightPumpPlugin insightPumpPlugin = InsightPumpPlugin.getPlugin();
|
||||
final List<StatusItem> l = insightPumpPlugin.getStatusItems();
|
||||
final List<StatusItem> l = insightPumpPlugin.getStatusItems(refresh);
|
||||
|
||||
holder.removeAllViews();
|
||||
|
||||
|
|
|
@ -73,6 +73,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
|||
static Integer batteryPercent = 0;
|
||||
static Integer reservoirInUnits = 0;
|
||||
static boolean initialized = false;
|
||||
private static volatile boolean update_pending = false;
|
||||
private static Logger log = LoggerFactory.getLogger(InsightPumpPlugin.class);
|
||||
private static volatile InsightPumpPlugin plugin;
|
||||
private final Handler handler = new Handler();
|
||||
|
@ -167,6 +168,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
|||
}
|
||||
|
||||
private static void updateGui() {
|
||||
update_pending = false;
|
||||
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
||||
}
|
||||
|
||||
|
@ -644,13 +646,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
|||
private synchronized UUID deliverBolus(float bolusValue) {
|
||||
log("DeliverBolus: " + bolusValue);
|
||||
|
||||
// Bare sanity checking should be done elsewhere
|
||||
if (bolusValue == 0) return null;
|
||||
if (bolusValue < 0) return null;
|
||||
|
||||
if (bolusValue > 20) return null;
|
||||
|
||||
// TODO check limits here?
|
||||
// TODO check limits here or they already occur via a previous constraint interface?
|
||||
|
||||
final StandardBolusMessage message = new StandardBolusMessage();
|
||||
message.setAmount(bolusValue);
|
||||
|
@ -661,15 +660,19 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
|||
@Override
|
||||
public JSONObject getJSONStatus() {
|
||||
|
||||
// TODO review
|
||||
if (Helpers.msSince(connector.getLastContactTime()) > (60 * 60 * 1000)) {
|
||||
log("getJSONStatus not returning as data likely stale");
|
||||
return null;
|
||||
}
|
||||
|
||||
JSONObject pump = new JSONObject();
|
||||
JSONObject battery = new JSONObject();
|
||||
JSONObject status = new JSONObject();
|
||||
JSONObject extended = new JSONObject();
|
||||
final JSONObject pump = new JSONObject();
|
||||
final JSONObject battery = new JSONObject();
|
||||
final JSONObject status = new JSONObject();
|
||||
final JSONObject extended = new JSONObject();
|
||||
try {
|
||||
battery.put("percent", batteryPercent);
|
||||
status.put("status", "normal");
|
||||
status.put("status", isSuspended() ? "suspended" : "normal");
|
||||
status.put("timestamp", DateUtil.toISOString(connector.getLastContactTime()));
|
||||
extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION);
|
||||
try {
|
||||
extended.put("ActiveProfile", MainApp.getConfigBuilder().getProfileName());
|
||||
|
@ -733,7 +736,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
|||
return statusResult.getPumpStatusMessage().getPumpStatus() == PumpStatus.STARTED;
|
||||
}
|
||||
|
||||
List<StatusItem> getStatusItems() {
|
||||
List<StatusItem> getStatusItems(boolean refresh) {
|
||||
final List<StatusItem> l = new ArrayList<>();
|
||||
|
||||
// Todo last contact time
|
||||
|
@ -802,17 +805,21 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
|||
}
|
||||
|
||||
Connector.get().requestHistorySync();
|
||||
if (refresh) scheduleGUIUpdate();
|
||||
|
||||
if (connector.uiFresh()) {
|
||||
return l;
|
||||
}
|
||||
|
||||
private synchronized void scheduleGUIUpdate() {
|
||||
if (!update_pending && connector.uiFresh()) {
|
||||
update_pending = true;
|
||||
Helpers.runOnUiThreadDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateGui();
|
||||
}
|
||||
}, 1000);
|
||||
}, 500);
|
||||
}
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
private void statusActiveBolus(ActiveBolus activeBolus, long offset_mins, List<StatusItem> l) {
|
||||
|
|
|
@ -39,6 +39,7 @@ public class Connector {
|
|||
private volatile Status lastStatus = null;
|
||||
private String compatabilityMessage = null;
|
||||
private volatile long lastStatusTime = -1;
|
||||
private volatile long lastContactTime = -1;
|
||||
private boolean companionAppInstalled = false;
|
||||
private int serviceReconnects = 0;
|
||||
private StatusCallback statusCallback = new StatusCallback() {
|
||||
|
@ -48,6 +49,9 @@ public class Connector {
|
|||
log("Status change: " + status);
|
||||
lastStatus = status;
|
||||
lastStatusTime = Helpers.tsl();
|
||||
if (status == Status.CONNECTED) {
|
||||
lastContactTime = lastStatusTime;
|
||||
}
|
||||
|
||||
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
||||
}
|
||||
|
@ -197,6 +201,10 @@ public class Connector {
|
|||
return isConnected() && getLastStatus() == Status.CONNECTING;
|
||||
}
|
||||
|
||||
public long getLastContactTime() {
|
||||
return lastContactTime;
|
||||
}
|
||||
|
||||
public String getLastStatusMessage() {
|
||||
|
||||
if (!companionAppInstalled) {
|
||||
|
@ -225,10 +233,9 @@ public class Connector {
|
|||
|
||||
switch (lastStatus) {
|
||||
case CONNECTED:
|
||||
if (lastStatusTime < 1) {
|
||||
if (Helpers.msSince(lastStatusTime) > (60 * 10 * 1000)) {
|
||||
tryToGetPumpStatusAgain();
|
||||
}
|
||||
// TODO other refresh?
|
||||
break;
|
||||
case INCOMPATIBLE:
|
||||
return lastStatus.toString() + " needs " + getLocalVersion();
|
||||
|
|
Loading…
Reference in a new issue