Skip to content

Commit

Permalink
sync seems to work, so far.
Browse files Browse the repository at this point in the history
  • Loading branch information
syslogic committed Aug 7, 2023
1 parent b54a585 commit d35275c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public interface RepositoriesDao {
@Query("SELECT * FROM " + Constants.TABLE_REPOSITORIES)
Cursor selectAll();

@Query("SELECT * FROM " + Constants.TABLE_REPOSITORIES + " WHERE id LIKE :itemId LIMIT 1")
Repository getItem(Long itemId);

@Insert()
Long insert(Repository item);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public class RepositorySyncAdapter extends AbstractThreadedSyncAdapter {

private final String username;

boolean hasRecords = true;

int pageSize = 100;

int pageNumber = 1;

/** Constructor. */
public RepositorySyncAdapter(@NonNull Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
Expand All @@ -74,43 +80,51 @@ public RepositorySyncAdapter(@NonNull Context context, boolean autoInitialize, b
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
if (this.accessToken != null && this.username != null) {

int pageNumber = 1;
int pageSize = 100;
while (hasRecords) {

Call<ArrayList<Repository>> api = GithubClient.getUserRepositories(accessToken, username, "owner", "full_name", "desc", pageSize, pageNumber);
if (BuildConfig.DEBUG && this.prefs.getBoolean(Constants.PREFERENCE_KEY_DEBUG_LOGGING, false)) {
Log.d(LOG_TAG, api.request().url() + "");
}

api.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<ArrayList<Repository>> call, @NonNull Response<ArrayList<Repository>> response) {
if (response.code() == 200) { // OK
if (response.body() != null) {

/* Updating the adapter with the initial response already. */
ArrayList<Repository> items = response.body();
for (Repository item : items) {
Abstraction.executorService.execute(() -> {
Call<ArrayList<Repository>> api = GithubClient.getUserRepositories(accessToken, username, "owner", "full_name", "desc", pageSize, pageNumber);
if (BuildConfig.DEBUG && this.prefs.getBoolean(Constants.PREFERENCE_KEY_DEBUG_LOGGING, false)) {
Log.d(LOG_TAG, api.request().url() + "");
}

});
api.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<ArrayList<Repository>> call, @NonNull Response<ArrayList<Repository>> response) {
if (response.code() == 200) { // OK
if (response.body() != null) {

ArrayList<Repository> items = response.body();
if (items.size() == pageSize) {pageNumber++;}
else {hasRecords = false;}

for (Repository item : items) {
Abstraction.executorService.execute(() -> {
assert dao != null;
if (dao.getItem(item.getId()) == null) {
dao.insert(item);
} else {
dao.update(item);
}
});
}
}
} else {
/* "bad credentials" means that the provided access-token is invalid. */
if (response.errorBody() != null) {
logError(response.errorBody());
hasRecords = false;
}
}
} else {
/* "bad credentials" means that the provided access-token is invalid. */
if (response.errorBody() != null) {
logError(response.errorBody());
}
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,35 @@ public void onNetworkAvailable() {
this.setUser(token, this);
}

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

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

0 comments on commit d35275c

Please sign in to comment.