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 java.util.UUID; import javax.inject.Inject; import dagger.hilt.android.lifecycle.HiltViewModel; @HiltViewModel public class PreviewViewModel extends BaseViewModel { public static WeakReference> weakReference; private final MutableLiveData title = new MutableLiveData<>(); private final MutableLiveData type = new MutableLiveData<>(); private final MutableLiveData previewUri = new MutableLiveData<>(); private final MutableLiveData isPlaying = new MutableLiveData<>(); private List imagePreviewList; private MutableLiveData currentImageFile = new MutableLiveData<>(); private int position; @Inject public PreviewViewModel() { } public LiveData getCurrentImageFile() { return currentImageFile; } public int getPosition() { return position; } public List getImagePreviewList() { return imagePreviewList; } public LiveData getTitle() { return title; } public LiveData getType() { return type; } public LiveData getPreviewUri() { return previewUri; } public LiveData 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())); 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(); } } }