numberpicker in calibration dialog

This commit is contained in:
Milos Kozak 2017-06-27 17:06:23 +02:00
parent 341b0660a8
commit ef4a1feda2
8 changed files with 318 additions and 55 deletions

View file

@ -9,7 +9,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import com.crashlytics.android.answers.Answers; import com.crashlytics.android.answers.Answers;
@ -25,16 +24,15 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.GlucoseStatus;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.NumberPicker;
import info.nightscout.utils.SafeParse; import info.nightscout.utils.SafeParse;
import info.nightscout.utils.XdripCalibrations; import info.nightscout.utils.XdripCalibrations;
public class CalibrationDialog extends DialogFragment implements View.OnClickListener { public class CalibrationDialog extends DialogFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(CalibrationDialog.class); private static Logger log = LoggerFactory.getLogger(CalibrationDialog.class);
PlusMinusEditText bgText; NumberPicker bgNumber;
TextView unitsView; TextView unitsView;
TextView bgView;
Context context; Context context;
@ -62,14 +60,15 @@ public class CalibrationDialog extends DialogFragment implements View.OnClickLis
String units = MainApp.getConfigBuilder().getProfileUnits(); String units = MainApp.getConfigBuilder().getProfileUnits();
Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, units); Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, units);
bgNumber = (NumberPicker) view.findViewById(R.id.overview_calibration_bg);
if (units.equals(Constants.MMOL)) if (units.equals(Constants.MMOL))
bgText = new PlusMinusEditText(view, R.id.overview_calibration_bg, R.id.overview_calibration_bg_plus, R.id.overview_calibration_bg_minus, bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); bgNumber.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
else else
bgText = new PlusMinusEditText(view, R.id.overview_calibration_bg, R.id.overview_calibration_bg_plus, R.id.overview_calibration_bg_minus, bg, 0d, 500d, 1d, new DecimalFormat("0"), false); bgNumber.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
unitsView = (TextView) view.findViewById(R.id.overview_calibration_units); unitsView = (TextView) view.findViewById(R.id.overview_calibration_units);
unitsView.setText(units); unitsView.setText(units);
bgView = (TextView) view.findViewById(R.id.overview_calibration_bg);
return view; return view;
} }
@ -78,8 +77,7 @@ public class CalibrationDialog extends DialogFragment implements View.OnClickLis
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.ok: case R.id.ok:
final Double bg = SafeParse.stringToDouble(this.bgView.getText().toString()); final Double bg = SafeParse.stringToDouble(bgNumber.getText().toString());
;
XdripCalibrations.confirmAndSendCalibration(bg, context); XdripCalibrations.confirmAndSendCalibration(bg, context);
dismiss(); dismiss();
Answers.getInstance().logCustom(new CustomEvent("Calibration")); Answers.getInstance().logCustom(new CustomEvent("Calibration"));

View file

