diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index 3ef6a54652..14d0abab79 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -41,6 +41,7 @@ public class Amenity extends MapObject { public static final String OSM_DELETE_TAG = "osmand_change"; public static final String IMAGE_TITLE = "image_title"; public static final String IS_PART = "is_part"; + public static final String IS_PARENT_OF = "is_parent_of"; public static final String IS_AGGR_PART = "is_aggr_part"; public static final String CONTENT_JSON = "content_json"; public static final String ROUTE_ID = "route_id"; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java index 960608a784..1018874d26 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java @@ -104,7 +104,7 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { WikivoyageSearchResult articleItem = listAdapter.getArticleItem(groupPosition, childPosition); - sendResults(articleItem.getArticleId()); + sendResults(articleItem.getArticleTitle()); dismiss(); return true; } @@ -113,10 +113,10 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { WikivoyageSearchResult articleItem = (WikivoyageSearchResult) listAdapter.getGroup(groupPosition); - if (Algorithms.isEmpty(articleItem.getArticleRouteId())) { + if (Algorithms.isEmpty(articleItem.getArticleTitle())) { Toast.makeText(getContext(), R.string.wiki_article_not_found, Toast.LENGTH_LONG).show(); } else { - sendResults(articleItem.getArticleId()); + sendResults(articleItem.getArticleTitle()); dismiss(); } return true; @@ -150,14 +150,14 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr return nightMode ? R.color.wikivoyage_bottom_bar_bg_dark : R.color.list_background_color_light; } - private void sendResults(TravelArticleIdentifier articleId) { - WikivoyageArticleDialogFragment.showInstance(getMyApplication(), getFragmentManager(), articleId, selectedLang); + private void sendResults(String title) { + WikivoyageArticleDialogFragment.showInstanceByTitle(getMyApplication(), getFragmentManager(), title, selectedLang); } public static boolean showInstance(@NonNull FragmentManager fm, - @Nullable Fragment targetFragment, - @NonNull TravelArticleIdentifier articleId, - @NonNull String selectedLang) { + @Nullable Fragment targetFragment, + @NonNull TravelArticleIdentifier articleId, + @NonNull String selectedLang) { try { Bundle args = new Bundle(); args.putParcelable(ARTICLE_ID_KEY, articleId); @@ -239,7 +239,7 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr boolean isLastChild, View convertView, ViewGroup parent) { WikivoyageSearchResult articleItem = getArticleItem(groupPosition, childPosition); String childTitle = articleItem.getArticleTitle(); - boolean selected = articleItem.getArticleId().equals(articleId) || parentsList.contains(childTitle); + boolean selected = childTitle.equals(article.getTitle()) || parentsList.contains(childTitle); if (convertView == null) { convertView = LayoutInflater.from(context) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java index 0989a7c2e1..cf38b7265f 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java @@ -31,6 +31,7 @@ public class TravelArticle { String title; String content; String isPartOf; + String isParentOf = ""; double lat = Double.NaN; double lon = Double.NaN; String imageTitle; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index 983663f3d1..6a3e156e5f 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -172,6 +172,7 @@ public class TravelObfHelper implements TravelHelper { res.title = Algorithms.isEmpty(title) ? amenity.getName() : title; res.content = amenity.getDescription(lang); res.isPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_PART, lang)); + res.isParentOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_PARENT_OF, lang)); res.lat = amenity.getLocation().getLatitude(); res.lon = amenity.getLocation().getLongitude(); res.imageTitle = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IMAGE_TITLE, null)); @@ -400,64 +401,29 @@ public class TravelObfHelper implements TravelHelper { Map> navMap = new HashMap<>(); Set headers = new LinkedHashSet<>(); Map headerObjs = new HashMap<>(); - Map> amenityMap = new HashMap<>(); - for (BinaryMapIndexReader reader : getReaders()) { - try { - SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, - Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, getSearchFilter(false), new ResultMatcher() { - - @Override - public boolean publish(Amenity amenity) { - String isPartOf = amenity.getTagContent(Amenity.IS_PART, lang); - if (Algorithms.stringsEqual(title, isPartOf)) { - return true; - } else if (parts != null && parts.length > 0) { - String title = amenity.getName(lang); - title = Algorithms.isEmpty(title) ? amenity.getName() : title; - for (int i = 0; i < parts.length; i++) { - String part = parts[i]; - if (i == 0 && Algorithms.stringsEqual(part, title) || Algorithms.stringsEqual(part, isPartOf)) { - return true; - } - } - } - return false; - } - - @Override - public boolean isCancelled() { - return false; - } - }); - List amenities = reader.searchPoi(req); - if (!Algorithms.isEmpty(amenities)) { - amenityMap.put(reader.getFile(), amenities); - } - } catch (Exception e) { - LOG.error(e.getMessage(), e); - } - } if (parts != null && parts.length > 0) { headers.addAll(Arrays.asList(parts)); - headers.add(title); } - if (!Algorithms.isEmpty(amenityMap)) { - for (Entry> entry : amenityMap.entrySet()) { - File file = entry.getKey(); - for (Amenity amenity : entry.getValue()) { - Set nameLangs = getLanguages(amenity); - if (nameLangs.contains(lang)) { - TravelArticle a = readArticle(file, amenity, lang, false); - WikivoyageSearchResult rs = new WikivoyageSearchResult(a, new ArrayList<>(nameLangs)); - List l = navMap.get(rs.isPartOf); - if (l == null) { - l = new ArrayList<>(); - navMap.put(rs.isPartOf, l); - } - l.add(rs); - if (headers.contains(a.getTitle())) { - headerObjs.put(a.getTitle(), rs); - } + + for (String header : headers) { + TravelArticle parentArticle = getParentArticleByTitle(header, lang); + if (parentArticle == null) { + continue; + } + navMap.put(header, new ArrayList()); + String[] isParentOf = parentArticle.isParentOf.split(";"); + for (String childTitle : isParentOf) { + if (!childTitle.isEmpty()) { + WikivoyageSearchResult searchResult = new WikivoyageSearchResult("", childTitle, null, + null, Collections.singletonList(lang)); + List resultList = navMap.get(header); + if (resultList == null) { + resultList = new ArrayList<>(); + navMap.put(header, resultList); + } + resultList.add(searchResult); + if (headers.contains(childTitle)) { + headerObjs.put(childTitle, searchResult); } } } @@ -482,6 +448,41 @@ public class TravelObfHelper implements TravelHelper { return res; } + private TravelArticle getParentArticleByTitle(final String title, final String lang) { + TravelArticle article = null; + final List amenities = new ArrayList<>(); + for (BinaryMapIndexReader reader : getReaders()) { + try { + SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( + 0, 0, title, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchFilter(false), + new ResultMatcher() { + boolean done = false; + + @Override + public boolean publish(Amenity amenity) { + if (Algorithms.stringsEqual(title, Algorithms.emptyIfNull(amenity.getName(lang)))) { + amenities.add(amenity); + done = true; + } + return false; + } + + @Override + public boolean isCancelled() { + return done; + } + }, null); + reader.searchPoiByName(req); + } catch (IOException e) { + LOG.error(e.getMessage()); + } + if (!Algorithms.isEmpty(amenities)) { + article = readArticle(reader.getFile(), amenities.get(0), lang, false); + } + } + return article; + } + @Override public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) { TravelArticle article = getCachedArticle(articleId, lang, true);