Refactor and format WikivoyageContentsFragment
This commit is contained in:
parent
e09d9ec8e4
commit
8ccfa46a29
2 changed files with 42 additions and 39 deletions
|
@ -18,10 +18,10 @@ import net.osmand.AndroidUtils;
|
|||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
|
||||
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.simpleitems.TitleItem;
|
||||
import net.osmand.plus.wikivoyage.data.ContentsJsonParser;
|
||||
import net.osmand.plus.wikivoyage.data.ContentsJsonParser.ContentsContainer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
|
@ -29,10 +29,12 @@ import java.util.List;
|
|||
|
||||
public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment {
|
||||
|
||||
public final static String TAG = "WikivoyageArticleContentsFragment";
|
||||
public final static String CONTENTS_JSON_KEY = "contents_json";
|
||||
public final static String CONTENTS_LINK_KEY = "contents_link";
|
||||
public final static int REQUEST_LINK_CODE = 0;
|
||||
public static final String TAG = "WikivoyageArticleContentsFragment";
|
||||
|
||||
public static final String CONTENTS_JSON_KEY = "contents_json";
|
||||
public static final String CONTENTS_LINK_KEY = "contents_link";
|
||||
|
||||
public static final int REQUEST_LINK_CODE = 0;
|
||||
|
||||
private LinkedHashMap<String, String> map;
|
||||
private String link;
|
||||
|
@ -40,16 +42,16 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
Bundle args = getArguments();
|
||||
String contentsJson;
|
||||
if (args != null) {
|
||||
contentsJson = args.getString(CONTENTS_JSON_KEY);
|
||||
} else {
|
||||
if (args == null) {
|
||||
return;
|
||||
}
|
||||
ContentsJsonParser.ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson);
|
||||
|
||||
String contentsJson = args.getString(CONTENTS_JSON_KEY);
|
||||
ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson);
|
||||
if (contentsContainer == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ArrayList<String> listDataHeader = contentsContainer.listDataHeader;
|
||||
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.setLayoutParams(new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT));
|
||||
LinearLayout.LayoutParams.MATCH_PARENT)
|
||||
);
|
||||
|
||||
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
|
||||
|
||||
@Override
|
||||
public boolean onChildClick(ExpandableListView parent, View v,
|
||||
int groupPosition, int childPosition, long id) {
|
||||
link = map.get(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
|
||||
sendResult(0);
|
||||
sendResult();
|
||||
dismiss();
|
||||
return false;
|
||||
}
|
||||
|
@ -80,15 +82,17 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
|
||||
int width = AndroidUtils.getScreenWidth(getActivity());
|
||||
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 {
|
||||
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() {
|
||||
@Override
|
||||
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
||||
link = map.get(listDataHeader.get(groupPosition));
|
||||
sendResult(0);
|
||||
sendResult();
|
||||
if (listDataChild.get(listDataHeader.get(groupPosition)) == null) {
|
||||
dismiss();
|
||||
}
|
||||
|
@ -97,19 +101,16 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
});
|
||||
LinearLayout container = new LinearLayout(getContext());
|
||||
container.addView(expListView);
|
||||
BaseBottomSheetItem favoritesItem = new SimpleBottomSheetItem.Builder()
|
||||
.setCustomView(container)
|
||||
.create();
|
||||
items.add(favoritesItem);
|
||||
|
||||
items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create());
|
||||
}
|
||||
|
||||
private void sendResult(int requestLinkCode) {
|
||||
private void sendResult() {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(CONTENTS_LINK_KEY, link);
|
||||
Fragment fragment = getTargetFragment();
|
||||
if (fragment != null) {
|
||||
fragment.onActivityResult(
|
||||
getTargetRequestCode(), requestLinkCode, intent);
|
||||
fragment.onActivityResult(getTargetRequestCode(), REQUEST_LINK_CODE, intent);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,21 +122,24 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
class ExpandableListAdapter extends OsmandBaseExpandableListAdapter {
|
||||
|
||||
private Context context;
|
||||
|
||||
private List<String> listDataHeader;
|
||||
private LinkedHashMap<String, List<String>> listDataChild;
|
||||
|
||||
private Drawable itemGroupIcon;
|
||||
private Drawable itemChildIcon;
|
||||
|
||||
public ExpandableListAdapter(Context context, List<String> listDataHeader,
|
||||
ExpandableListAdapter(Context context, List<String> listDataHeader,
|
||||
LinkedHashMap<String, List<String>> listChildData) {
|
||||
this.context = context;
|
||||
this.listDataHeader = listDataHeader;
|
||||
this.listDataChild = listChildData;
|
||||
|
||||
itemGroupIcon = getIcon(R.drawable.ic_action_contents,
|
||||
isNightMode() ? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light);
|
||||
itemChildIcon = getIcon(R.drawable.ic_action_list_bullet,
|
||||
isNightMode() ? R.color.route_info_unchecked_mode_icon_color : R.color.ctx_menu_nearby_routes_text_color_dark);
|
||||
itemGroupIcon = getIcon(R.drawable.ic_action_contents, nightMode
|
||||
? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light);
|
||||
itemChildIcon = getIcon(R.drawable.ic_action_list_bullet, nightMode
|
||||
? R.color.route_info_unchecked_mode_icon_color
|
||||
: R.color.ctx_menu_nearby_routes_text_color_dark);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -155,11 +159,13 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
if (convertView == null) {
|
||||
LayoutInflater infalInflater = (LayoutInflater) this.context
|
||||
.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);
|
||||
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);
|
||||
|
||||
return convertView;
|
||||
|
@ -167,11 +173,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
|
||||
@Override
|
||||
public int getChildrenCount(int groupPosition) {
|
||||
if (this.listDataChild.get(this.listDataHeader.get(groupPosition)) != null) {
|
||||
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
List<String> list = listDataChild.get(listDataHeader.get(groupPosition));
|
||||
return list == null ? 0 : list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -196,7 +199,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
if (convertView == null) {
|
||||
LayoutInflater infalInflater = (LayoutInflater) this.context
|
||||
.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);
|
||||
lblListHeader.setText(headerTitle);
|
||||
|
|
|
@ -104,7 +104,7 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
|
|||
fragment.setUsedOnMap(false);
|
||||
fragment.setArguments(args);
|
||||
fragment.setTargetFragment(WikivoyageArticleDialogFragment.this, 0);
|
||||
fragment.show(getChildFragmentManager(), WikivoyageArticleContentsFragment.TAG);
|
||||
fragment.show(getFragmentManager(), WikivoyageArticleContentsFragment.TAG);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue