This commit is contained in:
Milos Kozak 2019-08-23 21:48:07 +02:00
commit 2c712bd1c0
2 changed files with 94 additions and 96 deletions

View file

@ -1,9 +1,6 @@
package info.nightscout.androidaps.utils; package info.nightscout.androidaps.utils;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import androidx.core.content.ContextCompat;
import androidx.core.widget.TextViewCompat;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.Gravity; import android.view.Gravity;
@ -15,6 +12,10 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.widget.TextViewCompat;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -24,6 +25,7 @@ import org.slf4j.LoggerFactory;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
@ -77,8 +79,7 @@ public class TimeListEdit {
} }
private void buildView() { private void buildView() {
layout = (LinearLayout) view.findViewById(resLayoutId); layout = view.findViewById(resLayoutId);
layout.removeAllViews();
textlabel = new TextView(context); textlabel = new TextView(context);
textlabel.setText(label); textlabel.setText(label);
@ -96,72 +97,63 @@ public class TimeListEdit {
} }
// last "plus" to append new interval // last "plus" to append new interval
float factor = layout.getContext().getResources().getDisplayMetrics().density;
finalAdd = new ImageView(context); finalAdd = new ImageView(context);
finalAdd.setImageResource(R.drawable.add); finalAdd.setImageResource(R.drawable.add);
LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams((int) (35d * factor), (int) (35 * factor));
illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom); illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom);
illp.gravity = Gravity.CENTER; illp.gravity = Gravity.CENTER;
layout.addView(finalAdd); layout.addView(finalAdd);
finalAdd.setLayoutParams(illp); finalAdd.setLayoutParams(illp);
finalAdd.setOnClickListener(new View.OnClickListener() { finalAdd.setOnClickListener(view -> {
@Override addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
public void onClick(View view) { callSave();
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0); log();
callSave(); fillView();
log();
fillView();
}
}); });
fillView(); fillView();
} }
private void inflateRow(int i) { private void inflateRow(final int position) {
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
View childview = intervals[i] = inflater.inflate(R.layout.timelistedit_element, layout, false); View childView = intervals[position] = inflater.inflate(R.layout.timelistedit_element, layout, false);
spinners[i] = new SpinnerHelper(childview.findViewById(R.id.timelistedit_time)); spinners[position] = new SpinnerHelper(childView.findViewById(R.id.timelistedit_time));
numberPickers1[i] = (NumberPicker) childview.findViewById(R.id.timelistedit_edit1); numberPickers1[position] = childView.findViewById(R.id.timelistedit_edit1);
numberPickers2[i] = (NumberPicker) childview.findViewById(R.id.timelistedit_edit2); numberPickers2[position] = childView.findViewById(R.id.timelistedit_edit2);
addButtons[i] = (ImageView) childview.findViewById(R.id.timelistedit_add); addButtons[position] = childView.findViewById(R.id.timelistedit_add);
removeButtons[i] = (ImageView) childview.findViewById(R.id.timelistedit_remove); removeButtons[position] = childView.findViewById(R.id.timelistedit_remove);
final int fixedPos = i; addButtons[position].setOnClickListener(view -> {
addButtons[i].setOnClickListener(new View.OnClickListener() { int seconds = secondFromMidnight(position);
@Override addItem(position, seconds, 0, 0);
public void onClick(View view) { // for here for the rest of values
int seconds = secondFromMidnight(fixedPos); for (int i = position + 1; i < itemsCount(); i++) {
addItem(fixedPos, seconds, 0, 0); if (secondFromMidnight(i - 1) >= secondFromMidnight(i)) {
// for here for the rest of values editItem(i, secondFromMidnight(i - 1) + ONEHOURINSECONDS, value1(i), value2(i));
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);
callSave();
log();
fillView();
} }
while (itemsCount() > 24 || secondFromMidnight(itemsCount() - 1) > 23 * ONEHOURINSECONDS)
removeItem(itemsCount() - 1);
callSave();
log();
fillView();
}); });
removeButtons[i].setOnClickListener(new View.OnClickListener() { removeButtons[position].setOnClickListener(view -> {
@Override removeItem(position);
public void onClick(View view) { callSave();
removeItem(fixedPos); log();
callSave(); fillView();
log();
fillView();
}
}); });
spinners[i].setOnItemSelectedListener( spinners[position].setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() { new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int selected, long id) {
int seconds = DateUtil.toSeconds(spinners[fixedPos].getSelectedItem().toString()); int seconds = ((SpinnerAdapter) spinners[position].getAdapter()).valueForPosition(selected);
editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos)); editItem(position, seconds, value1(position), value2(position));
log(); log();
callSave(); callSave();
fillView(); fillView();
@ -173,30 +165,10 @@ public class TimeListEdit {
} }
); );
numberPickers1[i].setTextWatcher(new TextWatcher() { numberPickers1[position].setTextWatcher(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(numberPickers1[fixedPos].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) {
}
});
numberPickers2[i].setTextWatcher(new TextWatcher() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(numberPickers2[fixedPos].getText())); editItem(position, secondFromMidnight(position), SafeParse.stringToDouble(numberPickers1[position].getText()), value2(position));
callSave(); callSave();
log(); log();
} }
@ -212,7 +184,27 @@ public class TimeListEdit {
} }
}); });
layout.addView(childview);
numberPickers2[position].setTextWatcher(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
editItem(position, secondFromMidnight(position), value1(position), SafeParse.stringToDouble(numberPickers2[position].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) {
}
});
layout.addView(childView);
} }
private void fillView() { private void fillView() {
@ -220,7 +212,7 @@ public class TimeListEdit {
if (i < itemsCount()) { if (i < itemsCount()) {
intervals[i].setVisibility(View.VISIBLE); intervals[i].setVisibility(View.VISIBLE);
buildInterval(i); buildInterval(i);
} else if (i <= inflatedUntil){ } else if (i <= inflatedUntil) {
intervals[i].setVisibility(View.GONE); intervals[i].setVisibility(View.GONE);
} }
} }
@ -232,9 +224,8 @@ public class TimeListEdit {
} }
} }
private View buildInterval(int i) { private void buildInterval(int i) {
SpinnerHelper timeSpinner = spinners[i]; SpinnerHelper timeSpinner = spinners[i];
View childview = intervals[i];
final NumberPicker editText1 = numberPickers1[i]; final NumberPicker editText1 = numberPickers1[i];
final NumberPicker editText2 = numberPickers2[i]; final NumberPicker editText2 = numberPickers2[i];
@ -263,29 +254,38 @@ public class TimeListEdit {
addButtons[i].setVisibility(View.VISIBLE); addButtons[i].setVisibility(View.VISIBLE);
} }
return childview; }
class SpinnerAdapter extends ArrayAdapter<CharSequence> {
List<Integer> values;
SpinnerAdapter(@NonNull Context context, int resource, final @NonNull List<CharSequence> objects, final @NonNull List<Integer> values) {
super(context, resource, objects);
this.values = values;
}
int valueForPosition(int position) {
return values.get(position);
}
} }
private void fillSpinner(final SpinnerHelper spinner, int secondsFromMidnight, int previous, int next) { private void fillSpinner(final SpinnerHelper spinner, int secondsFromMidnight, int previous, int next) {
int posInList = 0; int posInList = 0;
ArrayList<CharSequence> timeList = new ArrayList<>(); ArrayList<CharSequence> timeList = new ArrayList<>();
ArrayList<Integer> timeListValues = 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.timeStringFromSeconds(t)); timeList.add(DateUtil.timeStringFromSeconds(t));
timeListValues.add(t);
if (secondsFromMidnight == t) posInList = pos; if (secondsFromMidnight == t) posInList = pos;
pos++; pos++;
} }
final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context, final SpinnerAdapter adapter = new SpinnerAdapter(context,
R.layout.spinner_centered, timeList); R.layout.spinner_centered, timeList, timeListValues);
spinner.setAdapter(adapter); spinner.setAdapter(adapter);
final int finalPosInList = posInList; spinner.setSelection(posInList, false);
new Handler().postDelayed(new Runnable() { adapter.notifyDataSetChanged();
public void run() {
spinner.setSelection(finalPosInList, false);
adapter.notifyDataSetChanged();
}
}, 100);
} }
private int itemsCount() { private int itemsCount() {
@ -362,7 +362,7 @@ public class TimeListEdit {
} }
private void addItem(int index, int timeAsSeconds, double value1, double value2) { private void addItem(int index, int timeAsSeconds, double value1, double value2) {
if(itemsCount()>inflatedUntil) { if (itemsCount() > inflatedUntil) {
layout.removeView(finalAdd); layout.removeView(finalAdd);
inflateRow(++inflatedUntil); inflateRow(++inflatedUntil);
layout.addView(finalAdd); layout.addView(finalAdd);
@ -389,10 +389,8 @@ public class TimeListEdit {
} }
private void log() { private void log() {
if (log.isDebugEnabled()) { for (int i = 0; i < data1.length(); i++) {
for (int i = 0; i < data1.length(); i++) { log.debug(i + ": @" + DateUtil.timeStringFromSeconds(secondFromMidnight(i)) + " " + value1(i) + (data2 != null ? " " + value2(i) : ""));
log.debug(i + ": @" + DateUtil.timeStringFromSeconds(secondFromMidnight(i)) + " " + value1(i) + (data2 != null ? " " + value2(i) : ""));
}
} }
} }
@ -400,9 +398,9 @@ public class TimeListEdit {
if (save != null) save.run(); if (save != null) save.run();
} }
public void updateLabel(String txt){ public void updateLabel(String txt) {
this.label = txt; this.label = txt;
if(textlabel!=null) if (textlabel != null)
textlabel.setText(txt); textlabel.setText(txt);
} }
} }

View file

@ -33,16 +33,16 @@
<ImageView <ImageView
android:id="@+id/timelistedit_add" android:id="@+id/timelistedit_add"
android:layout_width="wrap_content" android:layout_width="35dp"
android:layout_height="wrap_content" android:layout_height="35dp"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp" android:layout_marginLeft="15dp"
android:src="@drawable/add" /> android:src="@drawable/add" />
<ImageView <ImageView
android:id="@+id/timelistedit_remove" android:id="@+id/timelistedit_remove"
android:layout_width="wrap_content" android:layout_width="35dp"
android:layout_height="wrap_content" android:layout_height="35dp"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp" android:layout_marginLeft="15dp"
android:src="@drawable/remove" /> android:src="@drawable/remove" />