Skip to content

Commit

Permalink
BasalEditor: update to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Apr 6, 2021
1 parent 38fee03 commit 2a34e3c
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 22 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/JoH.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -649,6 +651,11 @@ public static HashMap<String, Object> JsonStringtoMap(String json) {
}.getType());
}

public static List<Float> JsonStringToFloatList(String json) {
return new Gson().fromJson(json, new TypeToken<ArrayList<Float>>() {
}.getType());
}

private static Gson gson_instance;
public static Gson defaultGsonInstance() {
if (gson_instance == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.eveningoutpost.dexdrip.Services.G5BaseService;
import com.eveningoutpost.dexdrip.Services.Ob1G5CollectionService;
import com.eveningoutpost.dexdrip.cgm.medtrum.MedtrumCollectionService;
import com.eveningoutpost.dexdrip.profileeditor.BasalProfileEditor;
import com.eveningoutpost.dexdrip.ui.activities.DatabaseAdmin;
import com.eveningoutpost.dexdrip.ui.dialog.G6CalibrationCodeDialog;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
Expand Down Expand Up @@ -90,6 +91,8 @@ public static void processVoiceCommand(String allWords, Activity mActivity) {
SdcardImportExport.deletePersistentStore();
} else if (allWords.contentEquals("delete uploader queue")) {
UploaderQueue.emptyQueue();
} else if (allWords.contentEquals("basal profile editor") || allWords.contentEquals("basil profile editor")) {
JoH.startActivity(BasalProfileEditor.class);
} else if (allWords.contentEquals("clear battery warning")) {
try {
final Sensor sensor = Sensor.currentSensor();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.eveningoutpost.dexdrip.profileeditor;

import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.UtilityModels.Pref;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import lombok.val;

import static com.eveningoutpost.dexdrip.Models.JoH.JsonStringToFloatList;

// jamorham

public class BasalProfile {

private static final String[] PROFILE_NAMES = {"1", "2", "3", "4", "5"};
private static final String BASAL_PREFIX = "BASAL-PROFILE-";

private static String getPrefix(final String ref) {
return BASAL_PREFIX + ref;
}

public static void save(final String ref, final List<Float> segments) {
Pref.setString(getPrefix(ref), JoH.defaultGsonInstance().toJson(segments));
//android.util.Log.d("PROFILEXX", "Saved value:" + Pref.getString(getPrefix(ref), "null"));
}

public static List<Float> load(final String ref) {
return JsonStringToFloatList(Pref.getString(getPrefix(ref), ""));
}


public static String getAllProfilesAsJson() {
val profiles = new JSONArray();
for (val profile : PROFILE_NAMES) {
val item = new JSONObject();
try {
val result = Pref.getString(getPrefix(profile), null);
if (result != null && result.length() > 5) {
item.put(profile, result);
profiles.put(item);
}
} catch (final JSONException e) {
e.printStackTrace();
}
}
return profiles.length() > 0 ? profiles.toString() : null;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eveningoutpost.dexdrip.profileeditor;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -24,6 +25,7 @@
import com.eveningoutpost.dexdrip.UtilityModels.ColorCache;
import com.eveningoutpost.dexdrip.UtilityModels.PersistentStore;
import com.eveningoutpost.dexdrip.ui.charts.BasalChart;
import com.eveningoutpost.dexdrip.ui.dialog.GenericConfirmDialog;
import com.eveningoutpost.dexdrip.ui.helpers.ColorUtil;

import java.util.ArrayList;
Expand All @@ -37,6 +39,7 @@
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.ColumnChartView;
import lombok.val;

import static com.eveningoutpost.dexdrip.UtilityModels.ColorCache.getCol;
import static com.eveningoutpost.dexdrip.ui.helpers.UiHelper.convertDpToPixel;
Expand Down Expand Up @@ -70,8 +73,6 @@ public class BasalProfileEditor extends AppCompatActivity implements AdapterView
// TODO long press on title bar swap subtitle / title?
// TODO jump graph scale better
// TODO add spline (later)
// TODO select from different profiles
// TODO actually load/save real profile data
// TODO leading zero on qs items in title bar
// TODO block till operation finished??
// TODO sync with nightscout button????
Expand All @@ -86,6 +87,8 @@ public class BasalProfileEditor extends AppCompatActivity implements AdapterView
Button plusButton;
Button minusButton;

TextView basalStepLabel;

Spinner basalSelectSpinner;
Spinner basalStepSpinner;

Expand All @@ -100,26 +103,65 @@ protected void onCreate(Bundle savedInstanceState) {
chart = (ColumnChartView) findViewById(R.id.basalChart);
basalSelectSpinner = findViewById(R.id.basalProfileSpinner);
basalStepSpinner = findViewById(R.id.basalStepSpinner);
basalStepLabel = findViewById(R.id.basalStepLabel);
setValue = (EditText) findViewById(R.id.basalSetText);
setButton = (Button) findViewById(R.id.basalSetButton);
plusButton = (Button) findViewById(R.id.basalPlusButton);
minusButton = (Button) findViewById(R.id.basalMinusButton);

chart.setDataAnimationListener(this);
chart.setOnValueTouchListener(new ValueTouchListener());
chart.setColumnChartData(BasalChart.columnData());
setChartFromSpinnerSelection();

autoSetViewPort(true);
refreshScreenElements();
refreshZoomFeatures();

populateBasalNameSpinner();
populateBasalStepSpinner();

}

private void refreshZoomFeatures() {
chart.setZoomType(ZoomType.HORIZONTAL);
chart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
//chart.setZoomLevel(0,0,1);
chart.setMaxZoom(4f);
chart.setZoomLevel(getNewMaxViewport().centerX(), getNewMaxViewport().centerY(), 1f);
chart.setTapZoomEnabled(false);
}

populateBasalStepSpinner();
private static final String LAST_BASAL_PROFILE_NAME = "LAST_BASAL_PROFILE_NAME";

private static void setLastBasalProfileName(final String value) {
PersistentStore.setString(LAST_BASAL_PROFILE_NAME, value);
}

private static String getLastBasalProfileName() {
return PersistentStore.getString(LAST_BASAL_PROFILE_NAME, "1");
}

private Rect goodMargin;

private void setChartFromSpinnerSelection() {
chart.getChartComputator().resetContentRect();
if (chart.getChartComputator().getContentRectMinusAxesMargins().right != 0) {
goodMargin = new Rect();
val currentMargin = chart.getChartComputator().getContentRectMinusAxesMargins();
goodMargin.bottom = currentMargin.bottom;
}

chart.setColumnChartData(BasalChart.columnData(getLastBasalProfileName()));

if (goodMargin != null) {
chart.getChartComputator().getContentRectMinusAllMargins().bottom = goodMargin.bottom;
chart.getChartComputator().getContentRectMinusAxesMargins().bottom = goodMargin.bottom;

BasalChart.refreshAxis(chart);
autoSetViewPort(true);
refreshZoomFeatures();
}
chart.getAxesRenderer().onChartDataChanged();
}

private void fixElipsus(ViewGroup root) {
Expand Down Expand Up @@ -150,7 +192,27 @@ private void fixElipsus(ViewGroup root) {
}
}

// TODO populate profile name spinner
private int findSpinnerPositionForName(final String name) {
val count = basalSelectSpinner.getAdapter().getCount();
for (int i = 0; i < count; i++) {
val item = basalSelectSpinner.getItemAtPosition(i);
if (item.toString().equals(name)) return i;
}
return 0;
}

private void populateBasalNameSpinner() {
ArrayList<String> nameSpinnerArray = new ArrayList<>();
nameSpinnerArray.add("1");
nameSpinnerArray.add("2");
nameSpinnerArray.add("3");
nameSpinnerArray.add("4");
nameSpinnerArray.add("5");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, nameSpinnerArray);
basalSelectSpinner.setAdapter(spinnerArrayAdapter);
basalSelectSpinner.setSelection(findSpinnerPositionForName(getLastBasalProfileName()));
basalSelectSpinner.setOnItemSelectedListener(this);
}

private void populateBasalStepSpinner() {
ArrayList<String> stepSpinnerArray = new ArrayList<>();
Expand Down Expand Up @@ -190,6 +252,20 @@ public void basalEditPlus(MenuItem x) {
adjustSelectedColumns(getStepValue(), false);
}

public void basalEditLoad(MenuItem x) {
setChartFromSpinnerSelection();
refreshTotals();
}

public void basalEditSave(MenuItem x) {
GenericConfirmDialog.show(this, "Confirm Overwrite", "Are you sure you want to save this to profile " + basalSelectSpinner.getSelectedItem().toString() + " ?", new Runnable() {
@Override
public void run() {
BasalProfile.save(getLastBasalProfileName(), getListOfValues());
}
});
}

public void basalEditMinus(MenuItem x) {
adjustSelectedColumns(-getStepValue(), false);
}
Expand Down Expand Up @@ -240,7 +316,8 @@ private void refreshTotals() {


try {
getSupportActionBar().setSubtitle(R.string.basal_editor);
// TODO i18n string format
getSupportActionBar().setSubtitle(getString(R.string.basal_editor) + " (loaded profile: " + getLastBasalProfileName() + ")");
getSupportActionBar().setTitle(span);
fixElipsus(null); // how often do we actually need to do this??
//get
Expand Down Expand Up @@ -272,7 +349,7 @@ private Viewport getNewMaxViewport() {
private Viewport autoSetViewPort(boolean reset) {

final Viewport moveViewPort = getNewMaxViewport();
android.util.Log.d(TAG, "Setting viewport: " + moveViewPort.top);
android.util.Log.d(TAG, "Setting viewport: " + moveViewPort.top + " " + moveViewPort.right);

chart.setViewportCalculationEnabled(false);
chart.setMaximumViewport(moveViewPort);
Expand Down Expand Up @@ -415,15 +492,32 @@ private void setPlusMinusVisible(final boolean visible) {
plusButton.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
minusButton.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
basalStepSpinner.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
basalStepLabel.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
}

private List<Float> getListOfValues() {
final int columns = chart.getChartData().getColumns().size();
final ArrayList<Float> values = new ArrayList<>(columns);
for (int col = 0; col < columns; col++) {
values.add(JoH.roundFloat(chart.getChartData().getColumns().get(col).getValues().get(0).getValue(), 2));
}
return values;
}

// spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
UserError.Log.d(TAG, "Spinner selected it: " + position);
basalStepSpinner.setSelection(position);
PersistentStore.setLong(PREF_STORED_BASAL_STEP, position + 1); // increment so we know 0 is unset
buttonsToMatchStep();
if (parent == basalStepSpinner) {
UserError.Log.d(TAG, "Step Spinner selected it: " + position);
basalStepSpinner.setSelection(position);
PersistentStore.setLong(PREF_STORED_BASAL_STEP, position + 1); // increment so we know 0 is unset
buttonsToMatchStep();
} else if (parent == basalSelectSpinner) {
UserError.Log.d(TAG, "Name Spinner selected it: " + position);
setLastBasalProfileName(parent.getSelectedItem().toString());
} else {
UserError.Log.wtf(TAG, "Unknown spinner selected");
}
}

// spinner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.UtilityModels.ColorCache;
import com.eveningoutpost.dexdrip.UtilityModels.Constants;
import com.eveningoutpost.dexdrip.profileeditor.BasalProfile;
import com.eveningoutpost.dexdrip.xdrip;

import java.text.DateFormatSymbols;
Expand Down Expand Up @@ -38,10 +39,10 @@ public class BasalChart {
// TODO disable double tap

public static float CHART_EXTRA = 0.5f;
private static final int segments = 48;
private static final int segments = 24;

public static ColumnChartData columnData() {
final ColumnChartData columnChartData = new ColumnChartData(getColumns(getCol(ColorCache.X.color_basal_tbr)));;
public static ColumnChartData columnData(final String profileName) {
final ColumnChartData columnChartData = new ColumnChartData(getColumns(profileName,getCol(ColorCache.X.color_basal_tbr)));;
columnChartData.setBaseValue(-0.1f);
columnChartData.setAxisXBottom(chartXAxis(columnChartData.getColumns().size()));
return columnChartData;
Expand All @@ -67,12 +68,23 @@ public static void refreshAxis(final ColumnChartView chart) {
chart.getChartData().setAxisYRight(chartYAxis(chart.getMaximumViewport().top));
}

private static List<Column> getColumns(@ColorInt int color) {
private static List<Column> getColumns(final String profileName, @ColorInt int color) {
final List<Float> loaded = BasalProfile.load(profileName);

final List<Column> columns = new ArrayList<>();
for (int i = 0; i < segments; i++) {

final List<SubcolumnValue> values = new ArrayList<>();
SubcolumnValue val = new SubcolumnValue(((float) ((int) (Math.random() * 10f))) / 10 + 0, color);

float units;
if (loaded != null && loaded.size() == segments) {
units = loaded.get(i);
} else {
units = 0.5f;
}

// SubcolumnValue val = new SubcolumnValue(((float) ((int) (Math.random() * 10f))) / 10 + 0, color);
SubcolumnValue val = new SubcolumnValue(units, color);

setLabelForSubcolumn(val);
values.add(val);
Expand Down Expand Up @@ -102,16 +114,16 @@ static private Axis chartXAxis(int size) {
xAxis.setTiltAngle(-90f);
xAxis.setMaxLabelChars(7);

SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.is24HourFormat(xdrip.getAppContext()) ? "HH:mm" : "a h:mm");
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.is24HourFormat(xdrip.getAppContext()) ? "HH:mm" : "a h:mm", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
// OVERRIDE SOME symbols WHILE RETAINING OTHERS
symbols.setAmPmStrings(new String[] { "a", "p" });
sdf.setDateFormatSymbols(symbols);

final java.text.DateFormat timeFormat = sdf;
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTimeInMillis(JoH.tsl());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
Expand All @@ -120,14 +132,13 @@ static private Axis chartXAxis(int size) {

xAxis.setAutoGenerated(false);

// TODO make this a better am/pm/24 hour thingy by dividing a day down? DST??? how does that work?? - done on load of value?
List<AxisValue> axisValues = new ArrayList<>();
final List<AxisValue> axisValues = new ArrayList<>();
final int step = size / segments;
final long dayStartMs = calendar.getTimeInMillis();
final long increment = Constants.DAY_IN_MS / segments;
for (int i = 0; i < size; i = i + step) {
calendar.setTimeInMillis(dayStartMs + i*increment);
axisValues.add(new AxisValue(i / step, timeFormat.format(calendar.getTimeInMillis()).toCharArray()));
axisValues.add(new AxisValue(((float)i / step)+((float)step/6), sdf.format(calendar.getTimeInMillis()).toCharArray()));
}
xAxis.setValues(axisValues);
return xAxis;
Expand Down
Loading

0 comments on commit 2a34e3c

Please sign in to comment.