more LocalProfile work

This commit is contained in:
Milos Kozak 2017-10-02 19:57:41 +02:00
parent 1a5b72f745
commit 8cecac0a7f
11 changed files with 261 additions and 234 deletions

View file

@ -40,7 +40,7 @@ public class Config {
public static final boolean logNSUpload = true; public static final boolean logNSUpload = true;
public static final boolean logPumpActions = true; public static final boolean logPumpActions = true;
public static final boolean logCongigBuilderActions = true; public static final boolean logCongigBuilderActions = true;
public static final boolean logAutosensData = true; public static final boolean logAutosensData = false;
// DanaR specific // DanaR specific
public static final boolean logDanaBTComm = true; public static final boolean logDanaBTComm = true;

View file

@ -195,7 +195,7 @@ public class Profile {
int offset = -(timeshift % 24) + 24; int offset = -(timeshift % 24) + 24;
Integer shiftedTime = originalTime + offset * 60 * 60; Integer shiftedTime = originalTime + offset * 60 * 60;
shiftedTime = shiftedTime % (24 * 60 * 60); shiftedTime = shiftedTime % (24 * 60 * 60);
log.debug("(Sec) Original time: " + new Date(originalTime).toLocaleString() + " ShiftedTime: " + new Date(shiftedTime).toLocaleString()); //log.debug("(Sec) Original time: " + new Date(originalTime).toLocaleString() + " ShiftedTime: " + new Date(shiftedTime).toLocaleString());
return shiftedTime; return shiftedTime;
} }

View file

@ -190,20 +190,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
// bg // bg
bgUnitsView.setText(units); bgUnitsView.setText(units);
Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL); TextWatcher bgTextWatcher = new TextWatcher() {
editBg = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_bginput);
editTemptarget = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_temptarget);
if (profile == null) {
editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false);
editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false);
} else if (profile.getUnits().equals(Constants.MMOL)) {
editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
} else {
editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
}
editBg.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
} }
@ -213,8 +200,21 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
if (sensorRadioButton.isChecked()) meterRadioButton.setChecked(true); if (sensorRadioButton.isChecked()) meterRadioButton.setChecked(true);
} }
}); };
Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL);
editBg = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_bginput);
editTemptarget = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_temptarget);
if (profile == null) {
editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, bgTextWatcher);
editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false);
} else if (profile.getUnits().equals(Constants.MMOL)) {
editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false, bgTextWatcher);
editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
} else {
editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false, bgTextWatcher);
editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
}
sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -236,10 +236,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
editDuration = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_durationinput); editDuration = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_durationinput);
editDuration.setParams(0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false); editDuration.setParams(0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false);
Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); TextWatcher percentTextWatcher = new TextWatcher() {
editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput);
editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true);
editPercent.addTextChangedListener(new TextWatcher() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
} }
@ -255,12 +252,13 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
layoutPercent.setVisibility(View.VISIBLE); layoutPercent.setVisibility(View.VISIBLE);
layoutAbsolute.setVisibility(View.GONE); layoutAbsolute.setVisibility(View.GONE);
} }
}); };
Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit);
editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput);
editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true); editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true, percentTextWatcher);
editAbsolute.addTextChangedListener(new TextWatcher() {
TextWatcher absoluteTextWatcher = new TextWatcher() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
} }
@ -276,7 +274,11 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
layoutPercent.setVisibility(View.GONE); layoutPercent.setVisibility(View.GONE);
layoutAbsolute.setVisibility(View.VISIBLE); layoutAbsolute.setVisibility(View.VISIBLE);
} }
}); };
Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit);
editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput);
editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true, absoluteTextWatcher);
editCarbTime = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput); editCarbTime = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput);
editCarbTime.setParams(0d, -60d, 60d, 5d, new DecimalFormat("0"), false); editCarbTime.setParams(0d, -60d, 60d, 5d, new DecimalFormat("0"), false);

View file

