From 43c6a37ae5fb98e61b25414a092c0a69b42bf467 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Tue, 17 Apr 2018 14:53:56 +0300 Subject: [PATCH] Fix wikivoyage navigation --- .../WikivoyageArticleDialogFragment.java | 18 +++++- .../WikivoyageArticleNavigationFragment.java | 61 ++++++++++++++----- .../plus/wikivoyage/data/TravelDbHelper.java | 33 +++++----- 3 files changed, 77 insertions(+), 35 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index ba5ca4082b..d811a5449e 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -222,6 +222,14 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen } else if (requestCode == WikivoyageShowPicturesDialogFragment.SHOW_PICTURES_CHANGED_REQUEST_CODE) { updateWebSettings(); populateArticle(); + } else if (requestCode == WikivoyageArticleNavigationFragment.OPEN_ARTICLE_REQUEST_CODE) { + long cityId = data.getLongExtra(WikivoyageArticleNavigationFragment.CITY_ID_KEY, -1); + String selectedLang = data.getStringExtra(WikivoyageArticleNavigationFragment.SELECTED_LANG_KEY); + if (cityId != -1 && !TextUtils.isEmpty(selectedLang)) { + this.cityId = cityId; + this.selectedLang = selectedLang; + populateArticle(); + } } } @@ -365,7 +373,10 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen sb.append(navBarString); } if (!TextUtils.isEmpty(current)) { - sb.append(" • ").append(current).append(""); + if (aggregatedPartOfArrayOrig.length > 1) { + sb.append(" • "); + } + sb.append("").append(current).append(""); } sb.append(""); } @@ -440,10 +451,11 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen @JavascriptInterface public void showNavigation() { FragmentManager fm = getFragmentManager(); - if (article == null || fm == null) { + if (article == null || fm == null || selectedLang == null) { return; } - WikivoyageArticleNavigationFragment.showInstance(fm, cityId, selectedLang); + WikivoyageArticleNavigationFragment.showInstance(fm, + WikivoyageArticleDialogFragment.this, cityId, selectedLang); } } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java index a4f71a0797..2d9b6d6410 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleNavigationFragment.java @@ -1,10 +1,12 @@ package net.osmand.plus.wikivoyage.article; import android.content.Context; +import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.text.TextUtils; @@ -33,8 +35,8 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr public static final String TAG = WikivoyageArticleNavigationFragment.class.getSimpleName(); - private static final String CITY_ID_KEY = "city_id_key"; - private static final String SELECTED_LANG_KEY = "selected_lang_key"; + public static final String CITY_ID_KEY = "city_id_key"; + public static final String SELECTED_LANG_KEY = "selected_lang_key"; public static final int OPEN_ARTICLE_REQUEST_CODE = 2; @@ -42,12 +44,18 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr private long cityId = UNDEFINED; private String selectedLang; + private TravelArticle article; private List parentsList; private ExpandableListView expListView; @Override public void createMenuItems(Bundle savedInstanceState) { + Context ctx = getContext(); + if (ctx == null) { + return; + } + if (savedInstanceState != null) { selectedLang = savedInstanceState.getString(SELECTED_LANG_KEY); cityId = savedInstanceState.getLong(CITY_ID_KEY); @@ -63,7 +71,7 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr return; } - TravelArticle article = getMyApplication().getTravelDbHelper().getArticle(cityId, selectedLang); + article = getMyApplication().getTravelDbHelper().getArticle(cityId, selectedLang); if (article == null) { return; } @@ -73,11 +81,11 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr items.add(new TitleItem(getString(R.string.shared_string_navigation))); - expListView = new ExpandableListView(getContext()); - ExpandableListAdapter listAdapter = new ExpandableListAdapter(getContext(), navigationMap); + expListView = new ExpandableListView(ctx); + final ExpandableListAdapter listAdapter = new ExpandableListAdapter(ctx, navigationMap); expListView.setAdapter(listAdapter); - Drawable transparent = ContextCompat.getDrawable(getContext(), R.color.color_transparent); + Drawable transparent = ContextCompat.getDrawable(ctx, R.color.color_transparent); expListView.setDivider(transparent); expListView.setGroupIndicator(transparent); expListView.setSelector(transparent); @@ -90,6 +98,8 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + WikivoyageSearchResult articleItem = listAdapter.getArticleItem(groupPosition, childPosition); + sendResults(articleItem.getCityId()); dismiss(); return true; } @@ -97,12 +107,16 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { - dismiss(); + if (!expListView.isGroupExpanded(groupPosition)) { + expListView.expandGroup(groupPosition); + } else { + expListView.collapseGroup(groupPosition); + } return true; } }); - LinearLayout container = new LinearLayout(getContext()); + LinearLayout container = new LinearLayout(ctx); container.addView(expListView); items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create()); @@ -124,14 +138,28 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr return false; } + private void sendResults(long cityId) { + Fragment fragment = getTargetFragment(); + if (fragment != null) { + Intent intent = new Intent(); + intent.putExtra(CITY_ID_KEY, cityId); + intent.putExtra(SELECTED_LANG_KEY, selectedLang); + fragment.onActivityResult(getTargetRequestCode(), OPEN_ARTICLE_REQUEST_CODE, intent); + } + } + public static boolean showInstance(@NonNull FragmentManager fm, + @Nullable Fragment targetFragment, long cityId, - @Nullable String selectedLang) { + @NonNull String selectedLang) { try { Bundle args = new Bundle(); args.putLong(CITY_ID_KEY, cityId); args.putString(SELECTED_LANG_KEY, selectedLang); WikivoyageArticleNavigationFragment fragment = new WikivoyageArticleNavigationFragment(); + if (targetFragment != null) { + fragment.setTargetFragment(targetFragment, OPEN_ARTICLE_REQUEST_CODE); + } fragment.setArguments(args); fragment.show(fm, TAG); return true; @@ -166,7 +194,7 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr return navigationMap.get(headers.get(groupPosition)); } - private WikivoyageSearchResult getArticleItem(int groupPosition, int childPosititon) { + public WikivoyageSearchResult getArticleItem(int groupPosition, int childPosititon) { return navigationMap.get(headers.get(groupPosition)).get(childPosititon); } @@ -204,14 +232,15 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { WikivoyageSearchResult articleItem = getArticleItem(groupPosition, childPosition); - String childText = articleItem.getArticleTitles().get(0); - boolean selected = cityId == articleItem.getCityId() || parentsList.contains(childText); + String childTitle = articleItem.getArticleTitles().get(0); + boolean selected = cityId == articleItem.getCityId() || parentsList.contains(childTitle); + if (convertView == null) { convertView = LayoutInflater.from(context) .inflate(R.layout.wikivoyage_contents_list_item, parent, false); } TextView txtListChild = (TextView) convertView.findViewById(R.id.item_label); - txtListChild.setText(childText); + txtListChild.setText(childTitle); if (selected) { txtListChild.setTextColor(ContextCompat.getColor(context, nightMode ? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light)); @@ -230,14 +259,14 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr @Override public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) { - String headerTitle = (String) getGroup(groupPosition); - boolean selected = parentsList.contains(headerTitle); + String groupTitle = (String) getGroup(groupPosition); + boolean selected = parentsList.contains(groupTitle) || article.getTitle().equals(groupTitle); if (convertView == null) { convertView = LayoutInflater.from(context) .inflate(R.layout.wikivoyage_contents_list_item, parent, false); } TextView lblListHeader = (TextView) convertView.findViewById(R.id.item_label); - lblListHeader.setText(headerTitle); + lblListHeader.setText(groupTitle); if (selected) { lblListHeader.setTextColor(ContextCompat.getColor(context, nightMode ? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light)); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java index c78fe2df13..58f77f89f5 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java @@ -24,6 +24,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -298,23 +299,23 @@ public class TravelDbHelper { cursor.close(); } LinkedHashMap> res = new LinkedHashMap<>(); - if (navMap.keySet().size() > 1 && parts != null && parts.length > 0) { - for (String part : parts) { - List partsList = navMap.get(part); - if (partsList != null) { - Collections.sort(partsList, new Comparator() { - @Override - public int compare(WikivoyageSearchResult o1, WikivoyageSearchResult o2) { - return collator.compare(o1.articleTitles.get(0), o2.articleTitles.get(0)); - } - }); - res.put(part, partsList); - } - } - } else { - res.putAll(navMap); + List partsList = new ArrayList<>(); + if (parts != null) { + partsList.addAll(Arrays.asList(parts)); + } + partsList.add(title); + for (String part : partsList) { + List results = navMap.get(part); + if (results != null) { + Collections.sort(results, new Comparator() { + @Override + public int compare(WikivoyageSearchResult o1, WikivoyageSearchResult o2) { + return collator.compare(o1.articleTitles.get(0), o2.articleTitles.get(0)); + } + }); + res.put(part, results); + } } - return res; }