Fix a few details

This commit is contained in:
Benoit Marty 2020-10-23 08:46:55 +02:00 committed by Benoit Marty
parent 03d925b5ab
commit cd56059a77
4 changed files with 27 additions and 10 deletions

View File

@ -33,7 +33,14 @@ import im.vector.lib.multipicker.MultiPicker
import im.vector.lib.multipicker.entity.MultiPickerImageType
import java.io.File
/**
* Use to let the user choose between Camera (with permission handling) and Gallery (with single image selection),
* then edit the image
* [Listener.onImageReady] will be called with an uri of a square image store in the cache of the application.
* It's up to the caller to delete the file.
*/
class GalleryOrCameraDialogHelper(
// must implement GalleryOrCameraDialogHelper.Listener
private val fragment: Fragment,
private val colorProvider: ColorProvider
) {
@ -41,9 +48,10 @@ class GalleryOrCameraDialogHelper(
fun onImageReady(uri: Uri?)
}
private val activity by lazy { fragment.requireActivity() }
private val activity
get() = fragment.requireActivity()
private val listener: Listener = fragment as? Listener ?: error("Fragment must implement GalleryOrCameraDialogHelper.Listener")
private val listener = fragment as? Listener ?: error("Fragment must implement GalleryOrCameraDialogHelper.Listener")
private val takePhotoPermissionActivityResultLauncher = fragment.registerForPermissionsResult { allGranted ->
if (allGranted) {
@ -78,39 +86,40 @@ class GalleryOrCameraDialogHelper(
}
private fun startUCrop(image: MultiPickerImageType) {
val destinationFile = File(activity.cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}")
val destinationFile = File(activity.cacheDir, "${image.displayName}_e_${System.currentTimeMillis()}")
val uri = image.contentUri
createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), image.displayName)
createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), fragment.getString(R.string.rotate_and_crop_screen_title))
.withAspectRatio(1f, 1f)
.getIntent(activity)
.let { uCropActivityResultLauncher.launch(it) }
}
private enum class Type {
Gallery,
Camera
Camera,
Gallery
}
fun show() {
AlertDialog.Builder(activity)
.setTitle(R.string.attachment_type_dialog_title)
.setItems(arrayOf(
fragment.getString(R.string.attachment_type_camera),
fragment.getString(R.string.attachment_type_gallery)
)) { dialog, which ->
dialog.cancel()
)) { _, which ->
onAvatarTypeSelected(if (which == 0) Type.Camera else Type.Gallery)
}
.setPositiveButton(R.string.cancel, null)
.show()
}
private fun onAvatarTypeSelected(type: Type) {
when (type) {
Type.Gallery ->
MultiPicker.get(MultiPicker.IMAGE).single().startWith(pickImageActivityResultLauncher)
Type.Camera ->
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, activity, takePhotoPermissionActivityResultLauncher)) {
avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(activity, takePhotoActivityResultLauncher)
}
Type.Gallery ->
MultiPicker.get(MultiPicker.IMAGE).single().startWith(pickImageActivityResultLauncher)
}
}

View File

@ -113,6 +113,7 @@ class CreateRoomFragment @Inject constructor(
}
override fun onBackPressed(toolbarButton: Boolean): Boolean {
// TODO BMA, as per the other PR, ask the user if he has started to input elements before leaving the screen
viewModel.handle(CreateRoomAction.Reset)
return false
}

View File

@ -16,6 +16,7 @@
package im.vector.app.features.roomdirectory.createroom
import androidx.core.net.toFile
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.ActivityViewModelContext
@ -104,6 +105,10 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr
private fun doReset() {
setState {
// Delete temporary file with the avatar
// TODO BMA Do this also in the other PR
avatarUri?.let { tryOrNull { it.toFile().delete() }}
CreateRoomViewState(
isEncrypted = adminE2EByDefault,
hsAdminHasDisabledE2E = !adminE2EByDefault

View File

@ -1840,12 +1840,14 @@
<string name="error_file_too_big">"The file '%1$s' (%2$s) is too large to upload. The limit is %3$s."</string>
<string name="error_attachment">"An error occurred while retrieving the attachment."</string>
<string name="attachment_type_dialog_title">"Add image from"</string>
<string name="attachment_type_file">"File"</string>
<string name="attachment_type_contact">"Contact"</string>
<string name="attachment_type_camera">"Camera"</string>
<string name="attachment_type_audio">"Audio"</string>
<string name="attachment_type_gallery">"Gallery"</string>
<string name="attachment_type_sticker">"Sticker"</string>
<string name="rotate_and_crop_screen_title">Rotate and crop</string>
<string name="error_handling_incoming_share">Couldn\'t handle share data</string>
<string name="uploads_media_title">MEDIA</string>