TimeListEdit most work done
This commit is contained in:
parent
e1c2e4c492
commit
2e6c168adc
6 changed files with 200 additions and 43 deletions
|
@ -130,4 +130,8 @@ dependencies {
|
|||
compile 'com.google.android.gms:play-services-wearable:7.5.0'
|
||||
compile 'junit:junit:4.12'
|
||||
testCompile 'org.json:json:20140107'
|
||||
testCompile 'org.mockito:mockito-core:2.+'
|
||||
androidTestCompile 'org.mockito:mockito-core:2.+'
|
||||
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
|
||||
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
|
||||
}
|
||||
|
|
|
@ -11,13 +11,17 @@ 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;
|
||||
|
||||
import org.json.JSONArray;
|
||||
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;
|
||||
|
@ -25,7 +29,9 @@ 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.Overview.events.EventNewNotification;
|
||||
import info.nightscout.utils.DecimalFormatter;
|
||||
import info.nightscout.utils.SafeParse;
|
||||
import info.nightscout.utils.TimeListEdit;
|
||||
|
||||
public class SimpleProfileFragment extends Fragment implements FragmentBase {
|
||||
private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class);
|
||||
|
@ -46,6 +52,8 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase {
|
|||
EditText targetlowView;
|
||||
EditText targethighView;
|
||||
Button profileswitchButton;
|
||||
TimeListEdit test;
|
||||
JSONArray data = new JSONArray();
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
|
@ -61,6 +69,7 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase {
|
|||
targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow);
|
||||
targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh);
|
||||
profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch);
|
||||
test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data, "ic1", null, new DecimalFormat("0.00"));
|
||||
|
||||
onStatusEvent(null);
|
||||
|
||||
|
|
|
@ -1,17 +1,31 @@
|
|||
package info.nightscout.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
|
||||
/**
|
||||
|
@ -19,60 +33,181 @@ import info.nightscout.androidaps.R;
|
|||
*/
|
||||
|
||||
public class TimeListEdit {
|
||||
private static Logger log = LoggerFactory.getLogger(TimeListEdit.class);
|
||||
|
||||
LinearLayout layout;
|
||||
|
||||
Context context;
|
||||
View view;
|
||||
int resLayoutId;
|
||||
String label;
|
||||
JSONArray data;
|
||||
boolean per30min = false;
|
||||
NumberFormat formatter;
|
||||
String array1;
|
||||
String array2;
|
||||
|
||||
public TimeListEdit(Context context, View view, int resLayoutId, JSONArray data, String array1, String array2, boolean per30min) {
|
||||
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data, String array1, String array2, NumberFormat formatter) {
|
||||
this.context = context;
|
||||
this.view = view;
|
||||
this.resLayoutId = resLayoutId;
|
||||
this.label = label;
|
||||
this.data = data;
|
||||
this.array1 = array1;
|
||||
this.array2 = array2;
|
||||
this.per30min = per30min;
|
||||
this.formatter = formatter;
|
||||
buildView();
|
||||
}
|
||||
|
||||
private void buildView() {
|
||||
/*
|
||||
LayoutInflater inflater = LayoutInflater.from(context);
|
||||
layout = (LinearLayout) view.findViewById(resLayoutId);
|
||||
|
||||
final EditText[] editTexts = new EditText[24];
|
||||
layout.removeAllViews();
|
||||
|
||||
for (int i = 0; i < 24; i++) {
|
||||
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);
|
||||
llp.setMargins(10, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
|
||||
textlabel.setLayoutParams(llp);
|
||||
layout.addView(textlabel);
|
||||
|
||||
for (int i = 0; i < itemsCount(); i++) {
|
||||
View childview = inflater.inflate(R.layout.timelistedit_element, layout, false);
|
||||
((TextView) childview.findViewById(R.id.basal_time_elem)).setText((i < 10 ? "0" : "") + i + ":00: ");
|
||||
final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time);
|
||||
int previous = i == 0 ? -1 * 60 * 60 : secondFromMidnight(i - 1);
|
||||
int next = i == itemsCount() - 1 ? 24 * 60 * 60 : secondFromMidnight(i + 1);
|
||||
if (i == 0) next = 60 * 60;
|
||||
fillSpinner(timeSpinner, secondFromMidnight(i), previous, next);
|
||||
|
||||
ImageView copyprevbutton = (ImageView) childview.findViewById(R.id.basal_copyprev_elem);
|
||||
final EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1);
|
||||
fillNumber(editText1, value1(i));
|
||||
final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2));
|
||||
fillNumber(editText2, value2(i));
|
||||
if (array2 == null) {
|
||||
editText2.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
if (i == 0) {
|
||||
copyprevbutton.setVisibility(View.INVISIBLE);
|
||||
;
|
||||
} else {
|
||||
final int j = i; //needs to be final to be passed to inner class.
|
||||
copyprevbutton.setOnClickListener(new View.OnClickListener() {
|
||||
|
||||
ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add);
|
||||
ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove);
|
||||
|
||||
|
||||
final int fixedPos = i;
|
||||
addbutton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
editTexts[j].setText(editTexts[j - 1].getText());
|
||||
addItem(fixedPos, 0, 0, 0);
|
||||
log();
|
||||
buildView();
|
||||
}
|
||||
});
|
||||
|
||||
removebutton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
removeItem(fixedPos);
|
||||
log();
|
||||
buildView();
|
||||
}
|
||||
});
|
||||
|
||||
timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString());
|
||||
editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos));
|
||||
log();
|
||||
buildView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos));
|
||||
log();
|
||||
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);
|
||||
}
|
||||
|
||||
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * 60 * 60)) {
|
||||
ImageView imageView = new ImageView(context);
|
||||
imageView.setImageResource(R.drawable.add);
|
||||
layout.addView(imageView);
|
||||
imageView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + 60 * 60 : 0, 0, 0);
|
||||
log();
|
||||
buildView();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
editTexts[i] = ((EditText) childview.findViewById(R.id.basal_edittext_elem));
|
||||
//editTexts[i].setText(DecimalFormatter.to2Decimal(values[i]));
|
||||
layout.addView(childview);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) {
|
||||
int posInList = 0;
|
||||
ArrayList<CharSequence> timeList = new ArrayList<>();
|
||||
DateFormat df = new SimpleDateFormat("HH:mm");
|
||||
int pos = 0;
|
||||
for (int t = previous + 60 * 60; t < next; t += 60 * 60) {
|
||||
timeList.add(df.format(DateUtil.toDate(t)));
|
||||
if (secondsFromMidnight == t) posInList = pos;
|
||||
pos++;
|
||||
}
|
||||
|
||||
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context,
|
||||
android.R.layout.simple_spinner_item, timeList);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinner.setAdapter(adapter);
|
||||
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() {
|
||||
|
@ -151,4 +286,12 @@ public class TimeListEdit {
|
|||
public void removeItem(int index) {
|
||||
data.remove(index);
|
||||
}
|
||||
|
||||
void log() {
|
||||
DateFormat df = new SimpleDateFormat("HH:mm");
|
||||
for (int i = 0; i < data.length(); i++) {
|
||||
int pos = 0;
|
||||
log.debug(i + ": " + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + array1 + ": " + value1(i) + (array2 != null ? " " + array2 + ": " + value2(i) : ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,16 +28,12 @@
|
|||
android:id="@+id/simpleprofile_mgdl"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/mgdl" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/simpleprofile_mmol"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="3"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/mmol" />
|
||||
|
||||
</LinearLayout>
|
||||
|
@ -45,7 +41,6 @@
|
|||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/dia"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium" />
|
||||
|
||||
|
@ -53,7 +48,6 @@
|
|||
android:id="@+id/simpleprofile_dia"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
<TextView
|
||||
|
@ -66,7 +60,6 @@
|
|||
android:id="@+id/simpleprofile_ic"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
<TextView
|
||||
|
@ -79,7 +72,6 @@
|
|||
android:id="@+id/simpleprofile_isf"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
<TextView
|
||||
|
@ -92,7 +84,6 @@
|
|||
android:id="@+id/simpleprofile_car"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
<TextView
|
||||
|
@ -105,7 +96,6 @@
|
|||
android:id="@+id/simpleprofile_basalrate"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
<TextView
|
||||
|
@ -123,16 +113,12 @@
|
|||
android:id="@+id/simpleprofile_targetlow"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:layout_weight="1"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/simpleprofile_targethigh"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_column="2"
|
||||
android:layout_weight="1"
|
||||
android:inputType="numberDecimal" />
|
||||
|
||||
</LinearLayout>
|
||||
|
@ -152,6 +138,13 @@
|
|||
android:paddingRight="10dp"
|
||||
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>
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@color/cardColorBackground"
|
||||
android:gravity="center_horizontal"
|
||||
android:orientation="horizontal"
|
||||
android:paddingTop="5dp"
|
||||
|
@ -34,7 +33,7 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_marginLeft="15dp"
|
||||
tools:src="@drawable/add" />
|
||||
android:src="@drawable/add" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/timelistedit_remove"
|
||||
|
@ -42,5 +41,5 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_marginLeft="15dp"
|
||||
tools:src="@drawable/remove" />
|
||||
android:src="@drawable/remove" />
|
||||
</LinearLayout>
|
||||
|
|
|
@ -1,21 +1,29 @@
|
|||
package info.nightscout.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Created by mike on 30.12.2016.
|
||||
*/
|
||||
public class TimeListEditTest {
|
||||
|
||||
/*
|
||||
JSONArray data = new JSONArray();
|
||||
JSONArray data2 = new JSONArray();
|
||||
TimeListEdit tle = new TimeListEdit(null, null, 0, data, "ic", null, false);
|
||||
TimeListEdit tle2 = new TimeListEdit(null, null, 0, data2, "ic", "ic2", false);
|
||||
TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00"));
|
||||
TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00"));
|
||||
|
||||
|
||||
@Test
|
||||
public void doArrayTest() throws Exception {
|
||||
|
@ -41,4 +49,5 @@ public class TimeListEditTest {
|
|||
assertEquals(2d, tle2.value2(0), 0.00001d);
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
Loading…
Reference in a new issue