Repository navigation
fix: keep torch and zoom state on screen rotation - #64
Conversation
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.
c8f80ee to
c51753c
Compare
| private var permissionRequestCount = 0 | ||
| private var showDialog by mutableStateOf(false) | ||
| private var permissionGiven by mutableStateOf(false) | ||
| private var isScanning = false |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Tested here, working good, nice! Approved
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### 01-12-2026 |
There was a problem hiding this comment.
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.
References: https://outsystemsrd.atlassian.net/browse/RMET-4398
Torch and zoom state lived in
rememberinside composables, so it was lost when the activity was recreated (rotation, theme, language, etc.). The state is now held at activity level, saved inonSaveInstanceState, 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.