From a4fe6fa3c403dcc5155b780a1fb84ab88fd5aae4 Mon Sep 17 00:00:00 2001 From: 0xRe1nk0 <0xre1nk0@gmail.com> Date: Thu, 12 Dec 2024 10:24:26 +0200 Subject: [PATCH] refactor basic fragment tab --- .../edit_poi_add_opening_hours_item.xml | 24 ++ .../res/layout/edit_poi_basic_info_item.xml | 112 ++++++++ OsmAnd/res/layout/list_item_poi_tag.xml | 2 +- .../dialogs/NewBasicEditPoiFragment.java | 264 ++++++++++++++++++ .../OpeningHoursDaysDialogFragment.java | 2 +- .../OpeningHoursHoursDialogFragment.java | 4 +- 6 files changed, 404 insertions(+), 4 deletions(-) create mode 100644 OsmAnd/res/layout/edit_poi_add_opening_hours_item.xml create mode 100644 OsmAnd/res/layout/edit_poi_basic_info_item.xml create mode 100644 OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/NewBasicEditPoiFragment.java diff --git a/OsmAnd/res/layout/edit_poi_add_opening_hours_item.xml b/OsmAnd/res/layout/edit_poi_add_opening_hours_item.xml new file mode 100644 index 00000000000..30a5103451a --- /dev/null +++ b/OsmAnd/res/layout/edit_poi_add_opening_hours_item.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/edit_poi_basic_info_item.xml b/OsmAnd/res/layout/edit_poi_basic_info_item.xml new file mode 100644 index 00000000000..7f0097c30dd --- /dev/null +++ b/OsmAnd/res/layout/edit_poi_basic_info_item.xml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/list_item_poi_tag.xml b/OsmAnd/res/layout/list_item_poi_tag.xml index b5e5fc0118b..d509f4c5162 100644 --- a/OsmAnd/res/layout/list_item_poi_tag.xml +++ b/OsmAnd/res/layout/list_item_poi_tag.xml @@ -2,7 +2,7 @@ getContentList() { + List list = new ArrayList<>(); + EditPoiData data = getData(); + if (data == null) { + return list; + } + + list.add(TYPE_BASIC_INFO); + for (int i = 0; i < openingHoursAdapter.openingHours.getRules().size(); i++) { + list.add(new OpenHoursItem(i, System.currentTimeMillis())); + } + list.add(TYPE_ADD_OPENING_HOURS); + + return list; + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putSerializable(OPENING_HOURS, openingHoursAdapter.openingHours); + super.onSaveInstanceState(outState); + } + + public void setBasicOpeningHoursRule(OpeningHoursParser.BasicOpeningHourRule item, int position) { + if (item.getStartTime() == 0 && item.getEndTime() == 0 && item.isOpenedEveryDay()) { + item.setEndTime(24 * 60); + } + openingHoursAdapter.setOpeningHoursRule(item, position); + } + + public void removeUnsavedOpeningHours() { + EditPoiData data = getData(); + if (data != null) { + OpeningHoursParser.OpeningHours openingHours = OpeningHoursParser.parseOpenedHoursHandleErrors(data.getTagValues() + .get(OSMSettings.OSMTagKey.OPENING_HOURS.getValue())); + if (openingHours == null) { + openingHours = new OpeningHoursParser.OpeningHours(); + } + openingHoursAdapter.replaceOpeningHours(openingHours); + updateViews(); + openingHoursAdapter.updateHoursData(); + } + contentAdapter.notifyDataSetChanged(); + } + + private EditPoiData getData() { + return getEditPoiFragment().getEditPoiData(); + } + + @Override + public void onFragmentActivated() { + EditPoiData data = getData(); + if (data == null) { + return; + } + basicTagsInitialized = false; + + Map tagValues = data.getTagValues(); + OpeningHoursParser.OpeningHours openingHours = OpeningHoursParser.parseOpenedHoursHandleErrors(tagValues.get(OSMSettings.OSMTagKey.OPENING_HOURS.getValue())); + if (openingHours == null) { + openingHours = new OpeningHoursParser.OpeningHours(); + } + openingHoursAdapter.replaceOpeningHours(openingHours); + updateViews(); + openingHoursAdapter.updateHoursData(); + + basicTagsInitialized = true; + } + + public class OpeningHoursAdapter { + + private final OsmandApplication app; + private OpeningHoursParser.OpeningHours openingHours; + + private final EditPoiData data; + private final Drawable clockDrawable; + private final Drawable deleteDrawable; + + public OpeningHoursAdapter(@NonNull OsmandApplication app, + @NonNull OpeningHoursParser.OpeningHours openingHours, + @NonNull EditPoiData data, + @NonNull Drawable clockDrawable, + @NonNull Drawable deleteDrawable) { + this.app = app; + this.openingHours = openingHours; + this.data = data; + this.clockDrawable = clockDrawable; + this.deleteDrawable = deleteDrawable; + } + + public Drawable getClockDrawable() { + return clockDrawable; + } + + public Drawable getDeleteDrawable() { + return deleteDrawable; + } + + public OpeningHoursParser.OpeningHours getOpeningHours() { + return openingHours; + } + + public void setOpeningHoursRule(OpeningHoursParser.BasicOpeningHourRule rule, int position) { + if (position == -1) { + openingHours.addRule(rule); + } else { + openingHours.getRules().set(position, rule); + } + updateViews(); + updateHoursData(); + contentAdapter.notifyDataSetChanged(); + } + + public void replaceOpeningHours(OpeningHoursParser.OpeningHours openingHours) { + this.openingHours = openingHours; + } + + public void updateHoursData() { + if (!data.isInEdit()) { + LocaleHelper helper = app.getLocaleHelper(); + helper.updateTimeFormatting(false, Locale.getDefault()); + String openingHoursString = openingHours.toString(); + helper.updateTimeFormatting(); + + if (!TextUtils.isEmpty(openingHoursString)) { + if (openingHours.getOriginal() == null || + !OpeningHoursParser.parseOpenedHoursHandleErrors(openingHours.getOriginal()).toString().equals(openingHoursString)) { + data.putTag(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(), openingHoursString); + } + } else if (basicTagsInitialized && isResumed()) { + data.removeTag(OSMSettings.OSMTagKey.OPENING_HOURS.getValue()); + } + } + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursDaysDialogFragment.java b/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursDaysDialogFragment.java index 42515a99a21..b4c95701e90 100644 --- a/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursDaysDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursDaysDialogFragment.java @@ -74,7 +74,7 @@ public void onClick(DialogInterface dialog, int which) { OpeningHoursHoursDialogFragment.createInstance(item, positionToAdd, true, 0) .show(getFragmentManager(), "TimePickerDialogFragment"); } else { - ((BasicEditPoiFragment) getParentFragment()) + ((NewBasicEditPoiFragment) getParentFragment()) .setBasicOpeningHoursRule(item, positionToAdd); } } else { diff --git a/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursHoursDialogFragment.java b/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursHoursDialogFragment.java index ab4e6a6a719..8a0b8e5889a 100644 --- a/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursHoursDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/OpeningHoursHoursDialogFragment.java @@ -79,13 +79,13 @@ public void onClick(DialogInterface dialog, int which) { } else { item.setEndTime(time, timePosition); } - ((BasicEditPoiFragment) getParentFragment()) + ((NewBasicEditPoiFragment) getParentFragment()) .setBasicOpeningHoursRule(item, rulePosition); } } }) .setNegativeButton(R.string.shared_string_cancel, (dialog, which) -> { - BasicEditPoiFragment editPoiFragment = ((BasicEditPoiFragment) getParentFragment()); + NewBasicEditPoiFragment editPoiFragment = ((NewBasicEditPoiFragment) getParentFragment()); if (editPoiFragment != null) { editPoiFragment.removeUnsavedOpeningHours(); }