Skip to content

Commit d305797

Browse files
Implemented butterknife in MediaDetailFragment [issue commons-app#1491]
1 parent 7e12346 commit d305797

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

+59-37
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import android.widget.ScrollView;
1515
import android.widget.TextView;
1616

17+
import butterknife.BindView;
18+
import butterknife.ButterKnife;
19+
import butterknife.OnClick;
1720
import java.io.IOException;
1821
import java.text.SimpleDateFormat;
1922
import java.util.ArrayList;
@@ -48,24 +51,35 @@ public static MediaDetailFragment forMedia(int index, boolean editable) {
4851
return mf;
4952
}
5053

51-
private MediaWikiImageView image;
52-
private MediaDetailSpacer spacer;
5354
private int initialListTop = 0;
5455

55-
private TextView title;
56-
private TextView desc;
57-
private TextView license;
58-
private TextView coordinates;
59-
private TextView uploadedDate;
60-
private LinearLayout categoryContainer;
61-
private ScrollView scrollView;
56+
//Changed the access specifiers of these view as well [ButterKnife wont let us bind private variables]
57+
@BindView(R.id.mediaDetailImage)
58+
MediaWikiImageView image;
59+
@BindView(R.id.mediaDetailSpacer)
60+
MediaDetailSpacer spacer;
61+
@BindView(R.id.mediaDetailTitle)
62+
TextView title;
63+
@BindView(R.id.mediaDetailDesc)
64+
TextView desc;
65+
@BindView(R.id.mediaDetailLicense)
66+
TextView license;
67+
@BindView(R.id.mediaDetailCoordinates)
68+
TextView coordinates;
69+
@BindView(R.id.mediaDetailuploadeddate)
70+
TextView uploadedDate;
71+
@BindView(R.id.mediaDetailCategoryContainer)
72+
LinearLayout categoryContainer;
73+
@BindView(R.id.mediaDetailScrollView)
74+
ScrollView scrollView;
75+
6276
private ArrayList<String> categoryNames;
6377
private boolean categoriesLoaded = false;
6478
private boolean categoriesPresent = false;
6579
private ViewTreeObserver.OnGlobalLayoutListener layoutListener; // for layout stuff, only used once!
6680
private ViewTreeObserver.OnScrollChangedListener scrollListener;
6781
DataSetObserver dataObserver;
68-
private AsyncTask<Void,Void,Boolean> detailFetchTask;
82+
private AsyncTask<Void, Void, Boolean> detailFetchTask;
6983
private LicenseList licenseList;
7084

7185
@Override
@@ -100,17 +114,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
100114

101115
final View view = inflater.inflate(R.layout.fragment_media_detail, container, false);
102116

103-
image = (MediaWikiImageView) view.findViewById(R.id.mediaDetailImage);
104-
scrollView = (ScrollView) view.findViewById(R.id.mediaDetailScrollView);
105-
106-
// Detail consists of a list view with main pane in header view, plus category list.
107-
spacer = (MediaDetailSpacer) view.findViewById(R.id.mediaDetailSpacer);
108-
title = (TextView) view.findViewById(R.id.mediaDetailTitle);
109-
desc = (TextView) view.findViewById(R.id.mediaDetailDesc);
110-
license = (TextView) view.findViewById(R.id.mediaDetailLicense);
111-
coordinates = (TextView) view.findViewById(R.id.mediaDetailCoordinates);
112-
uploadedDate = (TextView) view.findViewById(R.id.mediaDetailuploadeddate);
113-
categoryContainer = (LinearLayout) view.findViewById(R.id.mediaDetailCategoryContainer);
117+
ButterKnife.bind(this, view);
114118

115119
licenseList = new LicenseList(getActivity());
116120

@@ -271,22 +275,10 @@ private void rebuildCatList() {
271275

272276
private View buildCatLabel(String cat) {
273277
final String catName = cat;
274-
final View item = getLayoutInflater(null).inflate(R.layout.detail_category_item, null, false);
275-
final TextView textView = (TextView)item.findViewById(R.id.mediaDetailCategoryItemText);
276-
277-
textView.setText(cat);
278-
if (categoriesLoaded && categoriesPresent) {
279-
textView.setOnClickListener(new View.OnClickListener() {
280-
@Override
281-
public void onClick(View view) {
282-
String selectedCategoryTitle = "Category:" + catName;
283-
Intent viewIntent = new Intent();
284-
viewIntent.setAction(Intent.ACTION_VIEW);
285-
viewIntent.setData(Utils.uriForWikiPage(selectedCategoryTitle));
286-
startActivity(viewIntent);
287-
}
288-
});
289-
}
278+
final View item = getLayoutInflater(null)
279+
.inflate(R.layout.detail_category_item, null, false);
280+
CategoryItemViewHolder categoryItemViewHolder = new CategoryItemViewHolder(item);
281+
categoryItemViewHolder.init(catName);
290282
return item;
291283
}
292284

@@ -343,4 +335,34 @@ private String prettyUploadedDate(Media media) {
343335
private String prettyCoordinates(Media media) {
344336
return media.getCoordinates();
345337
}
338+
339+
/**
340+
* Holds the individual category item views
341+
*/
342+
343+
public class CategoryItemViewHolder {
344+
345+
@BindView(R.id.mediaDetailCategoryItemText)
346+
TextView mediaDetailCategoryItemText;
347+
348+
private String categoryTitle;
349+
350+
public CategoryItemViewHolder(View item) {
351+
ButterKnife.bind(this, item);
352+
}
353+
354+
public void init(String categoryName) {
355+
this.categoryTitle = categoryName;
356+
mediaDetailCategoryItemText.setText(categoryName);
357+
}
358+
359+
@OnClick(R.id.mediaDetailCategoryItemText)
360+
public void onCategoryItemClicked() {
361+
String selectedCategoryTitle = "Category:" + categoryTitle;
362+
Intent viewIntent = new Intent();
363+
viewIntent.setAction(Intent.ACTION_VIEW);
364+
viewIntent.setData(Utils.uriForWikiPage(selectedCategoryTitle));
365+
startActivity(viewIntent);
366+
}
367+
}
346368
}

0 commit comments

Comments
 (0)