Local Profile

This commit is contained in:
Milos Kozak 2016-12-31 20:10:36 +01:00
parent 6b18336fa2
commit 38a9c94258
7 changed files with 283 additions and 97 deletions

View file

@ -11,6 +11,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import com.squareup.otto.Subscribe;
@ -18,14 +19,16 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventInitializationChanged;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfilePlugin;
import info.nightscout.utils.SafeParse;
import info.nightscout.utils.TimeListEdit;
public class LocalProfileFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(LocalProfileFragment.class);
@ -39,40 +42,40 @@ public class LocalProfileFragment extends Fragment implements FragmentBase {
EditText diaView;
RadioButton mgdlView;
RadioButton mmolView;
EditText icView;
EditText isfView;
TimeListEdit icView;
TimeListEdit isfView;
EditText carView;
EditText basalView;
EditText targetlowView;
EditText targethighView;
TimeListEdit basalView;
TimeListEdit targetView;
Button profileswitchButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.simpleprofile_fragment, container, false);
diaView = (EditText) layout.findViewById(R.id.simpleprofile_dia);
mgdlView = (RadioButton) layout.findViewById(R.id.simpleprofile_mgdl);
mmolView = (RadioButton) layout.findViewById(R.id.simpleprofile_mmol);
icView = (EditText) layout.findViewById(R.id.simpleprofile_ic);
isfView = (EditText) layout.findViewById(R.id.simpleprofile_isf);
carView = (EditText) layout.findViewById(R.id.simpleprofile_car);
basalView = (EditText) layout.findViewById(R.id.simpleprofile_basalrate);
targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow);
targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh);
profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch);
Runnable save = new Runnable() {
@Override
public void run() {
localProfilePlugin.storeSettings();
}
};
View layout = inflater.inflate(R.layout.localprofile_fragment, container, false);
diaView = (EditText) layout.findViewById(R.id.localprofile_dia);
mgdlView = (RadioButton) layout.findViewById(R.id.localprofile_mgdl);
mmolView = (RadioButton) layout.findViewById(R.id.localprofile_mmol);
icView = new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.sResources.getString(R.string.nsprofileview_ic_label), getPlugin().ic, null, new DecimalFormat("0.0"), save);
isfView = new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.sResources.getString(R.string.nsprofileview_isf_label), getPlugin().isf, null, new DecimalFormat("0.0"), save);
carView = (EditText) layout.findViewById(R.id.localprofile_car);
basalView = new TimeListEdit(getContext(), layout, R.id.localprofile_basal, MainApp.sResources.getString(R.string.nsprofileview_basal_label), getPlugin().basal, null, new DecimalFormat("0.00"), save);
targetView = new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.sResources.getString(R.string.nsprofileview_target_label), getPlugin().targetLow, getPlugin().targetHigh, new DecimalFormat("0.0"), save);
profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch);
onStatusEvent(null);
mgdlView.setChecked(localProfilePlugin.mgdl);
mmolView.setChecked(localProfilePlugin.mmol);
diaView.setText(localProfilePlugin.dia.toString());
icView.setText(localProfilePlugin.ic.toString());
isfView.setText(localProfilePlugin.isf.toString());
carView.setText(localProfilePlugin.car.toString());
basalView.setText(localProfilePlugin.basal.toString());
targetlowView.setText(localProfilePlugin.targetLow.toString());
targethighView.setText(localProfilePlugin.targetHigh.toString());
mgdlView.setOnClickListener(new View.OnClickListener() {
@Override
@ -119,23 +122,13 @@ public class LocalProfileFragment extends Fragment implements FragmentBase {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString());
localProfilePlugin.ic = SafeParse.stringToDouble(icView.getText().toString());
localProfilePlugin.isf = SafeParse.stringToDouble(isfView.getText().toString());
localProfilePlugin.car = SafeParse.stringToDouble(carView.getText().toString());
localProfilePlugin.basal = SafeParse.stringToDouble(basalView.getText().toString());
localProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString());
localProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString());
localProfilePlugin.storeSettings();
}
};
diaView.addTextChangedListener(textWatch);
icView.addTextChangedListener(textWatch);
isfView.addTextChangedListener(textWatch);
carView.addTextChangedListener(textWatch);
basalView.addTextChangedListener(textWatch);
targetlowView.addTextChangedListener(textWatch);
targethighView.addTextChangedListener(textWatch);
onStatusEvent(null);

