divide "getView()" method into smaller methods

This commit is contained in:
nazar-kutz 2021-02-28 22:15:49 +02:00
parent 54a9ca41f7
commit 0d29c84a6e

View file

@ -10,12 +10,10 @@ import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
@ -33,7 +31,6 @@ import net.osmand.plus.R;
import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
import net.osmand.plus.chooseplan.ChoosePlanDialogFragment;
import net.osmand.plus.search.listitems.QuickSearchBannerListItem;
import net.osmand.plus.search.listitems.QuickSearchFreeBannerListItem;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.search.listitems.QuickSearchHeaderListItem;
import net.osmand.plus.search.listitems.QuickSearchListItem;
@ -42,6 +39,7 @@ import net.osmand.plus.search.listitems.QuickSearchMoreListItem;
import net.osmand.plus.search.listitems.QuickSearchSelectAllListItem;
import net.osmand.search.SearchUICore;
import net.osmand.search.core.SearchPhrase;
import net.osmand.search.core.SearchResult;
import net.osmand.search.core.SearchWord;
import net.osmand.util.Algorithms;
import net.osmand.util.OpeningHoursParser;
@ -209,15 +207,38 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final QuickSearchListItem listItem = getItem(position);
QuickSearchListItemType type = listItem.getType();
LinearLayout view;
if (type == QuickSearchListItemType.BANNER) {
final QuickSearchBannerListItem banner = (QuickSearchBannerListItem) listItem;
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.search_banner_list_item, null);
view = bindBannerItem(convertView, listItem);
} else if (type == QuickSearchListItemType.FREE_VERSION_BANNER) {
view = bindFreeVersionBannerItem(convertView);
} else if (type == QuickSearchListItemType.SEARCH_MORE) {
view = bindSearchMoreItem(convertView, listItem);
} else if (type == QuickSearchListItemType.BUTTON) {
view = bindButtonItem(convertView, listItem);
} else if (type == QuickSearchListItemType.SELECT_ALL) {
view = bindSelectAllItem(position, convertView);
} else if (type == QuickSearchListItemType.HEADER) {
view = bindHeaderItem(convertView, listItem);
} else if (type == QuickSearchListItemType.TOP_SHADOW) {
return bindTopShadowItem(convertView);
} else if (type == QuickSearchListItemType.BOTTOM_SHADOW) {
return bindBottomShadowItem(convertView);
} else {
view = (LinearLayout) convertView;
view = bindSearchResultItem(position, convertView, listItem);
}
setupBackground(view);
setupDivider(position, view, listItem);
ViewCompat.setAccessibilityDelegate(view, accessibilityAssistant);
return view;
}
private LinearLayout bindBannerItem(@Nullable View convertView,
@NonNull QuickSearchListItem listItem) {
QuickSearchBannerListItem banner = (QuickSearchBannerListItem) listItem;
LinearLayout view = getLinearLayout(convertView, R.layout.search_banner_list_item);
((TextView) view.findViewById(R.id.empty_search_description)).setText(R.string.nothing_found_descr);
SearchUICore searchUICore = app.getSearchUICore().getCore();
@ -252,14 +273,11 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
buttonContainer.addView(v);
}
}
} else if (type == QuickSearchListItemType.FREE_VERSION_BANNER) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(
R.layout.read_wikipedia_ofline_banner, null);
} else {
view = (LinearLayout) convertView;
return view;
}
private LinearLayout bindFreeVersionBannerItem(@Nullable View convertView) {
LinearLayout view = getLinearLayout(convertView, R.layout.read_wikipedia_ofline_banner);
View btnGet = view.findViewById(R.id.btn_get);
if (btnGet != null) {
btnGet.setOnClickListener(new View.OnClickListener() {
@ -270,13 +288,13 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
}
});
}
} else if (type == QuickSearchListItemType.SEARCH_MORE) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.search_more_list_item, null);
} else {
view = (LinearLayout) convertView;
return view;
}
private LinearLayout bindSearchMoreItem(@Nullable View convertView,
@NonNull final QuickSearchListItem listItem) {
LinearLayout view = getLinearLayout(convertView, R.layout.search_more_list_item);
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
@ -325,24 +343,24 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
searchMoreItem.onSecondaryButtonClick();
}
});
} else if (type == QuickSearchListItemType.BUTTON) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.search_custom_list_item, null);
} else {
view = (LinearLayout) convertView;
return view;
}
private LinearLayout bindButtonItem(@Nullable View convertView,
@NonNull QuickSearchListItem listItem) {
LinearLayout view = getLinearLayout(convertView, R.layout.search_custom_list_item);
((ImageView) view.findViewById(R.id.imageView)).setImageDrawable(listItem.getIcon());
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
} else if (type == QuickSearchListItemType.SELECT_ALL) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.select_all_list_item, null);
} else {
view = (LinearLayout) convertView;
return view;
}
private LinearLayout bindSelectAllItem(final int position,
@Nullable View convertView) {
LinearLayout view = getLinearLayout(convertView, R.layout.select_all_list_item);
final CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectAll);
@ -353,12 +371,12 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
toggleCheckbox(position, ch);
}
});
} else if (type == QuickSearchListItemType.HEADER) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.search_header_list_item, null);
} else {
view = (LinearLayout) convertView;
return view;
}
private LinearLayout bindHeaderItem(@Nullable View convertView,
@NonNull QuickSearchListItem listItem) {
LinearLayout view = getLinearLayout(convertView, R.layout.search_header_list_item);
view.findViewById(R.id.top_divider)
.setVisibility(((QuickSearchHeaderListItem)listItem).isShowTopDivider() ? View.VISIBLE : View.GONE);
if (listItem.getSpannableName() != null) {
@ -366,42 +384,23 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
} else if (type == QuickSearchListItemType.TOP_SHADOW) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.list_shadow_header, null);
} else {
view = (LinearLayout) convertView;
}
return view;
} else if (type == QuickSearchListItemType.BOTTOM_SHADOW) {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.list_shadow_footer, null);
} else {
view = (LinearLayout) convertView;
}
return view;
} else {
if (convertView == null) {
view = (LinearLayout) inflater.inflate(R.layout.search_list_item, null);
} else {
view = (LinearLayout) convertView;
}
final CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
if (selectionMode) {
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectedItems.contains(listItem));
ch.setOnClickListener(new View.OnClickListener() {
private LinearLayout bindTopShadowItem(@Nullable View convertView) {
return getLinearLayout(convertView, R.layout.list_shadow_header);
}
@Override
public void onClick(View v) {
toggleCheckbox(position, ch);
}
});
} else {
ch.setVisibility(View.GONE);
private LinearLayout bindBottomShadowItem(@Nullable View convertView) {
return getLinearLayout(convertView, R.layout.list_shadow_footer);
}
private LinearLayout bindSearchResultItem(int position,
@Nullable View convertView,
@NonNull QuickSearchListItem listItem) {
LinearLayout view = getLinearLayout(convertView, R.layout.search_list_item);
setupCheckBox(position, view, listItem);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
@ -483,9 +482,46 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
}
updateCompassVisibility(view, listItem);
return view;
}
view.setBackgroundColor(app.getResources().getColor(
app.getSettings().isLightContent() ? R.color.list_background_color_light : R.color.list_background_color_dark));
private LinearLayout getLinearLayout(@Nullable View convertView, int layoutId) {
if (convertView == null) {
convertView = inflater.inflate(layoutId, null);
}
return (LinearLayout) convertView;
}
private void setupCheckBox(final int position,
@NonNull View rootView,
@NonNull QuickSearchListItem listItem) {
final CheckBox ch = (CheckBox) rootView.findViewById(R.id.toggle_item);
if (selectionMode) {
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectedItems.contains(listItem));
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleCheckbox(position, ch);
}
});
} else {
ch.setVisibility(View.GONE);
}
}
private void setupBackground(View view) {
int bgColorResId = isNightMode() ?
R.color.list_background_color_dark :
R.color.list_background_color_light;
view.setBackgroundColor(app.getResources().getColor(bgColorResId));
}
private void setupDivider(final int position,
@NonNull View view,
@NonNull QuickSearchListItem listItem) {
View divider = view.findViewById(R.id.divider);
if (divider != null) {
if (position == getCount() - 1 || getItem(position + 1).getType() == QuickSearchListItemType.HEADER
@ -494,7 +530,7 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
} else {
divider.setVisibility(View.VISIBLE);
if (getItem(position + 1).getType() == QuickSearchListItemType.SEARCH_MORE
|| type == QuickSearchListItemType.SELECT_ALL) {
|| listItem.getType() == QuickSearchListItemType.SELECT_ALL) {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(0, 0, 0 ,0);
divider.setLayoutParams(p);
@ -505,8 +541,6 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
}
}
}
ViewCompat.setAccessibilityDelegate(view, accessibilityAssistant);
return view;
}
public static String getIncreaseSearchButtonTitle(OsmandApplication app, SearchPhrase searchPhrase) {
@ -576,4 +610,8 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
LatLon toloc = listItem.getSearchResult().location;
app.getUIUtilities().updateLocationView(updateLocationViewCache, direction, distanceText, toloc);
}
private boolean isNightMode() {
return !app.getSettings().isLightContent();
}
}