Skip to content

Commit 9696c72

Browse files
committed
considering landscape layout.
1 parent 1523ffa commit 9696c72

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

mobile/src/main/java/io/syslogic/github/fragment/RepositoriesFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ public void onNetworkAvailable() {
159159
this.getDataBinding().setPagerState(pagerState);
160160
}
161161

162-
/* when being online for the first time, adapter is null. */
162+
/* When being online for the first time, adapter is null. */
163163
RepositoriesAdapter adapter = ((RepositoriesAdapter) this.getDataBinding().recyclerviewRepositories.getAdapter());
164164
if (adapter == null) {
165-
/* needs to run on UiThread */
165+
/* Needs to run on UiThread */
166166
requireActivity().runOnUiThread(() -> {
167167
String queryString = getDataBinding().recyclerviewRepositories.getQueryString();
168168
if (queryString == null) {
@@ -176,7 +176,7 @@ public void onNetworkAvailable() {
176176
}
177177
});
178178
} else if (adapter.getItemCount() == 0) {
179-
/* if required, fetch page 1 */
179+
/* If required, fetch page 1 */
180180
adapter.fetchPage(1);
181181
}
182182
}

mobile/src/main/java/io/syslogic/github/fragment/RepositoryFragment.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public class RepositoryFragment extends BaseFragment implements TokenCallback {
7171
/** Constructor */
7272
public RepositoryFragment() {}
7373

74+
@NonNull
75+
public static RepositoryFragment newInstance(long itemId) {
76+
RepositoryFragment fragment = new RepositoryFragment();
77+
Bundle args = new Bundle();
78+
args.putLong(Constants.ARGUMENT_ITEM_ID, itemId);
79+
fragment.setArguments(args);
80+
return fragment;
81+
}
82+
7483
@Override
7584
@RequiresApi(api = Build.VERSION_CODES.N)
7685
public void onCreate(@Nullable Bundle savedInstanceState) {
@@ -135,7 +144,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
135144
public void onNothingSelected(AdapterView<?> parent) {}
136145
});
137146

