Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/swapnil-musale/KDeviceInfo
- Owner: swapnil-musale
- License: mit
- Created: 2024-01-26T06:32:43.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-04-14T08:59:29.000Z (7 months ago)
- Last Synced: 2024-04-14T13:20:18.404Z (7 months ago)
- Topics: android, compose-multiplatform-library, device-info, ios, kdeviceinfo, kmp, kotlin-multiplatform
- Language: Kotlin
- Homepage:
- Size: 231 KB
- Stars: 47
- Watchers: 1
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.MD
- Contributing: CONTRIBUTING.MD
- License: LICENSE
Awesome Lists containing this project
- awesome-list - swapnil-musale/KDeviceInfo - Multiplatform Library for accessing DeviceInfo (Kotlin)
README
KDeviceInfo
KDeviceInfo is Kotlin Multiplatform Library - allows to access the device details of android and
ios devices without writing expect actual boilerplate code.# Demo ❤️
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 LicenseCopyright (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.
```