Deletion of time spans is working

This commit is contained in:
GaidamakUA 2015-12-29 17:11:55 +02:00
parent 8cfbf26bc2
commit 072e23cd2d
5 changed files with 88 additions and 76 deletions

View file

@ -1,6 +1,6 @@
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'application' apply plugin: 'application'
mainClassName = "net.osmand.util.GeoPointParserUtil" mainClassName = "net.osmand.util.OpeningHoursParser"
sourceSets { sourceSets {
main { main {

View file

@ -531,14 +531,9 @@ public class Algorithms {
if (minutes < 60) { if (minutes < 60) {
return String.valueOf(minutes); return String.valueOf(minutes);
} else { } else {
String min; int min = minutes % 60;
if (minutes % 60 < 10) {
min = "0" + (minutes % 60);
} else {
min = (minutes % 60) + "";
}
int hours = minutes / 60; int hours = minutes / 60;
return hours + ":" + min; return String.format("%02d:%02d", hours, min);
} }
} }

View file

@ -222,14 +222,14 @@ public class OpeningHoursParser {
/** /**
* represents the list on which month it is open. * represents the list on which month it is open.
* Day number 0 is JANUAR. * Day number 0 is JANUARY.
*/ */
private boolean[] months = new boolean[12]; private boolean[] months = new boolean[12];
/** /**
* lists of equal size representing the start and end times * lists of equal size representing the start and end times
*/ */
private int[] startTimes = new int[0], endTimes = new int[0]; private TIntArrayList startTimes = new TIntArrayList(), endTimes = new TIntArrayList();
/** /**
* return an array representing the days of the rule * return an array representing the days of the rule
@ -252,20 +252,20 @@ public class OpeningHoursParser {
* @param s startTime to set * @param s startTime to set
*/ */
public void setStartTime(int s) { public void setStartTime(int s) {
startTimes = new int[]{s}; setSingleValueForArrayList(startTimes, s);
if(endTimes.length != 1) { if(endTimes.size() != 1) {
endTimes = new int[]{0}; setSingleValueForArrayList(endTimes, 0);
} }
} }
/** /**
* set a single end time, erase all previously added end times * set a single end time, erase all previously added end times
* @param e endTime to set * @param e endTime to set
*/ */
public void setEndTime(int e) { public void setEndTime(int e) {
endTimes = new int[]{e}; setSingleValueForArrayList(endTimes, e);
if(startTimes.length != 1) { if(startTimes.size() != 1) {
startTimes = new int[]{0}; setSingleValueForArrayList(startTimes, 0);
} }
} }
@ -279,14 +279,11 @@ public class OpeningHoursParser {
* @param position - position to add * @param position - position to add
*/ */
public void setStartTime(int s, int position) { public void setStartTime(int s, int position) {
if (position > startTimes.length) { if (position == startTimes.size()) {
throw new IllegalArgumentException("It is possible to only create 1 new " + startTimes.add(s);
"position. Size=" + startTimes.length + ", position=" + position); endTimes.add(0);
} else if (position == startTimes.length) {
startTimes = addValueToArray(startTimes, s);
endTimes = addValueToArray(endTimes, 0);
} else { } else {
startTimes[position] = s; startTimes.set(position, s);
} }
} }
@ -300,14 +297,11 @@ public class OpeningHoursParser {
* @param position - position to add * @param position - position to add
*/ */
public void setEndTime(int s, int position) { public void setEndTime(int s, int position) {
if (position > endTimes.length) { if (position == startTimes.size()) {
throw new IllegalArgumentException("It is possible to create only 1 new position." + endTimes.add(s);
" Size=" + endTimes.length + ", position=" + position); startTimes.add(0);
} else if (position == endTimes.length) {
endTimes = addValueToArray(endTimes, s);
startTimes = addValueToArray(startTimes, 0);
} else { } else {
endTimes[position] = s; endTimes.set(position, s);
} }
} }
@ -316,10 +310,10 @@ public class OpeningHoursParser {
* @return a single start time * @return a single start time
*/ */
public int getStartTime() { public int getStartTime() {
if(startTimes.length == 0) { if(startTimes.size() == 0) {
return 0; return 0;
} }
return startTimes[0]; return startTimes.get(0);
} }
/** /**
@ -328,7 +322,7 @@ public class OpeningHoursParser {
* @return a single start time * @return a single start time
*/ */
public int getStartTime(int position) { public int getStartTime(int position) {
return startTimes[position]; return startTimes.get(position);
} }
/** /**
@ -336,10 +330,10 @@ public class OpeningHoursParser {
* @return a single end time * @return a single end time
*/ */
public int getEndTime() { public int getEndTime() {
if(endTimes.length == 0) { if(endTimes.size() == 0) {
return 0; return 0;
} }
return endTimes[0]; return endTimes.get(0);
} }
/** /**
@ -348,7 +342,7 @@ public class OpeningHoursParser {
* @return a single end time * @return a single end time
*/ */
public int getEndTime(int position) { public int getEndTime(int position) {
return endTimes[position]; return endTimes.get(position);
} }
/** /**
@ -428,9 +422,9 @@ public class OpeningHoursParser {
p += 7; p += 7;
} }
int time = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE); // Time in minutes int time = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE); // Time in minutes
for (i = 0; i < startTimes.length; i++) { for (i = 0; i < startTimes.size(); i++) {
int startTime = this.startTimes[i]; int startTime = this.startTimes.get(i);
int endTime = this.endTimes[i]; int endTime = this.endTimes.get(i);
if (startTime < endTime || endTime == -1) { if (startTime < endTime || endTime == -1) {
// one day working like 10:00-20:00 (not 20:00-04:00) // one day working like 10:00-20:00 (not 20:00-04:00)
if (days[d] && !checkPrevious) { if (days[d] && !checkPrevious) {
@ -492,12 +486,12 @@ public class OpeningHoursParser {
} }
appendDaysString(b); appendDaysString(b);
// Time // Time
if (startTimes == null || startTimes.length == 0){ if (startTimes == null || startTimes.size() == 0){
b.append(" off "); b.append(" off ");
} else { } else {
for (int i = 0; i<startTimes.length; i++) { for (int i = 0; i<startTimes.size(); i++) {
int startTime = startTimes[i]; int startTime = startTimes.get(i);
int endTime = endTimes[i]; int endTime = endTimes.get(i);
if (open24_7 && startTime == 0 && endTime / 60 == 24) { if (open24_7 && startTime == 0 && endTime / 60 == 24) {
return "24/7"; return "24/7";
} }
@ -549,25 +543,20 @@ public class OpeningHoursParser {
* @param endTime endTime to add * @param endTime endTime to add
*/ */
public void addTimeRange(int startTime, int endTime) { public void addTimeRange(int startTime, int endTime) {
int l = startTimes.length; startTimes.add(startTime);
int[] newStartTimes = new int[l + 1]; endTimes.add(endTime);
int[] newEndTimes = new int[l + 1];
for (int i = 0; i < l; i++) {
newStartTimes[i] = startTimes[i];
newEndTimes[i] = endTimes[i];
}
newStartTimes[l] = startTime;
newEndTimes[l] = endTime;
startTimes = newStartTimes;
endTimes = newEndTimes;
} }
private int[] addValueToArray(int[] src, int newValue) { public void deleteTimeRange(int position) {
final int[] dest = new int[startTimes.length + 1]; startTimes.removeAt(position);
System.arraycopy(startTimes, 0, dest, 0, startTimes.length); endTimes.removeAt(position);
dest[startTimes.length] = newValue; }
return dest;
private static void setSingleValueForArrayList(TIntArrayList arrayList, int s) {
if (arrayList.size() > 0) {
arrayList.remove(0, arrayList.size());
}
arrayList.add(s);
} }
} }

View file

@ -6,9 +6,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical" android:gravity="center_vertical">
tools:showIn="@layout/open_time_list_item"
android:layout_marginTop="16dp">
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -55,6 +53,15 @@
android:layout_height="1dp" android:layout_height="1dp"
android:background="?android:textColorSecondary"/> android:background="?android:textColorSecondary"/>
</LinearLayout> </LinearLayout>
<ImageButton
android:id="@+id/deleteTimespanImageButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="top"
android:src="@drawable/ic_action_remove_dark"/>
</LinearLayout> </LinearLayout>

View file

@ -250,6 +250,8 @@ public class BasicEditPoiFragment extends Fragment
TextView daysTextView = (TextView) view.findViewById(R.id.daysTextView); TextView daysTextView = (TextView) view.findViewById(R.id.daysTextView);
LinearLayout timeListContainer = (LinearLayout) view.findViewById(R.id.timeListContainer); LinearLayout timeListContainer = (LinearLayout) view.findViewById(R.id.timeListContainer);
ImageButton deleteItemImageButton = (ImageButton) view.findViewById(R.id.deleteItemImageButton);
if (openingHours.getRules().get(position) instanceof BasicOpeningHourRule) { if (openingHours.getRules().get(position) instanceof BasicOpeningHourRule) {
final OpeningHoursParser.BasicOpeningHourRule rule = final OpeningHoursParser.BasicOpeningHourRule rule =
(BasicOpeningHourRule) openingHours.getRules().get(position); (BasicOpeningHourRule) openingHours.getRules().get(position);
@ -266,8 +268,8 @@ public class BasicEditPoiFragment extends Fragment
} }
}); });
TIntArrayList startTimes = rule.getStartTimes(); final TIntArrayList startTimes = rule.getStartTimes();
TIntArrayList endTimes = rule.getEndTimes(); final TIntArrayList endTimes = rule.getEndTimes();
for (int i = 0; i < startTimes.size(); i++) { for (int i = 0; i < startTimes.size(); i++) {
View timeFromToLayout = LayoutInflater.from(linearLayout.getContext()) View timeFromToLayout = LayoutInflater.from(linearLayout.getContext())
.inflate(R.layout.time_from_to_layout, timeListContainer, false); .inflate(R.layout.time_from_to_layout, timeListContainer, false);
@ -297,22 +299,41 @@ public class BasicEditPoiFragment extends Fragment
.show(getChildFragmentManager(), "OpeningHoursHoursDialogFragment"); .show(getChildFragmentManager(), "OpeningHoursHoursDialogFragment");
} }
}); });
ImageButton deleteTimespanImageButton = (ImageButton) timeFromToLayout
.findViewById(R.id.deleteTimespanImageButton);
deleteTimespanImageButton.setImageDrawable(deleteDrawable);
final int timespanPosition = i;
deleteTimespanImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (startTimes.size() == 1) {
openingHours.getRules().remove(position);
updateViews();
} else {
rule.deleteTimeRange(timespanPosition);
updateViews();
}
}
});
timeListContainer.addView(timeFromToLayout); timeListContainer.addView(timeFromToLayout);
} }
deleteItemImageButton.setVisibility(View.GONE);
} else if (openingHours.getRules().get(position) instanceof OpeningHoursParser.UnparseableRule) { } else if (openingHours.getRules().get(position) instanceof OpeningHoursParser.UnparseableRule) {
daysTextView.setText(openingHours.getRules().get(position).toRuleString(false)); daysTextView.setText(openingHours.getRules().get(position).toRuleString(false));
timeListContainer.removeAllViews(); timeListContainer.removeAllViews();
}
ImageButton deleteItemImageButton = (ImageButton) view.findViewById(R.id.deleteItemImageButton); deleteItemImageButton.setVisibility(View.VISIBLE);
deleteItemImageButton.setImageDrawable(deleteDrawable); deleteItemImageButton.setImageDrawable(deleteDrawable);
deleteItemImageButton.setOnClickListener(new View.OnClickListener() { deleteItemImageButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
openingHours.getRules().remove(position); openingHours.getRules().remove(position);
updateViews(); updateViews();
} }
}); });
}
return view; return view;
} }
} }