LocalProfile fix

This commit is contained in:
Milos Kozak 2017-10-04 23:17:19 +02:00
parent 8bf5933c88
commit 96edac5ddf
3 changed files with 168 additions and 135 deletions

View file

@ -125,6 +125,11 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
plusButton.setOnClickListener(this); plusButton.setOnClickListener(this);
} }
public void setTextWatcher(TextWatcher textWatcher) {
this.textWatcher = textWatcher;
editText.addTextChangedListener(textWatcher);
}
public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) { public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) {
setParams(initValue, minValue, maxValue, step, formater, allowZero); setParams(initValue, minValue, maxValue, step, formater, allowZero);
this.textWatcher = textWatcher; this.textWatcher = textWatcher;
@ -139,7 +144,11 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
this.formater = formater; this.formater = formater;
this.allowZero = allowZero; this.allowZero = allowZero;
if (textWatcher != null)
editText.removeTextChangedListener(textWatcher);
updateEditText(); updateEditText();
if (textWatcher != null)
editText.addTextChangedListener(textWatcher);
} }
public void setValue(Double value) { public void setValue(Double value) {

View file

@ -44,7 +44,11 @@ public class TimeListEdit {
private LinearLayout layout; private LinearLayout layout;
private View[] intervals = new View[24]; private View[] intervals = new View[24];
private Spinner[] spinners = new Spinner[24]; private Spinner[] spinners = new Spinner[24];
private NumberPicker[] numberPickers1 = new NumberPicker[24];
private NumberPicker[] numberPickers2 = new NumberPicker[24];
private ImageView[] addButtons = new ImageView[24];
private ImageView[] removeButtons = new ImageView[24];
ImageView finalAdd;
private Context context; private Context context;
private View view; private View view;
@ -70,11 +74,8 @@ public class TimeListEdit {
} }
private void buildView() { private void buildView() {
log.debug("buildView start");
layout = (LinearLayout) view.findViewById(resLayoutId); layout = (LinearLayout) view.findViewById(resLayoutId);
layout.removeAllViews();
TextView textlabel = new TextView(context); TextView textlabel = new TextView(context);
textlabel.setText(label); textlabel.setText(label);
textlabel.setGravity(Gravity.LEFT); textlabel.setGravity(Gravity.LEFT);
@ -85,110 +86,20 @@ public class TimeListEdit {
TextViewCompat.setTextAppearance(textlabel, android.R.style.TextAppearance_Medium); TextViewCompat.setTextAppearance(textlabel, android.R.style.TextAppearance_Medium);
layout.addView(textlabel); layout.addView(textlabel);
for (int i = 0; i < itemsCount(); i++) { for (int i = 0; i < 24; i++) {
View childview = intervals[i] = buildInterval(i);
layout.addView(childview);
}
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.add);
LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom);
illp.gravity = Gravity.CENTER;
layout.addView(imageView);
imageView.setLayoutParams(illp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
log.debug("gggggggg");
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
callSave();
log();
buildView();
}
});
}
log.debug("buildView end");
}
private View buildInterval(int i) {
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); View childview = intervals[i] = inflater.inflate(R.layout.timelistedit_element, layout, false);
final Spinner timeSpinner = spinners[i] = (Spinner) childview.findViewById(R.id.timelistedit_time); spinners[i] = (Spinner) childview.findViewById(R.id.timelistedit_time);
int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1); numberPickers1[i] = (NumberPicker) childview.findViewById(R.id.timelistedit_edit1);
int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1); numberPickers2[i] = (NumberPicker) childview.findViewById(R.id.timelistedit_edit2);
if (i == 0) next = ONEHOURINSECONDS; addButtons[i] = (ImageView) childview.findViewById(R.id.timelistedit_add);
fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); removeButtons[i] = (ImageView) childview.findViewById(R.id.timelistedit_remove);
final int fixedPos = i; final int fixedPos = i;
final NumberPicker editText1 = (NumberPicker) childview.findViewById(R.id.timelistedit_edit1); addButtons[i].setOnClickListener(new View.OnClickListener() {
TextWatcher tw1 = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
log.debug("aaaa");
editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.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) {
}
};
editText1.setParams(value1(i), 0.1d, 100d, step, formatter, false, tw1);
final NumberPicker editText2 = (NumberPicker) childview.findViewById(R.id.timelistedit_edit2);
TextWatcher tw2 = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
log.debug("bbbbb");
editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.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) {
}
};
editText2.setParams(value2(i), 0.1d, 100d, step, formatter, false, tw2);
if (data2 == null) {
editText2.setVisibility(View.GONE);
}
ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add);
ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove);
if (itemsCount() == 1 || i == 0) {
removebutton.setVisibility(View.INVISIBLE);
}
if (itemsCount() >= 24 || secondFromMidnight(i) >= 82800) {
addbutton.setVisibility(View.INVISIBLE);
}
addbutton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
log.debug("ccccc"); log.debug("ccccc");
layout.removeAllViews();
int seconds = secondFromMidnight(fixedPos); int seconds = secondFromMidnight(fixedPos);
addItem(fixedPos, seconds, 0, 0); addItem(fixedPos, seconds, 0, 0);
// for here for the rest of values // for here for the rest of values
@ -201,46 +112,155 @@ public class TimeListEdit {
removeItem(itemsCount() - 1); removeItem(itemsCount() - 1);
callSave(); callSave();
log(); log();
buildView(); fillView();
} }
}); });
removebutton.setOnClickListener(new View.OnClickListener() { removeButtons[i].setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
log.debug("dddd"); log.debug("dddd");
layout.removeAllViews();
removeItem(fixedPos); removeItem(fixedPos);
callSave(); callSave();
log(); log();
buildView(); fillView();
} }
}); });
addSpinnerListener(timeSpinner, i); spinners[i].setOnItemSelectedListener(
return childview; new AdapterView.OnItemSelectedListener() {
}
private void addSpinnerListener(final Spinner timeSpinner, final int fixedPos) {
timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
log.debug("eeeeee"); log.debug("eeeeee");
int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString()); int seconds = DateUtil.toSeconds(spinners[fixedPos].getSelectedItem().toString());
editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos)); editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos));
log(); log();
callSave(); callSave();
fillView();
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
log.debug("fffff");
editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos));
log();
callSave();
} }
} }
); );
numberPickers1[i].setTextWatcher(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
log.debug("aaaa");
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
public void afterTextChanged(Editable s) {
log.debug("bbbbb");
editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(numberPickers2[fixedPos].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);
}
// last "plus" to append new interval
finalAdd = new ImageView(context);
finalAdd.setImageResource(R.drawable.add);
LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
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) {
log.debug("gggggggg");
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
callSave();
log();
fillView();
}
});
fillView();
}
private void fillView() {
for (int i = 0; i < 24; i++) {
if (i < itemsCount()) {
intervals[i].setVisibility(View.VISIBLE);
buildInterval(i);
} else
intervals[i].setVisibility(View.GONE);
}
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) {
finalAdd.setVisibility(View.VISIBLE);
} else {
finalAdd.setVisibility(View.GONE);
}
}
private View buildInterval(int i) {
Spinner timeSpinner = spinners[i];
View childview = intervals[i];
final NumberPicker editText1 = numberPickers1[i];
final NumberPicker editText2 = numberPickers2[i];
int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1);
int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1);
if (i == 0) next = ONEHOURINSECONDS;
fillSpinner(timeSpinner, secondFromMidnight(i), previous, next);
final int fixedPos = i;
editText1.setParams(value1(i), 0.1d, 100d, step, formatter, false);
editText2.setParams(value2(i), 0.1d, 100d, step, formatter, false);
if (data2 == null) {
editText2.setVisibility(View.GONE);
}
if (itemsCount() == 1 || i == 0) {
removeButtons[i].setVisibility(View.INVISIBLE);
} else
removeButtons[i].setVisibility(View.VISIBLE);
if (itemsCount() >= 24 || secondFromMidnight(i) >= 82800) {
addButtons[i].setVisibility(View.INVISIBLE);
} else {
addButtons[i].setVisibility(View.VISIBLE);
}
return childview;
} }
private void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) { private void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) {
@ -256,7 +276,10 @@ public class TimeListEdit {
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context, ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context,
R.layout.spinner_centered, timeList); R.layout.spinner_centered, timeList);
spinner.setAdapter(adapter); spinner.setAdapter(adapter);
AdapterView.OnItemSelectedListener l = spinner.getOnItemSelectedListener();
spinner.setOnItemSelectedListener(null);
spinner.setSelection(posInList, false); spinner.setSelection(posInList, false);
spinner.setOnItemSelectedListener(l);
} }
private int itemsCount() { private int itemsCount() {

View file

@ -12,7 +12,8 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginRight="10dp" /> android:layout_marginRight="10dp"
android:minWidth="80dp" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"