Skip to content

Commit

Permalink
endless scrolling works, but pager state not yet reflects the adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
syslogic committed Aug 7, 2023
1 parent 6643ec3 commit 35d58e5
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
abstract protected void setDataBinding(@NonNull ViewDataBinding binding);

@Nullable
protected String getPersonalAccessToken() {
protected String getAccessToken() {
return TokenHelper.getAccessToken(requireContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
if (! isNetworkAvailable(activity)) {
this.onNetworkLost();
} else {
String token = this.getPersonalAccessToken();
String token = this.getAccessToken();
if (getCurrentUser() == null && token != null) {
this.setUser(token, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onPageCommitVisible (WebView view, String url) {
}
});

String token = this.getPersonalAccessToken();
String token = this.getAccessToken();
if (getCurrentUser() == null && token != null) {
this.setUser(token, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import io.syslogic.github.R;
import io.syslogic.github.activity.NavHostActivity;
import io.syslogic.github.api.model.User;
import io.syslogic.github.databinding.FragmentRepositoriesBinding;
import io.syslogic.github.model.PagerState;
import io.syslogic.github.network.TokenCallback;
import io.syslogic.github.provider.RepositoriesMenuProvider;
import io.syslogic.github.recyclerview.RepositoriesAdapter;

Expand All @@ -20,7 +23,7 @@
*
* @author Martin Zeitler
*/
public class RepositoriesFragment extends BaseFragment {
public class RepositoriesFragment extends BaseFragment implements TokenCallback {

/** Log Tag */
@SuppressWarnings("unused")
Expand All @@ -41,19 +44,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

NavHostActivity activity = ((NavHostActivity) this.requireActivity());
this.setDataBinding(FragmentRepositoriesBinding.inflate(inflater, container, false));
this.getDataBinding().setPagerState(new PagerState());

/* It removes & adds {@link BaseMenuProvider} */
activity.setMenuProvider(new RepositoriesMenuProvider(activity));

activity.setSupportActionBar(this.getDataBinding().toolbarWorkflows.toolbarWorkflows);
this.mDataBinding.toolbarWorkflows.home.setOnClickListener(view -> activity.onBackPressed());
activity.setSupportActionBar(this.getDataBinding().toolbarRepositories.toolbarRepositories);
this.mDataBinding.toolbarRepositories.home.setOnClickListener(view -> activity.onBackPressed());

if (! isNetworkAvailable(this.requireContext())) {
this.onNetworkLost();
} else {
RepositoriesAdapter adapter = new RepositoriesAdapter(requireContext());
this.getDataBinding().recyclerviewWorkflows.setAdapter(adapter);
adapter.fetchPage(1);
this.getDataBinding().recyclerviewRepositories.setAdapter(adapter);
}
return this.getDataBinding().getRoot();
}
Expand All @@ -71,16 +74,39 @@ protected void setDataBinding(@NonNull ViewDataBinding binding) {
@Override
public void onNetworkAvailable() {
super.onNetworkAvailable();
String token = this.getAccessToken();
if (getCurrentUser() == null && token != null) {
this.setUser(token, this);
}

//noinspection ConstantValue
if (this.getDataBinding() != null) {
PagerState pagerState = this.getDataBinding().getPagerState();
pagerState.setIsOffline(false);
this.getDataBinding().setPagerState(pagerState);
}

}

@Override
public void onNetworkLost() {
super.onNetworkLost();
PagerState pagerState = this.getDataBinding().getPagerState();
if (pagerState != null) {
pagerState.setIsLoading(false);
pagerState.setIsOffline(true);
this.getDataBinding().setPagerState(pagerState);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {}
}

@Override
public void onLogin(@NonNull User item) {
this.mDataBinding.setUser(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private void gitClone(@NonNull File destination, @Nullable String branch) {
Thread thread = new Thread(() -> {
CloneCommand cmd = Git.cloneRepository()
.setURI(getRepoUrl())
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(getPersonalAccessToken(), ""))
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(getAccessToken(), ""))
.setProgressMonitor((ProgressMonitor) currentDialog)
.setDirectory(destination)
.setRemote("github");
Expand Down Expand Up @@ -260,7 +260,7 @@ public void onNetworkAvailable() {
super.onNetworkAvailable();
if (this.getContext() != null) {

String token = this.getPersonalAccessToken();
String token = this.getAccessToken();
if (getCurrentUser() == null && token != null) {
this.setUser(token, this);
}
Expand Down Expand Up @@ -419,7 +419,7 @@ void downloadTarball(Repository item, String branch) {
}

void downloadRepository(@NonNull final Repository item, final String archiveFormat, String branch) {
String token = getPersonalAccessToken();
String token = getAccessToken();
assert token != null;
Call<ResponseBody> api = GithubClient.getArchiveLink(token, item.getOwner().getLogin(), item.getName(), archiveFormat, branch);
if (mDebug) {Log.d(LOG_TAG, api.request().url() + "");}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
/* It removes & adds {@link BaseMenuProvider} */
activity.setMenuProvider(new RepositorySearchMenuProvider(activity));

// the SpinnerItem has the same ID as the QueryString.
/* the SpinnerItem has the same ID as the QueryString. */
activity.setSupportActionBar(this.getDataBinding().toolbarRepositories.toolbarRepositories);
this.mDataBinding.toolbarRepositories.home.setOnClickListener(view -> activity.onBackPressed());

Expand Down Expand Up @@ -150,38 +150,36 @@ public void onNetworkAvailable() {

super.onNetworkAvailable();

String token = this.getPersonalAccessToken();
String token = this.getAccessToken();
if (getCurrentUser() == null && token != null) {
this.setUser(token, this);
}

if (mDataBinding != null) {
PagerState pagerState = this.getDataBinding().getPagerState();
if (pagerState != null) {
pagerState.setIsOffline(false);
this.getDataBinding().setPagerState(pagerState);
}
PagerState pagerState = this.getDataBinding().getPagerState();
if (pagerState != null) {
pagerState.setIsOffline(false);
this.getDataBinding().setPagerState(pagerState);
}

/* When being online for the first time, adapter is null. */
RepositorySearchAdapter adapter = ((RepositorySearchAdapter) this.getDataBinding().recyclerviewRepositories.getAdapter());
if (adapter == null) {
/* Needs to run on UiThread */
requireActivity().runOnUiThread(() -> {
String queryString = getDataBinding().recyclerviewRepositories.getQueryString();
if (queryString == null) {
QueryStringAdapter queryStringArrayAdapter = (QueryStringAdapter) getDataBinding().toolbarRepositories.spinnerQueryString.getAdapter();
if (queryStringArrayAdapter != null && queryStringArrayAdapter.getCount() > 0) {
queryString = queryStringArrayAdapter.getItem(0).getValue();
}
/* When being online for the first time, adapter is null. */
RepositorySearchAdapter adapter = ((RepositorySearchAdapter) this.getDataBinding().recyclerviewRepositories.getAdapter());
if (adapter == null) {
/* Needs to run on UiThread */
requireActivity().runOnUiThread(() -> {
String queryString = getDataBinding().recyclerviewRepositories.getQueryString();
if (queryString == null) {
QueryStringAdapter queryStringArrayAdapter = (QueryStringAdapter) getDataBinding().toolbarRepositories.spinnerQueryString.getAdapter();
if (queryStringArrayAdapter != null && queryStringArrayAdapter.getCount() > 0) {
queryString = queryStringArrayAdapter.getItem(0).getValue();
}
if (queryString != null) {
getDataBinding().recyclerviewRepositories.setAdapter(new RepositorySearchAdapter(requireActivity(), queryString, showRepositoryTopics, 1));
}
});
} else if (adapter.getItemCount() == 0) {
/* If required, fetch page 1 */
adapter.fetchPage(1);
}
}
if (queryString != null) {
getDataBinding().recyclerviewRepositories.setAdapter(new RepositorySearchAdapter(requireActivity(), queryString, showRepositoryTopics, 1));
}
});
} else if (adapter.getItemCount() == 0) {
/* If required, fetch page 1 */
adapter.fetchPage(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import io.syslogic.github.Constants;
import io.syslogic.github.R;
import io.syslogic.github.activity.BaseActivity;
import io.syslogic.github.databinding.CardviewWorkflowBinding;
import io.syslogic.github.databinding.CardviewRepositoryBinding;
import io.syslogic.github.databinding.FragmentRepositoriesBinding;
import io.syslogic.github.api.model.Repository;
import io.syslogic.github.api.model.Workflow;
Expand Down Expand Up @@ -63,8 +63,19 @@ public class RepositoriesAdapter extends RecyclerView.Adapter<RecyclerView.ViewH

private WeakReference<Context> mContext;

/** This may add the account in debug mode and therefore must be called first. */
private String accessToken = null;

/**
* This only returns a value on the second attempt, because the account
* from which it gets the cached username is being added asynchronously).
*/
private String username = null;

public RepositoriesAdapter(@NonNull Context context) {
this.mContext = new WeakReference<>(context);
this.getCredentials();
this.fetchPage(1);
}

@Override
Expand All @@ -76,32 +87,33 @@ public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
CardviewWorkflowBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.cardview_workflow, parent, false);
CardviewRepositoryBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.cardview_repository, parent, false);
binding.getRoot().setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return new RepositoriesAdapter.ViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
Repository item = getItem(position);
CardviewWorkflowBinding binding = ((ViewHolder) viewHolder).getDataBinding();
CardviewRepositoryBinding binding = ((ViewHolder) viewHolder).getDataBinding();
binding.setItem(item);
}

public void fetchPage(final int pageNumber) {
private boolean getCredentials() {

/* It may add the account and therefore must be called first */
String accessToken = getAccessToken();
/* This may add the account in debug mode and therefore must be called first. */
this.accessToken = getAccessToken();

/*
* This only returns a value on the second attempt, because the account
* from which it gets the cached username is being added asynchronously).
*/
String username = getUsername();
if (username == null) {
// TODO: try again with a delay?
return;
}
this.username = getUsername();

return this.accessToken != null && this.username != null;
}

public void fetchPage(final int pageNumber) {

Call<ArrayList<Repository>> api = GithubClient.getUserRepositories(accessToken, username,"owner", "full_name","desc", 100, pageNumber);
if (BuildConfig.DEBUG) {Log.w(LOG_TAG, api.request().url() + "");}
Expand All @@ -118,39 +130,8 @@ public void onResponse(@NonNull Call<ArrayList<Repository>> call, @NonNull Respo
getItems().addAll(items);
notifyItemRangeChanged(positionStart, getItemCount());

for (Repository item : items) {

Call<WorkflowsResponse> api2 = GithubClient.getWorkflows(accessToken, username,item.getName());
if (BuildConfig.DEBUG) {Log.w(LOG_TAG, api2.request().url() + "");}

api2.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<WorkflowsResponse> call, @NonNull Response<WorkflowsResponse> response) {
if (response.code() == 200) { // OK
if (response.body() != null) {
WorkflowsResponse items = response.body();
if (BuildConfig.DEBUG) {
if (items.getWorkflows() != null && items.getWorkflows().size() > 0) {
for (Workflow item2 : items.getWorkflows()) {
Log.d(LOG_TAG, item.getName() + " has workflow: " + item2.getName());
}
}
}
}
} else {
/* "bad credentials" means that the provided access-token is invalid. */
if (response.errorBody() != null) {
logError(response.errorBody());
}
}
}

@Override
public void onFailure(@NonNull Call<WorkflowsResponse> call, @NonNull Throwable t) {
if (BuildConfig.DEBUG) {Log.e(LOG_TAG, "onFailure: " + t.getMessage());}
}
});
}
getWorkflows();
}
} else {
/* "bad credentials" means that the provided access-token is invalid. */
Expand All @@ -167,6 +148,42 @@ public void onFailure(@NonNull Call<ArrayList<Repository>> call, @NonNull Throwa
});
}

private void getWorkflows() {
for (Repository item : this.getItems()) {

Call<WorkflowsResponse> api2 = GithubClient.getWorkflows(accessToken, username,item.getName());
if (BuildConfig.DEBUG) {Log.w(LOG_TAG, api2.request().url() + "");}

api2.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<WorkflowsResponse> call, @NonNull Response<WorkflowsResponse> response) {
if (response.code() == 200) { // OK
if (response.body() != null) {
WorkflowsResponse items = response.body();
if (BuildConfig.DEBUG) {
if (items.getWorkflows() != null && items.getWorkflows().size() > 0) {
for (Workflow item2 : items.getWorkflows()) {
Log.d(LOG_TAG, item.getName() + " has workflow: " + item2.getName());
}
}
}
}
} else {
/* "bad credentials" means that the provided access-token is invalid. */
if (response.errorBody() != null) {
logError(response.errorBody());
}
}
}

@Override
public void onFailure(@NonNull Call<WorkflowsResponse> call, @NonNull Throwable t) {
if (BuildConfig.DEBUG) {Log.e(LOG_TAG, "onFailure: " + t.getMessage());}
}
});
}
}

/** Getters */
private Repository getItem(int index) {
return this.mItems.get(index);
Expand Down Expand Up @@ -222,13 +239,13 @@ void logError(@NonNull ResponseBody responseBody) {
/** {@link RecyclerView.ViewHolder} for {@link CardView} of type {@link Workflow}. */
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private final CardviewWorkflowBinding mDataBinding;
private final CardviewRepositoryBinding mDataBinding;

/**
* ViewHolder Constructor
* @param binding the item's data-binding
**/
ViewHolder(@NonNull CardviewWorkflowBinding binding) {
ViewHolder(@NonNull CardviewRepositoryBinding binding) {
super(binding.getRoot());
binding.cardview.setOnClickListener(this);
this.mDataBinding = binding;
Expand All @@ -250,7 +267,7 @@ public void onClick(@NonNull View viewHolder) {
}

/** Getters */
CardviewWorkflowBinding getDataBinding() {
CardviewRepositoryBinding getDataBinding() {
return this.mDataBinding;
}
}
Expand Down
Loading

0 comments on commit 35d58e5

Please sign in to comment.