From 072e23cd2da6c2008012ca5861e76fef0cc2fdeb Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Tue, 29 Dec 2015 17:11:55 +0200 Subject: [PATCH] Deletion of time spans is working --- OsmAnd-java/build.gradle | 2 +- .../src/net/osmand/util/Algorithms.java | 9 +- .../net/osmand/util/OpeningHoursParser.java | 95 ++++++++----------- OsmAnd/res/layout/time_from_to_layout.xml | 13 ++- .../plus/osmedit/BasicEditPoiFragment.java | 45 ++++++--- 5 files changed, 88 insertions(+), 76 deletions(-) diff --git a/OsmAnd-java/build.gradle b/OsmAnd-java/build.gradle index 0deaef3eea..7df1fbdd62 100644 --- a/OsmAnd-java/build.gradle +++ b/OsmAnd-java/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'java' apply plugin: 'application' -mainClassName = "net.osmand.util.GeoPointParserUtil" +mainClassName = "net.osmand.util.OpeningHoursParser" sourceSets { main { diff --git a/OsmAnd-java/src/net/osmand/util/Algorithms.java b/OsmAnd-java/src/net/osmand/util/Algorithms.java index 3a716d50a2..2dfc8e3500 100644 --- a/OsmAnd-java/src/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/net/osmand/util/Algorithms.java @@ -531,14 +531,9 @@ public class Algorithms { if (minutes < 60) { return String.valueOf(minutes); } else { - String min; - if (minutes % 60 < 10) { - min = "0" + (minutes % 60); - } else { - min = (minutes % 60) + ""; - } + int min = minutes % 60; int hours = minutes / 60; - return hours + ":" + min; + return String.format("%02d:%02d", hours, min); } } diff --git a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java index a38833b9fd..a4027dc901 100644 --- a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java +++ b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java @@ -222,14 +222,14 @@ public class OpeningHoursParser { /** * 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]; /** * 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 @@ -252,20 +252,20 @@ public class OpeningHoursParser { * @param s startTime to set */ public void setStartTime(int s) { - startTimes = new int[]{s}; - if(endTimes.length != 1) { - endTimes = new int[]{0}; + setSingleValueForArrayList(startTimes, s); + if(endTimes.size() != 1) { + setSingleValueForArrayList(endTimes, 0); } } - + /** * set a single end time, erase all previously added end times * @param e endTime to set */ public void setEndTime(int e) { - endTimes = new int[]{e}; - if(startTimes.length != 1) { - startTimes = new int[]{0}; + setSingleValueForArrayList(endTimes, e); + if(startTimes.size() != 1) { + setSingleValueForArrayList(startTimes, 0); } } @@ -279,14 +279,11 @@ public class OpeningHoursParser { * @param position - position to add */ public void setStartTime(int s, int position) { - if (position > startTimes.length) { - throw new IllegalArgumentException("It is possible to only create 1 new " + - "position. Size=" + startTimes.length + ", position=" + position); - } else if (position == startTimes.length) { - startTimes = addValueToArray(startTimes, s); - endTimes = addValueToArray(endTimes, 0); + if (position == startTimes.size()) { + startTimes.add(s); + endTimes.add(0); } else { - startTimes[position] = s; + startTimes.set(position, s); } } @@ -300,14 +297,11 @@ public class OpeningHoursParser { * @param position - position to add */ public void setEndTime(int s, int position) { - if (position > endTimes.length) { - throw new IllegalArgumentException("It is possible to create only 1 new position." + - " Size=" + endTimes.length + ", position=" + position); - } else if (position == endTimes.length) { - endTimes = addValueToArray(endTimes, s); - startTimes = addValueToArray(startTimes, 0); + if (position == startTimes.size()) { + endTimes.add(s); + startTimes.add(0); } else { - endTimes[position] = s; + endTimes.set(position, s); } } @@ -316,10 +310,10 @@ public class OpeningHoursParser { * @return a single start time */ public int getStartTime() { - if(startTimes.length == 0) { + if(startTimes.size() == 0) { return 0; } - return startTimes[0]; + return startTimes.get(0); } /** @@ -328,7 +322,7 @@ public class OpeningHoursParser { * @return a single start time */ public int getStartTime(int position) { - return startTimes[position]; + return startTimes.get(position); } /** @@ -336,10 +330,10 @@ public class OpeningHoursParser { * @return a single end time */ public int getEndTime() { - if(endTimes.length == 0) { + if(endTimes.size() == 0) { return 0; } - return endTimes[0]; + return endTimes.get(0); } /** @@ -348,7 +342,7 @@ public class OpeningHoursParser { * @return a single end time */ public int getEndTime(int position) { - return endTimes[position]; + return endTimes.get(position); } /** @@ -428,9 +422,9 @@ public class OpeningHoursParser { p += 7; } int time = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE); // Time in minutes - for (i = 0; i < startTimes.length; i++) { - int startTime = this.startTimes[i]; - int endTime = this.endTimes[i]; + for (i = 0; i < startTimes.size(); i++) { + int startTime = this.startTimes.get(i); + int endTime = this.endTimes.get(i); if (startTime < endTime || endTime == -1) { // one day working like 10:00-20:00 (not 20:00-04:00) if (days[d] && !checkPrevious) { @@ -492,12 +486,12 @@ public class OpeningHoursParser { } appendDaysString(b); // Time - if (startTimes == null || startTimes.length == 0){ + if (startTimes == null || startTimes.size() == 0){ b.append(" off "); } else { - for (int i = 0; i 0) { + arrayList.remove(0, arrayList.size()); + } + arrayList.add(s); } } diff --git a/OsmAnd/res/layout/time_from_to_layout.xml b/OsmAnd/res/layout/time_from_to_layout.xml index 9f3770f942..587397cc55 100644 --- a/OsmAnd/res/layout/time_from_to_layout.xml +++ b/OsmAnd/res/layout/time_from_to_layout.xml @@ -6,9 +6,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:gravity="center_vertical" - tools:showIn="@layout/open_time_list_item" - android:layout_marginTop="16dp"> + android:gravity="center_vertical"> + + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmedit/BasicEditPoiFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/BasicEditPoiFragment.java index 3885faf7e4..e6a2b52e80 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/BasicEditPoiFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/BasicEditPoiFragment.java @@ -250,6 +250,8 @@ public class BasicEditPoiFragment extends Fragment TextView daysTextView = (TextView) view.findViewById(R.id.daysTextView); LinearLayout timeListContainer = (LinearLayout) view.findViewById(R.id.timeListContainer); + ImageButton deleteItemImageButton = (ImageButton) view.findViewById(R.id.deleteItemImageButton); + if (openingHours.getRules().get(position) instanceof BasicOpeningHourRule) { final OpeningHoursParser.BasicOpeningHourRule rule = (BasicOpeningHourRule) openingHours.getRules().get(position); @@ -266,8 +268,8 @@ public class BasicEditPoiFragment extends Fragment } }); - TIntArrayList startTimes = rule.getStartTimes(); - TIntArrayList endTimes = rule.getEndTimes(); + final TIntArrayList startTimes = rule.getStartTimes(); + final TIntArrayList endTimes = rule.getEndTimes(); for (int i = 0; i < startTimes.size(); i++) { View timeFromToLayout = LayoutInflater.from(linearLayout.getContext()) .inflate(R.layout.time_from_to_layout, timeListContainer, false); @@ -297,22 +299,41 @@ public class BasicEditPoiFragment extends Fragment .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); } + + deleteItemImageButton.setVisibility(View.GONE); } else if (openingHours.getRules().get(position) instanceof OpeningHoursParser.UnparseableRule) { daysTextView.setText(openingHours.getRules().get(position).toRuleString(false)); timeListContainer.removeAllViews(); - } - ImageButton deleteItemImageButton = (ImageButton) view.findViewById(R.id.deleteItemImageButton); - deleteItemImageButton.setImageDrawable(deleteDrawable); - deleteItemImageButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - openingHours.getRules().remove(position); - updateViews(); - } - }); + deleteItemImageButton.setVisibility(View.VISIBLE); + deleteItemImageButton.setImageDrawable(deleteDrawable); + deleteItemImageButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openingHours.getRules().remove(position); + updateViews(); + } + }); + } return view; } }