Skip to content

Commit bf50e74

Browse files
ujjwalagrawal17misaochan
authored andcommitted
Fixes #1801 (Media details view for Browse is lacking author field) in 2.8-release branch (#1810)
* In media search results, rotating screen triggers crash fixed #1753 * Updated API to get Author name too * Crash fixed due to notifyDataSetChange * search API duplicate images fixed
1 parent a5d4e8f commit bf50e74

File tree

9 files changed

+154
-23
lines changed

9 files changed

+154
-23
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<activity
115115
android:name=".explore.SearchActivity"
116116
android:label="@string/title_activity_search"
117+
android:configChanges="orientation|keyboardHidden"
117118
android:parentActivityName=".contributions.ContributionsActivity"
118119
/>
119120

app/src/main/java/fr/free/nrw/commons/category/CategoryDetailsActivity.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,24 @@ public void onBackPressed() {
250250
}
251251
super.onBackPressed();
252252
}
253+
254+
/**
255+
* This method is called on success of API call for Images inside a category.
256+
* The viewpager will notified that number of items have changed.
257+
*/
258+
public void viewPagerNotifyDataSetChanged() {
259+
if (mediaDetails!=null){
260+
mediaDetails.notifyDataSetChanged();
261+
}
262+
}
263+
264+
/**
265+
* This method is called when viewPager has reached its end.
266+
* Fetches more images using search query and adds it to the grid view and viewpager adapter
267+
*/
268+
public void requestMoreImages() {
269+
if (categoryImagesListFragment!=null){
270+
categoryImagesListFragment.fetchMoreImagesViewPager();
271+
}
272+
}
253273
}

app/src/main/java/fr/free/nrw/commons/category/CategoryImagesActivity.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ public Media getMediaAtPosition(int i) {
170170
}
171171
}
172172

173+
/**
174+
* This method is called on success of API call for featured Images.
175+
* The viewpager will notified that number of items have changed.
176+
*/
177+
public void viewPagerNotifyDataSetChanged() {
178+
if (mediaDetails!=null){
179+
mediaDetails.notifyDataSetChanged();
180+
}
181+
}
182+
173183
/**
174184
* This method is called on from getCount of MediaDetailPagerFragment
175185
* The viewpager will contain same number of media items as that of media elements in adapter.
@@ -236,4 +246,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
236246
return super.onOptionsItemSelected(item);
237247
}
238248
}
249+
250+
/**
251+
* This method is called when viewPager has reached its end.
252+
* Fetches more images using search query and adds it to the gridView and viewpager adapter
253+
*/
254+
public void requestMoreImages() {
255+
if (categoryImagesListFragment!=null){
256+
categoryImagesListFragment.fetchMoreImagesViewPager();
257+
}
258+
}
239259
}

app/src/main/java/fr/free/nrw/commons/category/CategoryImagesListFragment.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun
190190
});
191191
}
192192

193+
/**
194+
* This method is called when viewPager has reached its end.
195+
* Fetches more images for the category and adds it to the grid view and viewpager adapter
196+
*/
197+
public void fetchMoreImagesViewPager(){
198+
if (hasMoreImages && !isLoading) {
199+
isLoading = true;
200+
fetchMoreImages();
201+
}
202+
if (!hasMoreImages){
203+
progressBar.setVisibility(GONE);
204+
}
205+
}
206+
193207
/**
194208
* Fetches more images for the category and adds it to the grid view adapter
195209
*/
@@ -228,8 +242,17 @@ private void handleSuccess(List<Media> collection) {
228242
return;
229243
}
230244
gridAdapter.addItems(collection);
245+
try {
246+
((CategoryImagesActivity) getContext()).viewPagerNotifyDataSetChanged();
247+
}catch (Exception e){
248+
e.printStackTrace();
249+
}
250+
try {
251+
((CategoryDetailsActivity) getContext()).viewPagerNotifyDataSetChanged();
252+
}catch (Exception e){
253+
e.printStackTrace();
254+
}
231255
}
232-
233256
progressBar.setVisibility(GONE);
234257
isLoading = false;
235258
statusTextView.setVisibility(GONE);

app/src/main/java/fr/free/nrw/commons/explore/SearchActivity.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons.explore;
22

3+
import android.content.res.Configuration;
34
import android.database.DataSetObserver;
45
import android.os.Bundle;
56
import android.support.design.widget.TabLayout;
@@ -52,6 +53,7 @@ public class SearchActivity extends NavigationBaseActivity implements MediaDetai
5253
private FragmentManager supportFragmentManager;
5354
private MediaDetailPagerFragment mediaDetails;
5455
ViewPagerAdapter viewPagerAdapter;
56+
private String query;
5557

