Merge pull request #10630 from osmandapp/fix_travel_article_navigation

Fix travel guides navigation
This commit is contained in:
vshcherb 2021-01-21 11:14:20 +01:00 committed by GitHub
commit 0ba936962e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 64 deletions

View file

@ -41,6 +41,7 @@ public class Amenity extends MapObject {
public static final String OSM_DELETE_TAG = "osmand_change"; public static final String OSM_DELETE_TAG = "osmand_change";
public static final String IMAGE_TITLE = "image_title"; public static final String IMAGE_TITLE = "image_title";
public static final String IS_PART = "is_part"; 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 IS_AGGR_PART = "is_aggr_part";
public static final String CONTENT_JSON = "content_json"; public static final String CONTENT_JSON = "content_json";
public static final String ROUTE_ID = "route_id"; public static final String ROUTE_ID = "route_id";

View file

@ -104,7 +104,7 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr
public boolean onChildClick(ExpandableListView parent, View v, public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) { int groupPosition, int childPosition, long id) {
WikivoyageSearchResult articleItem = listAdapter.getArticleItem(groupPosition, childPosition); WikivoyageSearchResult articleItem = listAdapter.getArticleItem(groupPosition, childPosition);
sendResults(articleItem.getArticleId()); sendResults(articleItem.getArticleTitle());
dismiss(); dismiss();
return true; return true;
} }
@ -113,10 +113,10 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr
@Override @Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
WikivoyageSearchResult articleItem = (WikivoyageSearchResult) listAdapter.getGroup(groupPosition); 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(); Toast.makeText(getContext(), R.string.wiki_article_not_found, Toast.LENGTH_LONG).show();
} else { } else {
sendResults(articleItem.getArticleId()); sendResults(articleItem.getArticleTitle());
dismiss(); dismiss();
} }
return true; 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; return nightMode ? R.color.wikivoyage_bottom_bar_bg_dark : R.color.list_background_color_light;
} }
private void sendResults(TravelArticleIdentifier articleId) { private void sendResults(String title) {
WikivoyageArticleDialogFragment.showInstance(getMyApplication(), getFragmentManager(), articleId, selectedLang); WikivoyageArticleDialogFragment.showInstanceByTitle(getMyApplication(), getFragmentManager(), title, selectedLang);
} }
public static boolean showInstance(@NonNull FragmentManager fm, public static boolean showInstance(@NonNull FragmentManager fm,
@Nullable Fragment targetFragment, @Nullable Fragment targetFragment,
@NonNull TravelArticleIdentifier articleId, @NonNull TravelArticleIdentifier articleId,
@NonNull String selectedLang) { @NonNull String selectedLang) {
try { try {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelable(ARTICLE_ID_KEY, articleId); args.putParcelable(ARTICLE_ID_KEY, articleId);
@ -239,7 +239,7 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr
boolean isLastChild, View convertView, ViewGroup parent) { boolean isLastChild, View convertView, ViewGroup parent) {
WikivoyageSearchResult articleItem = getArticleItem(groupPosition, childPosition); WikivoyageSearchResult articleItem = getArticleItem(groupPosition, childPosition);
String childTitle = articleItem.getArticleTitle(); 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) { if (convertView == null) {
convertView = LayoutInflater.from(context) convertView = LayoutInflater.from(context)

View file

@ -31,6 +31,7 @@ public class TravelArticle {
String title; String title;
String content; String content;
String isPartOf; String isPartOf;
String isParentOf = "";
double lat = Double.NaN; double lat = Double.NaN;
double lon = Double.NaN; double lon = Double.NaN;
String imageTitle; String imageTitle;

View file

@ -172,6 +172,7 @@ public class TravelObfHelper implements TravelHelper {
res.title = Algorithms.isEmpty(title) ? amenity.getName() : title; res.title = Algorithms.isEmpty(title) ? amenity.getName() : title;
res.content = amenity.getDescription(lang); res.content = amenity.getDescription(lang);
res.isPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_PART, 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.lat = amenity.getLocation().getLatitude();
res.lon = amenity.getLocation().getLongitude(); res.lon = amenity.getLocation().getLongitude();
res.imageTitle = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IMAGE_TITLE, null)); res.imageTitle = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IMAGE_TITLE, null));
@ -401,64 +402,29 @@ public class TravelObfHelper implements TravelHelper {
Map<String, List<WikivoyageSearchResult>> navMap = new HashMap<>(); Map<String, List<WikivoyageSearchResult>> navMap = new HashMap<>();
Set<String> headers = new LinkedHashSet<>(); Set<String> headers = new LinkedHashSet<>();
Map<String, WikivoyageSearchResult> headerObjs = new HashMap<>(); Map<String, WikivoyageSearchResult> headerObjs = new HashMap<>();
Map<File, List<Amenity>> amenityMap = new HashMap<>();
for (BinaryMapIndexReader reader : getReaders()) {
try {
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(0,
Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, getSearchFilter(false), new ResultMatcher<Amenity>() {
@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<Amenity> 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) { if (parts != null && parts.length > 0) {
headers.addAll(Arrays.asList(parts)); headers.addAll(Arrays.asList(parts));
headers.add(title);
} }
if (!Algorithms.isEmpty(amenityMap)) {
for (Entry<File, List<Amenity>> entry : amenityMap.entrySet()) { for (String header : headers) {
File file = entry.getKey(); TravelArticle parentArticle = getParentArticleByTitle(header, lang);
for (Amenity amenity : entry.getValue()) { if (parentArticle == null) {
Set<String> nameLangs = getLanguages(amenity); continue;
if (nameLangs.contains(lang)) { }
TravelArticle a = readArticle(file, amenity, lang, false); navMap.put(header, new ArrayList<WikivoyageSearchResult>());
WikivoyageSearchResult rs = new WikivoyageSearchResult(a, new ArrayList<>(nameLangs)); String[] isParentOf = parentArticle.isParentOf.split(";");
List<WikivoyageSearchResult> l = navMap.get(rs.isPartOf); for (String childTitle : isParentOf) {
if (l == null) { if (!childTitle.isEmpty()) {
l = new ArrayList<>(); WikivoyageSearchResult searchResult = new WikivoyageSearchResult("", childTitle, null,
navMap.put(rs.isPartOf, l); null, Collections.singletonList(lang));
} List<WikivoyageSearchResult> resultList = navMap.get(header);
l.add(rs); if (resultList == null) {
if (headers.contains(a.getTitle())) { resultList = new ArrayList<>();
headerObjs.put(a.getTitle(), rs); navMap.put(header, resultList);
} }
resultList.add(searchResult);
if (headers.contains(childTitle)) {
headerObjs.put(childTitle, searchResult);
} }
} }
} }
@ -483,6 +449,41 @@ public class TravelObfHelper implements TravelHelper {
return res; return res;
} }
private TravelArticle getParentArticleByTitle(final String title, final String lang) {
TravelArticle article = null;
final List<Amenity> amenities = new ArrayList<>();
for (BinaryMapIndexReader reader : getReaders()) {
try {
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(
0, 0, title, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchFilter(false),
new ResultMatcher<Amenity>() {
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 @Override
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) { public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) {
TravelArticle article = getCachedArticle(articleId, lang, true); TravelArticle article = getCachedArticle(articleId, lang, true);