4
4
import android .app .Activity ;
5
5
import android .content .Context ;
6
6
import android .content .pm .PackageManager ;
7
+ import android .content .res .Configuration ;
7
8
import android .os .Build ;
8
9
import android .os .Bundle ;
9
10
import android .util .Log ;
26
27
import androidx .annotation .Nullable ;
27
28
import androidx .cardview .widget .CardView ;
28
29
import androidx .databinding .DataBindingUtil ;
30
+ import androidx .fragment .app .Fragment ;
31
+ import androidx .fragment .app .FragmentTransaction ;
29
32
import androidx .navigation .NavController ;
30
33
import androidx .navigation .Navigation ;
31
34
import androidx .recyclerview .widget .RecyclerView ;
36
39
import io .syslogic .github .Constants ;
37
40
import io .syslogic .github .databinding .FragmentRepositoriesBinding ;
38
41
import io .syslogic .github .databinding .CardviewRepositoryBinding ;
42
+ import io .syslogic .github .fragment .RepositoryFragment ;
39
43
import io .syslogic .github .model .PagerState ;
40
44
import io .syslogic .github .model .RateLimit ;
41
45
import io .syslogic .github .model .RateLimits ;
@@ -126,7 +130,7 @@ public void fetchPage(final int pageNumber) {
126
130
String accessToken = getAccessToken ();
127
131
if (this .getPagerState () != null && !this .getPagerState ().getIsOffline ()) {
128
132
129
- /* updating the pager data-binding */
133
+ /* Updating the pager data-binding */
130
134
setPagerState (pageNumber , true , null );
131
135
132
136
Call <Repositories > api = GithubClient .getRepositories (accessToken , this .queryString ,"stars" ,"desc" , pageNumber );
@@ -154,7 +158,7 @@ public void onResponse(@NonNull Call<Repositories> call, @NonNull Response<Repos
154
158
getItems ().addAll (items .getRepositories ());
155
159
notifyItemRangeChanged (positionStart , getItemCount ());
156
160
157
- /* updating the pager data-binding */
161
+ /* Updating the pager data-binding */
158
162
setPagerState (pageNumber , false , items .getTotalCount ());
159
163
}
160
164
break ;
@@ -165,7 +169,7 @@ public void onResponse(@NonNull Call<Repositories> call, @NonNull Response<Repos
165
169
if (response .errorBody () != null ) {
166
170
logError (response .errorBody ());
167
171
168
- /* updating the pager data-binding */
172
+ /* Updating the pager data-binding */
169
173
setPagerState (pageNumber , false , null );
170
174
}
171
175
break ;
@@ -175,7 +179,7 @@ public void onResponse(@NonNull Call<Repositories> call, @NonNull Response<Repos
175
179
if (response .errorBody () != null ) {
176
180
logError (response .errorBody ());
177
181
178
- /* updating the pager data-binding */
182
+ /* Updating the pager data-binding */
179
183
setPagerState (pageNumber , false , null );
180
184
181
185
resetOnScrollListener ();
@@ -205,7 +209,7 @@ private String getAccessToken() {
205
209
if (activity .checkSelfPermission (Manifest .permission .ACCOUNT_MANAGER ) == PackageManager .PERMISSION_GRANTED ) {
206
210
return TokenHelper .getAccessToken (activity );
207
211
} else {
208
- /* for testing purposes only: */
212
+ /* For testing purposes only: */
209
213
if (mDebug ) {return TokenHelper .getAccessToken (activity );}
210
214
else {return null ;}
211
215
}
@@ -219,7 +223,7 @@ void clearItems() {
219
223
notifyItemRangeChanged (0 , getItemCount ());
220
224
}
221
225
222
- /** reset the scroll listener. */
226
+ /** Reset the scroll listener. */
223
227
protected void resetOnScrollListener () {
224
228
if (this .mRecyclerView .getAdapter () != null ) {
225
229
ScrollListener listener = ((RepositoriesLinearView ) this .mRecyclerView ).getOnScrollListener ();
@@ -262,23 +266,19 @@ public void onResponse(@NonNull Call<RateLimits> call, @NonNull Response<RateLim
262
266
RateLimits items = response .body ();
263
267
RateLimit limit = null ;
264
268
switch (resourceName ) {
265
- case "graphql" :
266
- limit = items .getResources ().getGraphql ();
267
- break ;
268
- case "search" :
269
- limit = items .getResources ().getSearch ();
270
- break ;
271
- case "core" :
272
- limit = items .getResources ().getCore ();
273
- break ;
269
+ case "graphql" : limit = items .getResources ().getGraphql (); break ;
270
+ case "search" : limit = items .getResources ().getSearch (); break ;
271
+ case "core" : limit = items .getResources ().getCore (); break ;
274
272
}
273
+
274
+ /* For testing purposes only: */
275
275
if (limit != null && BuildConfig .DEBUG ) {
276
276
long seconds = (long ) Math .ceil ((new Date (limit .getReset () * 1000 ).getTime () - Math .ceil (new Date ().getTime ()) / 1000 ));
277
277
String text = String .format (Locale .getDefault (), "%s quota: %d / %d. reset in %d seconds." , resourceName , limit .getRemaining (), limit .getLimit (), seconds );
278
278
Toast .makeText (getContext (), text , Toast .LENGTH_SHORT ).show ();
279
279
}
280
280
281
- /* possible border-case: */
281
+ /* Possible border-case: */
282
282
if (limit != null && limit .getRemaining () > 0 ) {
283
283
Toast .makeText (getContext (), "the " + resourceName + " quota was reset already" , Toast .LENGTH_SHORT ).show ();
284
284
}
@@ -287,9 +287,7 @@ public void onResponse(@NonNull Call<RateLimits> call, @NonNull Response<RateLim
287
287
288
288
@ Override
289
289
public void onFailure (@ NonNull Call <RateLimits > call , @ NonNull Throwable t ) {
290
- if (mDebug ) {
291
- Log .e (LOG_TAG , "" + t .getMessage ());
292
- }
290
+ if (mDebug ) {Log .e (LOG_TAG , "" + t .getMessage ());}
293
291
}
294
292
});
295
293
}
@@ -348,13 +346,30 @@ public void onClick(@NonNull View viewHolder) {
348
346
RepositoriesLinearView mRecyclerView = (RepositoriesLinearView ) viewHolder .getParent ();
349
347
BaseActivity activity = (BaseActivity ) mRecyclerView .getContext ();
350
348
FragmentRepositoriesBinding databinding = (FragmentRepositoriesBinding ) activity .getFragmentDataBinding ();
349
+ int orientation = activity .getResources ().getConfiguration ().orientation ;
351
350
if (databinding != null ) {
352
351
View layout = databinding .getRoot ();
353
- Bundle args = new Bundle ();
354
352
Repository item = getDataBinding ().getItem ();
355
- args .putLong (Constants .ARGUMENT_ITEM_ID , item .getId ());
356
353
NavController controller = Navigation .findNavController (layout );
357
- controller .navigate (R .id .action_repositoriesFragment_to_repositoryFragment , args );
354
+ switch (orientation ) {
355
+ //noinspection deprecation
356
+ case Configuration .ORIENTATION_SQUARE :
357
+ case Configuration .ORIENTATION_UNDEFINED :
358
+ case Configuration .ORIENTATION_PORTRAIT : {
359
+ Bundle args = new Bundle ();
360
+ args .putLong (Constants .ARGUMENT_ITEM_ID , item .getId ());
361
+ controller .navigate (R .id .action_repositoriesFragment_to_repositoryFragment , args );
362
+ break ;
363
+ }
364
+ case Configuration .ORIENTATION_LANDSCAPE : {
365
+ int layoutId = databinding .layoutRepository .layoutRepository .getId ();
366
+ RepositoryFragment fragment = RepositoryFragment .newInstance (item .getId ());
367
+ FragmentTransaction ft = activity .getSupportFragmentManager ().beginTransaction ();
368
+ ft .replace (layoutId , fragment );
369
+ ft .commit ();
370
+ break ;
371
+ }
372
+ }
358
373
}
359
374
}
360
375
0 commit comments