@ -420,15 +420,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
BgReading lastBg = DatabaseHelper.actualBg(); BgReading lastBg = DatabaseHelper.actualBg();
if (lastBg != null) { if (lastBg != null) {
editBg.removeTextChangedListener(textWatcher);
//bgInput.setText(lastBg.valueToUnitsToString(units));
editBg.setValue(lastBg.valueToUnits(units)); editBg.setValue(lastBg.valueToUnits(units));
editBg.addTextChangedListener(textWatcher);
} else { } else {
editBg.removeTextChangedListener(textWatcher);
//bgInput.setText("");
editBg.setValue(0d); editBg.setValue(0d);
editBg.addTextChangedListener(textWatcher);
} }
ttCheckbox.setEnabled(MainApp.getConfigBuilder().getTempTargetFromHistory() != null); ttCheckbox.setEnabled(MainApp.getConfigBuilder().getTempTargetFromHistory() != null);
@ -465,19 +459,13 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
Double c_correction = SafeParse.stringToDouble(editCorr.getText()); Double c_correction = SafeParse.stringToDouble(editCorr.getText());
Double corrAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(c_correction); Double corrAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(c_correction);
if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work
editCorr.removeTextChangedListener(textWatcher);
editCorr.setValue(0d); editCorr.setValue(0d);
editCorr.addTextChangedListener(textWatcher);
//wizardDialogDeliverButton.setVisibility(Button.GONE);
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied));
return; return;
} }
Integer carbsAfterConstraint = MainApp.getConfigBuilder().applyCarbsConstraints(c_carbs); Integer carbsAfterConstraint = MainApp.getConfigBuilder().applyCarbsConstraints(c_carbs);
if (c_carbs - carbsAfterConstraint != 0) { if (c_carbs - carbsAfterConstraint != 0) {
editCarbs.removeTextChangedListener(textWatcher);
editCarbs.setValue(0d); editCarbs.setValue(0d);
editCarbs.addTextChangedListener(textWatcher);
//wizardDialogDeliverButton.setVisibility(Button.GONE);
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied));
return; return;
} }

View file

@ -9,7 +9,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton; import android.widget.RadioButton;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
@ -27,6 +26,7 @@ import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.utils.NumberPicker;
import info.nightscout.utils.SafeParse; import info.nightscout.utils.SafeParse;
import info.nightscout.utils.TimeListEdit; import info.nightscout.utils.TimeListEdit;
@ -39,7 +39,7 @@ public class LocalProfileFragment extends SubscriberFragment {
return localProfilePlugin; return localProfilePlugin;
} }
EditText diaView; NumberPicker diaView;
RadioButton mgdlView; RadioButton mgdlView;
RadioButton mmolView; RadioButton mmolView;
TimeListEdit icView; TimeListEdit icView;
@ -51,6 +51,7 @@ public class LocalProfileFragment extends SubscriberFragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
try {
Runnable save = new Runnable() { Runnable save = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -58,14 +59,34 @@ public class LocalProfileFragment extends SubscriberFragment {
} }
}; };
TextWatcher textWatch = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString());
localProfilePlugin.storeSettings();
}
};
View layout = inflater.inflate(R.layout.localprofile_fragment, container, false); View layout = inflater.inflate(R.layout.localprofile_fragment, container, false);
diaView = (EditText) layout.findViewById(R.id.localprofile_dia); diaView = (NumberPicker) layout.findViewById(R.id.localprofile_dia);
diaView.setParams(localProfilePlugin.dia, 2d, 48d, 0.1d, new DecimalFormat("0.0"), false, textWatch);
mgdlView = (RadioButton) layout.findViewById(R.id.localprofile_mgdl); mgdlView = (RadioButton) layout.findViewById(R.id.localprofile_mgdl);
mmolView = (RadioButton) layout.findViewById(R.id.localprofile_mmol); 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); icView = new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.sResources.getString(R.string.nsprofileview_ic_label), getPlugin().ic, null, 0.1d, 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); isfView = new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.sResources.getString(R.string.nsprofileview_isf_label), getPlugin().isf, null, 0.1d, new DecimalFormat("0.0"), save);
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); basalView = new TimeListEdit(getContext(), layout, R.id.localprofile_basal, MainApp.sResources.getString(R.string.nsprofileview_basal_label), getPlugin().basal, null, 0.01d, 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); targetView = new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.sResources.getString(R.string.nsprofileview_target_label), getPlugin().targetLow, getPlugin().targetHigh, 0.1d, new DecimalFormat("0.0"), save);
profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch); profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch);
PumpInterface pump = MainApp.getConfigBuilder(); PumpInterface pump = MainApp.getConfigBuilder();
@ -77,7 +98,6 @@ public class LocalProfileFragment extends SubscriberFragment {
mgdlView.setChecked(localProfilePlugin.mgdl); mgdlView.setChecked(localProfilePlugin.mgdl);
mmolView.setChecked(localProfilePlugin.mmol); mmolView.setChecked(localProfilePlugin.mmol);
diaView.setText(localProfilePlugin.dia.toString());
mgdlView.setOnClickListener(new View.OnClickListener() { mgdlView.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -109,30 +129,14 @@ public class LocalProfileFragment extends SubscriberFragment {
} }
}); });
TextWatcher textWatch = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString());
localProfilePlugin.storeSettings();
}
};
diaView.addTextChangedListener(textWatch);
updateGUI(); updateGUI();
return layout; return layout;
} catch (Exception e) {
log.error("Unhandled exception: ", e);
}
return null;
} }
@Subscribe @Subscribe

