mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Do not add duplicate photos when syncing
This commit is contained in:
parent
b8dd312869
commit
1986b2455c
1 changed files with 26 additions and 6 deletions
|
|
@ -25,6 +25,24 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||||
private int getLimit() {
|
private int getLimit() {
|
||||||
return 500; // FIXME: Parameterize!
|
return 500; // FIXME: Parameterize!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String[] existsQuery = { Contribution.Table.COLUMN_FILENAME };
|
||||||
|
private static final String existsSelection = Contribution.Table.COLUMN_FILENAME + " = ?";
|
||||||
|
private boolean fileExists(ContentProviderClient client, String filename) {
|
||||||
|
Cursor cursor = null;
|
||||||
|
try {
|
||||||
|
cursor = client.query(ContributionsContentProvider.BASE_URI,
|
||||||
|
existsQuery,
|
||||||
|
existsSelection,
|
||||||
|
new String[] { filename },
|
||||||
|
""
|
||||||
|
);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return cursor != null && cursor.getCount() != 0;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
|
public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
|
||||||
// This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
|
// This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
|
||||||
|
|
@ -52,20 +70,22 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||||
|
|
||||||
ArrayList<ApiResult> uploads = result.getNodes("/api/query/logevents/item");
|
ArrayList<ApiResult> uploads = result.getNodes("/api/query/logevents/item");
|
||||||
Log.d("Commons", uploads.size() + " results!");
|
Log.d("Commons", uploads.size() + " results!");
|
||||||
ContentValues[] imageValues = new ContentValues[uploads.size()];
|
ArrayList<ContentValues> imageValues = new ArrayList<ContentValues>();
|
||||||
for(int i=0; i < uploads.size(); i++) {
|
for(ApiResult image: uploads) {
|
||||||
ApiResult image = uploads.get(i);
|
|
||||||
String filename = image.getString("@title");
|
String filename = image.getString("@title");
|
||||||
|
if(fileExists(contentProviderClient, filename)) {
|
||||||
|
Log.d("Commons", "Skipping " + filename);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
String thumbUrl = Utils.makeThumbBaseUrl(filename);
|
String thumbUrl = Utils.makeThumbBaseUrl(filename);
|
||||||
Date dateUpdated = Utils.parseMWDate(image.getString("@timestamp"));
|
Date dateUpdated = Utils.parseMWDate(image.getString("@timestamp"));
|
||||||
Contribution contrib = new Contribution(null, thumbUrl, filename, "", -1, dateUpdated, dateUpdated, user, "");
|
Contribution contrib = new Contribution(null, thumbUrl, filename, "", -1, dateUpdated, dateUpdated, user, "");
|
||||||
contrib.setState(Contribution.STATE_COMPLETED);
|
contrib.setState(Contribution.STATE_COMPLETED);
|
||||||
imageValues[i] = contrib.toContentValues();
|
imageValues.add(contrib.toContentValues());
|
||||||
Log.d("Commons", "For " + imageValues[i].toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues);
|
contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues.toArray(new ContentValues[]{}));
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue