Skip to content

Commit

Permalink
[FM] Display progress for batch rename, delete and copy (paste)
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Sep 8, 2023
1 parent e89b98d commit 143d0fc
Showing 1 changed file with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.leinardi.android.speeddial.SpeedDialActionItem;
import com.leinardi.android.speeddial.SpeedDialView;

Expand Down Expand Up @@ -716,6 +717,7 @@ private void startBatchDeletion(@NonNull List<Path> paths) {
// TODO: 27/6/23 Ideally, these should be done in a bound service
AtomicReference<Future<?>> deletionThread = new AtomicReference<>();
View view = View.inflate(requireContext(), R.layout.dialog_progress, null);
LinearProgressIndicator progress = view.findViewById(R.id.progress_linear);
TextView label = view.findViewById(android.R.id.text1);
TextView counter = view.findViewById(android.R.id.text2);
counter.setText(String.format(Locale.getDefault(), "%d/%d", 0, paths.size()));
Expand All @@ -730,10 +732,17 @@ private void startBatchDeletion(@NonNull List<Path> paths) {
.setCancelable(false)
.show();
deletionThread.set(ThreadUtils.postOnBackgroundThread(() -> {
WeakReference<LinearProgressIndicator> progressRef = new WeakReference<>(progress);
WeakReference<TextView> labelRef = new WeakReference<>(label);
WeakReference<TextView> counterRef = new WeakReference<>(counter);
WeakReference<AlertDialog> dialogRef = new WeakReference<>(dialog);
try {
LinearProgressIndicator p = progressRef.get();
if (p != null) {
p.setMax(paths.size());
p.setProgress(0);
p.setIndeterminate(false);
}
int i = 1;
for (Path path : paths) {
// Update label
Expand All @@ -751,11 +760,15 @@ private void startBatchDeletion(@NonNull List<Path> paths) {
}
path.delete();
TextView c = counterRef.get();
if (c != null) {
int finalI = i;
ThreadUtils.postOnMainThread(() ->
c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size())));
}
int finalI = i;
ThreadUtils.postOnMainThread(() -> {
if (c != null) {
c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size()));
}
if (p != null) {
p.setProgress(finalI);
}
});
++i;
if (ThreadUtils.isInterrupted()) {
break;
Expand All @@ -777,6 +790,7 @@ private void startBatchDeletion(@NonNull List<Path> paths) {
private void startBatchRenaming(List<Path> paths, String prefix, @Nullable String extension) {
AtomicReference<Future<?>> renameThread = new AtomicReference<>();
View view = View.inflate(requireContext(), R.layout.dialog_progress, null);
LinearProgressIndicator progress = view.findViewById(R.id.progress_linear);
TextView label = view.findViewById(android.R.id.text1);
TextView counter = view.findViewById(android.R.id.text2);
counter.setText(String.format(Locale.getDefault(), "%d/%d", 0, paths.size()));
Expand All @@ -791,10 +805,17 @@ private void startBatchRenaming(List<Path> paths, String prefix, @Nullable Strin
.setCancelable(false)
.show();
renameThread.set(ThreadUtils.postOnBackgroundThread(() -> {
WeakReference<LinearProgressIndicator> progressRef = new WeakReference<>(progress);
WeakReference<TextView> labelRef = new WeakReference<>(label);
WeakReference<TextView> counterRef = new WeakReference<>(counter);
WeakReference<AlertDialog> dialogRef = new WeakReference<>(dialog);
try {
LinearProgressIndicator p = progressRef.get();
if (p != null) {
p.setMax(paths.size());
p.setProgress(0);
p.setIndeterminate(false);
}
int i = 1;
for (Path path : paths) {
// Update label
Expand All @@ -816,11 +837,15 @@ private void startBatchRenaming(List<Path> paths, String prefix, @Nullable Strin
path.renameTo(displayName);
}
TextView c = counterRef.get();
if (c != null) {
int finalI = i;
ThreadUtils.postOnMainThread(() ->
c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size())));
}
int finalI = i;
ThreadUtils.postOnMainThread(() -> {
if (c != null) {
c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, paths.size()));
}
if (p != null) {
p.setProgress(finalI);
}
});
++i;
if (ThreadUtils.isInterrupted()) {
break;
Expand All @@ -846,6 +871,7 @@ private void startBatchPaste(@NonNull FmTasks.FmTask task) {
}
AtomicReference<Future<?>> pasteThread = new AtomicReference<>();
View view = View.inflate(requireContext(), R.layout.dialog_progress, null);
LinearProgressIndicator progress = view.findViewById(R.id.progress_linear);
TextView label = view.findViewById(android.R.id.text1);
TextView counter = view.findViewById(android.R.id.text2);
counter.setText(String.format(Locale.getDefault(), "%d/%d", 0, task.files.size()));
Expand All @@ -860,11 +886,18 @@ private void startBatchPaste(@NonNull FmTasks.FmTask task) {
.setCancelable(false)
.show();
pasteThread.set(ThreadUtils.postOnBackgroundThread(() -> {
WeakReference<LinearProgressIndicator> progressRef = new WeakReference<>(progress);
WeakReference<TextView> labelRef = new WeakReference<>(label);
WeakReference<TextView> counterRef = new WeakReference<>(counter);
WeakReference<AlertDialog> dialogRef = new WeakReference<>(dialog);
Path targetPath = Paths.get(uri);
try {
LinearProgressIndicator p = progressRef.get();
if (p != null) {
p.setMax(task.files.size());
p.setProgress(0);
p.setIndeterminate(false);
}
int i = 1;
for (Path sourcePath : task.files) {
// Update label
Expand Down Expand Up @@ -901,11 +934,15 @@ private void startBatchPaste(@NonNull FmTasks.FmTask task) {
}
}
TextView c = counterRef.get();
if (c != null) {
int finalI = i;
ThreadUtils.postOnMainThread(() ->
c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, task.files.size())));
}
int finalI = i;
ThreadUtils.postOnMainThread(() -> {
if (c != null) {
c.setText(String.format(Locale.getDefault(), "%d/%d", finalI, task.files.size()));
}
if (p != null) {
p.setProgress(finalI);
}
});
++i;
if (ThreadUtils.isInterrupted()) {
break;
Expand Down

0 comments on commit 143d0fc

Please sign in to comment.