| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.datarecovery.master.module.preview;
- import android.net.Uri;
- import androidx.lifecycle.LiveData;
- import androidx.lifecycle.MutableLiveData;
- import com.atmob.app.lib.base.BaseViewModel;
- import com.atmob.common.runtime.ContextUtil;
- import com.datarecovery.master.R;
- import com.datarecovery.master.utils.BoxingUtil;
- 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 java.lang.ref.WeakReference;
- import java.util.List;
- import javax.inject.Inject;
- import dagger.hilt.android.lifecycle.HiltViewModel;
- @HiltViewModel
- public class PreviewViewModel extends BaseViewModel {
- public static WeakReference<List<ImageDeepDetector.ImageFile>> weakReference;
- private final MutableLiveData<String> title = new MutableLiveData<>();
- private final MutableLiveData<Integer> type = new MutableLiveData<>();
- private final MutableLiveData<Uri> previewUri = new MutableLiveData<>();
- private final MutableLiveData<Boolean> isPlaying = new MutableLiveData<>();
- private List<ImageDeepDetector.ImageFile> imagePreviewList;
- private MutableLiveData<ImageDeepDetector.ImageFile> currentImageFile = new MutableLiveData<>();
- private int position;
- @Inject
- public PreviewViewModel() {
- }
- public LiveData<ImageDeepDetector.ImageFile> getCurrentImageFile() {
- return currentImageFile;
- }
- public int getPosition() {
- return position;
- }
- public List<ImageDeepDetector.ImageFile> getImagePreviewList() {
- return imagePreviewList;
- }
- public LiveData<String> getTitle() {
- return title;
- }
- public LiveData<Integer> getType() {
- return type;
- }
- public LiveData<Uri> getPreviewUri() {
- return previewUri;
- }
- public LiveData<Boolean> getIsPlaying() {
- return isPlaying;
- }
- public void setPreviewData(@PreviewActivity.Type int type, int position) {
- this.type.setValue(type);
- this.position = position;
- switch (type) {
- case PreviewActivity.TYPE_AUDIO:
- title.setValue(ContextUtil.getContext().getString(R.string.preview_audio));
- break;
- case PreviewActivity.TYPE_IMG:
- dealImageInit();
- break;
- case PreviewActivity.TYPE_VIDEO:
- title.setValue(ContextUtil.getContext().getString(R.string.preview_video));
- break;
- }
- }
- private void dealImageInit() {
- title.setValue(ContextUtil.getContext().getString(R.string.preview_img));
- if (weakReference != null) {
- this.imagePreviewList = weakReference.get();
- }
- }
- public void setUri(Uri uri) {
- this.previewUri.setValue(uri);
- }
- public void setPosition(int position) {
- int te = BoxingUtil.boxing(type.getValue());
- if (te == PreviewActivity.TYPE_IMG) {
- if (position != -1 && imagePreviewList != null && position < imagePreviewList.size()) {
- ImageDeepDetector.ImageFile imageFile = imagePreviewList.get(position);
- currentImageFile.setValue(imageFile);
- }
- }
- }
- public void onExportClick() {
- if (BoxingUtil.boxing(type.getValue()) == PreviewActivity.TYPE_IMG) {
- ImageDeepDetector.ImageFile value = currentImageFile.getValue();
- if (value == null) {
- return;
- }
- try {
- MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, value.newInputStream(), FileUtil.getCreateFileName(value.getName(), ".jpg"));
- ToastUtil.show(R.string.export_success, ToastUtil.LENGTH_SHORT);
- } catch (Exception e) {
- ToastUtil.show(R.string.export_fail, ToastUtil.LENGTH_SHORT);
- }
- }
- }
- @Override
- protected void onCleared() {
- super.onCleared();
- if (weakReference != null) {
- weakReference.clear();
- }
- }
- }
|