mr0xf00 / easycrop

Image cropper for jetpack compose
Apache License 2.0
81 stars 17 forks source link

Make initial state setup #3

Open distivi opened 1 year ago

distivi commented 1 year ago

Hi there, cool library! Thanks for that.

For my use case would be super nice to have initial state setup before actually presenting the dialog.

Basically what I need in method CropState put my custom properties:

internal fun CropState(
    src: ImageSrc,
    transform: ImgTransform =  ImgTransform.Identity, // add
    shape: CropShape = RectCropShape, // add
   // ... here the rest of parameters like aspectLock, initialAspectRatio, region etc.
    onDone: () -> Unit = {},
): CropState = object : CropState {
    val defaultTransform: ImgTransform = transform  // modify
    val defaultShape: CropShape = shape // modify
    val defaultAspectLock: Boolean = false
    override val src: ImageSrc get() = src
    private var _transform: ImgTransform by mutableStateOf(defaultTransform)
    override var transform: ImgTransform
        get() = _transform
        set(value) {
            onTransformUpdated(transform, value)
            _transform = value
        }

    val defaultRegion = src.size.toSize().toRect()

Or maybe having separate data class with initial values, then it could be used in reset function as well.

mr0xf00 commented 1 year ago

Thanks for the suggestion. Currently, the CropState constuctor function is not part of the public api, also ImageCropper creates the instance internally. I guess in a future release you would have the option to pass your own CropState to ImageCropper::crop.

In the meantime you can do something like this

val cropState = imageCropper.cropState 
LaunchedEffect(key = cropState != null) {
    cropState?.let { state ->
        state.shape = TriangleCropShape
        state.aspectLock = true
    }
}
//.....
if (cropState != null) ImageCropperDialog(state = cropState)

Which would allow you change anything you want and would only be called once before the crop dialog is shown Hope that helps !

nguyennn-teq commented 1 year ago

Thanks for the suggestion. Currently, the CropState constuctor function is not part of the public api, also ImageCropper creates the instance internally. I guess in a future release you would have the option to pass your own CropState to ImageCropper::crop.

In the meantime you can do something like this

val cropState = imageCropper.cropState 
LaunchedEffect(key = cropState != null) {
    cropState?.let { state ->
        state.shape = TriangleCropShape
        state.aspectLock = true
    }
}
//.....
if (cropState != null) ImageCropperDialog(state = cropState)

Which would allow you change anything you want and would only be called once before the crop dialog is shown Hope that helps !

Hope this coming soon. And give you a big thanks for the plugin