| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- package com.datarecovery.master.module.imgrecover;
- import androidx.lifecycle.LiveData;
- import androidx.lifecycle.MutableLiveData;
- import androidx.lifecycle.Transformations;
- import com.atmob.app.lib.base.BaseViewModel;
- import com.atmob.app.lib.livedata.SingleLiveEvent;
- import com.atmob.common.runtime.ContextUtil;
- import com.datarecovery.master.R;
- import com.datarecovery.master.utils.FileUtil;
- import com.datarecovery.master.utils.ImageDeepDetector;
- import com.datarecovery.master.utils.MediaStoreHelper;
- import com.datarecovery.master.utils.ToastUtil;
- import org.reactivestreams.Subscription;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import javax.inject.Inject;
- import atmob.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
- import atmob.reactivex.rxjava3.annotations.NonNull;
- import atmob.reactivex.rxjava3.core.FlowableSubscriber;
- import atmob.reactivex.rxjava3.disposables.Disposable;
- import atmob.rxjava.utils.RxJavaUtil;
- import dagger.hilt.android.lifecycle.HiltViewModel;
- @HiltViewModel
- public class ImageRecoverViewModel extends BaseViewModel {
- private final long SCANNING_COUNTDOWN = 1000 * 60 * 6;
- private final MutableLiveData<List<ImageDeepDetector.ImageFile>> detectedPhotoImg = new MutableLiveData<>(new ArrayList<>());
- private final MutableLiveData<List<ImageDeepDetector.ImageFile>> detectedWxImg = new MutableLiveData<>(new ArrayList<>());
- private final MutableLiveData<List<ImageDeepDetector.ImageFile>> detectedQQImg = new MutableLiveData<>(new ArrayList<>());
- private final MutableLiveData<List<ImageDeepDetector.ImageFile>> detectedOtherImg = new MutableLiveData<>(new ArrayList<>());
- private LiveData<String> detectedPhotoTitle;
- private LiveData<String> detectedWxTitle;
- private LiveData<String> detectedQQTitle;
- private LiveData<String> detectedOtherTitle;
- private final SingleLiveEvent<ImageDeepDetector.ImageFile> previewEvent = new SingleLiveEvent<>();
- private final SingleLiveEvent<?> detectedFinish = new SingleLiveEvent<>();
- private final SingleLiveEvent<?> showClearDialog = new SingleLiveEvent<>();
- private final SingleLiveEvent<Boolean> showScanDialogEvent = new SingleLiveEvent<>();
- private final SingleLiveEvent<Boolean> showLoadingEvent = new SingleLiveEvent<>();
- private final SingleLiveEvent<?> notifyList = new SingleLiveEvent<>();
- private final MutableLiveData<String> barTitle = new MutableLiveData<>();
- private final MutableLiveData<Boolean> checkAll = new MutableLiveData<>(false);
- //总探测到的图片数量
- private int totalCount = 0;
- private final MutableLiveData<Integer> totalDetectedCount = new MutableLiveData<>();
- private final MutableLiveData<List<ImageDeepDetector.ImageFile>> selectedList = new MutableLiveData<>(new ArrayList<>());
- private LiveData<String> selectedCountTxt;
- private Disposable scanDisposable;
- private int type;
- @Inject
- public ImageRecoverViewModel() {
- barTitle.setValue(ContextUtil.getContext().getString(R.string.iamge_recover_all));
- startImageScanning();
- initLiveData();
- }
- public LiveData<?> getShowClearDialog() {
- return showClearDialog;
- }
- public LiveData<?> getNotifyList() {
- return notifyList;
- }
- public LiveData<Boolean> getShowLoadingEvent() {
- return showLoadingEvent;
- }
- public LiveData<ImageDeepDetector.ImageFile> getPreviewEvent() {
- return previewEvent;
- }
- public LiveData<?> getDetectedFinish() {
- return detectedFinish;
- }
- public LiveData<Integer> getTotalDetectedCount() {
- return totalDetectedCount;
- }
- public LiveData<String> getSelectedCountTxt() {
- return selectedCountTxt;
- }
- public LiveData<List<ImageDeepDetector.ImageFile>> getSelectedList() {
- return selectedList;
- }
- public LiveData<List<ImageDeepDetector.ImageFile>> getDetectedPhotoImg() {
- return detectedPhotoImg;
- }
- public LiveData<List<ImageDeepDetector.ImageFile>> getDetectedWxImg() {
- return detectedWxImg;
- }
- public LiveData<List<ImageDeepDetector.ImageFile>> getDetectedQQImg() {
- return detectedQQImg;
- }
- public LiveData<List<ImageDeepDetector.ImageFile>> getDetectedOtherImg() {
- return detectedOtherImg;
- }
- public LiveData<String> getDetectedPhotoTitle() {
- return detectedPhotoTitle;
- }
- public LiveData<String> getDetectedWxTitle() {
- return detectedWxTitle;
- }
- public LiveData<String> getDetectedQQTitle() {
- return detectedQQTitle;
- }
- public LiveData<String> getDetectedOtherTitle() {
- return detectedOtherTitle;
- }
- public LiveData<Boolean> getShowScanDialogEvent() {
- return showScanDialogEvent;
- }
- public LiveData<Boolean> getCheckAll() {
- return checkAll;
- }
- public LiveData<String> getBarTitle() {
- return barTitle;
- }
- public int getType() {
- return type;
- }
- private void initLiveData() {
- detectedPhotoTitle = Transformations.map(detectedPhotoImg, list ->
- ContextUtil.getContext().getString(R.string.photo_count, (list == null || list.isEmpty()) ? 0 : list.size()));
- detectedWxTitle = Transformations.map(detectedWxImg, list ->
- ContextUtil.getContext().getString(R.string.wx_count, (list == null || list.isEmpty()) ? 0 : list.size()));
- detectedQQTitle = Transformations.map(detectedQQImg, list ->
- ContextUtil.getContext().getString(R.string.qq_count, (list == null || list.isEmpty()) ? 0 : list.size()));
- detectedOtherTitle = Transformations.map(detectedOtherImg, list ->
- ContextUtil.getContext().getString(R.string.other_count, (list == null || list.isEmpty()) ? 0 : list.size()));
- }
- public void onCheckAllClick(boolean isCheck) {
- checkAll.setValue(isCheck);
- List<ImageDeepDetector.ImageFile> selectList = getList(selectedList);
- setListCheck(detectedPhotoImg.getValue(), isCheck);
- setListCheck(detectedWxImg.getValue(), isCheck);
- setListCheck(detectedQQImg.getValue(), isCheck);
- setListCheck(detectedOtherImg.getValue(), isCheck);
- if (isCheck) {
- selectList.clear();
- selectList.addAll(getList(detectedPhotoImg));
- selectList.addAll(getList(detectedWxImg));
- selectList.addAll(getList(detectedQQImg));
- selectList.addAll(getList(detectedOtherImg));
- } else {
- selectList.clear();
- }
- selectedList.setValue(selectList);
- }
- private void setListCheck(List<ImageDeepDetector.ImageFile> list, boolean isCheck) {
- if (list == null || list.isEmpty()) {
- return;
- }
- for (ImageDeepDetector.ImageFile imageFile : list) {
- imageFile.setCheck(isCheck);
- }
- }
- private List<ImageDeepDetector.ImageFile> getList(LiveData<List<ImageDeepDetector.ImageFile>> liveData) {
- List<ImageDeepDetector.ImageFile> selectList = liveData.getValue();
- if (selectList == null) {
- selectList = new ArrayList<>();
- }
- return selectList;
- }
- private void startImageScanning() {
- ImageDeepDetector.detect(ContextUtil.getContext())
- .take(SCANNING_COUNTDOWN, TimeUnit.MILLISECONDS)
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new FlowableSubscriber<List<ImageDeepDetector.ImageFile>>() {
- @Override
- public void onSubscribe(@NonNull Subscription s) {
- s.request(Integer.MAX_VALUE);
- scanDisposable = Disposable.fromSubscription(s);
- addDisposable(scanDisposable);
- showScanDialogEvent.setValue(true);
- checkAll.setValue(false);
- totalCount = 0;
- totalDetectedCount.setValue(0);
- }
- @Override
- public void onNext(List<ImageDeepDetector.ImageFile> imageFiles) {
- if (imageFiles == null) {
- return;
- }
- totalCount += imageFiles.size();
- totalDetectedCount.setValue(totalCount);
- for (ImageDeepDetector.ImageFile imageFile : imageFiles) {
- MutableLiveData<List<ImageDeepDetector.ImageFile>> liveData;
- if (imageFile.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_GALLERY) {
- liveData = detectedPhotoImg;
- } else if (imageFile.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_WECHAT) {
- liveData = detectedWxImg;
- } else if (imageFile.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_QQ) {
- liveData = detectedQQImg;
- } else {
- liveData = detectedOtherImg;
- }
- List<ImageDeepDetector.ImageFile> list = getList(liveData);
- list.add(0, imageFile);
- liveData.setValue(list);
- }
- }
- @Override
- public void onError(Throwable t) {
- showScanDialogEvent.setValue(false);
- }
- @Override
- public void onComplete() {
- detectedFinish.call();
- }
- });
- }
- public void cancelScan() {
- if (scanDisposable != null) scanDisposable.dispose();
- }
- public void setItemCheck(@NonNull ImageDeepDetector.ImageFile imageFile) {
- imageFile.setCheck(!imageFile.isCheck());
- List<ImageDeepDetector.ImageFile> list = getList(this.selectedList);
- if (imageFile.isCheck()) {
- list.add(imageFile);
- } else {
- list.remove(imageFile);
- }
- selectedList.setValue(list);
- }
- public void checkPreview(@NonNull ImageDeepDetector.ImageFile imageFile) {
- if (type == ImageRecoverActivity.Type.DELETE) {
- return;
- }
- //TODO 判断是否有会员
- previewEvent.setValue(imageFile);
- }
- public void onOperationClick() {
- if (type == ImageRecoverActivity.Type.DELETE) {
- showClearDialog.call();
- } else {
- onExportClick();
- }
- }
- public void executeImageDelete() {
- List<ImageDeepDetector.ImageFile> list = getList(selectedList);
- if (list.size() == 0) {
- return;
- }
- showLoadingEvent.setValue(true);
- RxJavaUtil.doInBackground(() -> {
- for (ImageDeepDetector.ImageFile item : list) {
- item.delete();
- MutableLiveData<List<ImageDeepDetector.ImageFile>> liveData;
- if (item.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_GALLERY) {
- liveData = detectedPhotoImg;
- } else if (item.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_WECHAT) {
- liveData = detectedWxImg;
- } else if (item.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_QQ) {
- liveData = detectedQQImg;
- } else {
- liveData = detectedOtherImg;
- }
- List<ImageDeepDetector.ImageFile> itemList = getList(liveData);
- itemList.remove(item);
- }
- return true;
- }, o -> {
- checkAll.setValue(false);
- showLoadingEvent.setValue(false);
- ToastUtil.show(R.string.delete_success, ToastUtil.LENGTH_SHORT);
- list.clear();
- selectedList.setValue(list);
- notifyList.call();
- }, throwable -> {
- showLoadingEvent.setValue(false);
- ToastUtil.show(R.string.delete_fail, ToastUtil.LENGTH_SHORT);
- });
- }
- public void onExportClick() {
- List<ImageDeepDetector.ImageFile> list = getList(selectedList);
- if (list.size() == 0) {
- return;
- }
- //TODO 判断是否有会员
- showLoadingEvent.setValue(true);
- RxJavaUtil.doInBackground(() -> {
- for (ImageDeepDetector.ImageFile item : list) {
- MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, item.newInputStream(), FileUtil.getExportImageFileName(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(R.string.export_fail, ToastUtil.LENGTH_SHORT);
- });
- }
- public List<ImageDeepDetector.ImageFile> getAllDetectedList() {
- ArrayList<ImageDeepDetector.ImageFile> allList = new ArrayList<>();
- allList.addAll(getList(detectedPhotoImg));
- allList.addAll(getList(detectedWxImg));
- allList.addAll(getList(detectedQQImg));
- allList.addAll(getList(detectedOtherImg));
- return allList;
- }
- public int getPosition(@NonNull List<ImageDeepDetector.ImageFile> list, ImageDeepDetector.ImageFile target) {
- return list.indexOf(target);
- }
- public void setOperationType(@ImageRecoverActivity.Type int type) {
- this.type = type;
- switch (type) {
- case ImageRecoverActivity.Type.DELETE:
- selectedCountTxt = Transformations.map(selectedList, list -> {
- if (list == null || list.isEmpty()) {
- return ContextUtil.getContext().getString(R.string.delete);
- }
- return ContextUtil.getContext().getString(R.string.delete_count, list.size());
- });
- break;
- case ImageRecoverActivity.Type.RECOVER:
- selectedCountTxt = Transformations.map(selectedList, list -> {
- if (list == null || list.isEmpty()) {
- return ContextUtil.getContext().getString(R.string.export);
- }
- return ContextUtil.getContext().getString(R.string.export_count, list.size());
- });
- break;
- }
- }
- }
|