refactor TimeListEdit

This commit is contained in:
Milos Kozak 2017-10-04 15:46:23 +02:00
parent 8cecac0a7f
commit 8bf5933c88

View file

@ -41,17 +41,20 @@ public class TimeListEdit {
final int ONEHOURINSECONDS = 60 * 60;
LinearLayout layout;
private LinearLayout layout;
private View[] intervals = new View[24];
private Spinner[] spinners = new Spinner[24];
Context context;
View view;
int resLayoutId;
String label;
JSONArray data1;
JSONArray data2;
double step;
NumberFormat formatter;
Runnable save;
private Context context;
private View view;
private int resLayoutId;
private String label;
private JSONArray data1;
private JSONArray data2;
private double step;
private NumberFormat formatter;
private Runnable save;
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, double step, NumberFormat formatter, Runnable save) {
this.context = context;
@ -68,7 +71,6 @@ public class TimeListEdit {
private void buildView() {
log.debug("buildView start");
LayoutInflater inflater = LayoutInflater.from(context);
layout = (LinearLayout) view.findViewById(resLayoutId);
layout.removeAllViews();
@ -84,10 +86,36 @@ public class TimeListEdit {
layout.addView(textlabel);
for (int i = 0; i < itemsCount(); 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);
View childview = inflater.inflate(R.layout.timelistedit_element, layout, false);
//childview.setId(View.generateViewId());
final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time);
//timeSpinner.setId(View.generateViewId());
final Spinner timeSpinner = spinners[i] = (Spinner) childview.findViewById(R.id.timelistedit_time);
int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1);
int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1);
if (i == 0) next = ONEHOURINSECONDS;
@ -146,9 +174,7 @@ public class TimeListEdit {
ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add);
//addbutton.setId(View.generateViewId());
ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove);
//removebutton.setId(View.generateViewId());
if (itemsCount() == 1 || i == 0) {
removebutton.setVisibility(View.INVISIBLE);
@ -191,55 +217,33 @@ public class TimeListEdit {
}
});
addSpinnerListener(timeSpinner, i);
return childview;
}
private void addSpinnerListener(final Spinner timeSpinner, final int fixedPos) {
timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
log.debug("eeeeee");
layout.removeAllViews();
int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString());
editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos));
log();
callSave();
buildView();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
log.debug("fffff");
layout.removeAllViews();
editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos));
log();
callSave();
buildView();
}
}
);
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");
}
public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) {
private void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) {
int posInList = 0;
ArrayList<CharSequence> timeList = new ArrayList<>();
int pos = 0;
@ -255,11 +259,11 @@ public class TimeListEdit {
spinner.setSelection(posInList, false);
}
public int itemsCount() {
private int itemsCount() {
return data1.length();
}
public int secondFromMidnight(int index) {
private int secondFromMidnight(int index) {
try {
JSONObject item = (JSONObject) data1.get(index);
if (item.has("timeAsSeconds")) {
@ -277,7 +281,7 @@ public class TimeListEdit {
return 0;
}
public double value1(int index) {
private double value1(int index) {
try {
JSONObject item = (JSONObject) data1.get(index);
if (item.has("value")) {
@ -289,7 +293,7 @@ public class TimeListEdit {
return 0d;
}
public double value2(int index) {
private double value2(int index) {
if (data2 != null) {
try {
JSONObject item = (JSONObject) data2.get(index);
@ -303,7 +307,7 @@ public class TimeListEdit {
return 0d;
}
public void editItem(int index, int timeAsSeconds, double value1, double value2) {
private void editItem(int index, int timeAsSeconds, double value1, double value2) {
try {
String time;
int hour = timeAsSeconds / 60 / 60;
@ -328,7 +332,7 @@ public class TimeListEdit {
}
public void addItem(int index, int timeAsSeconds, double value1, double value2) {
private void addItem(int index, int timeAsSeconds, double value1, double value2) {
try {
// shift data
for (int i = data1.length(); i > index; i--) {
@ -344,19 +348,19 @@ public class TimeListEdit {
}
public void removeItem(int index) {
private void removeItem(int index) {
data1.remove(index);
if (data2 != null)
data2.remove(index);
}
void log() {
private void log() {
for (int i = 0; i < data1.length(); i++) {
log.debug(i + ": @" + DateUtil.timeStringFromSeconds(secondFromMidnight(i)) + " " + value1(i) + (data2 != null ? " " + value2(i) : ""));
}
}
void callSave() {
private void callSave() {
if (save != null) save.run();
}
}