Skip to content

fix: keep torch and zoom state on screen rotation - #64

Merged
alexgerardojacinto merged 2 commits into
mainfrom
fix/RMET-4398-keep-state-on-rotation
Dec 4, 2026
Merged

alexgerardojacinto merged 2 commits into
mainfrom
fix/RMET-4398-keep-state-on-rotation

Conversation

@alexgerardojacinto

@alexgerardojacinto alexgerardojacinto commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

References: https://outsystemsrd.atlassian.net/browse/RMET-4398

Torch and zoom state lived in remember inside composables, so it was lost when the activity was recreated (rotation, theme, language, etc.). The state is now held at activity level, saved in onSaveInstanceState, and re-applied to the camera once it is bound.

Tested on a Pixel (Android 17) with the Capacitor barcode sample app, built against this branch: turned the torch on and selected 2x zoom, rotated to landscape and back; both were kept, and the torch stayed physically on.
Not tested: the Cordova plugin path, other devices / Android versions, or large screens.

@alexgerardojacinto
alexgerardojacinto marked this pull request as ready for review December 1, 2026 20:55
@alexgerardojacinto
alexgerardojacinto requested a review from a team as a code owner December 1, 2026 20:55
@alexgerardojacinto
alexgerardojacinto requested review from ItsChaceD and OS-pedrogustavobilro and removed request for a team December 1, 2026 20:55
References: https://outsystemsrd.atlassian.net/browse/RMET-4398

Torch and zoom state lived in `remember` inside composables, so it was lost when the activity was recreated (rotation, theme, language, etc.). The state is now held at activity level, saved in `onSaveInstanceState`, and re-applied to the camera once it is bound.

Not yet tested on a device.
@alexgerardojacinto
alexgerardojacinto force-pushed the fix/RMET-4398-keep-state-on-rotation branch from c8f80ee to c51753c Compare December 2, 2026 13:05
@OS-pedrogustavobilro OS-pedrogustavobilro self-assigned this Dec 4, 2026

@OS-pedrogustavobilro OS-pedrogustavobilro left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The solution seems alright, but looking at the code I noticed some stuff.

private var permissionRequestCount = 0
private var showDialog by mutableStateOf(false)
private var permissionGiven by mutableStateOf(false)
private var isScanning = false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This wasn't originally in the task, but now I'm seeing that isScanning does not survive configuration changes either. If you turn on the scan button, tap it, rotate the screen (or other config changes), the button is no longer scanning.

We can create a separate task for this if preferable not to fix here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, fixed in 2c44d38. isScanning is now a mutableStateOf saved in onSaveInstanceState and restored in onCreate (when there is a scan button; without one it still starts as scanning). ScanButton reads the activity's value instead of keeping its own remember state, so the button and the scan callback can't disagree. Tested on a Pixel with the Capacitor example app: scan button on, tap it, rotate to landscape and back, it stays in the scanning state; tap again to turn it off, rotate, it stays off.

class OSBARCScannerActivity : ComponentActivity() {
private var camera: Camera? = null
private lateinit var selector: CameraSelector
private var permissionRequestCount = 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again, unrelated to this PR specifically, but this value permissionRequestCount also does not survive configuration changes.

This one is less impactful than isScanning imo. On a fresh install, you get prompted the camera permission, if you deny, we show a dialog about enabling it in seconds. If you rotate the screen, you get the permission prompt a second time.

Then there's the fact that the SideEffect keeps calling requestPermissionLauncher.launch(Manifest.permission.CAMERA) whenever there's a config change, not just when the screen first opens which I think would be the expected. But that part could probably be fixed if we checked if the permission was granted properly before requesting it.

But yeah, not specific to this PR, so we don't need to tackle it here if you don't want to.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 2c44d38 as well. permissionRequestCount is now saved and restored, and the SideEffect only requests the permission if it hasn't been requested yet and the camera permission isn't already granted. Tested on a Pixel with a fresh install: rotating while the system prompt is showing gives a single prompt; after denying, rotating twice shows the 'Camera Access Not Enabled' dialog again with no second system prompt. I didn't run the old build for comparison, so the before behaviour is from reading the code.

Addresses review feedback: isScanning was reset on every recreation, and
permissionRequestCount made the camera permission be requested again after
rotation. Only request the permission when it is not already granted.

@OS-pedrogustavobilro OS-pedrogustavobilro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tested here, working good, nice! Approved

Comment thread CHANGELOG.md

## [Unreleased]

### 01-12-2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

minor: The date on this changelog seems reversed when compared to older entries

Also, idk if we want to go ahead with a release right away or not, but in case we do, it's also missing a pom.xml and setting the version in the changelog here too.

@alexgerardojacinto
alexgerardojacinto merged commit 56eed58 into main Dec 4, 2026
7 checks passed
@alexgerardojacinto alexgerardojacinto mentioned this pull request Dec 4, 2026
1 task done
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