138-
/* the download button; TODO: consider tarball? */
147+
/* The download button; TODO: consider tarball? */
139148
this.mDataBinding.toolbarDownload.buttonDownload.setOnClickListener(view -> {
140149
String branch = getDataBinding().toolbarDownload.spinnerBranch.getSelectedItem().toString();
141150
downloadBranchAsZip(branch);
@@ -256,11 +265,11 @@ public void onResponse(@NonNull Call<ArrayList<Branch>> call, @NonNull Response<
256265
case 200: {
257266
if (response.body() != null && getContext() != null) {
258267

259-
/* updating the branches */
268+
/* Updating the branches */
260269
ArrayList<Branch> items = response.body();
261270
getDataBinding().setBranches(items);
262271

263-
/* debug output */
272+
/* Debug output */
264273
if (mDebug && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
265274
ArrayList<String> elements = new ArrayList<>();
266275
for (int i = 0; i < items.size(); i++) {
@@ -271,7 +280,7 @@ public void onResponse(@NonNull Call<ArrayList<Branch>> call, @NonNull Response<
271280
Log.d(LOG_TAG, String.format(formatString, repoName, items.size(), String.join(", ", elements)));
272281
}
273282

274-
/* attempting to select branch master */
283+
/* Attempting to select branch master */
275284
int defaultIndex = -1;
276285
for (int i = 0; i < items.size(); i++) {
277286
if (items.get(i).getName().equals("main") && items.get(i).getName().equals("master")) {
@@ -283,7 +292,7 @@ public void onResponse(@NonNull Call<ArrayList<Branch>> call, @NonNull Response<
283292
}
284293
}
285294

286-
/* debug output */
295+
/* Debug output */
287296
if (mDebug && defaultIndex == -1) {
288297
Log.d(LOG_TAG, repoName + " has no master branch.");
289298
}

mobile/src/main/java/io/syslogic/github/recyclerview/RepositoriesAdapter.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.Activity;
55
import android.content.Context;
66
import android.content.pm.PackageManager;
7+
import android.content.res.Configuration;
78
import android.os.Build;
89
import android.os.Bundle;
910
import android.util.Log;
@@ -26,6 +27,8 @@
2627
import androidx.annotation.Nullable;
2728
import androidx.cardview.widget.CardView;
2829
import androidx.databinding.DataBindingUtil;
30+
import androidx.fragment.app.Fragment;
31+
import androidx.fragment.app.FragmentTransaction;
2932
import androidx.navigation.NavController;
3033
import androidx.navigation.Navigation;
3134
import androidx.recyclerview.widget.RecyclerView;
@@ -36,6 +39,7 @@
3639
import io.syslogic.github.Constants;
3740
import io.syslogic.github.databinding.FragmentRepositoriesBinding;
3841
import io.syslogic.github.databinding.CardviewRepositoryBinding;
42+
import io.syslogic.github.fragment.RepositoryFragment;
3943
import io.syslogic.github.model.PagerState;
4044
import io.syslogic.github.model.RateLimit;
4145
import io.syslogic.github.model.RateLimits;
@@ -126,7 +130,7 @@ public void fetchPage(final int pageNumber) {
126130
String accessToken = getAccessToken();
127131
if (this.getPagerState() != null && !this.getPagerState().getIsOffline()) {
128132

129-
/* updating the pager data-binding */
133+
/* Updating the pager data-binding */
130134
setPagerState(pageNumber, true, null);
131135

132136
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
154158
getItems().addAll(items.getRepositories());
155159
notifyItemRangeChanged(positionStart, getItemCount());
156160

157-
/* updating the pager data-binding */
161+
/* Updating the pager data-binding */
158162
setPagerState(pageNumber, false, items.getTotalCount());
159163
}
160164
break;
@@ -165,7 +169,7 @@ public void onResponse(@NonNull Call<Repositories> call, @NonNull Response<Repos
165169
if (response.errorBody() != null) {
166170
logError(response.errorBody());
167171

168-
/* updating the pager data-binding */
172+
/* Updating the pager data-binding */
169173
setPagerState(pageNumber, false, null);
170174
}
171175
break;
@@ -175,7 +179,7 @@ public void onResponse(@NonNull Call<Repositories> call, @NonNull Response<Repos
175179
if (response.errorBody() != null) {
176180
logError(response.errorBody());
177181

178-
/* updating the pager data-binding */
182+
/* Updating the pager data-binding */
179183
setPagerState(pageNumber, false, null);
180184

181185
resetOnScrollListener();
@@ -205,7 +209,7 @@ private String getAccessToken() {
205209
if (activity.checkSelfPermission(Manifest.permission.ACCOUNT_MANAGER) == PackageManager.PERMISSION_GRANTED) {
206210
return TokenHelper.getAccessToken(activity);
207211
} else {
208-
/* for testing purposes only: */
212+
/* For testing purposes only: */
209213
if (mDebug) {return TokenHelper.getAccessToken(activity);}
210214
else {return null;}
211215
}
@@ -219,7 +223,7 @@ void clearItems() {
219223
notifyItemRangeChanged(0, getItemCount());
220224
}
221225

222-
/** reset the scroll listener. */
226+
/** Reset the scroll listener. */
223227
protected void resetOnScrollListener() {
224228
if (this.mRecyclerView.getAdapter() != null) {
225229
ScrollListener listener = ((RepositoriesLinearView) this.mRecyclerView).getOnScrollListener();
@@ -262,23 +266,19 @@ public void onResponse(@NonNull Call<RateLimits> call, @NonNull Response<RateLim
262266
RateLimits items = response.body();
263267
RateLimit limit = null;
264268
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;
274272
}
273+
274+
/* For testing purposes only: */
275275
if (limit != null && BuildConfig.DEBUG) {
276276
long seconds = (long) Math.ceil((new Date(limit.getReset() * 1000).getTime() - Math.ceil(new Date().getTime()) / 1000));
277277
String text = String.format(Locale.getDefault(), "%s quota: %d / %d. reset in %d seconds.", resourceName, limit.getRemaining(), limit.getLimit(), seconds);
278278
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
279279
}
280280

281-
/* possible border-case: */
281+
/* Possible border-case: */
282282
if (limit != null && limit.getRemaining() > 0) {
283283
Toast.makeText(getContext(), "the " + resourceName + " quota was reset already", Toast.LENGTH_SHORT).show();
284284
}
@@ -287,9 +287,7 @@ public void onResponse(@NonNull Call<RateLimits> call, @NonNull Response<RateLim
287287

288288
@Override
289289
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());}
293291
}
294292
});
295293
}
@@ -348,13 +346,30 @@ public void onClick(@NonNull View viewHolder) {
348346
RepositoriesLinearView mRecyclerView = (RepositoriesLinearView) viewHolder.getParent();
349347
BaseActivity activity = (BaseActivity) mRecyclerView.getContext();
350348
FragmentRepositoriesBinding databinding = (FragmentRepositoriesBinding) activity.getFragmentDataBinding();
349+
int orientation = activity.getResources().getConfiguration().orientation;
351350
if (databinding != null) {
352351
View layout = databinding.getRoot();
353-
Bundle args = new Bundle();
354352
Repository item = getDataBinding().getItem();
355-
args.putLong(Constants.ARGUMENT_ITEM_ID, item.getId());
356353
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+
}
358373
}
359374
}
360375

0 commit comments

Comments
 (0)