Repository navigation
fix: keep image editing state on screen rotation - #26
Conversation
andredestro
left a comment
There was a problem hiding this comment.
Thanks for this! I built it into the Capacitor camera example app and tried it on an Android emulator, using a 30MP image so the transforms are slow enough to hit timing issues. The happy path works: flip, rotate and crop survive a device rotation, and OK after rotating saves what's on screen. A few things I ran into:
-
Tapping rotate twice quickly showed 90°, but after rotating the device it came back at 180°. Could both coroutines be reading the same drawable while
appliedOperationsrecords two entries? Would disabling the buttons while a transform runs be enough? -
Tapping rotate and immediately rotating the device dropped the rotation (with a 1s gap it was kept). Could
onSaveInstanceStatebe running before the op is added, sinceCoroutineScope(Default)isn't tied to the activity? -
With the crop at minimum height in portrait (~366px), landscape restored it at ~203px, below the ~286px minimum there, and the first touch snapped it ~88px. Should
applyPendingNormalizedCropclamp to the min size and limits frame?
A few smaller questions from reading the code:
- Since rotate and flip compose, could we store just
(quarterTurns % 4, flipped)instead of the full list? That would also cap the replay cost on large images. pendingNormalizedCropis cleared before the empty-frame check, so could the crop be dropped ifsetupCornersruns beforesetLimitFrame? And wouldroundToInt()avoid the 1px shrink per rotation?- Would a round-trip test for the crop normalization and the replay order fit in
EditPictureTests.kt?
Rotating and flipping compose into one of eight orientations, so the editor state is now (quarterTurns, isFlipped) instead of the list of operations. Restoring it applies at most one flip and three rotations, whatever the number of taps, which keeps the cost bounded on large images.
…running The edit was recorded only after the transform finished, on a scope that is not tied to the activity, so rotating the device right after a tap dropped it. Two quick taps also read the same image, so the second result replaced the first while both were recorded, and the restored image came back rotated twice. The new state is now recorded on the main thread when the tap is accepted, and further rotate/flip taps are ignored until the transform is done. If the transform fails the previous state is put back.
The crop was restored as plain fractions of the new limits frame, so a minimum-size crop in portrait came back below the minimum in landscape and snapped on the first touch. It now grows to the minimum size around its center and is moved inside the limits, and the corners are rounded instead of truncated so a crop does not shrink by a pixel on every rotation. The pending crop was also forgotten before it was known whether the corners and the limits frame existed. It is now kept until it has been applied, and is also tried when the limits frame is set.
|
Thanks for the thorough review, @andredestro. I pushed one commit per point:
The tests are in their own files ( I did not move the transforms to Tested on a Pixel with the Capacitor camera example app: rotate then rotating the device straight away keeps the rotation, two quick taps give the same image before and after rotating the device, a minimum-height crop in portrait (~365px) is restored at ~285px in landscape with no snap on the first touch, and OK after rotating saves what is on screen. I could not reproduce the slow-transform timing on that device with a small image, so the 30MP case is covered by the code change, not by a test of mine. If you can, please run it again with your large image. |
Description
The image editor activity is recreated on rotation, losing crop, rotation and mirroring. Applied rotate/flip operations and the normalized crop rectangle are now saved in the activity state and replayed after the image reloads.
Context
https://outsystemsrd.atlassian.net/browse/RMET-4411
Type of changes
Tests
Tested on a Pixel (Android 17) with the Capacitor camera example app, built against this branch: opened the editor on a grid test image, flipped, rotated left and dragged the crop corner, then rotated the device to landscape and back. Flip, rotation and crop were all kept.
Not tested: confirming OK after a rotation saves the expected cropped image, the Cordova plugin path, other devices / Android versions, or tablets.