1
1
package fr .free .nrw .commons .explore .depictions ;
2
2
3
+ import static android .view .View .GONE ;
4
+ import static android .view .View .VISIBLE ;
5
+
3
6
import android .content .Context ;
4
7
import android .content .res .Configuration ;
5
8
import android .os .Bundle ;
6
9
import android .view .LayoutInflater ;
7
10
import android .view .View ;
8
11
import android .view .ViewGroup ;
12
+ import android .widget .LinearLayout ;
9
13
import android .widget .ProgressBar ;
10
14
import android .widget .TextView ;
11
-
12
15
import androidx .annotation .NonNull ;
13
16
import androidx .recyclerview .widget .GridLayoutManager ;
14
17
import androidx .recyclerview .widget .LinearLayoutManager ;
15
18
import androidx .recyclerview .widget .RecyclerView ;
16
-
17
- import com .pedrogomez .renderers .RVRendererAdapter ;
18
-
19
- import java .util .ArrayList ;
20
- import java .util .List ;
21
- import java .util .Locale ;
22
-
23
- import javax .inject .Inject ;
24
-
25
19
import butterknife .BindView ;
26
20
import butterknife .ButterKnife ;
21
+ import com .pedrogomez .renderers .RVRendererAdapter ;
27
22
import fr .free .nrw .commons .R ;
28
23
import fr .free .nrw .commons .depictions .DepictedImagesActivity ;
29
24
import fr .free .nrw .commons .di .CommonsDaggerSupportFragment ;
30
25
import fr .free .nrw .commons .upload .structure .depicts .DepictedItem ;
31
26
import fr .free .nrw .commons .utils .NetworkUtils ;
32
27
import fr .free .nrw .commons .utils .ViewUtil ;
33
-
34
- import static android .view .View .GONE ;
35
- import static android .view .View .VISIBLE ;
28
+ import java .util .ArrayList ;
29
+ import java .util .List ;
30
+ import java .util .Locale ;
31
+ import javax .inject .Inject ;
36
32
37
33
/**
38
34
* Display depictions in search fragment
@@ -48,9 +44,10 @@ public class SearchDepictionsFragment extends CommonsDaggerSupportFragment imple
48
44
TextView depictionNotFound ;
49
45
@ BindView (R .id .bottomProgressBar )
50
46
ProgressBar bottomProgressBar ; int i =0 ;
51
- LinearLayoutManager layoutManager ;
47
+ RecyclerView . LayoutManager layoutManager ;
52
48
private boolean hasMoreImages = true ;
53
49
private boolean isLoading = true ;
50
+ private int PAGE_SIZE = 25 ;
54
51
@ Inject
55
52
SearchDepictionsFragmentPresenter presenter ;
56
53
private final SearchDepictionsAdapterFactory adapterFactory = new SearchDepictionsAdapterFactory (new SearchDepictionsRenderer .DepictCallback () {
@@ -67,49 +64,60 @@ public void fetchThumbnailUrlForEntity(String entityId,int position) {
67
64
68
65
});
69
66
private RVRendererAdapter <DepictedItem > depictionsAdapter ;
67
+ private boolean isLastPage ;
70
68
71
69
@ Override
72
70
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
73
71
View rootView = inflater .inflate (R .layout .fragment_browse_image , container , false );
74
72
ButterKnife .bind (this , rootView );
75
- if (getActivity ().getResources ().getConfiguration ().orientation == Configuration .ORIENTATION_PORTRAIT ) {
76
- depictionsRecyclerView .setLayoutManager (new LinearLayoutManager (getContext ()));
73
+ if (getActivity ().getResources ().getConfiguration ().orientation
74
+ == Configuration .ORIENTATION_PORTRAIT ) {
75
+ layoutManager = new LinearLayoutManager (getContext ());
77
76
} else {
78
- depictionsRecyclerView . setLayoutManager ( new GridLayoutManager (getContext (), 2 ) );
77
+ layoutManager = new GridLayoutManager (getContext (), 2 );
79
78
}
79
+ depictionsRecyclerView .setLayoutManager (layoutManager );
80
80
ArrayList <DepictedItem > items = new ArrayList <>();
81
81
depictionsAdapter = adapterFactory .create (items );
82
82
depictionsRecyclerView .setAdapter (depictionsAdapter );
83
- layoutManager = new LinearLayoutManager (getActivity ());
84
83
depictionsRecyclerView .addOnScrollListener (new RecyclerView .OnScrollListener () {
85
84
@ Override
86
85
public void onScrollStateChanged (RecyclerView recyclerView , int newState ) {
87
86
super .onScrollStateChanged (recyclerView , newState );
88
- // check if end of recycler view is reached, if yes then add more results to existing results
89
- /*if (!recyclerView.canScrollVertically(1)) {
90
- presenter.addDepictionsToList();
91
- }*/
92
87
}
93
88
94
89
@ Override
95
90
public void onScrolled (@ NonNull RecyclerView recyclerView , int dx , int dy ) {
96
91
super .onScrolled (recyclerView , dx , dy );
92
+
97
93
int visibleItemCount = layoutManager .getChildCount ();
98
94
int totalItemCount = layoutManager .getItemCount ();
99
- int firstVisibleItemPosition = layoutManager .findFirstVisibleItemPosition ();
100
-
101
- if (!isLoading && (firstVisibleItemPosition + visibleItemCount >= totalItemCount )) {
102
- isLoading = true ;
103
- presenter .updateDepictionList (presenter .getQuery ());
104
- /*if (!hasMoreImages) {
105
- progressBar.setVisibility(GONE);
106
- }*/
95
+ int firstVisibleItemPosition =0 ;
96
+ if (layoutManager instanceof GridLayoutManager ){
97
+ firstVisibleItemPosition =((GridLayoutManager ) layoutManager ).findFirstVisibleItemPosition ();
98
+ }else {
99
+ firstVisibleItemPosition =((LinearLayoutManager )layoutManager ).findFirstVisibleItemPosition ();
100
+ }
101
+
102
+ if (!isLoading && !isLastPage ) {
103
+ if ((visibleItemCount + firstVisibleItemPosition ) >= totalItemCount
104
+ && firstVisibleItemPosition >= 0
105
+ && totalItemCount >= PAGE_SIZE ) {
106
+ loadMoreItems ();
107
+ }
107
108
}
108
109
}
109
110
});
110
111
return rootView ;
111
112
}
112
113
114
+ /**
115
+ * Fetch PAGE_SIZE number of items
116
+ */
117
+ private void loadMoreItems () {
118
+ presenter .updateDepictionList (presenter .getQuery (),PAGE_SIZE );
119
+ }
120
+
113
121
@ Override
114
122
public void onAttach (Context context ) {
115
123
super .onAttach (context );
@@ -129,7 +137,7 @@ public void updateDepictionList(String query) {
129
137
handleNoInternet ();
130
138
return ;
131
139
}
132
- presenter . updateDepictionList ( query );
140
+ loadMoreItems ( );
133
141
}
134
142
135
143
/**
@@ -162,8 +170,13 @@ public void onSuccess(List<DepictedItem> mediaList) {
162
170
progressBar .setVisibility (View .GONE );
163
171
depictionNotFound .setVisibility (GONE );
164
172
bottomProgressBar .setVisibility (GONE );
173
+ int itemCount =layoutManager .getItemCount ();
165
174
depictionsAdapter .addAll (mediaList );
166
- depictionsAdapter .notifyDataSetChanged ();
175
+ if (itemCount !=0 ) {
176
+ depictionsAdapter .notifyItemRangeInserted (itemCount , mediaList .size ()-1 );
177
+ }else {
178
+ depictionsAdapter .notifyDataSetChanged ();
179
+ }
167
180
}
168
181
169
182
@ Override
@@ -193,4 +206,15 @@ public void onImageUrlFetched(String response, int position) {
193
206
depictionsAdapter .getItem (position ).setImageUrl (response );
194
207
depictionsAdapter .notifyItemChanged (position );
195
208
}
209
+
210
+ /**
211
+ * Inform the view that there are no more items to be loaded for this search query
212
+ * or reset the isLastPage for the current query
213
+ * @param isLastPage
214
+ */
215
+ @ Override
216
+ public void setIsLastPage (boolean isLastPage ) {
217
+ this .isLastPage =isLastPage ;
218
+ progressBar .setVisibility (GONE );
219
+ }
196
220
}
0 commit comments