fixed TimeListEdit when time is localized, larger add/remove buttons

This commit is contained in:
Milos Kozak 2019-08-22 20:33:18 +02:00
parent 0bdaa17082
commit d414b9b651
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,44 +97,39 @@ 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) {
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);
addButtons[position].setOnClickListener(view -> {
int seconds = secondFromMidnight(position);
addItem(position, seconds, 0, 0);
// for here for the rest of values
for (int i = fixedPos + 1; i < itemsCount(); i++) {
for (int i = position + 1; i < itemsCount(); i++) {
if (secondFromMidnight(i - 1) >= secondFromMidnight(i)) {
editItem(i, secondFromMidnight(i - 1) + ONEHOURINSECONDS, value1(i), value2(i));
}
@ -143,25 +139,21 @@ public class TimeListEdit {
callSave();
log();
fillView();
}
});
removeButtons[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeItem(fixedPos);
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,10 +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));
editItem(position, secondFromMidnight(position), SafeParse.stringToDouble(numberPickers1[position].getText()), value2(position));
callSave();
log();
}
@ -193,10 +185,10 @@ public class TimeListEdit {
});
numberPickers2[i].setTextWatcher(new TextWatcher() {
numberPickers2[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), value1(position), SafeParse.stringToDouble(numberPickers2[position].getText()));
callSave();
log();
}
@ -212,7 +204,7 @@ public class TimeListEdit {
}
});
layout.addView(childview);
layout.addView(childView);
}
private void fillView() {
@ -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,30 +254,39 @@ 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);
spinner.setSelection(posInList, false);
adapter.notifyDataSetChanged();
}
}, 100);
}
private int itemsCount() {
return data1.length();
@ -389,12 +389,10 @@ 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) : ""));
}
}
}
private void callSave() {
if (save != null) save.run();

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" />