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

View file

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