View file

@ -24,11 +24,11 @@ import info.nightscout.utils.SP;
public class LocalProfilePlugin implements PluginBase, ProfileInterface { public class LocalProfilePlugin implements PluginBase, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(LocalProfilePlugin.class); private static Logger log = LoggerFactory.getLogger(LocalProfilePlugin.class);
private static boolean fragmentEnabled = false; private boolean fragmentEnabled = false;
private static boolean fragmentVisible = true; private boolean fragmentVisible = true;
private static ProfileStore convertedProfile = null; private ProfileStore convertedProfile = null;
private static String convertedProfileName = null; private String convertedProfileName = null;
final private String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]"; final private String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]";
@ -107,8 +107,6 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
} }
public void storeSettings() { public void storeSettings() {
if (Config.logPrefsChange)
log.debug("Storing settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("LocalProfile" + "mmol", mmol); editor.putBoolean("LocalProfile" + "mmol", mmol);
@ -120,8 +118,10 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
editor.putString("LocalProfile" + "targetlow", targetLow.toString()); editor.putString("LocalProfile" + "targetlow", targetLow.toString());
editor.putString("LocalProfile" + "targethigh", targetHigh.toString()); editor.putString("LocalProfile" + "targethigh", targetHigh.toString());
editor.commit(); editor.apply();
createConvertedProfile(); createConvertedProfile();
if (Config.logPrefsChange)
log.debug("Storing settings: " + getProfile().getData().toString());
} }
private void loadSettings() { private void loadSettings() {
@ -136,7 +136,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
} catch (JSONException e1) { } catch (JSONException e1) {
try { try {
ic = new JSONArray(DEFAULTARRAY); ic = new JSONArray(DEFAULTARRAY);
} catch (JSONException e2) { } catch (JSONException ignored) {
} }
} }
try { try {
@ -144,7 +144,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
} catch (JSONException e1) { } catch (JSONException e1) {
try { try {
isf = new JSONArray(DEFAULTARRAY); isf = new JSONArray(DEFAULTARRAY);
} catch (JSONException e2) { } catch (JSONException ignored) {
} }
} }
try { try {
@ -152,7 +152,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
} catch (JSONException e1) { } catch (JSONException e1) {
try { try {
basal = new JSONArray(DEFAULTARRAY); basal = new JSONArray(DEFAULTARRAY);
} catch (JSONException e2) { } catch (JSONException ignored) {
} }
} }
try { try {
@ -160,7 +160,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
} catch (JSONException e1) { } catch (JSONException e1) {
try { try {
targetLow = new JSONArray(DEFAULTARRAY); targetLow = new JSONArray(DEFAULTARRAY);
} catch (JSONException e2) { } catch (JSONException ignored) {
} }
} }
try { try {
@ -168,7 +168,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
} catch (JSONException e1) { } catch (JSONException e1) {
try { try {
targetHigh = new JSONArray(DEFAULTARRAY); targetHigh = new JSONArray(DEFAULTARRAY);
} catch (JSONException e2) { } catch (JSONException ignored) {
} }
} }
createConvertedProfile(); createConvertedProfile();
@ -212,7 +212,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface {
"created_at": "2016-06-16T08:34:41.256Z" "created_at": "2016-06-16T08:34:41.256Z"
} }
*/ */
void createConvertedProfile() { private void createConvertedProfile() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONObject store = new JSONObject(); JSONObject store = new JSONObject();
JSONObject profile = new JSONObject(); JSONObject profile = new JSONObject();

View file

@ -1,6 +1,8 @@
package info.nightscout.utils; package info.nightscout.utils;
import android.support.v4.util.LongSparseArray;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.SparseIntArray;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
@ -132,4 +134,16 @@ public class DateUtil {
return String.format(MainApp.sResources.getString(R.string.minago), mins); return String.format(MainApp.sResources.getString(R.string.minago), mins);
} }
private static LongSparseArray<String> timeStrings = new LongSparseArray<>();
public static String timeStringFromSeconds(int seconds) {
String cached = timeStrings.get(seconds);
if (cached != null)
return cached;
String t = DateUtils.formatDateTime(MainApp.instance(), toDate(seconds).getTime(), DateUtils.FORMAT_SHOW_TIME);
timeStrings.put(seconds, t);
return t;
}
} }

