PreviewViewModel.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.datarecovery.master.module.preview;
  2. import android.net.Uri;
  3. import androidx.lifecycle.LiveData;
  4. import androidx.lifecycle.MutableLiveData;
  5. import com.atmob.app.lib.base.BaseViewModel;
  6. import com.atmob.common.runtime.ContextUtil;
  7. import com.datarecovery.master.R;
  8. import com.datarecovery.master.utils.BoxingUtil;
  9. import com.datarecovery.master.utils.FileUtil;
  10. import com.datarecovery.master.utils.ImageDeepDetector;
  11. import com.datarecovery.master.utils.MediaStoreHelper;
  12. import com.datarecovery.master.utils.ToastUtil;
  13. import java.lang.ref.WeakReference;
  14. import java.util.List;
  15. import javax.inject.Inject;
  16. import dagger.hilt.android.lifecycle.HiltViewModel;
  17. @HiltViewModel
  18. public class PreviewViewModel extends BaseViewModel {
  19. public static WeakReference<List<ImageDeepDetector.ImageFile>> weakReference;
  20. private final MutableLiveData<String> title = new MutableLiveData<>();
  21. private final MutableLiveData<Integer> type = new MutableLiveData<>();
  22. private final MutableLiveData<Uri> previewUri = new MutableLiveData<>();
  23. private final MutableLiveData<Boolean> isPlaying = new MutableLiveData<>();
  24. private List<ImageDeepDetector.ImageFile> imagePreviewList;
  25. private MutableLiveData<ImageDeepDetector.ImageFile> currentImageFile = new MutableLiveData<>();
  26. private int position;
  27. @Inject
  28. public PreviewViewModel() {
  29. }
  30. public LiveData<ImageDeepDetector.ImageFile> getCurrentImageFile() {
  31. return currentImageFile;
  32. }
  33. public int getPosition() {
  34. return position;
  35. }
  36. public List<ImageDeepDetector.ImageFile> getImagePreviewList() {
  37. return imagePreviewList;
  38. }
  39. public LiveData<String> getTitle() {
  40. return title;
  41. }
  42. public LiveData<Integer> getType() {
  43. return type;
  44. }
  45. public LiveData<Uri> getPreviewUri() {
  46. return previewUri;
  47. }
  48. public LiveData<Boolean> getIsPlaying() {
  49. return isPlaying;
  50. }
  51. public void setPreviewData(@PreviewActivity.Type int type, int position) {
  52. this.type.setValue(type);
  53. this.position = position;
  54. switch (type) {
  55. case PreviewActivity.TYPE_AUDIO:
  56. title.setValue(ContextUtil.getContext().getString(R.string.preview_audio));
  57. break;
  58. case PreviewActivity.TYPE_IMG:
  59. dealImageInit();
  60. break;
  61. case PreviewActivity.TYPE_VIDEO:
  62. title.setValue(ContextUtil.getContext().getString(R.string.preview_video));
  63. break;
  64. }
  65. }
  66. private void dealImageInit() {
  67. title.setValue(ContextUtil.getContext().getString(R.string.preview_img));
  68. if (weakReference != null) {
  69. this.imagePreviewList = weakReference.get();
  70. }
  71. }
  72. public void setUri(Uri uri) {
  73. this.previewUri.setValue(uri);
  74. }
  75. public void setPosition(int position) {
  76. int te = BoxingUtil.boxing(type.getValue());
  77. if (te == PreviewActivity.TYPE_IMG) {
  78. if (position != -1 && imagePreviewList != null && position < imagePreviewList.size()) {
  79. ImageDeepDetector.ImageFile imageFile = imagePreviewList.get(position);
  80. currentImageFile.setValue(imageFile);
  81. }
  82. }
  83. }
  84. public void onExportClick() {
  85. if (BoxingUtil.boxing(type.getValue()) == PreviewActivity.TYPE_IMG) {
  86. ImageDeepDetector.ImageFile value = currentImageFile.getValue();
  87. if (value == null) {
  88. return;
  89. }
  90. try {
  91. MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, value.newInputStream(), FileUtil.getCreateFileName(value.getName(), ".jpg"));
  92. ToastUtil.show(R.string.export_success, ToastUtil.LENGTH_SHORT);
  93. } catch (Exception e) {
  94. ToastUtil.show(R.string.export_fail, ToastUtil.LENGTH_SHORT);
  95. }
  96. }
  97. }
  98. @Override
  99. protected void onCleared() {
  100. super.onCleared();
  101. if (weakReference != null) {
  102. weakReference.clear();
  103. }
  104. }
  105. }