handle arrays separately

This commit is contained in:
Milos Kozak 2016-12-31 14:52:23 +01:00
parent 2e6c168adc
commit 0458ad65ce
3 changed files with 93 additions and 69 deletions

View file

@ -53,7 +53,8 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase {
EditText targethighView; EditText targethighView;
Button profileswitchButton; Button profileswitchButton;
TimeListEdit test; TimeListEdit test;
JSONArray data = new JSONArray(); JSONArray data1 = new JSONArray();
JSONArray data2 = new JSONArray();
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -69,7 +70,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")); test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data1, data2, new DecimalFormat("0.00"));
onStatusEvent(null); onStatusEvent(null);

View file

@ -35,25 +35,25 @@ import info.nightscout.androidaps.R;
public class TimeListEdit { public class TimeListEdit {
private static Logger log = LoggerFactory.getLogger(TimeListEdit.class); private static Logger log = LoggerFactory.getLogger(TimeListEdit.class);
final int ONEHOURINSECONDS = 60 * 60;
LinearLayout layout; LinearLayout layout;
Context context; Context context;
View view; View view;
int resLayoutId; int resLayoutId;
String label; String label;
JSONArray data; JSONArray data1;
JSONArray data2;
NumberFormat formatter; NumberFormat formatter;
String array1;
String array2;
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data, String array1, String array2, NumberFormat formatter) { public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter) {
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.data = data; this.data1 = data1;
this.array1 = array1; this.data2 = data2;
this.array2 = array2;
this.formatter = formatter; this.formatter = formatter;
buildView(); buildView();
} }
@ -75,16 +75,16 @@ 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);
final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time); final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time);
int previous = i == 0 ? -1 * 60 * 60 : secondFromMidnight(i - 1); int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1);
int next = i == itemsCount() - 1 ? 24 * 60 * 60 : secondFromMidnight(i + 1); int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1);
if (i == 0) next = 60 * 60; 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 EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1);
fillNumber(editText1, value1(i)); fillNumber(editText1, value1(i));
final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2)); final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2));
fillNumber(editText2, value2(i)); fillNumber(editText2, value2(i));
if (array2 == null) { if (data2 == null) {
editText2.setVisibility(View.GONE); editText2.setVisibility(View.GONE);
} }
@ -92,12 +92,28 @@ public class TimeListEdit {
ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add); ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add);
ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove); ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove);
if (itemsCount() == 1 && i == 0) {
removebutton.setVisibility(View.GONE);
}
if (itemsCount() >= 24) {
addbutton.setVisibility(View.GONE);
}
final int fixedPos = i; 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) {
addItem(fixedPos, 0, 0, 0); int seconds = secondFromMidnight(fixedPos);
addItem(fixedPos, seconds, 0, 0);
// for here for the rest of values
for (int i = fixedPos + 1; i < itemsCount(); i++) {
if (secondFromMidnight(i - 1) >= secondFromMidnight(i)) {
editItem(i, secondFromMidnight(i - 1) + ONEHOURINSECONDS, value1(i), value2(i));
}
}
while (itemsCount() > 24 || secondFromMidnight(itemsCount() - 1) > 23 * ONEHOURINSECONDS)
removeItem(itemsCount() - 1);
log(); log();
buildView(); buildView();
} }
@ -169,14 +185,14 @@ public class TimeListEdit {
layout.addView(childview); layout.addView(childview);
} }
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * 60 * 60)) { if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) {
ImageView imageView = new ImageView(context); ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.add); imageView.setImageResource(R.drawable.add);
layout.addView(imageView); layout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() { imageView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + 60 * 60 : 0, 0, 0); addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
log(); log();
buildView(); buildView();
} }
@ -190,7 +206,7 @@ public class TimeListEdit {
ArrayList<CharSequence> timeList = new ArrayList<>(); ArrayList<CharSequence> timeList = new ArrayList<>();
DateFormat df = new SimpleDateFormat("HH:mm"); DateFormat df = new SimpleDateFormat("HH:mm");
int pos = 0; int pos = 0;
for (int t = previous + 60 * 60; t < next; t += 60 * 60) { for (int t = previous + ONEHOURINSECONDS; t < next; t += ONEHOURINSECONDS) {
timeList.add(df.format(DateUtil.toDate(t))); timeList.add(df.format(DateUtil.toDate(t)));
if (secondsFromMidnight == t) posInList = pos; if (secondsFromMidnight == t) posInList = pos;
pos++; pos++;
@ -211,12 +227,12 @@ public class TimeListEdit {
} }
public int itemsCount() { public int itemsCount() {
return data.length(); return data1.length();
} }
public int secondFromMidnight(int index) { public int secondFromMidnight(int index) {
try { try {
JSONObject item = (JSONObject) data.get(index); JSONObject item = (JSONObject) data1.get(index);
if (item.has("timeAsSeconds")) { if (item.has("timeAsSeconds")) {
return item.getInt("timeAsSeconds"); return item.getInt("timeAsSeconds");
} }
@ -228,9 +244,9 @@ public class TimeListEdit {
public double value1(int index) { public double value1(int index) {
try { try {
JSONObject item = (JSONObject) data.get(index); JSONObject item = (JSONObject) data1.get(index);
if (item.has(array1)) { if (item.has("value")) {
return item.getDouble(array1); return item.getDouble("value");
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -240,9 +256,9 @@ public class TimeListEdit {
public double value2(int index) { public double value2(int index) {
try { try {
JSONObject item = (JSONObject) data.get(index); JSONObject item = (JSONObject) data2.get(index);
if (item.has(array2)) { if (item.has("value")) {
return item.getDouble(array2); return item.getDouble("value");
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -252,12 +268,16 @@ public class TimeListEdit {
public void editItem(int index, int timeAsSeconds, double value1, double value2) { public void editItem(int index, int timeAsSeconds, double value1, double value2) {
try { try {
JSONObject newObject = new JSONObject(); JSONObject newObject1 = new JSONObject();
newObject.put("timeAsSeconds", timeAsSeconds); newObject1.put("timeAsSeconds", timeAsSeconds);
newObject.put(array1, value1); newObject1.put("value", value1);
if (array2 != null) data1.put(index, newObject1);
newObject.put(array2, value2); if (data2 != null) {
data.put(index, newObject); JSONObject newObject2 = new JSONObject();
newObject2.put("timeAsSeconds", timeAsSeconds);
newObject2.put("value", value2);
data2.put(index, newObject2);
}
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -267,16 +287,13 @@ public class TimeListEdit {
public void addItem(int index, int timeAsSeconds, double value1, double value2) { public void addItem(int index, int timeAsSeconds, double value1, double value2) {
try { try {
// shift data // shift data
for (int i = data.length(); i > index; i--) { for (int i = data1.length(); i > index; i--) {
data.put(i, data.get(i - 1)); data1.put(i, data1.get(i - 1));
if (data2 != null)
data2.put(i, data2.get(i - 1));
} }
// add new object // add new object
JSONObject newObject = new JSONObject(); editItem(index, timeAsSeconds, value1, value2);
newObject.put("timeAsSeconds", timeAsSeconds);
newObject.put(array1, value1);
if (array2 != null)
newObject.put(array2, value2);
data.put(index, newObject);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -284,14 +301,16 @@ public class TimeListEdit {
} }
public void removeItem(int index) { public void removeItem(int index) {
data.remove(index); data1.remove(index);
if (data2 != null)
data2.remove(index);
} }
void log() { void log() {
DateFormat df = new SimpleDateFormat("HH:mm"); DateFormat df = new SimpleDateFormat("HH:mm");
for (int i = 0; i < data.length(); i++) { for (int i = 0; i < data1.length(); i++) {
int pos = 0; int pos = 0;
log.debug(i + ": " + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + array1 + ": " + value1(i) + (array2 != null ? " " + array2 + ": " + value2(i) : "")); log.debug(i + ": @" + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + value1(i) + (data2 != null ? " " + value2(i) : ""));
} }
} }
} }

View file

@ -18,36 +18,40 @@ import static org.mockito.Mockito.when;
*/ */
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, "Test1", data, "ic", null, new DecimalFormat("0.00")); 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")); TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00"));
@Test
public void doArrayTest() throws Exception {
tle.addItem(0, 0, 0.1, 0);
tle.addItem(1, 60 * 60, 0.2, 0);
assertEquals(2, tle.itemsCount());
tle.editItem(0, 2 * 60 * 60, 1, 0);
assertEquals(2, tle.itemsCount());
assertEquals(1d, tle.value1(0), 0.00001d);
assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0));
tle.removeItem(0);
assertEquals(0.2d, tle.value1(0), 0.00001d);
assertEquals(0, tle.value2(0), 0.00001d);
assertEquals(60 * 60, tle.secondFromMidnight(0));
//System.out.print(tle2.toString());
assertEquals(0, tle2.itemsCount());
tle2.addItem(0, 0, 1, 2);
assertEquals(1, tle2.itemsCount());
assertEquals(0, tle2.secondFromMidnight(0));
assertEquals(1d, tle2.value1(0), 0.00001d);
assertEquals(2d, tle2.value2(0), 0.00001d);
}
*/
@Test @Test
public void doArrayTest() throws Exception { public void fakeTest() throws Exception {
tle.addItem(0, 0, 0.1, 0);
tle.addItem(1, 60 * 60, 0.2, 0);
assertEquals(2, tle.itemsCount());
tle.editItem(0, 2 * 60 * 60, 1, 0);
assertEquals(2, tle.itemsCount());
assertEquals(1d, tle.value1(0), 0.00001d);
assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0));
tle.removeItem(0);
assertEquals(0.2d, tle.value1(0), 0.00001d);
assertEquals(0, tle.value2(0), 0.00001d);
assertEquals(60 * 60, tle.secondFromMidnight(0));
//System.out.print(tle2.toString());
assertEquals(0, tle2.itemsCount());
tle2.addItem(0, 0, 1, 2);
assertEquals(1, tle2.itemsCount());
assertEquals(0, tle2.secondFromMidnight(0));
assertEquals(1d, tle2.value1(0), 0.00001d);
assertEquals(2d, tle2.value2(0), 0.00001d);
} }
*/
} }