@ -0,0 +1,234 @@
package info.nightscout.utils;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.NumberFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
/**
* Created by mike on 28.06.2016.
*/
public class NumberPicker extends LinearLayout implements View.OnKeyListener,
View.OnTouchListener, View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(NumberPicker.class);
TextView editText;
Button minusButton;
Button plusButton;
Double value;
Double minValue = 0d;
Double maxValue = 1d;
Double step = 1d;
NumberFormat formater;
boolean allowZero = false;
private Handler mHandler;
private ScheduledExecutorService mUpdater;
private class UpdateCounterTask implements Runnable {
private boolean mInc;
private int repeated = 0;
private int multiplier = 1;
private final int doubleLimit = 5;
public UpdateCounterTask(boolean inc) {
mInc = inc;
}
public void run() {
Message msg = new Message();
if (repeated % doubleLimit == 0) multiplier *= 2;
repeated++;
msg.arg1 = multiplier;
msg.arg2 = repeated;
if (mInc) {
msg.what = MSG_INC;
} else {
msg.what = MSG_DEC;
}
mHandler.sendMessage(msg);
}
}
private static final int MSG_INC = 0;
private static final int MSG_DEC = 1;
public NumberPicker(Context context) {
super(context, null);
}
public NumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
this.initialize(context, attrs);
}
public NumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initialize(Context context, AttributeSet attrs) {
// set layout view
LayoutInflater.from(context).inflate(R.layout.number_picker_layout, this, true);
// init ui components
this.minusButton = (Button) findViewById(R.id.decrement);
this.plusButton = (Button) findViewById(R.id.increment);
this.editText = (EditText) findViewById(R.id.display);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INC:
inc(msg.arg1);
return;
case MSG_DEC:
dec(msg.arg1);
return;
}
super.handleMessage(msg);
}
};
minusButton.setOnTouchListener(this);
minusButton.setOnKeyListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnTouchListener(this);
plusButton.setOnKeyListener(this);
plusButton.setOnClickListener(this);
}
public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero) {
this.value = initValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.step = step;
this.formater = formater;
this.allowZero = allowZero;
updateEditText();
}
public void setValue(Double value) {
this.value = value;
updateEditText();
}
public Double getValue() {
return value;
}
public String getText() {
return editText.getText().toString();
}
public void setStep(Double step) {
this.step = step;
}
private void inc(int multiplier) {
value += step * multiplier;
if (value > maxValue) {
value = maxValue;
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.youareonallowedlimit));
stopUpdating();
}
updateEditText();
}
private void dec( int multiplier) {
value -= step * multiplier;
if (value < minValue) {
value = minValue;
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.youareonallowedlimit));
stopUpdating();
}
updateEditText();
}
private void updateEditText() {
if (value == 0d && !allowZero)
editText.setText("");
else
editText.setText(formater.format(value));
}
private void startUpdating(boolean inc) {
if (mUpdater != null) {
log.debug("Another executor is still active");
return;
}
mUpdater = Executors.newSingleThreadScheduledExecutor();
mUpdater.scheduleAtFixedRate(new UpdateCounterTask(inc), 200, 200,
TimeUnit.MILLISECONDS);
}
private void stopUpdating() {
if (mUpdater != null) {
mUpdater.shutdownNow();
mUpdater = null;
}
}
@Override
public void onClick(View v) {
if (mUpdater == null) {
if (v == plusButton) {
inc(1);
} else {
dec(1);
}
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
&& event.getAction() != KeyEvent.ACTION_MULTIPLE;
if (isKeyOfInterest && isReleased) {
stopUpdating();
} else if (isKeyOfInterest && isPressed) {
startUpdating(v == plusButton);
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
if (isReleased) {
stopUpdating();
} else if (isPressed) {
startUpdating(v == plusButton);
}
return false;
}
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#505050"/>
<stroke android:width="1dp" android:color="@color/colorLightGray"/>
<corners android:radius="4dp" />
</shape>

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
<item android:bottom="0.5dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/colorLightGray" />
<solid android:color="@android:color/white" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="130dp"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@drawable/background_darkgray">
<Button
android:id="@+id/decrement"
android:layout_width="30dp"
android:layout_height="match_parent"
android:padding="0dp"
android:textColor="@color/mdtp_white"
android:background="@null"
android:textStyle="bold"
android:text="—"/>
<EditText
android:id="@+id/display"
android:layout_width="70dp"
android:background="@drawable/border_gray"
android:layout_height="match_parent"
android:text="1"
android:textColor="@android:color/black"
android:focusable="false"
android:inputType="number"
android:gravity="center"
android:imeOptions="actionDone"
/>
<Button
android:id="@+id/increment"
android:layout_width="30dp"
android:layout_height="match_parent"
android:padding="0dp"
android:textSize="25sp"
android:textColor="@color/mdtp_white"
android:background="@null"
android:text="+"/>
</LinearLayout>

View file

@ -29,60 +29,23 @@
android:textAppearance="?android:attr/textAppearanceLarge" /> android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView <TextView
android:id="@+id/overview_calibration_units"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:padding="10dp" android:layout_marginBottom="10dp"
android:id="@+id/overview_calibration_units"
android:textAppearance="?android:attr/textAppearanceSmall" /> android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout <info.nightscout.utils.NumberPicker
android:layout_width="wrap_content" android:id="@+id/overview_calibration_bg"
android:layout_height="wrap_content" android:layout_width="130dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginLeft="30dp" android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" android:layout_marginRight="30dp" />
android:orientation="horizontal">
<ImageView
android:id="@+id/overview_calibration_bg_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="@drawable/circle"
android:backgroundTint="#ffffff"
android:src="@drawable/ic_action_minus"
android:tint="#ffffff" />
<EditText
android:id="@+id/overview_calibration_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:inputType="numberDecimal"
android:minWidth="100dp"
android:padding="10dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/overview_calibration_bg_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="@drawable/circle"
android:backgroundTint="#ffffff"
android:src="@drawable/ic_action_add"
android:tint="#ffffff" />
</LinearLayout>
<include layout="@layout/mdtp_done_button" /> <include layout="@layout/mdtp_done_button" />
</LinearLayout> </LinearLayout>

View file

@ -16,6 +16,7 @@
<color name="tempbasal">#C803A9F4</color> <color name="tempbasal">#C803A9F4</color>
<color name="graphgrid">#757575</color> <color name="graphgrid">#757575</color>
<color name="updating">#50ffffff</color> <color name="updating">#50ffffff</color>
<color name="colorLightGray">#d8d8d8</color>
<color name="defaultbackground">#424242</color> <color name="defaultbackground">#424242</color>

View file

@ -483,7 +483,7 @@
<string name="copied_to_clipboard">Copied to clipboard</string> <string name="copied_to_clipboard">Copied to clipboard</string>
<string name="nav_show_logcat">Show log</string> <string name="nav_show_logcat">Show log</string>
<string name="overview_calibration">Calibration</string> <string name="overview_calibration">Calibration</string>
<string name="overview_calibration_bg_label">Calibration BG</string> <string name="overview_calibration_bg_label">Calibration</string>
<string name="send_calibration" formatted="false">Send calibration %.1f to xDrip?</string> <string name="send_calibration" formatted="false">Send calibration %.1f to xDrip?</string>
<string name="xdripnotinstalled">xDrip+ not installed</string> <string name="xdripnotinstalled">xDrip+ not installed</string>
<string name="calibrationsent">Calibration sent to xDrip</string> <string name="calibrationsent">Calibration sent to xDrip</string>