refactor SearchMoreListItem

This commit is contained in:
Nazar-Kutz 2020-06-24 08:30:17 +03:00
parent 9fa3c0f313
commit 6aa58257fe
3 changed files with 60 additions and 74 deletions

View file

@ -119,6 +119,7 @@ import static net.osmand.plus.search.SendSearchQueryBottomSheet.MISSING_SEARCH_Q
import static net.osmand.search.core.ObjectType.POI_TYPE;
import static net.osmand.search.core.ObjectType.SEARCH_STARTED;
import static net.osmand.search.core.SearchCoreFactory.SEARCH_AMENITY_TYPE_PRIORITY;
import static net.osmand.plus.search.listitems.QuickSearchMoreListItem.*;
public class QuickSearchDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener {
@ -1710,7 +1711,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
if (!Version.isPaidVersion(app)) {
mainSearchFragment.addListItem(new QuickSearchFreeBannerListItem(app));
} else {
addNotFoundButton(searchUICore.isSearchMoreAvailable(phrase), true);
addNotFoundButton(searchUICore.isSearchMoreAvailable(phrase), SearchMoreType.WIKIPEDIA);
}
} else {
addNotFoundButton(searchUICore.isSearchMoreAvailable(phrase));
@ -1983,16 +1984,16 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
}
private void addNotFoundButton(boolean searchMoreAvailable) {
addNotFoundButton(searchMoreAvailable, false);
addNotFoundButton(searchMoreAvailable, SearchMoreType.STANDARD);
}
private void addNotFoundButton(boolean searchMoreAvailable, final boolean isWiki) {
private void addNotFoundButton(boolean searchMoreAvailable, final SearchMoreType type) {
if (!paused && !cancelPrev && mainSearchFragment != null && !isTextEmpty()) {
final WikipediaPlugin wikiPlugin = OsmandPlugin.getPlugin(WikipediaPlugin.class);
QuickSearchMoreListItem moreListItem =
new QuickSearchMoreListItem(app, null, isWiki, new SearchMoreItemOnClickListener() {
new QuickSearchMoreListItem(app, null, type, new SearchMoreItemOnClickListener() {
@Override
public void increaseRadiusOnClick() {
public void onPrimaryButtonClick() {
if (!interruptedSearch) {
SearchSettings settings = searchUICore.getSearchSettings();
searchUICore.updateSettings(settings.setRadiusLevel(settings.getRadiusLevel() + 1));
@ -2001,31 +2002,34 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
}
@Override
public void onlineSearchOnClick() {
final OsmandSettings settings = app.getSettings();
if (!settings.isInternetConnectionAvailable()) {
Toast.makeText(app, R.string.internet_not_available, Toast.LENGTH_LONG).show();
return;
}
startOnlineSearch();
mainSearchFragment.getAdapter().clear();
updateTabbarVisibility(false);
runCoreSearch(searchQuery, false, true);
}
@Override
public void downloadWikiOnClick() {
if (wikiPlugin != null) {
wikiPlugin.showDownloadWikiScreen();
public void onSecondaryButtonClick() {
if (type == SearchMoreType.WIKIPEDIA) {
if (wikiPlugin != null) {
wikiPlugin.showDownloadWikiScreen();
}
} else if (type == SearchMoreType.STANDARD) {
final OsmandSettings settings = app.getSettings();
if (!settings.isInternetConnectionAvailable()) {
Toast.makeText(app, R.string.internet_not_available, Toast.LENGTH_LONG).show();
return;
}
startOnlineSearch();
mainSearchFragment.getAdapter().clear();
updateTabbarVisibility(false);
runCoreSearch(searchQuery, false, true);
}
}
});
moreListItem.setInterruptedSearch(interruptedSearch);
moreListItem.setEmptySearch(isResultEmpty());
moreListItem.setOnlineSearch(isOnlineSearch());
moreListItem.setSearchMoreAvailable(searchMoreAvailable);
moreListItem.setSecondaryButtonVisible(!isWiki ||
(wikiPlugin != null && wikiPlugin.hasMapsToDownload()));
boolean secondaryButtonVisible = false;
if (type == SearchMoreType.STANDARD) {
secondaryButtonVisible = isOnlineSearch();
} else if (type == SearchMoreType.WIKIPEDIA) {
secondaryButtonVisible = wikiPlugin != null && wikiPlugin.hasMapsToDownload();
}
moreListItem.setSecondaryButtonVisible(secondaryButtonVisible);
mainSearchFragment.addListItem(moreListItem);
updateSendEmptySearchBottomBar(isResultEmpty() && !interruptedSearch);
}

View file

@ -268,31 +268,21 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
primaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((QuickSearchMoreListItem) listItem).increaseRadiusOnClick();
((QuickSearchMoreListItem) listItem).onPrimaryButtonClick();
}
});
View secondaryButton = view.findViewById(R.id.secondary_button);
if (searchMoreItem.isWiki()) {
secondaryButton.setVisibility(
searchMoreItem.isSecondaryButtonVisible() ? View.VISIBLE : View.GONE);
TextView tvTitle = view.findViewById(R.id.secondary_button_title);
tvTitle.setText(R.string.search_download_wikipedia_maps);
secondaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchMoreItem.downloadWikiOnClick();
}
});
} else if (!searchMoreItem.isOnlineSearch()) {
secondaryButton.setVisibility(View.VISIBLE);
secondaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchMoreItem.onlineSearchOnClick();
}
});
}
secondaryButton.setVisibility(searchMoreItem.isSecondaryButtonVisible() ?
View.VISIBLE : View.GONE);
TextView tvSecondaryButtonTitle = view.findViewById(R.id.secondary_button_title);
tvSecondaryButtonTitle.setText(searchMoreItem.getSecondaryButtonTitle());
secondaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchMoreItem.onSecondaryButtonClick();
}
});
} else if (type == QuickSearchListItemType.BUTTON) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.search_custom_list_item, null);

