Vadim Zuev
07/28/2024, 4:56 PMComposeUIViewController
which looks like this:
@ExportObjCClass
@OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
class MyComposeUIViewControllerDelegate : ComposeUIViewControllerDelegate {
override fun viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter.addObserver(
observer = this,
selector = NSSelectorFromString("orientationDidChange"),
name = UIDeviceOrientationDidChangeNotification,
`object` = null
)
}
@ObjCAction
fun orientationDidChange(notification: NSNotification) {
Log.w("###") { "orientation changed" }
}
}
It always crashes with an 'NSInvalidArgumentException', reason: '-[ComposeAppMyComposeUIViewControllerDelegate orientationDidChange]: unrecognized selector sent to instance
. The root cause seems to be that the orientationDidChange
method somehow gets an invalid / mismatching signature in objC. I’ve tried adding a colon when passing the selector like`NSSelectorFromString("orientationDidChange:"),` adding an @ObjCName("orientationDidChange")
annotation to the method, to no avail. I suppose there is a bug somewhere in the cinterop layer, but before I move on raising an issue, does anyone have any further idea?brucemax
07/28/2024, 5:51 PMfun ScreenSizeInfo.getScreenOrientation(): ScreenOrientation {
return if (widthDP > heightDP) ScreenOrientation.LANDSCAPE else ScreenOrientation.PORTRAIT
}
iOS implementation for getting ScreenSizeInfo:
actual fun getScreenSizeInfo(): ScreenSizeInfo {
val density = LocalDensity.current
val config = LocalWindowInfo.current.containerSize
return ScreenSizeInfo(
heightDP = with(density) { config.height.toDp() },
widthDP = with(density) { config.width.toDp() }
)
}
P.S. Divers thread 😅Vadim Zuev
07/28/2024, 6:07 PMactual fun getPhysicalCameraPosition(): LandscapeCameraPosition =
when (getCurrentDisplay()?.rotation) {
Surface.ROTATION_90 -> LandscapeCameraPosition.LEFT
Surface.ROTATION_270 -> LandscapeCameraPosition.RIGHT
else -> LandscapeCameraPosition.UNSPECIFIED
}
The problem: there is no automatic recomposition when the device (no matter which OS) is rotated by 180 degrees, as opposed to a 90 deg rotation. So I had to trigger it on my own by mutating a state. For Android, I solved this using the DisplayManager.DisplayListener
and for iOS, I want to use the approach described in my post. Indeed I could do the detection in Swift and then call some Kotlin code, but my preferred approach would be Kotlin-only.