diff options
-rw-r--r-- | build.gradle | 1 | ||||
-rw-r--r-- | kvision-modules/kvision-cordova/build.gradle | 5 | ||||
-rw-r--r-- | kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Battery.kt | 54 | ||||
-rw-r--r-- | kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Device.kt | 69 | ||||
-rw-r--r-- | settings.gradle | 3 |
5 files changed, 131 insertions, 1 deletions
diff --git a/build.gradle b/build.gradle index cb32f60f..8e1ed991 100644 --- a/build.gradle +++ b/build.gradle @@ -178,6 +178,7 @@ dokka { 'kvision-modules/kvision-server-jooby/src/main/kotlin', 'kvision-modules/kvision-server-ktor/src/main/kotlin', 'kvision-modules/kvision-server-spring-boot/src/main/kotlin', + 'kvision-modules/kvision-cordova/src/main/kotlin', 'kvision-modules/kvision-electron/src/main/kotlin') classpath = [new File("dokka/kvision-dokka-helper.jar")] outputFormat = 'html' diff --git a/kvision-modules/kvision-cordova/build.gradle b/kvision-modules/kvision-cordova/build.gradle new file mode 100644 index 00000000..4aaef76d --- /dev/null +++ b/kvision-modules/kvision-cordova/build.gradle @@ -0,0 +1,5 @@ +apply from: "../shared.gradle" + +dependencies { + compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion" +} diff --git a/kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Battery.kt b/kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Battery.kt new file mode 100644 index 00000000..c8c26475 --- /dev/null +++ b/kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Battery.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * 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. + */ + +package pl.treksoft.kvision.cordova + +import kotlin.browser.window + +/** + * Battery status event types. + */ +enum class BatteryEvent(internal var event: String) { + BATTERY_STATUS("batterystatus"), + BATTERY_LOW("batterylow"), + BATTERY_CRITICAL("batterycritical") +} + +/** + * Battery status. + */ +external class BatteryStatus { + val level: Int = definedExternally + val isPlugged: Boolean = definedExternally +} + +/** + * Add listeners for battery status Cordova events. + */ +fun addBatteryStatusListener(event: BatteryEvent, listener: (BatteryStatus) -> Unit) { + addDeviceReadyListener { + window.addEventListener(event.event, { status -> + @Suppress("UnsafeCastFromDynamic") + listener(status.asDynamic()) + }, false) + } +} diff --git a/kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Device.kt b/kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Device.kt new file mode 100644 index 00000000..bf273ca7 --- /dev/null +++ b/kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Device.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * 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. + */ + +package pl.treksoft.kvision.cordova + +import kotlin.browser.document +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine + +/** + * Device information class. + */ +external class Device { + val cordova: String = definedExternally + val model: String = definedExternally + val platform: String = definedExternally + val uuid: String = definedExternally + val version: String = definedExternally + val manufacturer: String = definedExternally + val isVirtual: Boolean = definedExternally + val serial: String = definedExternally +} + +/** + * External device information object. + */ +external val device: Device + +private var intDevice: Device? = null + +/** + * Add listeners for 'deviceready' Cordova event. + */ +fun addDeviceReadyListener(listener: (Device) -> Unit) { + document.addEventListener("deviceready", { + listener(device) + }, false) +} + +/** + * Suspending function to return device information object. + */ +suspend fun getDevice(): Device { + return intDevice ?: suspendCoroutine { continuation -> + addDeviceReadyListener { + intDevice = device + continuation.resume(device) + } + } +} diff --git a/settings.gradle b/settings.gradle index 995d49dd..767cdd7f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,4 +22,5 @@ include 'kvision-modules:kvision-base', 'kvision-modules:kvision-server-jooby', 'kvision-modules:kvision-server-ktor', 'kvision-modules:kvision-server-spring-boot', - 'kvision-modules:kvision-electron' + 'kvision-modules:kvision-electron', + 'kvision-modules:kvision-cordova' |