View file

@ -10,21 +10,24 @@ public class QuickSearchMoreListItem extends QuickSearchListItem {
private String name;
private SearchMoreItemOnClickListener onClickListener;
private boolean emptySearch;
private boolean onlineSearch;
private boolean searchMoreAvailable;
private boolean interruptedSearch;
private String findMore;
private String restartSearch;
private String increaseRadius;
private boolean isWiki;
private SearchMoreType type;
private boolean secondaryButtonVisibility;
public QuickSearchMoreListItem(OsmandApplication app, String name, boolean isWiki,
public enum SearchMoreType {
STANDARD, WIKIPEDIA
}
public QuickSearchMoreListItem(OsmandApplication app, String name, SearchMoreType type,
@Nullable SearchMoreItemOnClickListener onClickListener) {
super(app, null);
this.name = name;
this.onClickListener = onClickListener;
this.isWiki = isWiki;
this.type = type;
findMore = app.getString(R.string.search_POI_level_btn).toUpperCase();
restartSearch = app.getString(R.string.restart_search).toUpperCase();
increaseRadius = app.getString(R.string.increase_search_radius).toUpperCase();
@ -49,6 +52,14 @@ public class QuickSearchMoreListItem extends QuickSearchListItem {
}
}
public String getSecondaryButtonTitle() {
if (type == SearchMoreType.WIKIPEDIA) {
return app.getString(R.string.search_download_wikipedia_maps);
} else {
return "";
}
}
public boolean isInterruptedSearch() {
return interruptedSearch;
}
@ -65,14 +76,6 @@ public class QuickSearchMoreListItem extends QuickSearchListItem {
this.emptySearch = emptySearch;
}
public boolean isOnlineSearch() {
return onlineSearch;
}
public void setOnlineSearch(boolean onlineSearch) {
this.onlineSearch = onlineSearch;
}
public boolean isSearchMoreAvailable() {
return searchMoreAvailable;
}
@ -81,28 +84,18 @@ public class QuickSearchMoreListItem extends QuickSearchListItem {
this.searchMoreAvailable = searchMoreAvailable;
}
public void increaseRadiusOnClick() {
public void onPrimaryButtonClick() {
if (onClickListener != null) {
onClickListener.increaseRadiusOnClick();
onClickListener.onPrimaryButtonClick();
}
}
public void onlineSearchOnClick() {
public void onSecondaryButtonClick() {
if (onClickListener != null) {
onClickListener.onlineSearchOnClick();
onClickListener.onSecondaryButtonClick();
}
}
public void downloadWikiOnClick() {
if (onClickListener != null) {
onClickListener.downloadWikiOnClick();
}
}
public boolean isWiki() {
return isWiki;
}
public void setSecondaryButtonVisible(boolean secondaryButtonVisibility) {
this.secondaryButtonVisibility = secondaryButtonVisibility;
}
@ -113,10 +106,9 @@ public class QuickSearchMoreListItem extends QuickSearchListItem {
public interface SearchMoreItemOnClickListener {
void increaseRadiusOnClick();
void onPrimaryButtonClick();
void onlineSearchOnClick();
void onSecondaryButtonClick();
void downloadWikiOnClick();
}
}