Skip to content

Commit cd538c6

Browse files
committed
fix(upload-list-adapter): do not refresh list after immediately async call
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 9f65bab commit cd538c6

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ class FileUploadHelper {
264264
uploadsStorageManager.uploadDao.deleteByRemotePathAndAccountName(remotePath, accountName)
265265
}
266266

267-
fun updateUploadStatus(remotePath: String, accountName: String, status: UploadStatus) {
267+
@JvmOverloads
268+
fun updateUploadStatus(remotePath: String, accountName: String, status: UploadStatus, onCompleted: () -> Unit = {}) {
268269
ioScope.launch {
269270
uploadsStorageManager.uploadDao.updateStatus(remotePath, accountName, status.value)
271+
onCompleted()
270272
}
271273
}
272274

app/src/main/java/com/owncloud/android/ui/activity/UploadListActivity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,7 @@ public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationRe
317317
private class UploadFinishReceiver extends BroadcastReceiver {
318318
@Override
319319
public void onReceive(Context context, Intent intent) {
320-
throttler.run("update_upload_list", () -> uploadListAdapter.loadUploadItemsFromDb(() -> {
321-
322-
}));
320+
throttler.run("update_upload_list", () -> uploadListAdapter.loadUploadItemsFromDb(() -> {}));
323321
}
324322
}
325323
}

app/src/main/java/com/owncloud/android/ui/adapter/UploadListAdapter.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464

6565
import androidx.annotation.NonNull;
6666
import kotlin.Unit;
67+
import kotlin.jvm.functions.Function0;
6768

6869
/**
6970
* This Adapter populates a ListView with following types of uploads: pending, active, completed. Filtering possible.
@@ -176,11 +177,31 @@ public void onBindHeaderViewHolder(SectionedViewHolder holder, int section, bool
176177
case CURRENT -> {
177178
String accountName = group.items()[0].getAccountName();
178179

179-
for (OCUpload upload: group.items) {
180-
uploadHelper.updateUploadStatus(upload.getRemotePath(), accountName, UploadStatus.UPLOAD_CANCELLED);
181-
FileUploadWorker.Companion.cancelCurrentUpload(upload.getRemotePath(), accountName, () -> Unit.INSTANCE);
180+
final int totalUploads = group.items().length;
181+
final int[] completedCount = {0};
182+
183+
for (int i=0; i<group.items.length; i++) {
184+
OCUpload upload = group.items[i];
185+
uploadHelper.updateUploadStatus(upload.getRemotePath(), accountName, UploadStatus.UPLOAD_CANCELLED, new Function0<Unit>() {
186+
@Override
187+
public Unit invoke() {
188+
FileUploadWorker.Companion.cancelCurrentUpload(upload.getRemotePath(), accountName, new Function0<Unit>() {
189+
@Override
190+
public Unit invoke() {
191+
completedCount[0]++;
192+
if (completedCount[0] == totalUploads) {
193+
Log_OC.d(TAG, "refreshing upload items");
194+
195+
// All uploads finished, refresh UI once
196+
loadUploadItemsFromDb(() -> {});
197+
}
198+
return Unit.INSTANCE;
199+
}
200+
});
201+
return Unit.INSTANCE;
202+
}
203+
});
182204
}
183-
loadUploadItemsFromDb(() -> {});
184205
}
185206
case FINISHED -> {
186207
uploadsStorageManager.clearSuccessfulUploads();
@@ -211,7 +232,6 @@ private void showFailedPopupMenu(HeaderViewHolder headerViewHolder) {
211232
connectivityService,
212233
accountManager,
213234
powerManagementService);
214-
loadUploadItemsFromDb(() -> {});
215235
}
216236

217237
return true;
@@ -254,7 +274,6 @@ private void retryCancelledUploads() {
254274
connectivityService,
255275
accountManager,
256276
powerManagementService);
257-
loadUploadItemsFromDb(() -> {});
258277
parentActivity.runOnUiThread(() -> {
259278
if (showNotExistMessage) {
260279
showNotExistMessage();
@@ -407,9 +426,13 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
407426
itemViewHolder.binding.uploadRightButton.setImageResource(R.drawable.ic_action_cancel_grey);
408427
itemViewHolder.binding.uploadRightButton.setVisibility(View.VISIBLE);
409428
itemViewHolder.binding.uploadRightButton.setOnClickListener(v -> {
410-
uploadHelper.updateUploadStatus(item.getRemotePath(), item.getAccountName(), UploadStatus.UPLOAD_CANCELLED);
411-
FileUploadWorker.Companion.cancelCurrentUpload(item.getRemotePath(), item.getAccountName(), () -> Unit.INSTANCE);
412-
loadUploadItemsFromDb(() -> {});
429+
uploadHelper.updateUploadStatus(item.getRemotePath(), item.getAccountName(), UploadStatus.UPLOAD_CANCELLED, () -> {
430+
FileUploadWorker.Companion.cancelCurrentUpload(item.getRemotePath(), item.getAccountName(), () -> {
431+
loadUploadItemsFromDb(() -> {});
432+
return Unit.INSTANCE;
433+
});
434+
return Unit.INSTANCE;
435+
});
413436
});
414437

415438
} else if (item.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
@@ -456,7 +479,6 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
456479
Optional<User> user = accountManager.getUser(item.getAccountName());
457480
if (file.exists() && user.isPresent()) {
458481
uploadHelper.retryUpload(item, user.get());
459-
loadUploadItemsFromDb(() -> {});
460482
} else {
461483
DisplayUtils.showSnackMessage(v.getRootView().findViewById(android.R.id.content), R.string.local_file_not_found_message);
462484
}

0 commit comments

Comments
 (0)