View file

@ -42,6 +42,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
Double step = 1d; Double step = 1d;
NumberFormat formater; NumberFormat formater;
boolean allowZero = false; boolean allowZero = false;
TextWatcher textWatcher = null;
private Handler mHandler; private Handler mHandler;
private ScheduledExecutorService mUpdater; private ScheduledExecutorService mUpdater;
@ -94,9 +95,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
LayoutInflater.from(context).inflate(R.layout.number_picker_layout, this, true); LayoutInflater.from(context).inflate(R.layout.number_picker_layout, this, true);
// init ui components // init ui components
this.minusButton = (Button) findViewById(R.id.decrement); minusButton = (Button) findViewById(R.id.decrement);
this.plusButton = (Button) findViewById(R.id.increment); minusButton.setId(View.generateViewId());
this.editText = (EditText) findViewById(R.id.display); plusButton = (Button) findViewById(R.id.increment);
plusButton.setId(View.generateViewId());
editText = (EditText) findViewById(R.id.display);
editText.setId(View.generateViewId());
mHandler = new Handler() { mHandler = new Handler() {
@Override @Override
@ -121,16 +125,9 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
plusButton.setOnClickListener(this); plusButton.setOnClickListener(this);
} }
public void removeTextChangedListener(TextWatcher textWatcher) {
editText.removeTextChangedListener(textWatcher);
}
public void addTextChangedListener(TextWatcher textWatcher) {
editText.addTextChangedListener(textWatcher);
}
public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) { public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) {
setParams(initValue, minValue, maxValue, step, formater, allowZero); setParams(initValue, minValue, maxValue, step, formater, allowZero);
this.textWatcher = textWatcher;
editText.addTextChangedListener(textWatcher); editText.addTextChangedListener(textWatcher);
} }
@ -146,8 +143,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
} }
public void setValue(Double value) { public void setValue(Double value) {
if (textWatcher != null)
editText.removeTextChangedListener(textWatcher);
this.value = value; this.value = value;
updateEditText(); updateEditText();
if (textWatcher != null)
editText.addTextChangedListener(textWatcher);
} }
public Double getValue() { public Double getValue() {

View file

@ -49,22 +49,25 @@ public class TimeListEdit {
String label; String label;
JSONArray data1; JSONArray data1;
JSONArray data2; JSONArray data2;
double step;
NumberFormat formatter; NumberFormat formatter;
Runnable save; Runnable save;
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter, Runnable save) { public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, double step, NumberFormat formatter, Runnable save) {
this.context = context; this.context = context;
this.view = view; this.view = view;
this.resLayoutId = resLayoutId; this.resLayoutId = resLayoutId;
this.label = label; this.label = label;
this.data1 = data1; this.data1 = data1;
this.data2 = data2; this.data2 = data2;
this.step = step;
this.formatter = formatter; this.formatter = formatter;
this.save = save; this.save = save;
buildView(); buildView();
} }
private void buildView() { private void buildView() {
log.debug("buildView start");
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
layout = (LinearLayout) view.findViewById(resLayoutId); layout = (LinearLayout) view.findViewById(resLayoutId);
@ -82,42 +85,84 @@ public class TimeListEdit {
for (int i = 0; i < itemsCount(); i++) { for (int i = 0; i < itemsCount(); i++) {
View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); View childview = inflater.inflate(R.layout.timelistedit_element, layout, false);
childview.setId(View.generateViewId()); //childview.setId(View.generateViewId());
final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time); final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time);
timeSpinner.setId(View.generateViewId()); //timeSpinner.setId(View.generateViewId());
int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1); int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1);
int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1); int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1);
if (i == 0) next = ONEHOURINSECONDS; if (i == 0) next = ONEHOURINSECONDS;
fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); fillSpinner(timeSpinner, secondFromMidnight(i), previous, next);
final EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1); final int fixedPos = i;
editText1.setId(View.generateViewId()); final NumberPicker editText1 = (NumberPicker) childview.findViewById(R.id.timelistedit_edit1);
fillNumber(editText1, value1(i));
final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2)); TextWatcher tw1 = new TextWatcher() {
fillNumber(editText2, value2(i)); @Override
editText2.setId(View.generateViewId()); public void afterTextChanged(Editable s) {
log.debug("aaaa");
editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.getText()), value2(fixedPos));
callSave();
log();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
};
editText1.setParams(value1(i), 0.1d, 100d, step, formatter, false, tw1);
final NumberPicker editText2 = (NumberPicker) childview.findViewById(R.id.timelistedit_edit2);
TextWatcher tw2 = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
log.debug("bbbbb");
editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.getText()));
callSave();
log();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
};
editText2.setParams(value2(i), 0.1d, 100d, step, formatter, false, tw2);
if (data2 == null) { if (data2 == null) {
editText2.setVisibility(View.GONE); editText2.setVisibility(View.GONE);
} }
ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add); ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add);
addbutton.setId(View.generateViewId()); //addbutton.setId(View.generateViewId());
ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove); ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove);
removebutton.setId(View.generateViewId()); //removebutton.setId(View.generateViewId());
if (itemsCount() == 1 && i == 0) { if (itemsCount() == 1 || i == 0) {
removebutton.setVisibility(View.GONE); removebutton.setVisibility(View.INVISIBLE);
} }
if (itemsCount() >= 24) { if (itemsCount() >= 24 || secondFromMidnight(i) >= 82800) {
addbutton.setVisibility(View.GONE); addbutton.setVisibility(View.INVISIBLE);
} }
final int fixedPos = i;
addbutton.setOnClickListener(new View.OnClickListener() { addbutton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
log.debug("ccccc");
layout.removeAllViews();
int seconds = secondFromMidnight(fixedPos); int seconds = secondFromMidnight(fixedPos);
addItem(fixedPos, seconds, 0, 0); addItem(fixedPos, seconds, 0, 0);
// for here for the rest of values // for here for the rest of values
@ -128,6 +173,7 @@ public class TimeListEdit {
} }
while (itemsCount() > 24 || secondFromMidnight(itemsCount() - 1) > 23 * ONEHOURINSECONDS) while (itemsCount() > 24 || secondFromMidnight(itemsCount() - 1) > 23 * ONEHOURINSECONDS)
removeItem(itemsCount() - 1); removeItem(itemsCount() - 1);
callSave();
log(); log();
buildView(); buildView();
} }
@ -136,7 +182,10 @@ public class TimeListEdit {
removebutton.setOnClickListener(new View.OnClickListener() { removebutton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
log.debug("dddd");
layout.removeAllViews();
removeItem(fixedPos); removeItem(fixedPos);
callSave();
log(); log();
buildView(); buildView();
} }
@ -145,57 +194,26 @@ public class TimeListEdit {
timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
log.debug("eeeeee");
layout.removeAllViews();
int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString()); int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString());
editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos)); editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos));
log(); log();
callSave();
buildView(); buildView();
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
log.debug("fffff");
layout.removeAllViews();
editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos)); editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos));
log(); log();
callSave();
buildView(); buildView();
} }
} }
); );
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.getText().toString()), value2(fixedPos));
log();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.getText().toString()));
log();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
layout.addView(childview); layout.addView(childview);
} }
@ -210,13 +228,15 @@ public class TimeListEdit {
imageView.setOnClickListener(new View.OnClickListener() { imageView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
log.debug("gggggggg");
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0); addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
callSave();
log(); log();
buildView(); buildView();
} }
}); });
} }
log.debug("buildView end");
} }
public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) { public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) {
@ -224,24 +244,17 @@ public class TimeListEdit {
ArrayList<CharSequence> timeList = new ArrayList<>(); ArrayList<CharSequence> timeList = new ArrayList<>();
int pos = 0; int pos = 0;
for (int t = previous + ONEHOURINSECONDS; t < next; t += ONEHOURINSECONDS) { for (int t = previous + ONEHOURINSECONDS; t < next; t += ONEHOURINSECONDS) {
timeList.add(DateUtil.timeString(DateUtil.toDate(t))); timeList.add(DateUtil.timeStringFromSeconds(t));
if (secondsFromMidnight == t) posInList = pos; if (secondsFromMidnight == t) posInList = pos;
pos++; pos++;
} }
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context,
R.layout.spinner_centered, timeList); R.layout.spinner_centered, timeList);
spinner.setAdapter(adapter); spinner.setAdapter(adapter);
spinner.setSelection(posInList, false); spinner.setSelection(posInList, false);
} }
public void fillNumber(EditText edit, Double value) {
if (value.equals(0d))
edit.setText("");
else
edit.setText(formatter.format(value));
}
public int itemsCount() { public int itemsCount() {
return data1.length(); return data1.length();
} }
@ -250,7 +263,13 @@ public class TimeListEdit {
try { try {
JSONObject item = (JSONObject) data1.get(index); JSONObject item = (JSONObject) data1.get(index);
if (item.has("timeAsSeconds")) { if (item.has("timeAsSeconds")) {
return item.getInt("timeAsSeconds"); int time = item.getInt("timeAsSeconds");
if (index == 0 && time != 0) {
// fix the bug, every array must start with 0
item.put("timeAsSeconds", 0);
time = 0;
}
return time;
} }
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -303,7 +322,6 @@ public class TimeListEdit {
newObject2.put("value", value2); newObject2.put("value", value2);
data2.put(index, newObject2); data2.put(index, newObject2);
} }
if (save != null) save.run();
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
@ -320,7 +338,6 @@ public class TimeListEdit {
} }
// add new object // add new object
editItem(index, timeAsSeconds, value1, value2); editItem(index, timeAsSeconds, value1, value2);
if (save != null) save.run();
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
@ -331,13 +348,15 @@ public class TimeListEdit {
data1.remove(index); data1.remove(index);
if (data2 != null) if (data2 != null)
data2.remove(index); data2.remove(index);
if (save != null) save.run();
} }
void log() { void log() {
for (int i = 0; i < data1.length(); i++) { for (int i = 0; i < data1.length(); i++) {
int pos = 0; log.debug(i + ": @" + DateUtil.timeStringFromSeconds(secondFromMidnight(i)) + " " + value1(i) + (data2 != null ? " " + value2(i) : ""));
log.debug(i + ": @" + DateUtil.timeString(DateUtil.toDate(secondFromMidnight(i))) + " " + value1(i) + (data2 != null ? " " + value2(i) : ""));
} }
} }
void callSave() {
if (save != null) save.run();
}
} }

