aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules')
-rw-r--r--kvision-modules/kvision-cordova/build.gradle5
-rw-r--r--kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Battery.kt54
-rw-r--r--kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Device.kt69
3 files changed, 128 insertions, 0 deletions
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)
+ }
+ }
+}