Refactor and format WikivoyageContentsFragment

This commit is contained in:
Alex Sytnyk 2018-04-10 17:28:59 +03:00
parent e09d9ec8e4
commit 8ccfa46a29
2 changed files with 42 additions and 39 deletions

View file

@ -18,10 +18,10 @@ import net.osmand.AndroidUtils;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.wikivoyage.data.ContentsJsonParser; import net.osmand.plus.wikivoyage.data.ContentsJsonParser;
import net.osmand.plus.wikivoyage.data.ContentsJsonParser.ContentsContainer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -29,10 +29,12 @@ import java.util.List;
public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment { public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment {
public final static String TAG = "WikivoyageArticleContentsFragment"; public static final String TAG = "WikivoyageArticleContentsFragment";
public final static String CONTENTS_JSON_KEY = "contents_json";
public final static String CONTENTS_LINK_KEY = "contents_link"; public static final String CONTENTS_JSON_KEY = "contents_json";
public final static int REQUEST_LINK_CODE = 0; public static final String CONTENTS_LINK_KEY = "contents_link";
public static final int REQUEST_LINK_CODE = 0;
private LinkedHashMap<String, String> map; private LinkedHashMap<String, String> map;
private String link; private String link;
@ -40,16 +42,16 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
Bundle args = getArguments(); Bundle args = getArguments();
String contentsJson; if (args == null) {
if (args != null) {
contentsJson = args.getString(CONTENTS_JSON_KEY);
} else {
return; return;
} }
ContentsJsonParser.ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson);
String contentsJson = args.getString(CONTENTS_JSON_KEY);
ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson);
if (contentsContainer == null) { if (contentsContainer == null) {
return; return;
} }
final ArrayList<String> listDataHeader = contentsContainer.listDataHeader; final ArrayList<String> listDataHeader = contentsContainer.listDataHeader;
final LinkedHashMap<String, List<String>> listDataChild = contentsContainer.listDataChild; final LinkedHashMap<String, List<String>> listDataChild = contentsContainer.listDataChild;
@ -64,15 +66,15 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
expListView.setChildDivider(ContextCompat.getDrawable(getContext(), R.color.color_transparent)); expListView.setChildDivider(ContextCompat.getDrawable(getContext(), R.color.color_transparent));
expListView.setLayoutParams(new LinearLayout.LayoutParams( expListView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT)); LinearLayout.LayoutParams.MATCH_PARENT)
);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override @Override
public boolean onChildClick(ExpandableListView parent, View v, public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) { int groupPosition, int childPosition, long id) {
link = map.get(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition)); link = map.get(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
sendResult(0); sendResult();
dismiss(); dismiss();
return false; return false;
} }
@ -80,15 +82,17 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
int width = AndroidUtils.getScreenWidth(getActivity()); int width = AndroidUtils.getScreenWidth(getActivity());
if (android.os.Build.VERSION.SDK_INT < 18) { if (android.os.Build.VERSION.SDK_INT < 18) {
expListView.setIndicatorBounds(width - (AndroidUtils.dpToPx(getContext(), 50)), width - (AndroidUtils.dpToPx(getContext(), 10))); expListView.setIndicatorBounds(width - (AndroidUtils.dpToPx(getContext(), 50)),
width - (AndroidUtils.dpToPx(getContext(), 10)));
} else { } else {
expListView.setIndicatorBoundsRelative(width - (AndroidUtils.dpToPx(getContext(), 50)), width - (AndroidUtils.dpToPx(getContext(), 10))); expListView.setIndicatorBoundsRelative(width - (AndroidUtils.dpToPx(getContext(), 50)),
width - (AndroidUtils.dpToPx(getContext(), 10)));
} }
expListView.setOnGroupClickListener(new OnGroupClickListener() { expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override @Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
link = map.get(listDataHeader.get(groupPosition)); link = map.get(listDataHeader.get(groupPosition));
sendResult(0); sendResult();
if (listDataChild.get(listDataHeader.get(groupPosition)) == null) { if (listDataChild.get(listDataHeader.get(groupPosition)) == null) {
dismiss(); dismiss();
} }
@ -97,19 +101,16 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
}); });
LinearLayout container = new LinearLayout(getContext()); LinearLayout container = new LinearLayout(getContext());
container.addView(expListView); container.addView(expListView);
BaseBottomSheetItem favoritesItem = new SimpleBottomSheetItem.Builder()
.setCustomView(container) items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create());
.create();
items.add(favoritesItem);
} }
private void sendResult(int requestLinkCode) { private void sendResult() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.putExtra(CONTENTS_LINK_KEY, link); intent.putExtra(CONTENTS_LINK_KEY, link);
Fragment fragment = getTargetFragment(); Fragment fragment = getTargetFragment();
if (fragment != null) { if (fragment != null) {
fragment.onActivityResult( fragment.onActivityResult(getTargetRequestCode(), REQUEST_LINK_CODE, intent);
getTargetRequestCode(), requestLinkCode, intent);
} }
} }
@ -121,21 +122,24 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
class ExpandableListAdapter extends OsmandBaseExpandableListAdapter { class ExpandableListAdapter extends OsmandBaseExpandableListAdapter {
private Context context; private Context context;
private List<String> listDataHeader; private List<String> listDataHeader;
private LinkedHashMap<String, List<String>> listDataChild; private LinkedHashMap<String, List<String>> listDataChild;
private Drawable itemGroupIcon; private Drawable itemGroupIcon;
private Drawable itemChildIcon; private Drawable itemChildIcon;
public ExpandableListAdapter(Context context, List<String> listDataHeader, ExpandableListAdapter(Context context, List<String> listDataHeader,
LinkedHashMap<String, List<String>> listChildData) { LinkedHashMap<String, List<String>> listChildData) {
this.context = context; this.context = context;
this.listDataHeader = listDataHeader; this.listDataHeader = listDataHeader;
this.listDataChild = listChildData; this.listDataChild = listChildData;
itemGroupIcon = getIcon(R.drawable.ic_action_contents, itemGroupIcon = getIcon(R.drawable.ic_action_contents, nightMode
isNightMode() ? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light); ? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light);
itemChildIcon = getIcon(R.drawable.ic_action_list_bullet, itemChildIcon = getIcon(R.drawable.ic_action_list_bullet, nightMode
isNightMode() ? R.color.route_info_unchecked_mode_icon_color : R.color.ctx_menu_nearby_routes_text_color_dark); ? R.color.route_info_unchecked_mode_icon_color
: R.color.ctx_menu_nearby_routes_text_color_dark);
} }
@Override @Override
@ -155,11 +159,13 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
if (convertView == null) { if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, null); convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, parent, false);
} }
TextView txtListChild = (TextView) convertView.findViewById(R.id.item_label); TextView txtListChild = (TextView) convertView.findViewById(R.id.item_label);
txtListChild.setText(childText); txtListChild.setText(childText);
txtListChild.setTextColor(getResolvedColor(isNightMode() ? R.color.wikivoyage_contents_icon_dark : R.color.wikivoyage_contents_icon_light)); txtListChild.setTextColor(getResolvedColor(nightMode
? R.color.wikivoyage_contents_icon_dark
: R.color.wikivoyage_contents_icon_light));
txtListChild.setCompoundDrawablesWithIntrinsicBounds(itemChildIcon, null, null, null); txtListChild.setCompoundDrawablesWithIntrinsicBounds(itemChildIcon, null, null, null);
return convertView; return convertView;
@ -167,11 +173,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
@Override @Override
public int getChildrenCount(int groupPosition) { public int getChildrenCount(int groupPosition) {
if (this.listDataChild.get(this.listDataHeader.get(groupPosition)) != null) { List<String> list = listDataChild.get(listDataHeader.get(groupPosition));
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size(); return list == null ? 0 : list.size();
} else {
return 0;
}
} }
@Override @Override
@ -196,7 +199,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
if (convertView == null) { if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, null); convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, parent, false);
} }
TextView lblListHeader = (TextView) convertView.findViewById(R.id.item_label); TextView lblListHeader = (TextView) convertView.findViewById(R.id.item_label);
lblListHeader.setText(headerTitle); lblListHeader.setText(headerTitle);

View file

@ -104,7 +104,7 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
fragment.setUsedOnMap(false); fragment.setUsedOnMap(false);
fragment.setArguments(args); fragment.setArguments(args);
fragment.setTargetFragment(WikivoyageArticleDialogFragment.this, 0); fragment.setTargetFragment(WikivoyageArticleDialogFragment.this, 0);
fragment.show(getChildFragmentManager(), WikivoyageArticleContentsFragment.TAG); fragment.show(getFragmentManager(), WikivoyageArticleContentsFragment.TAG);
} }
}); });