Make WikivoyageSearchResult not Parcelable

This commit is contained in:
Alex Sytnyk 2018-04-03 16:43:30 +03:00
parent e96a67ce93
commit c16465da8f
3 changed files with 22 additions and 57 deletions

View file

@ -22,15 +22,18 @@ import net.osmand.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.data.WikivoyageArticle; import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import java.io.File; import java.io.File;
import java.util.ArrayList;
public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragment { public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragment {
public static final String TAG = "WikivoyageArticleDialogFragment"; public static final String TAG = "WikivoyageArticleDialogFragment";
private static final String SEARCH_RESULT_KEY = "search_result_key"; private static final long NO_VALUE = -1;
private static final String CITY_ID_KEY = "city_id_key";
private static final String LANGS_KEY = "langs_key";
private static final String SELECTED_LANG_KEY = "selected_lang_key"; private static final String SELECTED_LANG_KEY = "selected_lang_key";
private static final String HEADER_INNER = "<html><head>\n" + private static final String HEADER_INNER = "<html><head>\n" +
@ -40,7 +43,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
"</head><body>"; "</head><body>";
private static final String FOOTER_INNER = "</div></body></html>"; private static final String FOOTER_INNER = "</div></body></html>";
private WikivoyageSearchResult searchResult; private long cityId = NO_VALUE;
private ArrayList<String> langs;
private String selectedLang; private String selectedLang;
private TextView selectedLangTv; private TextView selectedLangTv;
@ -97,12 +101,12 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
} }
private void showPopupLangMenu(View view) { private void showPopupLangMenu(View view) {
if (searchResult == null) { if (langs == null) {
return; return;
} }
final PopupMenu popup = new PopupMenu(view.getContext(), view, Gravity.END); final PopupMenu popup = new PopupMenu(view.getContext(), view, Gravity.END);
for (final String lang : searchResult.getLangs()) { for (final String lang : langs) {
MenuItem item = popup.getMenu().add(lang); MenuItem item = popup.getMenu().add(lang);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
@ -120,23 +124,24 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
} }
private void populateArticle() { private void populateArticle() {
if (searchResult == null) { if (cityId == NO_VALUE || langs == null) {
Bundle args = getArguments(); Bundle args = getArguments();
if (args != null) { if (args != null) {
searchResult = (WikivoyageSearchResult) args.getParcelable(SEARCH_RESULT_KEY); cityId = args.getLong(CITY_ID_KEY);
langs = args.getStringArrayList(LANGS_KEY);
} }
} }
if (searchResult == null) { if (cityId == NO_VALUE || langs == null || langs.isEmpty()) {
return; return;
} }
if (selectedLang == null) { if (selectedLang == null) {
selectedLang = searchResult.getLangs().get(0); selectedLang = langs.get(0);
} }
selectedLangTv.setText(selectedLang); selectedLangTv.setText(selectedLang);
WikivoyageArticle article = getMyApplication().getWikivoyageDbHelper() WikivoyageArticle article = getMyApplication().getWikivoyageDbHelper()
.getArticle(searchResult.getCityId(), selectedLang); .getArticle(cityId, selectedLang);
if (article == null) { if (article == null) {
return; return;
} }
@ -180,10 +185,11 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
return normal; return normal;
} }
public static boolean showInstance(FragmentManager fm, WikivoyageSearchResult searchResult) { public static boolean showInstance(FragmentManager fm, long cityId, ArrayList<String> langs) {
try { try {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelable(SEARCH_RESULT_KEY, searchResult); args.putLong(CITY_ID_KEY, cityId);
args.putStringArrayList(LANGS_KEY, langs);
WikivoyageArticleDialogFragment fragment = new WikivoyageArticleDialogFragment(); WikivoyageArticleDialogFragment fragment = new WikivoyageArticleDialogFragment();
fragment.setArguments(args); fragment.setArguments(args);
fragment.show(fm, TAG); fragment.show(fm, TAG);

View file

@ -1,14 +1,11 @@
package net.osmand.plus.wikivoyage.data; package net.osmand.plus.wikivoyage.data;
import android.os.Parcel;
import android.os.Parcelable;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class WikivoyageSearchResult implements Parcelable { public class WikivoyageSearchResult {
private static final int SHOW_LANGS = 3; private static final int SHOW_LANGS = 3;
@ -19,19 +16,6 @@ public class WikivoyageSearchResult implements Parcelable {
String isPartOf; String isPartOf;
String imageTitle; String imageTitle;
WikivoyageSearchResult() {
}
private WikivoyageSearchResult(Parcel in) {
searchTerms = in.createStringArrayList();
cityId = in.readLong();
articleTitles = in.createStringArrayList();
langs = in.createStringArrayList();
isPartOf = in.readString();
imageTitle = in.readString();
}
public List<String> getSearchTerms() { public List<String> getSearchTerms() {
return searchTerms; return searchTerms;
} }
@ -67,31 +51,4 @@ public class WikivoyageSearchResult implements Parcelable {
} }
return res.toString(); return res.toString();
} }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(searchTerms);
dest.writeLong(cityId);
dest.writeStringList(articleTitles);
dest.writeStringList(langs);
dest.writeString(isPartOf);
dest.writeString(imageTitle);
}
public static final Creator<WikivoyageSearchResult> CREATOR = new Creator<WikivoyageSearchResult>() {
@Override
public WikivoyageSearchResult createFromParcel(Parcel in) {
return new WikivoyageSearchResult(in);
}
@Override
public WikivoyageSearchResult[] newArray(int size) {
return new WikivoyageSearchResult[size];
}
};
} }

View file

@ -22,6 +22,7 @@ import net.osmand.plus.wikivoyage.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment; import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult; import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment { public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment {
@ -100,8 +101,9 @@ public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment
if (pos != RecyclerView.NO_POSITION) { if (pos != RecyclerView.NO_POSITION) {
Object item = adapter.getItem(pos); Object item = adapter.getItem(pos);
if (item instanceof WikivoyageSearchResult) { if (item instanceof WikivoyageSearchResult) {
WikivoyageSearchResult res = (WikivoyageSearchResult) item;
WikivoyageArticleDialogFragment.showInstance(getFragmentManager(), WikivoyageArticleDialogFragment.showInstance(getFragmentManager(),
(WikivoyageSearchResult) item); res.getCityId(), new ArrayList<>(res.getLangs()));
} }
} }
} }