Use new way to start Activity with uCrop

This commit is contained in:
Benoit Marty 2020-10-22 22:54:11 +02:00 committed by Benoit Marty
parent 1555c06b21
commit 72bf2f9c1a
3 changed files with 33 additions and 60 deletions

View File

@ -19,20 +19,26 @@ package im.vector.app.core.dialogs
import android.app.Activity import android.app.Activity
import android.net.Uri import android.net.Uri
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.yalantis.ucrop.UCrop
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.registerStartForActivityResult 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.PERMISSIONS_FOR_TAKING_PHOTO
import im.vector.app.core.utils.checkPermissions import im.vector.app.core.utils.checkPermissions
import im.vector.app.core.utils.registerForPermissionsResult 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.MultiPicker
import im.vector.lib.multipicker.entity.MultiPickerImageType import im.vector.lib.multipicker.entity.MultiPickerImageType
import java.io.File
class GalleryOrCameraDialogHelper( class GalleryOrCameraDialogHelper(
private val fragment: Fragment private val fragment: Fragment,
private val colorProvider: ColorProvider
) { ) {
interface Listener { interface Listener {
fun onImageReady(image: MultiPickerImageType) fun onImageReady(uri: Uri?)
} }
private val activity by lazy { fragment.requireActivity() } private val activity by lazy { fragment.requireActivity() }
@ -50,7 +56,7 @@ class GalleryOrCameraDialogHelper(
avatarCameraUri?.let { uri -> avatarCameraUri?.let { uri ->
MultiPicker.get(MultiPicker.CAMERA) MultiPicker.get(MultiPicker.CAMERA)
.getTakenPhoto(fragment.requireContext(), uri) .getTakenPhoto(fragment.requireContext(), uri)
?.let { listener.onImageReady(it) } ?.let { startUCrop(it) }
} }
} }
} }
@ -61,7 +67,23 @@ class GalleryOrCameraDialogHelper(
.get(MultiPicker.IMAGE) .get(MultiPicker.IMAGE)
.getSelectedFiles(fragment.requireContext(), activityResult.data) .getSelectedFiles(fragment.requireContext(), activityResult.data)
.firstOrNull() .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)) }
} }
} }

View File

@ -16,15 +16,12 @@
package im.vector.app.features.roomdirectory.createroom package im.vector.app.features.roomdirectory.createroom
import android.app.Activity import android.net.Uri
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.core.net.toUri
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState import com.airbnb.mvrx.withState
import com.yalantis.ucrop.UCrop
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper
import im.vector.app.core.extensions.cleanup 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.OnBackPressed
import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.core.resources.ColorProvider 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.RoomDirectorySharedAction
import im.vector.app.features.roomdirectory.RoomDirectorySharedActionViewModel import im.vector.app.features.roomdirectory.RoomDirectorySharedActionViewModel
import im.vector.lib.multipicker.entity.MultiPickerImageType
import kotlinx.android.synthetic.main.fragment_create_room.* import kotlinx.android.synthetic.main.fragment_create_room.*
import timber.log.Timber import timber.log.Timber
import java.io.File
import javax.inject.Inject import javax.inject.Inject
class CreateRoomFragment @Inject constructor( class CreateRoomFragment @Inject constructor(
@ -52,7 +46,7 @@ class CreateRoomFragment @Inject constructor(
private lateinit var sharedActionViewModel: RoomDirectorySharedActionViewModel private lateinit var sharedActionViewModel: RoomDirectorySharedActionViewModel
private val viewModel: CreateRoomViewModel by activityViewModel() 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 override fun getLayoutResId() = R.layout.fragment_create_room
@ -85,25 +79,8 @@ class CreateRoomFragment @Inject constructor(
galleryOrCameraDialogHelper.show() galleryOrCameraDialogHelper.show()
} }
override fun onImageReady(image: MultiPickerImageType) { override fun onImageReady(uri: Uri?) {
val destinationFile = File(requireContext().cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") viewModel.handle(CreateRoomAction.SetAvatar(uri))
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 onNameChange(newName: String) { override fun onNameChange(newName: String) {

View File

@ -18,8 +18,6 @@
package im.vector.app.features.settings package im.vector.app.features.settings
import android.app.Activity
import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
@ -28,7 +26,6 @@ import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.Preference import androidx.preference.Preference
@ -38,7 +35,6 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.cache.DiskCache import com.bumptech.glide.load.engine.cache.DiskCache
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import com.yalantis.ucrop.UCrop
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper
import im.vector.app.core.extensions.hideKeyboard 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.core.utils.toast
import im.vector.app.features.MainActivity import im.vector.app.features.MainActivity
import im.vector.app.features.MainActivityArgs import im.vector.app.features.MainActivityArgs
import im.vector.app.features.media.createUCropWithDefaultSettings
import im.vector.app.features.workers.signout.SignOutUiWorker import im.vector.app.features.workers.signout.SignOutUiWorker
import im.vector.lib.multipicker.entity.MultiPickerImageType
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
@ -74,7 +68,7 @@ import java.util.UUID
import javax.inject.Inject import javax.inject.Inject
class VectorSettingsGeneralFragment @Inject constructor( class VectorSettingsGeneralFragment @Inject constructor(
private val colorProvider: ColorProvider colorProvider: ColorProvider
): ):
VectorSettingsBaseFragment(), VectorSettingsBaseFragment(),
GalleryOrCameraDialogHelper.Listener { GalleryOrCameraDialogHelper.Listener {
@ -82,7 +76,7 @@ class VectorSettingsGeneralFragment @Inject constructor(
override var titleRes = R.string.settings_general_title override var titleRes = R.string.settings_general_title
override val preferenceXmlRes = R.xml.vector_settings_general override val preferenceXmlRes = R.xml.vector_settings_general
private val galleryOrCameraDialogHelper = GalleryOrCameraDialogHelper(this) private val galleryOrCameraDialogHelper = GalleryOrCameraDialogHelper(this, colorProvider)
private val mUserSettingsCategory by lazy { private val mUserSettingsCategory by lazy {
findPreference<PreferenceCategory>(VectorPreferences.SETTINGS_USER_SETTINGS_PREFERENCE_KEY)!! findPreference<PreferenceCategory>(VectorPreferences.SETTINGS_USER_SETTINGS_PREFERENCE_KEY)!!
@ -281,18 +275,6 @@ class VectorSettingsGeneralFragment @Inject constructor(
session.integrationManagerService().removeListener(integrationServiceListener) 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() { private fun refreshIntegrationManagerSettings() {
val integrationAllowed = session.integrationManagerService().isIntegrationEnabled() val integrationAllowed = session.integrationManagerService().isIntegrationEnabled()
(findPreference<SwitchPreference>(VectorPreferences.SETTINGS_ALLOW_INTEGRATIONS_KEY))!!.let { (findPreference<SwitchPreference>(VectorPreferences.SETTINGS_ALLOW_INTEGRATIONS_KEY))!!.let {
@ -312,15 +294,7 @@ class VectorSettingsGeneralFragment @Inject constructor(
} }
} }
override fun onImageReady(image: MultiPickerImageType) { override fun onImageReady(uri: Uri?) {
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?) {
if (uri != null) { if (uri != null) {
uploadAvatar(uri) uploadAvatar(uri)
} else { } else {