Browse Source

优化音频导出流程

zk 1 year ago
parent
commit
60fce3ca97

+ 53 - 20
app/src/main/java/com/datarecovery/master/module/audiorecover/AudioRecoverViewModel.java

@@ -28,6 +28,7 @@ import org.reactivestreams.Subscription;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -35,6 +36,8 @@ import javax.inject.Inject;
 
 import atmob.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
 import atmob.reactivex.rxjava3.annotations.NonNull;
+import atmob.reactivex.rxjava3.core.Single;
+import atmob.reactivex.rxjava3.core.SingleObserver;
 import atmob.reactivex.rxjava3.disposables.Disposable;
 import atmob.rxjava.utils.RxJavaUtil;
 import dagger.hilt.android.lifecycle.HiltViewModel;
@@ -187,11 +190,11 @@ public class AudioRecoverViewModel extends BaseViewModel {
                         }
                         originalDetectedList.addAll(0, documentFiles);
                         List<FilesSearch.DocumentFile> videoList = getList(detectedVideoList);
+                        videoList.addAll(0, documentFiles);
                         for (FilesSearch.DocumentFile documentFile : documentFiles) {
                             detectedLastFileName.setValue(documentFile.getName());
-                            videoList.add(0, documentFile);
-                            detectedVideoList.setValue(videoList);
                         }
+                        detectedVideoList.setValue(videoList);
                         scrollTop.call();
                     }
 
@@ -268,24 +271,54 @@ public class AudioRecoverViewModel extends BaseViewModel {
                 return;
             }
         }
-        showLoadingEvent.setValue(true);
-        RxJavaUtil.doInBackground(() -> {
-            for (FilesSearch.DocumentFile item : list) {
-                MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_AUDIO, item.newInputStream(), FileUtil.getCreateFileName(MediaStoreHelper.TYPE_AUDIO, item.getName()));
-                item.setCheck(false);
-            }
-            return true;
-        }, o -> {
-            checkAll.setValue(false);
-            showLoadingEvent.setValue(false);
-            ToastUtil.show(R.string.export_success, ToastUtil.LENGTH_SHORT);
-            list.clear();
-            selectedList.setValue(list);
-        }, throwable -> {
-            showLoadingEvent.setValue(false);
-            ToastUtil.show(throwable.getMessage(), ToastUtil.LENGTH_SHORT);
-            BuglyHelper.postCatchedException(new Exception("音频导出:", throwable));
-        });
+
+        Single.just(list.size())
+                .map(oldSize -> {
+                    Iterator<FilesSearch.DocumentFile> iterator = list.iterator();
+                    int errorCount = 0;
+                    Exception exception = null;
+                    while (iterator.hasNext()) {
+                        FilesSearch.DocumentFile item = iterator.next();
+                        try {
+                            MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_AUDIO, item.newInputStream(), FileUtil.getCreateFileName(MediaStoreHelper.TYPE_AUDIO, item.getName()));
+                            item.setCheck(false);
+                            iterator.remove();
+                        } catch (Exception throwable) {
+                            errorCount++;
+                            exception = throwable;
+                            BuglyHelper.postCatchedException(new Exception("音频导出:", throwable));
+                        }
+                    }
+                    if (errorCount == oldSize) {
+                        throw new Exception("导出失败" + (exception == null ? "" : ":" + exception.getMessage()));
+                    } else if (errorCount > 0) {
+                        throw new Exception("部分导出失败" + ":" + exception.getMessage());
+                    }
+                    return errorCount;
+                })
+                .compose(RxJavaUtil.SingleSchedule.io2Main())
+                .subscribe(new SingleObserver<Integer>() {
+                    @Override
+                    public void onSubscribe(@NonNull Disposable d) {
+                        showLoadingEvent.setValue(true);
+                        addDisposable(d);
+                    }
+
+                    @Override
+                    public void onSuccess(@NonNull Integer integer) {
+                        showLoadingEvent.setValue(false);
+                        selectedList.setValue(list);
+                        checkAll.setValue(false);
+                        ToastUtil.show(R.string.export_success, ToastUtil.LENGTH_SHORT);
+                    }
+
+                    @Override
+                    public void onError(@NonNull Throwable throwable) {
+                        showLoadingEvent.setValue(false);
+                        selectedList.setValue(list);
+                        ToastUtil.show(throwable.getMessage(), ToastUtil.LENGTH_SHORT);
+                    }
+                });
     }
 
     public void setItemCheck(@NonNull FilesSearch.DocumentFile file) {