This commit is contained in:
Benoit Marty 2020-10-22 22:56:51 +02:00
parent 72bf2f9c1a
commit 487e82df3f
1 changed files with 14 additions and 15 deletions

View File

@ -43,7 +43,7 @@ class GalleryOrCameraDialogHelper(
private val activity by lazy { fragment.requireActivity() } private val activity by lazy { fragment.requireActivity() }
private val listener: Listener = fragment as? Listener ?: error("Fragment must implements GalleryOrCameraDialogHelper.Listener") private val listener: Listener = fragment as? Listener ?: error("Fragment must implement GalleryOrCameraDialogHelper.Listener")
private val takePhotoPermissionActivityResultLauncher = fragment.registerForPermissionsResult { allGranted -> private val takePhotoPermissionActivityResultLauncher = fragment.registerForPermissionsResult { allGranted ->
if (allGranted) { if (allGranted) {
@ -55,7 +55,7 @@ class GalleryOrCameraDialogHelper(
if (activityResult.resultCode == Activity.RESULT_OK) { if (activityResult.resultCode == Activity.RESULT_OK) {
avatarCameraUri?.let { uri -> avatarCameraUri?.let { uri ->
MultiPicker.get(MultiPicker.CAMERA) MultiPicker.get(MultiPicker.CAMERA)
.getTakenPhoto(fragment.requireContext(), uri) .getTakenPhoto(activity, uri)
?.let { startUCrop(it) } ?.let { startUCrop(it) }
} }
} }
@ -65,35 +65,34 @@ class GalleryOrCameraDialogHelper(
if (activityResult.resultCode == Activity.RESULT_OK) { if (activityResult.resultCode == Activity.RESULT_OK) {
MultiPicker MultiPicker
.get(MultiPicker.IMAGE) .get(MultiPicker.IMAGE)
.getSelectedFiles(fragment.requireContext(), activityResult.data) .getSelectedFiles(activity, activityResult.data)
.firstOrNull() .firstOrNull()
?.let { startUCrop(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 -> private val uCropActivityResultLauncher = fragment.registerStartForActivityResult { activityResult ->
if (activityResult.resultCode == Activity.RESULT_OK) { if (activityResult.resultCode == Activity.RESULT_OK) {
activityResult.data?.let { listener.onImageReady(UCrop.getOutput(it)) } activityResult.data?.let { listener.onImageReady(UCrop.getOutput(it)) }
} }
} }
private fun startUCrop(image: MultiPickerImageType) {
val destinationFile = File(activity.cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}")
val uri = image.contentUri
createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), image.displayName)
.withAspectRatio(1f, 1f)
.getIntent(activity)
.let { uCropActivityResultLauncher.launch(it) }
}
private enum class Type { private enum class Type {
Gallery, Gallery,
Camera Camera
} }
fun show() { fun show() {
AlertDialog.Builder(fragment.requireContext()) AlertDialog.Builder(activity)
.setItems(arrayOf( .setItems(arrayOf(
fragment.getString(R.string.attachment_type_camera), fragment.getString(R.string.attachment_type_camera),
fragment.getString(R.string.attachment_type_gallery) fragment.getString(R.string.attachment_type_gallery)
@ -110,7 +109,7 @@ class GalleryOrCameraDialogHelper(
MultiPicker.get(MultiPicker.IMAGE).single().startWith(pickImageActivityResultLauncher) MultiPicker.get(MultiPicker.IMAGE).single().startWith(pickImageActivityResultLauncher)
Type.Camera -> Type.Camera ->
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, activity, takePhotoPermissionActivityResultLauncher)) { if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, activity, takePhotoPermissionActivityResultLauncher)) {
avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(fragment.requireContext(), takePhotoActivityResultLauncher) avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(activity, takePhotoActivityResultLauncher)
} }
} }
} }