5658
@Override
5759
protected void onCreate(Bundle savedInstanceState) {
@@ -103,6 +105,7 @@ public void setTabs() {
103105
.debounce(500, TimeUnit.MILLISECONDS)
104106
.observeOn(AndroidSchedulers.mainThread())
105107
.subscribe( query -> {
108+
this.query = query.toString();
106109
//update image list
107110
if (!TextUtils.isEmpty(query)) {
108111
viewPager.setVisibility(View.VISIBLE);
@@ -144,7 +147,16 @@ public int getTotalMediaCount() {
144147
*/
145148
@Override
146149
public void notifyDatasetChanged() {
150+
}
147151

152+
/**
153+
* This method is called on success of API call for image Search.
154+
* The viewpager will notified that number of items have changed.
155+
*/
156+
public void viewPagerNotifyDataSetChanged() {
157+
if (mediaDetails!=null){
158+
mediaDetails.notifyDataSetChanged();
159+
}
148160
}
149161

150162
/**
@@ -194,6 +206,10 @@ public void onSearchImageClicked(int index) {
194206
mediaDetails.showImage(index);
195207
forceInitBackButton();
196208
}
209+
@Override
210+
public void onConfigurationChanged(Configuration newConfig) {
211+
super.onConfigurationChanged(newConfig);
212+
}
197213

198214
/**
199215
* This method is called on Screen Rotation
@@ -240,4 +256,14 @@ public void updateText(String query) {
240256
// https://stackoverflow.com/questions/6117967/how-to-remove-focus-without-setting-focus-to-another-control/15481511
241257
viewPager.requestFocus();
242258
}
259+
260+
/**
261+
* This method is called when viewPager has reached its end.
262+
* Fetches more images using search query and adds it to the recycler view and viewpager adapter
263+
*/
264+
public void requestMoreImages() {
265+
if (searchImageFragment!=null){
266+
searchImageFragment.addImagesToList(query);
267+
}
268+
}
243269
}

app/src/main/java/fr/free/nrw/commons/explore/images/SearchImageFragment.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import android.view.ViewGroup;
1414
import android.widget.ProgressBar;
1515
import android.widget.TextView;
16+
import android.widget.Toast;
17+
1618
import com.pedrogomez.renderers.RVRendererAdapter;
1719
import java.util.ArrayList;
1820
import java.util.Date;
@@ -156,10 +158,15 @@ public void addImagesToList(String query) {
156158
* @param mediaList List of media to be added
157159
*/
158160
private void handlePaginationSuccess(List<Media> mediaList) {
159-
queryList.addAll(mediaList);
160161
progressBar.setVisibility(View.GONE);
161-
imagesAdapter.addAll(mediaList);
162-
imagesAdapter.notifyDataSetChanged();
162+
if (mediaList.size()!=0){
163+
if (!queryList.get(queryList.size()-1).getFilename().equals(mediaList.get(mediaList.size()-1).getFilename())) {
164+
queryList.addAll(mediaList);
165+
imagesAdapter.addAll(mediaList);
166+
imagesAdapter.notifyDataSetChanged();
167+
((SearchActivity)getContext()).viewPagerNotifyDataSetChanged();
168+
}
169+
}
163170
}
164171

165172

@@ -179,6 +186,7 @@ private void handleSuccess(List<Media> mediaList) {
179186
progressBar.setVisibility(View.GONE);
180187
imagesAdapter.addAll(mediaList);
181188
imagesAdapter.notifyDataSetChanged();
189+
((SearchActivity)getContext()).viewPagerNotifyDataSetChanged();
182190

183191
// check if user is waiting for 5 seconds if yes then save search query to history.
184192
Handler handler = new Handler();
@@ -193,7 +201,6 @@ private void handleSuccess(List<Media> mediaList) {
193201
private void handleError(Throwable throwable) {
194202
Timber.e(throwable, "Error occurred while loading queried images");
195203
try {
196-
initErrorView();
197204
ViewUtil.showSnackbar(imagesRecyclerView, R.string.error_loading_images);
198205
}catch (Exception e){
199206
e.printStackTrace();
@@ -239,7 +246,7 @@ public Media getImageAtPosition(int i) {
239246
return null;
240247
}
241248
else {
242-
return new Media(imagesAdapter.getItem(i).getFilename());
249+
return imagesAdapter.getItem(i);
243250
}
244251
}
245252
}

app/src/main/java/fr/free/nrw/commons/explore/images/SearchImagesRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ interface ImageClickedListener {
6565
*/
6666
private void setAuthorView(Media item, TextView author) {
6767
if (item.getCreator() != null && !item.getCreator().equals("")) {
68-
author.setVisibility(View.GONE);
68+
author.setVisibility(View.VISIBLE);
6969
String uploadedByTemplate = getContext().getString(R.string.image_uploaded_by);
7070
author.setText(String.format(uploadedByTemplate, item.getCreator()));
7171
} else {
72-
author.setVisibility(View.VISIBLE);
72+
author.setVisibility(View.GONE);
7373
}
7474
}
7575
}

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
import fr.free.nrw.commons.Media;
3636
import fr.free.nrw.commons.R;
3737
import fr.free.nrw.commons.auth.SessionManager;
38+
import fr.free.nrw.commons.category.CategoryDetailsActivity;
39+
import fr.free.nrw.commons.category.CategoryImagesActivity;
3840
import fr.free.nrw.commons.contributions.Contribution;
3941
import fr.free.nrw.commons.contributions.ContributionsActivity;
4042
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
43+
import fr.free.nrw.commons.explore.SearchActivity;
4144
import fr.free.nrw.commons.mwapi.MediaWikiApi;
4245
import fr.free.nrw.commons.utils.ImageUtils;
4346
import timber.log.Timber;
@@ -62,6 +65,7 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
6265
ViewPager pager;
6366
private Boolean editable;
6467
private boolean isFeaturedImage;
68+
MediaDetailAdapter adapter;
6569

6670
public MediaDetailPagerFragment() {
6771
this(false, false);
@@ -81,7 +85,7 @@ public View onCreateView(LayoutInflater inflater,
8185
ButterKnife.bind(this,view);
8286
pager.addOnPageChangeListener(this);
8387

84-
final MediaDetailAdapter adapter = new MediaDetailAdapter(getChildFragmentManager());
88+
adapter = new MediaDetailAdapter(getChildFragmentManager());
8589

8690
if (savedInstanceState != null) {
8791
final int pageNumber = savedInstanceState.getInt("current-page");
@@ -269,11 +273,35 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
269273

270274
public void showImage(int i) {
271275
Handler handler = new Handler();
272-
handler.postDelayed(() -> pager.setCurrentItem(i), 10);
276+
handler.postDelayed(() -> pager.setCurrentItem(i), 5);
277+
}
278+
279+
/**
280+
* The method notify the viewpager that number of items have changed.
281+
*/
282+
public void notifyDataSetChanged(){
283+
adapter.notifyDataSetChanged();
273284
}
274285

275286
@Override
276287
public void onPageScrolled(int i, float v, int i2) {
288+
if (i+1 >= adapter.getCount()){
289+
try{
290+
((CategoryImagesActivity) getContext()).requestMoreImages();
291+
}catch (Exception e){
292+
e.printStackTrace();
293+
}
294+
try{
295+
((CategoryDetailsActivity) getContext()).requestMoreImages();
296+
}catch (Exception e){
297+
e.printStackTrace();
298+
}
299+
try{
300+
((SearchActivity) getContext()).requestMoreImages();
301+
}catch (Exception e){
302+
e.printStackTrace();
303+
}
304+
}
277305
getActivity().supportInvalidateOptionsMenu();
278306
}
279307

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -736,17 +736,21 @@ public List<Media> getCategoryImages(String categoryName) {
736736
@NonNull
737737
public List<Media> searchImages(String query, int offset) {
738738
List<CustomApiResult> imageNodes = null;
739+
List<CustomApiResult> authorNodes = null;
740+
CustomApiResult customApiResult;
739741
try {
740-
imageNodes = api.action("query")
742+
customApiResult= api.action("query")
741743
.param("format", "xml")
742-
.param("list", "search")
743-
.param("srwhat", "text")
744-
.param("srnamespace", "6")
745-
.param("srlimit", "25")
746-
.param("sroffset",offset)
747-
.param("srsearch", query)
748-
.get()
749-
.getNodes("/api/query/search/p/@title");
744+
.param("generator", "search")
745+
.param("gsrwhat", "text")
746+
.param("gsrnamespace", "6")
747+
.param("gsrlimit", "25")
748+
.param("gsroffset",offset)
749+
.param("gsrsearch", query)
750+
.param("prop", "imageinfo")
751+
.get();
752+
imageNodes= customApiResult.getNodes("/api/query/pages/page/@title");
753+
authorNodes= customApiResult.getNodes("/api/query/pages/page/imageinfo/ii/@user");
750754
} catch (IOException e) {
751755
Timber.e("Failed to obtain searchImages", e);
752756
}
@@ -756,11 +760,13 @@ public List<Media> searchImages(String query, int offset) {
756760
}
757761

758762
List<Media> images = new ArrayList<>();
759-
for (CustomApiResult imageNode : imageNodes) {
760-
String imgName = imageNode.getDocument().getTextContent();
761-
images.add(new Media(imgName));
762-
}
763763

764+
for (int i=0; i< imageNodes.size();i++){
765+
String imgName = imageNodes.get(i).getDocument().getTextContent();
766+
Media media = new Media(imgName);
767+
media.setCreator(authorNodes.get(i).getDocument().getTextContent());
768+
images.add(media);
769+
}
764770
return images;
765771
}
766772

0 commit comments

Comments
 (0)