Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/swapnil-musale/KDeviceInfo

Multiplatform Library for accessing DeviceInfo
https://github.com/swapnil-musale/KDeviceInfo

android compose-multiplatform-library device-info ios kdeviceinfo kmp kotlin-multiplatform

Last synced: 3 months ago
JSON representation

Multiplatform Library for accessing DeviceInfo

Awesome Lists containing this project

README

        

KDeviceInfo

KDeviceInfo is Kotlin Multiplatform Library - allows to access the device details of android and
ios devices without writing expect actual boilerplate code.


Android
Ios
Maven Central
CI Status

# Demo ❤️



Android
Ios


android
ios

> **IMPORTANT:**

> The project is in the experimental phase. All APIs can change incompatibly or be dropped without the deprecation cycle!

# Installation

This library is available on mavenCentral. To use it in your project add following block of code
in ```settings.gradle.kts``` file if not available.

```
repositories {
...
mavenCentral()
}
```

### 1. Kotlin Multiplatform
Add the library dependency in ```shared``` module (```build.gradle.kts```)

```
commonMain.dependencies {
...
api or implementation("io.github.swapnil-musale:KDeviceInfo:0.0.7")
}
```

### 2. Compose Multiplatform

Add the library dependency in ```composeApp``` module (```build.gradle.kts```)

```
commonMain.dependencies {
...
implementation("io.github.swapnil-musale:KDeviceInfo:0.0.7")
}
```

# Usage

Implementation of **KDeviceInfo** is pretty straightforward. Simply import the **DeviceInfoXState**
and start accessing device information in your Compose Multiplatform project.

```
@Composable
fun DeviceInfoExample() {

val deviceInfoXState: DeviceInfoXState = rememberDeviceInfoXState()

Column(modifier = Modifier.fillMaxSize()) {
if (deviceInfoXState.isAndroid) {
val androidInfo = deviceInfoXState.androidInfo
Text(text = "App Name : ${androidInfo.appName}")
Text(text = "SdkInt : ${androidInfo.version.sdkInt}")
Text(text = "PackageName : ${androidInfo.packageName}")
} else if(deviceInfoXState.isIos) {
val iosInfo = deviceInfoXState.iosInfo
Text(text = "SystemName : ${iosInfo.systemName}")
Text(text = "SystemVersion : ${iosInfo.systemVersion}")
Text(text = "IsPhysicalDevice : ${iosInfo.isPhysicalDevice}")
} else if(deviceInfoXState.isDesktop) {
Text(text = "Welcome to Desktop App")
}
}
}
```

**OR** Use helper function to access device information

```
@Composable
fun DeviceInfoExample() {
Column(modifier = Modifier.fillMaxSize()) {
OnPlatform(
onAndroid = { androidInfo ->
Text(text = "App Name : ${androidInfo.appName}")
Text(text = "SdkInt : ${androidInfo.version.sdkInt}")
Text(text = "PackageName : ${androidInfo.packageName}")
},
onIos = { iosInfo ->
Text(text = "SystemName : ${iosInfo.systemName}")
Text(text = "SystemVersion : ${iosInfo.systemVersion}")
Text(text = "IsPhysicalDevice : ${iosInfo.isPhysicalDevice}")
}
)
}
}
```

To access the device details outside the Composable function you can make use of **DeviceInfoState** as below

```
class AppViewModel {

private val deviceInfoXState: DeviceInfoXState = DeviceInfoXState()

init {
if (deviceInfoXState.isAndroid) {
val androidInfo: AndroidInfo = deviceInfoXState.androidInfo
println("DeviceInfoX - App Name : ${androidInfo.appName}")
println("DeviceInfoX - Version Name : ${androidInfo.versionName}")
} else else if (deviceInfoXState.isIos) {
val iosInfo: IosInfo = deviceInfoXState.iosInfo
println("DeviceInfoX - App Name : ${iosInfo.appName}")
println("DeviceInfoX - App Version : ${iosInfo.appVersion}")
}
}
}
```

**OR** Use helper function to access device information

```
class AppViewModel {
init {
onPlatform(
onAndroid = { androidInfo ->
println("DeviceInfoX - App Name : ${androidInfo.appName}")
println("DeviceInfoX - Version Name : ${androidInfo.versionName}")
},
onIos = { iosInfo ->
println("DeviceInfoX - App Name : ${iosInfo.appName}")
println("DeviceInfoX - App Version : ${iosInfo.appVersion}")
}
)
}
}
```

You can access more info using this **State** API, for more information about API usage in your
Compose Multiplatform project check out [Sample App][0].

# Contributions

Contributions to KDeviceInfo are super welcome checkout [Contributions Guidelines][4]. If you encounter any issues or have suggestions for
improvements, please feel free to open an issue or submit a pull request on GitHub.

## Connect with me

[![Github Profile](https://skillicons.dev/icons?i=github)][1]
[![LinkedIn](https://skillicons.dev/icons?i=linkedin)][2]
[![Twitter](https://skillicons.dev/icons?i=twitter)][3]

[0]: https://github.com/swapnil-musale/KDeviceInfo/blob/master/sampleApp/composeApp/src/commonMain/kotlin/com/devx/kdeviceinfo/sample/App.kt
[1]: https://github.com/swapnil-musale
[2]: https://linkedin.com/in/swapnil-musale
[3]: https://twitter.com/swapnil_musale
[4]: https://github.com/swapnil-musale/KDeviceInfo/blob/master/CONTRIBUTING.MD

# Licence

```
MIT License

Copyright (c) 2024 Swapnil Musale

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```