PreviewViewModel.java 4.0 KB

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