Skip to content

Commit

Permalink
Refactor Device Orientation API
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnil-musale committed Apr 4, 2024
1 parent 31a19fc commit 6c7e2d4
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.devx.kdeviceinfo.model

import android.content.res.Configuration
import com.devx.kdeviceinfo.initilizer.applicationContext
import com.devx.kdeviceinfo.model.android.DeviceOrientation
import com.devx.kdeviceinfo.model.common.DeviceOrientation

internal class AndroidDeviceOrientation : DeviceOrientation {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import androidx.core.app.LocaleManagerCompat
import androidx.core.content.pm.PackageInfoCompat
import com.devx.kdeviceinfo.initilizer.applicationContext
import com.devx.kdeviceinfo.model.android.AndroidInfo
import com.devx.kdeviceinfo.model.android.DeviceOrientation
import com.devx.kdeviceinfo.model.android.DisplayMetrics
import com.devx.kdeviceinfo.model.android.Version
import com.devx.kdeviceinfo.model.android.VersionCode
import com.devx.kdeviceinfo.model.common.DeviceOrientation
import com.devx.kdeviceinfo.model.common.Locale

internal class AndroidInfoImpl : AndroidInfo {
Expand All @@ -33,6 +33,10 @@ internal class AndroidInfoImpl : AndroidInfo {
AndroidDisplayMetricsImpl()
}

private val androidDeviceOrientation: DeviceOrientation by lazy {
AndroidDeviceOrientation()
}

override val appName: String
get() = packageInfo.applicationInfo?.loadLabel(packageManager)?.toString().orEmpty()

Expand Down Expand Up @@ -102,7 +106,7 @@ internal class AndroidInfoImpl : AndroidInfo {
}

override val deviceOrientation: DeviceOrientation
get() = AndroidDeviceOrientation()
get() = androidDeviceOrientation

private fun getIsPhysicalDevice(): Boolean {
return !((Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.devx.kdeviceinfo.model.android

import com.devx.kdeviceinfo.model.common.DeviceOrientation
import com.devx.kdeviceinfo.model.common.Locale

interface AndroidInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
@file:Suppress("PropertyName")

package com.devx.kdeviceinfo.model.android
package com.devx.kdeviceinfo.model.common

interface DeviceOrientation {
val UNKNOWN: String
val PORTRAIT: String
val LANDSCAPE: String
val isPortrait: Boolean
val isLandscape: Boolean

fun getDeviceOrientation(): String
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devx.kdeviceinfo.model.ios

import com.devx.kdeviceinfo.model.common.DeviceOrientation
import com.devx.kdeviceinfo.model.common.Locale

interface IosInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package com.devx.kdeviceinfo

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.interop.LocalUIViewController
import com.devx.kdeviceinfo.model.IosInfoImpl
import com.devx.kdeviceinfo.model.android.AndroidInfo
import com.devx.kdeviceinfo.model.ios.IosInfo
import platform.UIKit.interfaceOrientation

actual class DeviceInfoXState {

Expand All @@ -29,5 +31,6 @@ actual class DeviceInfoXState {

@Composable
actual fun rememberDeviceInfoXState(): DeviceInfoXState {
return remember { DeviceInfoXState() }
val orientation = LocalUIViewController.current.interfaceOrientation
return remember(key1 = orientation) { DeviceInfoXState() }
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
package com.devx.kdeviceinfo.model

import com.devx.kdeviceinfo.model.ios.DeviceOrientation
import platform.UIKit.UIDevice
import com.devx.kdeviceinfo.model.common.DeviceOrientation
import platform.UIKit.UIApplication
import platform.UIKit.UIWindow
import platform.UIKit.UIWindowScene

internal class IosDeviceOrientationImpl : DeviceOrientation {

private val windows =
(UIApplication.sharedApplication.connectedScenes.first() as UIWindowScene).windows
private val windowOrientation =
(windows.first() as UIWindow).windowScene?.interfaceOrientation?.toInt()

override val UNKNOWN: String
get() = "unknown"
override val PORTRAIT: String
get() = "portrait"
override val LANDSCAPE: String
get() = "landscape"
override val PORTRAIT_UPSIDE_DOWN: String
get() = "portraitUpsideDown"
override val LANDSCAPE_LEFT: String
get() = "landscapeLeft"
override val LANDSCAPE_RIGHT: String
get() = "landscapeRight"
override val FACE_UP: String
get() = "faceUp"
override val FACE_DOWN: String
get() = "faceDown"
override val isPortrait: Boolean
get() = UIDevice.currentDevice.orientation.value.toString() == PORTRAIT
get() = windowOrientation == 1
override val isLandscape: Boolean
get() = windowOrientation == 3 || windowOrientation == 4

override fun getDeviceOrientation(): String {
return when (UIDevice.currentDevice.orientation.value.toInt()) {
return when (windowOrientation) {
0 -> UNKNOWN
1 -> PORTRAIT
2 -> PORTRAIT_UPSIDE_DOWN
3 -> LANDSCAPE_LEFT
4 -> LANDSCAPE_RIGHT
5 -> FACE_UP
6 -> FACE_DOWN
3, 4 -> LANDSCAPE
else -> UNKNOWN
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.devx.kdeviceinfo.model

import com.devx.kdeviceinfo.model.common.DeviceOrientation
import com.devx.kdeviceinfo.model.common.Locale
import com.devx.kdeviceinfo.model.ios.DeviceOrientation
import com.devx.kdeviceinfo.model.ios.IosInfo
import platform.Foundation.NSBundle
import platform.Foundation.NSLocale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ private fun ShowAndroidDeviceInfo(androidInfo: AndroidInfo) {
Text(text = "IsPhysicalDevice : ${androidInfo.isPhysicalDevice}")
Text(text = "Manufacturer : ${androidInfo.manufacturer}")
Text(text = "Model : ${androidInfo.model}")
Text(text = "Orientation : ${androidInfo.deviceOrientation.getDeviceOrientation()}")
Text(text = "Device Orientation : ${androidInfo.deviceOrientation.getDeviceOrientation()}")
Text(text = "IsPortrait : ${androidInfo.deviceOrientation.isPortrait}")
Spacer(modifier = Modifier.fillMaxWidth().height(height = 20.dp))

// Locale
Expand Down Expand Up @@ -103,7 +104,8 @@ private fun ShowIosDeviceInfo(iosInfo: IosInfo) {
Text(text = "SystemVersion : ${iosInfo.systemVersion}")
Text(text = "LocalizedModel : ${iosInfo.localizedModel}")
Text(text = "IsPhysicalDevice : ${iosInfo.isPhysicalDevice}")
Text(text = "Orientation : ${iosInfo.deviceOrientation.getDeviceOrientation()}")
Text(text = "Device Orientation : ${iosInfo.deviceOrientation.getDeviceOrientation()}")
Text(text = "IsPortrait : ${iosInfo.deviceOrientation.isPortrait}")
Spacer(modifier = Modifier.fillMaxWidth().height(height = 20.dp))

// Locale Info
Expand Down

0 comments on commit 6c7e2d4

Please sign in to comment.