Skip to content

fix: keep image editing state on screen rotation - #26

Merged
alexgerardojacinto merged 6 commits into
mainfrom
fix/RMET-4411-keep-edit-state-on-rotation
Dec 4, 2026
Merged

alexgerardojacinto merged 6 commits into
mainfrom
fix/RMET-4411-keep-edit-state-on-rotation

Conversation

@alexgerardojacinto

@alexgerardojacinto alexgerardojacinto commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Refactor (cosmetic changes)
  • Breaking change (change that would cause existing functionality to not work as expected)

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.

@alexgerardojacinto
alexgerardojacinto marked this pull request as ready for review December 1, 2026 20:55

@andredestro andredestro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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 appliedOperations records two entries? Would disabling the buttons while a transform runs be enough?

  2. Tapping rotate and immediately rotating the device dropped the rotation (with a 1s gap it was kept). Could onSaveInstanceState be running before the op is added, since CoroutineScope(Default) isn't tied to the activity?

  3. 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 applyPendingNormalizedCrop clamp 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.
  • pendingNormalizedCrop is cleared before the empty-frame check, so could the crop be dropped if setupCorners runs before setLimitFrame? And would roundToInt() 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.
@alexgerardojacinto

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review, @andredestro. I pushed one commit per point:

  • 63ed117 keeps (quarterTurns, isFlipped) instead of the list of operations. Rotate and flip compose, so a flip after rotations negates the turns. Restoring now applies at most one flip and three rotations, whatever the number of taps.
  • 9bb8ca2 (bugs 1 and 2) records the new state on the main thread when the tap is accepted, so onSaveInstanceState already has it while the transform is still running. Rotate/flip taps are ignored until the transform is done, so two quick taps no longer read the same image. If the transform fails, the previous state is put back.
  • 25484e6 (bug 3) grows the restored crop to the minimum size around its center and moves it inside the limits frame. Corners are rounded instead of truncated. pendingNormalizedCrop is now cleared only after it has been applied, and it is also tried from setLimitFrame, so the order of setupCorners and setLimitFrame no longer matters.
  • d910829 adds tests for the crop normalization (round trip, 20 rotations without shrinking, growing to the minimum, staying inside the limits) and for the rotate/flip replay order (every sequence up to 8 operations against a literal rotate/flip of a small grid).

The tests are in their own files (IONCAMRNormalizedCropTests, IONCAMRImageEditStateTests) instead of EditPictureTests.kt, because the logic is in plain classes that don't need the Mockito setup of that file.

I did not move the transforms to lifecycleScope: the state is now recorded when the tap is accepted, so a transform that finishes on a destroyed view loses nothing, because the recreated view applies the recorded state itself.

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.

@alexgerardojacinto
alexgerardojacinto merged commit 2688ce5 into main Dec 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants