From 03d925b5abb828a3adc8cb5ed1a8e90ccddff469 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 22 Oct 2020 23:12:44 +0200 Subject: [PATCH] Use new way to start Activity with uCrop - other case --- .../preview/AttachmentsPreviewFragment.kt | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt b/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt index fe4902728b..9f3ba39bbe 100644 --- a/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt +++ b/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt @@ -19,7 +19,6 @@ package im.vector.app.features.attachments.preview import android.app.Activity.RESULT_CANCELED import android.app.Activity.RESULT_OK -import android.content.Intent import android.os.Bundle import android.os.Parcelable import android.view.Menu @@ -39,6 +38,7 @@ import com.airbnb.mvrx.withState import com.yalantis.ucrop.UCrop import im.vector.app.R import im.vector.app.core.extensions.cleanup +import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.resources.ColorProvider import im.vector.app.core.utils.OnSnapPositionChangeListener @@ -49,7 +49,6 @@ import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_attachments_preview.* import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.content.ContentAttachmentData -import timber.log.Timber import java.io.File import javax.inject.Inject @@ -80,20 +79,15 @@ class AttachmentsPreviewFragment @Inject constructor( } } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - // TODO handle this one (Ucrop lib) - @Suppress("DEPRECATION") - super.onActivityResult(requestCode, resultCode, data) - - if (resultCode == RESULT_OK) { - if (requestCode == UCrop.REQUEST_CROP && data != null) { - Timber.v("Crop success") - handleCropResult(data) + private val uCropActivityResultLauncher = registerStartForActivityResult { activityResult -> + if (activityResult.resultCode == RESULT_OK) { + val resultUri = activityResult.data?.let { UCrop.getOutput(it) } + if (resultUri != null) { + viewModel.handle(AttachmentsPreviewAction.UpdatePathOfCurrentAttachment(resultUri)) + } else { + Toast.makeText(requireContext(), "Cannot retrieve cropped value", Toast.LENGTH_SHORT).show() } } - if (resultCode == UCrop.RESULT_ERROR) { - Timber.v("Crop error") - } } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -170,15 +164,6 @@ class AttachmentsPreviewFragment @Inject constructor( } } - private fun handleCropResult(result: Intent) { - val resultUri = UCrop.getOutput(result) - if (resultUri != null) { - viewModel.handle(AttachmentsPreviewAction.UpdatePathOfCurrentAttachment(resultUri)) - } else { - Toast.makeText(requireContext(), "Cannot retrieve cropped value", Toast.LENGTH_SHORT).show() - } - } - private fun handleRemoveAction() { viewModel.handle(AttachmentsPreviewAction.RemoveCurrentAttachment) } @@ -188,7 +173,8 @@ class AttachmentsPreviewFragment @Inject constructor( val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}") val uri = currentAttachment.queryUri createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), currentAttachment.name) - .start(requireContext(), this) + .getIntent(requireContext()) + .let { intent -> uCropActivityResultLauncher.launch(intent) } } private fun setupRecyclerViews() {