View file

@ -30,15 +30,17 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
private static NSProfile convertedProfile = null;
final private String DEFAULTARRAY = "[{\"timeAsSeconds\":0,\"value\":0}]";
boolean mgdl;
boolean mmol;
Double dia;
Double ic;
Double isf;
JSONArray ic;
JSONArray isf;
Double car;
Double basal;
Double targetLow;
Double targetHigh;
JSONArray basal;
JSONArray targetLow;
JSONArray targetHigh;
public LocalProfilePlugin() {
loadSettings();
@ -89,15 +91,15 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
log.debug("Storing settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("SimpleProfile" + "mmol", mmol);
editor.putBoolean("SimpleProfile" + "mgdl", mgdl);
editor.putString("SimpleProfile" + "dia", dia.toString());
editor.putString("SimpleProfile" + "ic", ic.toString());
editor.putString("SimpleProfile" + "isf", isf.toString());
editor.putString("SimpleProfile" + "car", car.toString());
editor.putString("SimpleProfile" + "basal", basal.toString());
editor.putString("SimpleProfile" + "targetlow", targetLow.toString());
editor.putString("SimpleProfile" + "targethigh", targetHigh.toString());
editor.putBoolean("LocalProfile" + "mmol", mmol);
editor.putBoolean("LocalProfile" + "mgdl", mgdl);
editor.putString("LocalProfile" + "dia", dia.toString());
editor.putString("LocalProfile" + "ic", ic.toString());
editor.putString("LocalProfile" + "isf", isf.toString());
editor.putString("LocalProfile" + "car", car.toString());
editor.putString("LocalProfile" + "basal", basal.toString());
editor.putString("LocalProfile" + "targetlow", targetLow.toString());
editor.putString("LocalProfile" + "targethigh", targetHigh.toString());
editor.commit();
createConvertedProfile();
@ -108,69 +110,124 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
log.debug("Loading stored settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
if (settings.contains("SimpleProfile" + "mgdl"))
if (settings.contains("LocalProfile" + "mgdl"))
try {
mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true);
mgdl = settings.getBoolean("LocalProfile" + "mgdl", false);
} catch (Exception e) {
log.debug(e.getMessage());
}
else mgdl = true;
if (settings.contains("SimpleProfile" + "mmol"))
else mgdl = false;
if (settings.contains("LocalProfile" + "mmol"))
try {
mmol = settings.getBoolean("SimpleProfile" + "mmol", false);
mmol = settings.getBoolean("LocalProfile" + "mmol", true);
} catch (Exception e) {
log.debug(e.getMessage());
}
else mmol = false;
if (settings.contains("SimpleProfile" + "dia"))
else mmol = true;
if (settings.contains("LocalProfile" + "dia"))
try {
dia = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "dia", "3"));
dia = SafeParse.stringToDouble(settings.getString("LocalProfile" + "dia", "3"));
} catch (Exception e) {
log.debug(e.getMessage());
}
else dia = 3d;
if (settings.contains("SimpleProfile" + "ic"))
if (settings.contains("LocalProfile" + "ic"))
try {
ic = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "ic", "20"));
ic = new JSONArray(settings.getString("LocalProfile" + "ic", DEFAULTARRAY));
} catch (Exception e) {
log.debug(e.getMessage());
try {
ic = new JSONArray(DEFAULTARRAY);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
else ic = 20d;
if (settings.contains("SimpleProfile" + "isf"))
else {
try {
isf = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "isf", "200"));
ic = new JSONArray(DEFAULTARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (settings.contains("LocalProfile" + "isf"))
try {
isf = new JSONArray(settings.getString("LocalProfile" + "isf", DEFAULTARRAY));
} catch (Exception e) {
log.debug(e.getMessage());
try {
isf = new JSONArray(DEFAULTARRAY);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
else isf = 200d;
if (settings.contains("SimpleProfile" + "car"))
else {
try {
car = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "car", "20"));
isf = new JSONArray(DEFAULTARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (settings.contains("LocalProfile" + "car"))
try {
car = SafeParse.stringToDouble(settings.getString("LocalProfile" + "car", "20"));
} catch (Exception e) {
log.debug(e.getMessage());
}
else car = 20d;
if (settings.contains("SimpleProfile" + "basal"))
if (settings.contains("LocalProfile" + "basal"))
try {
basal = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "basal", "1"));
basal = new JSONArray(settings.getString("LocalProfile" + "basal", DEFAULTARRAY));
} catch (Exception e) {
log.debug(e.getMessage());
try {
basal = new JSONArray(DEFAULTARRAY);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
else basal = 1d;
if (settings.contains("SimpleProfile" + "targetlow"))
else {
try {
targetLow = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targetlow", "80"));
basal = new JSONArray(DEFAULTARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (settings.contains("LocalProfile" + "targetlow"))
try {
targetLow = new JSONArray(settings.getString("LocalProfile" + "targetlow", DEFAULTARRAY));
} catch (Exception e) {
log.debug(e.getMessage());
try {
targetLow = new JSONArray(DEFAULTARRAY);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
else targetLow = 80d;
if (settings.contains("SimpleProfile" + "targethigh"))
else {
try {
targetHigh = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targethigh", "120"));
targetLow = new JSONArray(DEFAULTARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (settings.contains("LocalProfile" + "targethigh"))
try {
targetHigh = new JSONArray(settings.getString("LocalProfile" + "targethigh", DEFAULTARRAY));
} catch (Exception e) {
log.debug(e.getMessage());
try {
targetHigh = new JSONArray(DEFAULTARRAY);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
else targetHigh = 120d;
else {
try {
targetHigh = new JSONArray(DEFAULTARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
createConvertedProfile();
}
@ -218,21 +275,21 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
JSONObject profile = new JSONObject();
try {
json.put("defaultProfile", "SimpleProfile");
json.put("defaultProfile", "LocalProfile");
json.put("store", store);
profile.put("dia", dia);
profile.put("carbratio", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", ic)));
profile.put("carbratio", ic);
profile.put("carbs_hr", car);
profile.put("sens", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", isf)));
profile.put("basal", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", basal)));
profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow)));
profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh)));
profile.put("sens", isf);
profile.put("basal", basal);
profile.put("target_low", targetLow);
profile.put("target_high", targetHigh);
profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL);
store.put("SimpleProfile", profile);
store.put("LocalProfile", profile);
} catch (JSONException e) {
e.printStackTrace();
}
convertedProfile = new NSProfile(json, "SimpleProfile");
convertedProfile = new NSProfile(json, "LocalProfile");
}
@Override

View file

@ -1,11 +1,14 @@
package info.nightscout.utils;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
@ -46,8 +49,9 @@ public class TimeListEdit {
JSONArray data1;
JSONArray data2;
NumberFormat formatter;
Runnable save;
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter) {
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter, Runnable save) {
this.context = context;
this.view = view;
this.resLayoutId = resLayoutId;
@ -55,6 +59,7 @@ public class TimeListEdit {
this.data1 = data1;
this.data2 = data2;
this.formatter = formatter;
this.save = save;
buildView();
}
@ -67,9 +72,14 @@ public class TimeListEdit {
TextView textlabel = new TextView(context);
textlabel.setText(label);
textlabel.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(10, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
textlabel.setLayoutParams(llp);
textlabel.setBackgroundColor(MainApp.sResources.getColor(R.color.linearBlockBackground));
if (Build.VERSION.SDK_INT < 23)
textlabel.setTextAppearance(context, android.R.style.TextAppearance_Medium);
else
textlabel.setTextAppearance(android.R.style.TextAppearance_Medium);
layout.addView(textlabel);
for (int i = 0; i < itemsCount(); i++) {
@ -188,7 +198,11 @@ public class TimeListEdit {
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.add);
LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom);
illp.gravity = Gravity.CENTER;
layout.addView(imageView);
imageView.setLayoutParams(illp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -255,13 +269,15 @@ public class TimeListEdit {
}
public double value2(int index) {
try {
JSONObject item = (JSONObject) data2.get(index);
if (item.has("value")) {
return item.getDouble("value");
if (data2 != null) {
try {
JSONObject item = (JSONObject) data2.get(index);
if (item.has("value")) {
return item.getDouble("value");
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
return 0d;
}
@ -278,6 +294,7 @@ public class TimeListEdit {
newObject2.put("value", value2);
data2.put(index, newObject2);
}
if (save != null) save.run();
} catch (JSONException e) {
e.printStackTrace();
}
@ -294,6 +311,7 @@ public class TimeListEdit {
}
// add new object
editItem(index, timeAsSeconds, value1, value2);
if (save != null) save.run();
} catch (JSONException e) {
e.printStackTrace();
}
@ -304,6 +322,7 @@ public class TimeListEdit {
data1.remove(index);
if (data2 != null)
data2.remove(index);
if (save != null) save.run();
}
void log() {

View file

@ -0,0 +1,118 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plugins.LocalProfile.LocalProfileFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/units"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="@color/linearBlockBackground" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/localprofile_mgdl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mgdl" />
<RadioButton
android:id="@+id/localprofile_mmol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mmol" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dia"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="@color/linearBlockBackground" />
<EditText
android:id="@+id/localprofile_dia"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:textAlignment="center"
android:layout_gravity="center_horizontal"
android:layout_width="70dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/localprofile_ic"></LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/localprofile_isf"></LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/absorption_rate"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="@color/linearBlockBackground" />
<EditText
android:id="@+id/localprofile_car"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:textAlignment="center"
android:layout_gravity="center_horizontal"
android:layout_width="70dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/localprofile_basal"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/localprofile_target">
</LinearLayout>
<Button
android:id="@+id/localprofile_profileswitch"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/send_to_pump"
android:textColor="@color/colorProfileSwitchButton" />
</LinearLayout>
</ScrollView>
</FrameLayout>

View file

@ -41,7 +41,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dia"
android:text="@string/dia"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
@ -139,12 +139,6 @@
android:text="@string/send_to_pump"
android:textColor="@color/colorProfileSwitchButton" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/simpleprofile_test"></LinearLayout>
</LinearLayout>
</ScrollView>

View file

@ -11,21 +11,25 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timelistedit_time"
/>
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/timelistedit_edit1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:inputType="numberDecimal"/>
android:inputType="numberDecimal"
android:layout_gravity="center_horizontal"
android:textAlignment="center" />
<EditText
android:id="@+id/timelistedit_edit2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:inputType="numberDecimal"/>
android:inputType="numberDecimal"
android:layout_gravity="center_horizontal"
android:textAlignment="center" />
<ImageView
android:id="@+id/timelistedit_add"

View file

@ -384,4 +384,5 @@
<string name="smscommunicator_bolusdelivered">Bolus %.2fU aplikován úspěšně</string>
<string name="ongoingnotificaction">Průběžné oznámení</string>
<string name="old_data">ZASTARALÉ</string>
<string name="localprofile">Místní profil</string>
</resources>