14
14
import android .widget .AdapterView ;
15
15
import android .widget .AdapterView .OnItemSelectedListener ;
16
16
import android .widget .ArrayAdapter ;
17
- import android .widget .Button ;
18
- import android .widget .ProgressBar ;
19
- import android .widget .Spinner ;
20
17
import android .widget .Toast ;
21
18
import androidx .annotation .Nullable ;
22
19
import androidx .lifecycle .ViewModelProvider ;
23
20
import androidx .recyclerview .widget .LinearLayoutManager ;
24
21
import androidx .recyclerview .widget .MergeAdapter ;
25
- import androidx .recyclerview .widget .RecyclerView ;
26
- import butterknife .BindView ;
27
- import butterknife .ButterKnife ;
28
22
import fr .free .nrw .commons .R ;
29
23
import fr .free .nrw .commons .auth .SessionManager ;
24
+ import fr .free .nrw .commons .databinding .FragmentLeaderboardBinding ;
30
25
import fr .free .nrw .commons .di .CommonsDaggerSupportFragment ;
31
26
import fr .free .nrw .commons .mwapi .OkHttpJsonApiClient ;
32
27
import fr .free .nrw .commons .profile .ProfileActivity ;
44
39
*/
45
40
public class LeaderboardFragment extends CommonsDaggerSupportFragment {
46
41
47
- @ BindView (R .id .leaderboard_list )
48
- RecyclerView leaderboardListRecyclerView ;
49
-
50
- @ BindView (R .id .progressBar )
51
- ProgressBar progressBar ;
52
-
53
- @ BindView (R .id .category_spinner )
54
- Spinner categorySpinner ;
55
-
56
- @ BindView (R .id .duration_spinner )
57
- Spinner durationSpinner ;
58
-
59
- @ BindView (R .id .scroll )
60
- Button scrollButton ;
61
42
62
43
@ Inject
63
44
SessionManager sessionManager ;
@@ -110,6 +91,8 @@ public class LeaderboardFragment extends CommonsDaggerSupportFragment {
110
91
111
92
private String userName ;
112
93
94
+ private FragmentLeaderboardBinding binding ;
95
+
113
96
@ Override
114
97
public void onCreate (@ Nullable final Bundle savedInstanceState ) {
115
98
super .onCreate (savedInstanceState );
@@ -120,19 +103,18 @@ public void onCreate(@Nullable final Bundle savedInstanceState) {
120
103
121
104
@ Override
122
105
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
123
- View rootView = inflater .inflate (R .layout .fragment_leaderboard , container , false );
124
- ButterKnife .bind (this , rootView );
106
+ binding = FragmentLeaderboardBinding .inflate (inflater , container , false );
125
107
126
108
hideLayouts ();
127
109
128
110
// Leaderboard currently unimplemented in Beta flavor. Skip all API calls and disable menu
129
111
if (ConfigUtils .isBetaFlavour ()) {
130
- progressBar .setVisibility (View .GONE );
131
- scrollButton .setVisibility (View .GONE );
132
- return rootView ;
112
+ binding . progressBar .setVisibility (View .GONE );
113
+ binding . scroll .setVisibility (View .GONE );
114
+ return binding . getRoot () ;
133
115
}
134
116
135
- progressBar .setVisibility (View .VISIBLE );
117
+ binding . progressBar .setVisibility (View .VISIBLE );
136
118
setSpinners ();
137
119
138
120
/**
@@ -152,11 +134,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
152
134
153
135
setLeaderboard (duration , category , limit , offset );
154
136
155
- durationSpinner .setOnItemSelectedListener (new OnItemSelectedListener () {
137
+ binding . durationSpinner .setOnItemSelectedListener (new OnItemSelectedListener () {
156
138
@ Override
157
139
public void onItemSelected (AdapterView <?> adapterView , View view , int i , long l ) {
158
140
159
- duration = durationValues [durationSpinner .getSelectedItemPosition ()];
141
+ duration = durationValues [binding . durationSpinner .getSelectedItemPosition ()];
160
142
refreshLeaderboard ();
161
143
}
162
144
@@ -165,10 +147,10 @@ public void onNothingSelected(AdapterView<?> adapterView) {
165
147
}
166
148
});
167
149
168
- categorySpinner .setOnItemSelectedListener (new OnItemSelectedListener () {
150
+ binding . categorySpinner .setOnItemSelectedListener (new OnItemSelectedListener () {
169
151
@ Override
170
152
public void onItemSelected (AdapterView <?> adapterView , View view , int i , long l ) {
171
- category = categoryValues [categorySpinner .getSelectedItemPosition ()];
153
+ category = categoryValues [binding . categorySpinner .getSelectedItemPosition ()];
172
154
refreshLeaderboard ();
173
155
}
174
156
@@ -178,10 +160,10 @@ public void onNothingSelected(AdapterView<?> adapterView) {
178
160
});
179
161
180
162
181
- scrollButton .setOnClickListener (view -> scrollToUserRank ());
163
+ binding . scroll .setOnClickListener (view -> scrollToUserRank ());
182
164
183
165
184
- return rootView ;
166
+ return binding . getRoot () ;
185
167
}
186
168
187
169
@ Override
@@ -226,9 +208,12 @@ private void scrollToUserRank() {
226
208
if (userRank ==0 ){
227
209
Toast .makeText (getContext (),R .string .no_achievements_yet ,Toast .LENGTH_SHORT ).show ();
228
210
}else {
229
- if (Objects .requireNonNull (leaderboardListRecyclerView .getAdapter ()).getItemCount ()
211
+ if (binding == null ) {
212
+ return ;
213
+ }
214
+ if (Objects .requireNonNull (binding .leaderboardList .getAdapter ()).getItemCount ()
230
215
> userRank + 1 ) {
231
- leaderboardListRecyclerView .smoothScrollToPosition (userRank + 1 );
216
+ binding . leaderboardList .smoothScrollToPosition (userRank + 1 );
232
217
} else {
233
218
if (viewModel != null ) {
234
219
viewModel .refresh (duration , category , userRank + 1 , 0 );
@@ -247,12 +232,12 @@ private void setSpinners() {
247
232
ArrayAdapter <CharSequence > categoryAdapter = ArrayAdapter .createFromResource (getContext (),
248
233
R .array .leaderboard_categories , android .R .layout .simple_spinner_item );
249
234
categoryAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
250
- categorySpinner .setAdapter (categoryAdapter );
235
+ binding . categorySpinner .setAdapter (categoryAdapter );
251
236
252
237
ArrayAdapter <CharSequence > durationAdapter = ArrayAdapter .createFromResource (getContext (),
253
238
R .array .leaderboard_durations , android .R .layout .simple_spinner_item );
254
239
durationAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
255
- durationSpinner .setAdapter (durationAdapter );
240
+ binding . durationSpinner .setAdapter (durationAdapter );
256
241
}
257
242
258
243
/**
@@ -297,16 +282,16 @@ private void setViews(LeaderboardResponse response, String duration, String cate
297
282
UserDetailAdapter userDetailAdapter = new UserDetailAdapter (response );
298
283
MergeAdapter mergeAdapter = new MergeAdapter (userDetailAdapter , leaderboardListAdapter );
299
284
LinearLayoutManager linearLayoutManager = new LinearLayoutManager (getContext ());
300
- leaderboardListRecyclerView .setLayoutManager (linearLayoutManager );
301
- leaderboardListRecyclerView .setAdapter (mergeAdapter );
285
+ binding . leaderboardList .setLayoutManager (linearLayoutManager );
286
+ binding . leaderboardList .setAdapter (mergeAdapter );
302
287
viewModel .getListLiveData ().observe (getViewLifecycleOwner (), leaderboardListAdapter ::submitList );
303
288
viewModel .getProgressLoadStatus ().observe (getViewLifecycleOwner (), status -> {
304
289
if (Objects .requireNonNull (status ).equalsIgnoreCase (LOADING )) {
305
290
showProgressBar ();
306
291
} else if (status .equalsIgnoreCase (LOADED )) {
307
292
hideProgressBar ();
308
293
if (scrollToRank ) {
309
- leaderboardListRecyclerView .smoothScrollToPosition (userRank + 1 );
294
+ binding . leaderboardList .smoothScrollToPosition (userRank + 1 );
310
295
}
311
296
}
312
297
});
@@ -316,32 +301,32 @@ private void setViews(LeaderboardResponse response, String duration, String cate
316
301
* to hide progressbar
317
302
*/
318
303
private void hideProgressBar () {
319
- if (progressBar != null ) {
320
- progressBar .setVisibility (View .GONE );
321
- categorySpinner .setVisibility (View .VISIBLE );
322
- durationSpinner .setVisibility (View .VISIBLE );
323
- scrollButton .setVisibility (View .VISIBLE );
324
- leaderboardListRecyclerView .setVisibility (View .VISIBLE );
304
+ if (binding != null ) {
305
+ binding . progressBar .setVisibility (View .GONE );
306
+ binding . categorySpinner .setVisibility (View .VISIBLE );
307
+ binding . durationSpinner .setVisibility (View .VISIBLE );
308
+ binding . scroll .setVisibility (View .VISIBLE );
309
+ binding . leaderboardList .setVisibility (View .VISIBLE );
325
310
}
326
311
}
327
312
328
313
/**
329
314
* to show progressbar
330
315
*/
331
316
private void showProgressBar () {
332
- if (progressBar != null ) {
333
- progressBar .setVisibility (View .VISIBLE );
317
+ if (binding != null ) {
318
+ binding .progressBar .setVisibility (View .VISIBLE );
319
+ binding .scroll .setVisibility (View .INVISIBLE );
334
320
}
335
- scrollButton .setVisibility (View .INVISIBLE );
336
321
}
337
322
338
323
/**
339
324
* used to hide the layouts while fetching results from api
340
325
*/
341
326
private void hideLayouts (){
342
- categorySpinner .setVisibility (View .INVISIBLE );
343
- durationSpinner .setVisibility (View .INVISIBLE );
344
- leaderboardListRecyclerView .setVisibility (View .INVISIBLE );
327
+ binding . categorySpinner .setVisibility (View .INVISIBLE );
328
+ binding . durationSpinner .setVisibility (View .INVISIBLE );
329
+ binding . leaderboardList .setVisibility (View .INVISIBLE );
345
330
}
346
331
347
332
/**
@@ -364,7 +349,15 @@ private boolean checkAccount(){
364
349
*/
365
350
private void onError () {
366
351
ViewUtil .showLongToast (getActivity (), getResources ().getString (R .string .error_occurred ));
367
- progressBar .setVisibility (View .GONE );
352
+ if (binding !=null ) {
353
+ binding .progressBar .setVisibility (View .GONE );
354
+ }
368
355
}
369
356
357
+ @ Override
358
+ public void onDestroy () {
359
+ super .onDestroy ();
360
+ compositeDisposable .clear ();
361
+ binding = null ;
362
+ }
370
363
}
0 commit comments