Browse Source

Fix result from image edit always null

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
0ec3691cac
  1. 21
      app/src/main/java/awais/instagrabber/fragments/imageedit/ImageEditFragment.java
  2. 2
      app/src/main/java/awais/instagrabber/viewmodels/ImageEditViewModel.java

21
app/src/main/java/awais/instagrabber/fragments/imageedit/ImageEditFragment.java

@ -34,12 +34,14 @@ import com.yalantis.ucrop.UCropActivity;
import com.yalantis.ucrop.UCropFragment; import com.yalantis.ucrop.UCropFragment;
import com.yalantis.ucrop.UCropFragmentCallback; import com.yalantis.ucrop.UCropFragmentCallback;
import java.io.File;
import java.util.List; import java.util.List;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.databinding.FragmentImageEditBinding; import awais.instagrabber.databinding.FragmentImageEditBinding;
import awais.instagrabber.fragments.imageedit.filters.filters.Filter; import awais.instagrabber.fragments.imageedit.filters.filters.Filter;
import awais.instagrabber.models.SavedImageEditState; import awais.instagrabber.models.SavedImageEditState;
import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import awais.instagrabber.viewmodels.ImageEditViewModel; import awais.instagrabber.viewmodels.ImageEditViewModel;
@ -173,18 +175,27 @@ public class ImageEditFragment extends Fragment {
binding.cancel.setOnClickListener(v -> { binding.cancel.setOnClickListener(v -> {
viewModel.cancel(); viewModel.cancel();
final NavController navController = NavHostFragment.findNavController(this); final NavController navController = NavHostFragment.findNavController(this);
setNavControllerResult(navController, null);
navController.navigateUp(); navController.navigateUp();
}); });
binding.done.setOnClickListener(v -> { binding.done.setOnClickListener(v -> {
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
Utils.mediaScanFile(context, viewModel.getDestinationFile(), (path, uri) -> {
final NavBackStackEntry navBackStackEntry = NavHostFragment.findNavController(this).getPreviousBackStackEntry();
final Uri resultUri = viewModel.getResultUri().getValue();
if (resultUri == null) return;
Utils.mediaScanFile(context, new File(resultUri.toString()), (path, uri) -> AppExecutors.getInstance().mainThread().execute(() -> {
final NavController navController = NavHostFragment.findNavController(this);
setNavControllerResult(navController, resultUri);
navController.navigateUp();
}));
});
}
private void setNavControllerResult(@NonNull final NavController navController, final Uri resultUri) {
final NavBackStackEntry navBackStackEntry = navController.getPreviousBackStackEntry();
if (navBackStackEntry == null) return; if (navBackStackEntry == null) return;
final SavedStateHandle savedStateHandle = navBackStackEntry.getSavedStateHandle(); final SavedStateHandle savedStateHandle = navBackStackEntry.getSavedStateHandle();
savedStateHandle.set("result", uri);
});
});
savedStateHandle.set("result", resultUri);
} }
private void setupCropFragment() { private void setupCropFragment() {

2
app/src/main/java/awais/instagrabber/viewmodels/ImageEditViewModel.java

@ -148,7 +148,7 @@ public class ImageEditViewModel extends AndroidViewModel {
gpuImage.setFilter(new GPUImageFilterGroup(list)); gpuImage.setFilter(new GPUImageFilterGroup(list));
final Uri uri = cropDestinationUri != null ? cropDestinationUri : originalUri; final Uri uri = cropDestinationUri != null ? cropDestinationUri : originalUri;
gpuImage.setImage(uri); gpuImage.setImage(uri);
gpuImage.saveToPictures(new File(destinationUri.toString()), false, uri1 -> setResultUri(uri));
gpuImage.saveToPictures(new File(destinationUri.toString()), false, uri1 -> setResultUri(destinationUri));
}); });
return; return;
} }

Loading…
Cancel
Save