From 72bf2f9c1a94e09514b031f7cab7b7d3e2c9bc81 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 22 Oct 2020 22:54:11 +0200 Subject: [PATCH] Use new way to start Activity with uCrop --- .../dialogs/GalleryOrCameraDialogHelper.kt | 30 ++++++++++++++--- .../createroom/CreateRoomFragment.kt | 31 +++--------------- .../settings/VectorSettingsGeneralFragment.kt | 32 ++----------------- 3 files changed, 33 insertions(+), 60 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/dialogs/GalleryOrCameraDialogHelper.kt b/vector/src/main/java/im/vector/app/core/dialogs/GalleryOrCameraDialogHelper.kt index 1f357b4cfc..0386a7a334 100644 --- a/vector/src/main/java/im/vector/app/core/dialogs/GalleryOrCameraDialogHelper.kt +++ b/vector/src/main/java/im/vector/app/core/dialogs/GalleryOrCameraDialogHelper.kt @@ -19,20 +19,26 @@ package im.vector.app.core.dialogs import android.app.Activity import android.net.Uri import androidx.appcompat.app.AlertDialog +import androidx.core.net.toUri import androidx.fragment.app.Fragment +import com.yalantis.ucrop.UCrop import im.vector.app.R import im.vector.app.core.extensions.registerStartForActivityResult +import im.vector.app.core.resources.ColorProvider import im.vector.app.core.utils.PERMISSIONS_FOR_TAKING_PHOTO import im.vector.app.core.utils.checkPermissions import im.vector.app.core.utils.registerForPermissionsResult +import im.vector.app.features.media.createUCropWithDefaultSettings import im.vector.lib.multipicker.MultiPicker import im.vector.lib.multipicker.entity.MultiPickerImageType +import java.io.File class GalleryOrCameraDialogHelper( - private val fragment: Fragment + private val fragment: Fragment, + private val colorProvider: ColorProvider ) { interface Listener { - fun onImageReady(image: MultiPickerImageType) + fun onImageReady(uri: Uri?) } private val activity by lazy { fragment.requireActivity() } @@ -50,7 +56,7 @@ class GalleryOrCameraDialogHelper( avatarCameraUri?.let { uri -> MultiPicker.get(MultiPicker.CAMERA) .getTakenPhoto(fragment.requireContext(), uri) - ?.let { listener.onImageReady(it) } + ?.let { startUCrop(it) } } } } @@ -61,7 +67,23 @@ class GalleryOrCameraDialogHelper( .get(MultiPicker.IMAGE) .getSelectedFiles(fragment.requireContext(), activityResult.data) .firstOrNull() - ?.let { listener.onImageReady(it) } + ?.let { startUCrop(it) } + } + } + + private fun startUCrop(image: MultiPickerImageType) { + val context = fragment.requireContext() + val destinationFile = File(context.cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") + val uri = image.contentUri + createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), image.displayName) + .withAspectRatio(1f, 1f) + .getIntent(context) + .let { uCropActivityResultLauncher.launch(it) } + } + + private val uCropActivityResultLauncher = fragment.registerStartForActivityResult { activityResult -> + if (activityResult.resultCode == Activity.RESULT_OK) { + activityResult.data?.let { listener.onImageReady(UCrop.getOutput(it)) } } } diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt index 532d6701ce..b3819a1707 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt @@ -16,15 +16,12 @@ package im.vector.app.features.roomdirectory.createroom -import android.app.Activity -import android.content.Intent +import android.net.Uri import android.os.Bundle import android.view.View -import androidx.core.net.toUri import com.airbnb.mvrx.Success import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.withState -import com.yalantis.ucrop.UCrop import im.vector.app.R import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper import im.vector.app.core.extensions.cleanup @@ -32,13 +29,10 @@ import im.vector.app.core.extensions.configureWith import im.vector.app.core.platform.OnBackPressed import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.resources.ColorProvider -import im.vector.app.features.media.createUCropWithDefaultSettings import im.vector.app.features.roomdirectory.RoomDirectorySharedAction import im.vector.app.features.roomdirectory.RoomDirectorySharedActionViewModel -import im.vector.lib.multipicker.entity.MultiPickerImageType import kotlinx.android.synthetic.main.fragment_create_room.* import timber.log.Timber -import java.io.File import javax.inject.Inject class CreateRoomFragment @Inject constructor( @@ -52,7 +46,7 @@ class CreateRoomFragment @Inject constructor( private lateinit var sharedActionViewModel: RoomDirectorySharedActionViewModel private val viewModel: CreateRoomViewModel by activityViewModel() - private val galleryOrCameraDialogHelper = GalleryOrCameraDialogHelper(this) + private val galleryOrCameraDialogHelper = GalleryOrCameraDialogHelper(this, colorProvider) override fun getLayoutResId() = R.layout.fragment_create_room @@ -85,25 +79,8 @@ class CreateRoomFragment @Inject constructor( galleryOrCameraDialogHelper.show() } - override fun onImageReady(image: MultiPickerImageType) { - val destinationFile = File(requireContext().cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") - val uri = image.contentUri - createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), image.displayName) - .withAspectRatio(1f, 1f) - .start(requireContext(), this) - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - // TODO handle this one (Ucrop lib) - @Suppress("DEPRECATION") - super.onActivityResult(requestCode, resultCode, data) - - if (resultCode == Activity.RESULT_OK) { - when (requestCode) { - UCrop.REQUEST_CROP -> - viewModel.handle(CreateRoomAction.SetAvatar(data?.let { UCrop.getOutput(it) })) - } - } + override fun onImageReady(uri: Uri?) { + viewModel.handle(CreateRoomAction.SetAvatar(uri)) } override fun onNameChange(newName: String) { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt index a7f4381446..d8171bd30d 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt @@ -18,8 +18,6 @@ package im.vector.app.features.settings -import android.app.Activity -import android.content.Intent import android.net.Uri import android.os.Bundle import android.text.Editable @@ -28,7 +26,6 @@ import android.view.ViewGroup import android.widget.ImageView import android.widget.Toast import androidx.appcompat.app.AlertDialog -import androidx.core.net.toUri import androidx.core.view.isVisible import androidx.preference.EditTextPreference import androidx.preference.Preference @@ -38,7 +35,6 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.cache.DiskCache import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout -import com.yalantis.ucrop.UCrop import im.vector.app.R import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper import im.vector.app.core.extensions.hideKeyboard @@ -54,9 +50,7 @@ import im.vector.app.core.utils.getSizeOfFiles import im.vector.app.core.utils.toast import im.vector.app.features.MainActivity import im.vector.app.features.MainActivityArgs -import im.vector.app.features.media.createUCropWithDefaultSettings import im.vector.app.features.workers.signout.SignOutUiWorker -import im.vector.lib.multipicker.entity.MultiPickerImageType import io.reactivex.android.schedulers.AndroidSchedulers import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -74,7 +68,7 @@ import java.util.UUID import javax.inject.Inject class VectorSettingsGeneralFragment @Inject constructor( - private val colorProvider: ColorProvider + colorProvider: ColorProvider ): VectorSettingsBaseFragment(), GalleryOrCameraDialogHelper.Listener { @@ -82,7 +76,7 @@ class VectorSettingsGeneralFragment @Inject constructor( override var titleRes = R.string.settings_general_title override val preferenceXmlRes = R.xml.vector_settings_general - private val galleryOrCameraDialogHelper = GalleryOrCameraDialogHelper(this) + private val galleryOrCameraDialogHelper = GalleryOrCameraDialogHelper(this, colorProvider) private val mUserSettingsCategory by lazy { findPreference(VectorPreferences.SETTINGS_USER_SETTINGS_PREFERENCE_KEY)!! @@ -281,18 +275,6 @@ class VectorSettingsGeneralFragment @Inject constructor( session.integrationManagerService().removeListener(integrationServiceListener) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - // TODO handle this one (Ucrop lib) - @Suppress("DEPRECATION") - super.onActivityResult(requestCode, resultCode, data) - - if (resultCode == Activity.RESULT_OK) { - when (requestCode) { - UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } - } - } - } - private fun refreshIntegrationManagerSettings() { val integrationAllowed = session.integrationManagerService().isIntegrationEnabled() (findPreference(VectorPreferences.SETTINGS_ALLOW_INTEGRATIONS_KEY))!!.let { @@ -312,15 +294,7 @@ class VectorSettingsGeneralFragment @Inject constructor( } } - override fun onImageReady(image: MultiPickerImageType) { - val destinationFile = File(requireContext().cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") - val uri = image.contentUri - createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), image.displayName) - .withAspectRatio(1f, 1f) - .start(requireContext(), this) - } - - private fun onAvatarCropped(uri: Uri?) { + override fun onImageReady(uri: Uri?) { if (uri != null) { uploadAvatar(uri) } else {