`actual class ScreenOrientationObserver {` `pr...
# multiplatform
k
actual class ScreenOrientationObserver {
private val currentOrientation = mutableStateOf(ScreenOrientation.Portrait)
init {
class OrientationListener : NSObject() {
@Suppress("UNUSED_PARAMETER")
@ObjCAction
fun orientationDidChange(arg: NSNotification) {
val newOrientation = when (UIDevice.currentDevice.orientation) {
UIDeviceOrientation.UIDeviceOrientationPortrait,
UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown -> ScreenOrientation.Portrait
UIDeviceOrientation.UIDeviceOrientationLandscapeLeft,
UIDeviceOrientation.UIDeviceOrientationLandscapeRight -> ScreenOrientation.Landscape
else -> currentOrientation.value
}
currentOrientation.value = newOrientation
}
}
val listener = OrientationListener()
val notificationName = platform.UIKit.UIDeviceOrientationDidChangeNotification
NSNotificationCenter.defaultCenter.addObserver(
observer = listener,
selector = NSSelectorFromString(
OrientationListener::orientationDidChange.name + ":"
),
name = notificationName,
``object` = null`
)
}
actual val orientation: State<ScreenOrientation> = currentOrientation
}
actual fun createScreenOrientationObserver(): ScreenOrientationObserver {
`return ScreenOrientati`onObserver() }