code cleanup

This commit is contained in:
Milos Kozak 2018-04-05 23:21:08 +02:00
parent 36d94ecdef
commit e5bf36bfe3
2 changed files with 70 additions and 114 deletions

View file

@ -13,12 +13,9 @@ import android.widget.TextView;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
@ -27,7 +24,6 @@ import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.events.EventCareportalEventChange; import info.nightscout.androidaps.events.EventCareportalEventChange;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus;
import info.nightscout.androidaps.plugins.Overview.OverviewFragment; import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.FabricPrivacy;
@ -216,44 +212,24 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli
public static void updateAge(Activity activity, final TextView sage, final TextView iage, final TextView cage, final TextView pbage) { public static void updateAge(Activity activity, final TextView sage, final TextView iage, final TextView cage, final TextView pbage) {
if (activity != null) { if (activity != null) {
activity.runOnUiThread( activity.runOnUiThread(
new Runnable() { () -> {
@Override
public void run() {
CareportalEvent careportalEvent; CareportalEvent careportalEvent;
NSSettingsStatus nsSettings = new NSSettingsStatus().getInstance(); NSSettingsStatus nsSettings = new NSSettingsStatus().getInstance();
JSONObject extendedSettings = nsSettings.getExtendedValues();
double iageUrgent = 0;
double iageWarn = 0;
double cageUrgent = 0;
double cageWarn = 0;
double sageUrgent = 0;
double sageWarn = 0;
double pbageUrgent = 0;
double pbageWarn = 0;
log.debug("Values from NSSettings "+extendedSettings.toString());
// Thresholds in NS are in hours
try {
iageUrgent = extendedSettings.getDouble("iageUrgent");
iageWarn = extendedSettings.getDouble("iageWarn");
cageUrgent = extendedSettings.getDouble("cageUrgent");
cageWarn = extendedSettings.getDouble("cageWarn");
sageUrgent = extendedSettings.getDouble("sageUrgent");
sageWarn = extendedSettings.getDouble("sageWarn");
pbageUrgent = extendedSettings.getDouble("pbageUrgent");
pbageWarn = extendedSettings.getDouble("pbageWarn");
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
double iageUrgent = nsSettings.getExtendedWarnValue("iage", "urgent", 72);
double iageWarn = nsSettings.getExtendedWarnValue("iage", "warn", 48);
double cageUrgent = nsSettings.getExtendedWarnValue("cage", "urgent", 72);
double cageWarn = nsSettings.getExtendedWarnValue("cage", "warn", 48);
double sageUrgent = nsSettings.getExtendedWarnValue("sage", "urgent", 166);
double sageWarn = nsSettings.getExtendedWarnValue("sage", "warn", 164);
double pbageUrgent = nsSettings.getExtendedWarnValue("pgage", "urgent", 360);
double pbageWarn = nsSettings.getExtendedWarnValue("pgage", "warn", 240);
String notavailable = OverviewFragment.shorttextmode ? "-" : MainApp.sResources.getString(R.string.notavailable); String notavailable = OverviewFragment.shorttextmode ? "-" : MainApp.sResources.getString(R.string.notavailable);
if (sage != null) { if (sage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SENSORCHANGE); careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SENSORCHANGE);
if (careportalEvent != null) { if (careportalEvent != null) {
sage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, sageUrgent, sageWarn)); sage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, sageWarn, sageUrgent));
sage.setText(careportalEvent.age()); sage.setText(careportalEvent.age());
} else { } else {
sage.setText(notavailable); sage.setText(notavailable);
@ -262,7 +238,7 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli
if (iage != null) { if (iage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.INSULINCHANGE); careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.INSULINCHANGE);
if (careportalEvent != null) { if (careportalEvent != null) {
iage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, iageUrgent, iageWarn)); iage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, iageWarn, iageUrgent));
iage.setText(careportalEvent.age()); iage.setText(careportalEvent.age());
} else { } else {
iage.setText(notavailable); iage.setText(notavailable);
@ -271,7 +247,7 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli
if (cage != null) { if (cage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SITECHANGE); careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SITECHANGE);
if (careportalEvent != null) { if (careportalEvent != null) {
cage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, cageUrgent, cageWarn)); cage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, cageWarn, cageUrgent));
cage.setText(careportalEvent.age()); cage.setText(careportalEvent.age());
} else { } else {
cage.setText(notavailable); cage.setText(notavailable);
@ -280,14 +256,13 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli
if (pbage != null) { if (pbage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.PUMPBATTERYCHANGE); careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.PUMPBATTERYCHANGE);
if (careportalEvent != null) { if (careportalEvent != null) {
pbage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, pbageUrgent, pbageWarn)); pbage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, pbageWarn, pbageUrgent));
pbage.setText(careportalEvent.age()); pbage.setText(careportalEvent.age());
} else { } else {
pbage.setText(notavailable); pbage.setText(notavailable);
} }
} }
} }
}
); );
} }
} }

View file

@ -170,36 +170,17 @@ public class NSSettingsStatus {
} }
// return JSONObject with the WARN and URGENT values of cage,sage and iage // valid property is "warn" or "urgent"
// added pbage for pump battery warnings // plugings "iage" "sage" "cage" "pbage"
public JSONObject getExtendedValues(){
JSONObject extendedValues = new JSONObject(); // public double getExtendedWarnValue(String plugin, String property, double defaultvalue) {
JSONObject defaultSettings = new JSONObject();
JSONObject extendedSettings = this.getExtendedSettings(); JSONObject extendedSettings = this.getExtendedSettings();
// Thresholds in NS are in hours if (extendedSettings == null)
try { return defaultvalue;
// Default JSONObject pluginJson = extendedSettings.optJSONObject(plugin);
defaultSettings.put("urgent", 7*24D); if (pluginJson == null)
defaultSettings.put("warn", 5*24D); return defaultvalue;
return pluginJson.optDouble(property, defaultvalue);
JSONObject iageSettings = extendedSettings.optJSONObject("iage") != null ? extendedSettings.optJSONObject("iage") : defaultSettings;
JSONObject cageSettings = extendedSettings.optJSONObject("cage") != null ? extendedSettings.optJSONObject("cage") : defaultSettings;
JSONObject sageSettings = extendedSettings.optJSONObject("sage") != null ? extendedSettings.optJSONObject("sage") : defaultSettings;
extendedValues.put("cageUrgent", cageSettings.optDouble("urgent", 72));
extendedValues.put("cageWarn", cageSettings.optDouble("warn", 48));
extendedValues.put("sageUrgent", sageSettings.optDouble("urgent", 166));
extendedValues.put("sageWarn", sageSettings.optDouble("warn", 164));
extendedValues.put("iageUrgent", iageSettings.optDouble("urgent", 72));
extendedValues.put("iageWarn", iageSettings.optDouble("warn", 48));
extendedValues.put("pbageWarn", 240D);
extendedValues.put("pbageUrgent", 360D);
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return extendedValues;
} }
public String getActiveProfile() { public String getActiveProfile() {