private static <T> void updateIfValueChanged( ContentProviderStoreCoreBase<T> store, Pair<T, Uri> pair) { final Cursor cursor = store.contentResolver.query(pair.second, store.getProjection(), null, null, null); T newItem = pair.first; boolean valuesEqual = false; if (cursor != null) { if (cursor.moveToFirst()) { T currentItem = store.read(cursor); valuesEqual = newItem.equals(currentItem); if (!valuesEqual) { Log.v(TAG, "Merging values at " + pair.second); newItem = store.mergeValues(currentItem, newItem); valuesEqual = newItem.equals(currentItem); } } cursor.close(); } if (valuesEqual) { Log.v(TAG, "Data already up to date at " + pair.second); return; } final ContentValues contentValues = store.getContentValuesForItem(newItem); if (store.contentResolver.update(pair.second, contentValues, null, null) == 0) { final Uri resultUri = store.contentResolver.insert(pair.second, contentValues); Log.v(TAG, "Inserted at " + resultUri); } else { Log.v(TAG, "Updated at " + pair.second); } }
public RepositoryViewModel( @NonNull DataFunctions.GetUserSettings getUserSettings, @NonNull DataFunctions.FetchAndGetGitHubRepository fetchAndGetGitHubRepository) { Preconditions.checkNotNull(getUserSettings, "Gey User Settings cannot be null."); Preconditions.checkNotNull( fetchAndGetGitHubRepository, "Fetch And Get GitHub Repository cannot be null."); this.getUserSettings = getUserSettings; this.fetchAndGetGitHubRepository = fetchAndGetGitHubRepository; Log.v(TAG, "RepositoryViewModel"); }
@NonNull protected Observable<T> getOne(Uri uri) { return get(uri) .map( queryResults -> { if (queryResults.size() == 0) { return null; } else if (queryResults.size() > 1) { Log.w( TAG, "Multiple items found in a get for a single item:" + queryResults.size()); } return queryResults.get(0); }); }
@NonNull public Action1<Throwable> doOnError(@NonNull final String uri) { Preconditions.checkNotNull(uri, "URI cannot be null."); return throwable -> { if (throwable instanceof RetrofitError) { RetrofitError retrofitError = (RetrofitError) throwable; int statusCode = retrofitError.getResponse() != null ? retrofitError.getResponse().getStatus() : NO_ERROR_CODE; errorRequest(uri, statusCode, retrofitError.getMessage()); } else { Log.e(TAG, "The error was not a RetroFitError"); errorRequest(uri, NO_ERROR_CODE, null); } }; }
@NonNull private List<T> queryList(Uri uri) { Preconditions.checkNotNull(uri, "Uri cannot be null."); Cursor cursor = contentResolver.query(uri, getProjection(), null, null, null); List<T> list = new ArrayList<>(); if (cursor != null) { if (cursor.moveToFirst()) { list.add(read(cursor)); } while (cursor.moveToNext()) { list.add(read(cursor)); } cursor.close(); } if (list.size() == 0) { Log.v(TAG, "Could not find with id: " + uri); } return list; }
protected void completeRequest(@NonNull String uri) { Preconditions.checkNotNull(uri, "URI cannot be null."); Log.v(TAG, "completeRequest(" + uri + ")"); updateNetworkRequestStatus.call(NetworkRequestStatus.completed(uri)); }
protected void errorRequest(@NonNull String uri, int errorCode, String errorMessage) { Preconditions.checkNotNull(uri, "URI cannot be null."); Log.v(TAG, "errorRequest(" + uri + ", " + errorCode + ", " + errorMessage + ")"); updateNetworkRequestStatus.call(NetworkRequestStatus.error(uri, errorCode, errorMessage)); }