View file

@ -23,6 +23,7 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"> android:orientation="horizontal">
<RadioButton <RadioButton
@ -46,13 +47,11 @@
android:text="@string/dia" android:text="@string/dia"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText <info.nightscout.utils.NumberPicker
android:id="@+id/localprofile_dia" android:id="@+id/localprofile_dia"
android:layout_width="70dp" android:layout_width="130dp"
android:layout_height="wrap_content" android:layout_height="40dp"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal" />
android:inputType="numberDecimal"
android:textAlignment="center" />
<LinearLayout <LinearLayout
android:id="@+id/localprofile_ic" android:id="@+id/localprofile_ic"

View file

@ -8,28 +8,28 @@
android:textAppearance="?android:attr/textAppearanceSmall"> android:textAppearance="?android:attr/textAppearanceSmall">
<Spinner <Spinner
android:id="@+id/timelistedit_time"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/timelistedit_time" android:layout_gravity="center"
android:layout_marginTop="10dp" /> android:layout_marginRight="10dp" />
<EditText <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<info.nightscout.utils.NumberPicker
android:id="@+id/timelistedit_edit1" android:id="@+id/timelistedit_edit1"
android:layout_width="70dp" android:layout_width="130dp"
android:layout_height="wrap_content" android:layout_height="40dp"
android:layout_marginLeft="15dp" android:layout_marginRight="5dp" />
android:inputType="numberDecimal"
android:layout_gravity="center_horizontal"
android:textAlignment="center" />
<EditText <info.nightscout.utils.NumberPicker
android:id="@+id/timelistedit_edit2" android:id="@+id/timelistedit_edit2"
android:layout_width="70dp" android:layout_width="130dp"
android:layout_height="wrap_content" android:layout_height="40dp" />
android:layout_marginLeft="15dp" </LinearLayout>
android:inputType="numberDecimal"
android:layout_gravity="center_horizontal"
android:textAlignment="center" />
<ImageView <ImageView
android:id="@+id/timelistedit_add" android:id="@+id/timelistedit_add"