Skip to content

Commit

Permalink
Support conflict resolution for partial uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Sep 8, 2024
1 parent d4bf834 commit 4838c04
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/blau/android/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4402,7 +4402,7 @@ protected void onPostExecute(UploadResult result) {
} else if (conflict instanceof ApiResponse.ChangesetLocked) {
ErrorAlert.showDialog(activity, ErrorCodes.UPLOAD_PROBLEM, result.getMessage());
} else {
UploadConflict.showDialog(activity, conflict);
UploadConflict.showDialog(activity, conflict, elements);
}
break;
case ErrorCodes.INVALID_LOGIN:
Expand Down
41 changes: 30 additions & 11 deletions src/main/java/de/blau/android/dialogs/UploadConflict.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package de.blau.android.dialogs;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -54,6 +58,7 @@
import de.blau.android.util.InfoDialogFragment;
import de.blau.android.util.ScreenMessage;
import de.blau.android.util.ThemeUtils;
import de.blau.android.util.Util;

/**
* Dialog to resolve upload conflicts one by one
Expand All @@ -62,14 +67,16 @@
*
*/
public class UploadConflict extends ImmersiveDialogFragment {

private static final String DEBUG_TAG = UploadConflict.class.getSimpleName().substring(0, Math.min(23, UploadConflict.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, UploadConflict.class.getSimpleName().length());
private static final String DEBUG_TAG = UploadConflict.class.getSimpleName().substring(0, TAG_LEN);

private static final String CONFLICT_KEY = "uploadresult";
private static final String ELEMENTS_KEY = "elements";

private static final String TAG = "fragment_upload_conflict";

private Conflict conflict;
private Conflict conflict;
private List<OsmElement> elements;

private class RestartHandler implements PostAsyncActionHandler {
private final String errorMessage;
Expand All @@ -92,7 +99,7 @@ public void onSuccess() {
((Main) activity).invalidateMap();
}
if (App.getDelegator().hasChanges()) {
ReviewAndUpload.showDialog(activity, null);
ReviewAndUpload.showDialog(activity, elements);
}
}

Expand All @@ -106,14 +113,15 @@ public void onError(@Nullable AsyncResult result) {
* Show a dialog after a conflict has been detected and allow the user to fix it
*
* @param activity the calling Activity
* @param elements optional list of elements in upload
* @param result the UploadResult
*/
public static void showDialog(@NonNull FragmentActivity activity, @NonNull Conflict conflict) {
public static void showDialog(@NonNull FragmentActivity activity, @NonNull Conflict conflict, @Nullable List<OsmElement> elements) {
dismissDialog(activity);

FragmentManager fm = activity.getSupportFragmentManager();
try {
UploadConflict uploadConflictDialogFragment = newInstance(conflict);
UploadConflict uploadConflictDialogFragment = newInstance(conflict, elements);
uploadConflictDialogFragment.show(fm, TAG);
} catch (IllegalStateException isex) {
Log.e(DEBUG_TAG, "dismissDialog", isex);
Expand All @@ -132,15 +140,20 @@ private static void dismissDialog(@NonNull FragmentActivity activity) {
/**
* Construct a new UploadConflict dialog
*
* @param result an UploadResult
* @param conflict an COnflict object with the relevant info
* @param elements optional list of elements in upload
*
* @return an UploadConflict dialog
*/
@NonNull
private static UploadConflict newInstance(@NonNull final Conflict conflict) {
private static UploadConflict newInstance(@NonNull final Conflict conflict, List<OsmElement> elements) {
UploadConflict f = new UploadConflict();

Bundle args = new Bundle();
args.putSerializable(CONFLICT_KEY, conflict);
if (elements != null) {
args.putSerializable(ELEMENTS_KEY, new ArrayList<>(elements));
}

f.setArguments(args);
f.setShowsDialog(true);
Expand All @@ -154,8 +167,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
Log.d(DEBUG_TAG, "restoring from saved state");
conflict = de.blau.android.util.Util.getSerializeable(savedInstanceState, CONFLICT_KEY, Conflict.class);
elements = Util.getSerializeableArrayList(savedInstanceState, ELEMENTS_KEY, OsmElement.class);
} else {
conflict = de.blau.android.util.Util.getSerializeable(getArguments(), CONFLICT_KEY, Conflict.class);
elements = Util.getSerializeableArrayList(getArguments(), ELEMENTS_KEY, OsmElement.class);
}
}

Expand Down Expand Up @@ -218,13 +233,14 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {
logic.createCheckpoint(activity, R.string.undo_action_fix_conflict);
delegator.undoLast(elementLocal);
if (delegator.getApiElementCount() > 0) {
ReviewAndUpload.showDialog(activity, null);
ReviewAndUpload.showDialog(activity, elements);
}
});
resolveActions.put(res.getString(R.string.deleting_references_on_server), () -> {
logic.createCheckpoint(activity, R.string.undo_action_fix_conflict);
// first undelete
delegator.removeFromUpload(elementLocal, OsmElement.STATE_UNCHANGED);
elements.remove(elementLocal);
delegator.insertElementSafe(elementLocal);
// now download referring elements
for (long id : usedByElementIds) {
Expand All @@ -248,7 +264,7 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {
default:
throw new IllegalStateException("Unknown element type");
}
ReviewAndUpload.showDialog(activity, null);
ReviewAndUpload.showDialog(activity, elements);
});
} else if (conflict instanceof ApiResponse.VersionConflict) {
//
Expand All @@ -262,7 +278,7 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {
activity.getString(R.string.toast_download_server_version_failed, elementLocal.getDescription()));
resolveActions.put(res.getString(R.string.use_local_version), () -> {
logic.fixElementWithConflict(activity, elementOnServer.getOsmVersion(), elementLocal, elementOnServer, true);
ReviewAndUpload.showDialog(activity, null);
ReviewAndUpload.showDialog(activity, elements);
});
resolveActions.put(res.getString(R.string.merge_tags_in_to_server), () -> {
Map<String, String> mergedTags = MergeAction.mergeTags(elementOnServer, elementLocal);
Expand Down Expand Up @@ -512,5 +528,8 @@ public static TableRow createMissingReferenceRow(@NonNull Context context, @NonN
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(CONFLICT_KEY, conflict);
if (elements != null) {
outState.putSerializable(ELEMENTS_KEY, new ArrayList<>(elements));
}
}
}

0 comments on commit 4838c04

Please sign in to comment.