TimeListEdit most work done

This commit is contained in:
Milos Kozak 2016-12-31 13:11:32 +01:00
parent e1c2e4c492
commit 2e6c168adc
6 changed files with 200 additions and 43 deletions

View file

@ -130,4 +130,8 @@ dependencies {
compile 'com.google.android.gms:play-services-wearable:7.5.0' compile 'com.google.android.gms:play-services-wearable:7.5.0'
compile 'junit:junit:4.12' compile 'junit:junit:4.12'
testCompile 'org.json:json:20140107' 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"
} }

View file

@ -11,13 +11,17 @@ 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.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton; import android.widget.RadioButton;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.json.JSONArray;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventInitializationChanged; 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.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.SafeParse; import info.nightscout.utils.SafeParse;
import info.nightscout.utils.TimeListEdit;
public class SimpleProfileFragment extends Fragment implements FragmentBase { public class SimpleProfileFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class); private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class);
@ -46,6 +52,8 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase {
EditText targetlowView; EditText targetlowView;
EditText targethighView; EditText targethighView;
Button profileswitchButton; Button profileswitchButton;
TimeListEdit test;
JSONArray data = new JSONArray();
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 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); targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow);
targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh);
profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); 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); onStatusEvent(null);

View file

@ -1,17 +1,31 @@
package info.nightscout.utils; package info.nightscout.utils;
import android.content.Context; import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; 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; import info.nightscout.androidaps.R;
/** /**
@ -19,60 +33,181 @@ import info.nightscout.androidaps.R;
*/ */
public class TimeListEdit { public class TimeListEdit {
private static Logger log = LoggerFactory.getLogger(TimeListEdit.class);
LinearLayout layout; LinearLayout layout;
Context context; Context context;
View view; View view;
int resLayoutId; int resLayoutId;
String label;
JSONArray data; JSONArray data;
boolean per30min = false; NumberFormat formatter;
String array1; String array1;
String array2; 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.context = context;
this.view = view; this.view = view;
this.resLayoutId = resLayoutId; this.resLayoutId = resLayoutId;
this.label = label;
this.data = data; this.data = data;
this.array1 = array1; this.array1 = array1;
this.array2 = array2; this.array2 = array2;
this.per30min = per30min; this.formatter = formatter;
buildView(); buildView();
} }
private void buildView() { private void buildView() {
/*
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
layout = (LinearLayout) view.findViewById(resLayoutId); 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); 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); ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add);
; ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove);
} else {
final int j = i; //needs to be final to be passed to inner class.
copyprevbutton.setOnClickListener(new View.OnClickListener() { final int fixedPos = i;
addbutton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { 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() { public int itemsCount() {
@ -151,4 +286,12 @@ public class TimeListEdit {
public void removeItem(int index) { public void removeItem(int index) {
data.remove(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) : ""));
}
}
} }

View file

@ -28,16 +28,12 @@
android:id="@+id/simpleprofile_mgdl" android:id="@+id/simpleprofile_mgdl"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="@string/mgdl" /> android:text="@string/mgdl" />
<RadioButton <RadioButton
android:id="@+id/simpleprofile_mmol" android:id="@+id/simpleprofile_mmol"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:text="@string/mmol" /> android:text="@string/mmol" />
</LinearLayout> </LinearLayout>
@ -45,7 +41,6 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dia" android:text="@string/dia"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
@ -53,7 +48,6 @@
android:id="@+id/simpleprofile_dia" android:id="@+id/simpleprofile_dia"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<TextView <TextView
@ -66,7 +60,6 @@
android:id="@+id/simpleprofile_ic" android:id="@+id/simpleprofile_ic"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<TextView <TextView
@ -79,7 +72,6 @@
android:id="@+id/simpleprofile_isf" android:id="@+id/simpleprofile_isf"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<TextView <TextView
@ -92,7 +84,6 @@
android:id="@+id/simpleprofile_car" android:id="@+id/simpleprofile_car"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<TextView <TextView
@ -105,7 +96,6 @@
android:id="@+id/simpleprofile_basalrate" android:id="@+id/simpleprofile_basalrate"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<TextView <TextView
@ -123,16 +113,12 @@
android:id="@+id/simpleprofile_targetlow" android:id="@+id/simpleprofile_targetlow"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<EditText <EditText
android:id="@+id/simpleprofile_targethigh" android:id="@+id/simpleprofile_targethigh"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
</LinearLayout> </LinearLayout>
@ -152,6 +138,13 @@
android:paddingRight="10dp" android:paddingRight="10dp"
android:text="@string/send_to_pump" android:text="@string/send_to_pump"
android:textColor="@color/colorProfileSwitchButton" /> 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> </LinearLayout>
</ScrollView> </ScrollView>

View file

@ -2,7 +2,6 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/cardColorBackground"
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:orientation="horizontal" android:orientation="horizontal"
android:paddingTop="5dp" android:paddingTop="5dp"
@ -34,7 +33,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp" android:layout_marginLeft="15dp"
tools:src="@drawable/add" /> android:src="@drawable/add" />
<ImageView <ImageView
android:id="@+id/timelistedit_remove" android:id="@+id/timelistedit_remove"
@ -42,5 +41,5 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp" android:layout_marginLeft="15dp"
tools:src="@drawable/remove" /> android:src="@drawable/remove" />
</LinearLayout> </LinearLayout>

View file

@ -1,21 +1,29 @@
package info.nightscout.utils; package info.nightscout.utils;
import android.content.Context;
import android.view.LayoutInflater;
import org.json.JSONArray; import org.json.JSONArray;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.text.DecimalFormat;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
/** /**
* Created by mike on 30.12.2016. * Created by mike on 30.12.2016.
*/ */
public class TimeListEditTest { public class TimeListEditTest {
/*
JSONArray data = new JSONArray(); JSONArray data = new JSONArray();
JSONArray data2 = new JSONArray(); JSONArray data2 = new JSONArray();
TimeListEdit tle = new TimeListEdit(null, null, 0, data, "ic", null, false); TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00"));
TimeListEdit tle2 = new TimeListEdit(null, null, 0, data2, "ic", "ic2", false); TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00"));
@Test @Test
public void doArrayTest() throws Exception { public void doArrayTest() throws Exception {
@ -41,4 +49,5 @@ public class TimeListEditTest {
assertEquals(2d, tle2.value2(0), 0.00001d); assertEquals(2d, tle2.value2(0), 0.00001d);
} }
*/
} }