diff --git a/OsmAnd/res/layout/fragment_wikivoyage_article_dialog.xml b/OsmAnd/res/layout/fragment_wikivoyage_article_dialog.xml index 3654d566f2..80f28a8b28 100644 --- a/OsmAnd/res/layout/fragment_wikivoyage_article_dialog.xml +++ b/OsmAnd/res/layout/fragment_wikivoyage_article_dialog.xml @@ -127,13 +127,13 @@ android:paddingLeft="@dimen/bottom_sheet_content_padding_small" android:paddingRight="@dimen/bottom_sheet_content_padding_small" android:paddingTop="@dimen/context_menu_padding_margin_tiny" - android:text="@string/shared_string_save" android:textColor="?attr/wikivoyage_active_color" android:textSize="@dimen/default_desc_text_size" osmand:typeface="@string/font_roboto_medium" tools:drawableRight="@drawable/ic_action_read_later_fill" tools:drawableTint="?attr/wikivoyage_active_color" - tools:ignore="UnusedAttribute"/> + tools:ignore="UnusedAttribute" + tools:text="Save"/> diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleContentsFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java similarity index 93% rename from OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleContentsFragment.java rename to OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java index f6c2924897..7fd95a3ec8 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleContentsFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.wikivoyage; +package net.osmand.plus.wikivoyage.article; import android.content.Context; import android.content.Intent; @@ -47,6 +47,10 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag } String contentsJson = args.getString(CONTENTS_JSON_KEY); + if (contentsJson == null) { + return; + } + ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson); if (contentsContainer == null) { return; @@ -157,9 +161,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag boolean isLastChild, View convertView, ViewGroup parent) { String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { - LayoutInflater infalInflater = (LayoutInflater) this.context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, parent, false); + 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); @@ -197,9 +200,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { - LayoutInflater infalInflater = (LayoutInflater) this.context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, parent, false); + 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); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java similarity index 85% rename from OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java rename to OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index f11f48b14f..6c131f9c68 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.wikivoyage; +package net.osmand.plus.wikivoyage.article; import android.annotation.SuppressLint; import android.content.Intent; @@ -23,6 +23,7 @@ import android.widget.TextView; import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.plus.R; +import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment; import net.osmand.plus.wikivoyage.data.WikivoyageArticle; import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper; import net.osmand.util.Algorithms; @@ -51,9 +52,10 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen private long cityId = NO_VALUE; private ArrayList langs; private String selectedLang; - private String contentsJson; + private WikivoyageArticle article; private TextView selectedLangTv; + private TextView saveBtn; private WebView contentWebView; @SuppressLint("SetJavaScriptEnabled") @@ -98,20 +100,21 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen contentsBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + FragmentManager fm = getFragmentManager(); + if (article == null || fm == null) { + return; + } Bundle args = new Bundle(); - args.putString(WikivoyageArticleContentsFragment.CONTENTS_JSON_KEY, contentsJson); + args.putString(WikivoyageArticleContentsFragment.CONTENTS_JSON_KEY, article.getContentsJson()); WikivoyageArticleContentsFragment fragment = new WikivoyageArticleContentsFragment(); fragment.setUsedOnMap(false); fragment.setArguments(args); fragment.setTargetFragment(WikivoyageArticleDialogFragment.this, 0); - fragment.show(getFragmentManager(), WikivoyageArticleContentsFragment.TAG); + fragment.show(fm, WikivoyageArticleContentsFragment.TAG); } }); - TextView saveBtn = (TextView) mainView.findViewById(R.id.save_button); - saveBtn.setCompoundDrawablesWithIntrinsicBounds( - null, null, getActiveIcon(R.drawable.ic_action_read_later), null - ); + saveBtn = (TextView) mainView.findViewById(R.id.save_button); contentWebView = (WebView) mainView.findViewById(R.id.content_web_view); contentWebView.getSettings().setJavaScriptEnabled(true); @@ -144,6 +147,29 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen return nightMode ? R.color.status_bar_wikivoyage_article_dark : R.color.status_bar_wikivoyage_article_light; } + private void updateSaveButton() { + if (article != null) { + final WikivoyageLocalDataHelper helper = WikivoyageLocalDataHelper.getInstance(getMyApplication()); + final boolean saved = helper.isArticleSaved(article); + Drawable icon = getActiveIcon(saved ? R.drawable.ic_action_read_later_fill : R.drawable.ic_action_read_later); + saveBtn.setText(getString(saved ? R.string.shared_string_delete : R.string.shared_string_save)); + saveBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null); + saveBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (article != null) { + if (saved) { + helper.removeArticleFromSaved(article); + } else { + helper.addArticleToSaved(article); + } + updateSaveButton(); + } + } + }); + } + } + private void showPopupLangMenu(View view) { if (langs == null) { return; @@ -182,17 +208,15 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen selectedLang = langs.get(0); } - selectedLangTv.setText(Algorithms.capitalizeFirstLetter(selectedLang)); - - WikivoyageArticle article = getMyApplication().getWikivoyageDbHelper() - .getArticle(cityId, selectedLang); + article = getMyApplication().getWikivoyageDbHelper().getArticle(cityId, selectedLang); if (article == null) { return; } - contentsJson = article.getContentsJson(); WikivoyageLocalDataHelper.getInstance(getMyApplication()).addToHistory(article); + updateSaveButton(); + selectedLangTv.setText(Algorithms.capitalizeFirstLetter(selectedLang)); contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(article), "text/html", "UTF-8", null); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/SavedArticlesRvAdapter.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/SavedArticlesRvAdapter.java index 2972d7249d..11aa2fdd45 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/SavedArticlesRvAdapter.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/SavedArticlesRvAdapter.java @@ -32,7 +32,7 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter savedArticles = WikivoyageLocalDataHelper.getInstance(app).getSavedArticles(); if (!savedArticles.isEmpty()) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java index 32e262e7bf..d06820d8fc 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java @@ -19,7 +19,7 @@ import android.widget.ProgressBar; import net.osmand.ResultMatcher; import net.osmand.plus.R; -import net.osmand.plus.wikivoyage.WikivoyageArticleDialogFragment; +import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment; import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment; import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper; import net.osmand.plus.wikivoyage.data.WikivoyageSearchHistoryItem;