aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules')
-rw-r--r--kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Battery.kt4
-rw-r--r--kvision-modules/kvision-cordova/src/main/kotlin/pl/treksoft/kvision/cordova/Device.kt60
2 files changed, 49 insertions, 15 deletions
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
index c6ac2a00..5d9b8e93 100644
--- 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
@@ -27,7 +27,7 @@ import kotlin.browser.window
/**
* Battery status event types.
*/
-enum class BatteryEvent(internal val event: String) {
+enum class BatteryEvent(internal val type: String) {
BATTERY_STATUS("batterystatus"),
BATTERY_LOW("batterylow"),
BATTERY_CRITICAL("batterycritical")
@@ -46,7 +46,7 @@ external class BatteryStatus {
*/
fun addBatteryStatusListener(event: BatteryEvent, listener: (BatteryStatus) -> Unit) {
addDeviceReadyListener {
- window.addEventListener(event.event, { status ->
+ window.addEventListener(event.type, { 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
index 69e3defb..d83b79d4 100644
--- 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
@@ -22,6 +22,7 @@
package pl.treksoft.kvision.cordova
+import org.w3c.dom.events.Event
import kotlin.browser.document
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@@ -58,17 +59,35 @@ external class ResumeEvent {
}
/**
- * External device information object.
+ * Cordova event types.
*/
-external val device: Device
+enum class CordovaEvent(internal val type: String) {
+ DEVICEREADY("deviceready"),
+ PAUSE("pause"),
+ RESUME("resume"),
+ BACKBUTTON("backbutton"),
+ MENUBUTTON("menubutton"),
+ SEARCHBUTTON("searchbutton"),
+ STARTCALLBUTTON("startcallbutton"),
+ ENDCALLBUTTON("endcallbutton"),
+ VOLUMEDOWNBUTTON("volumedownbutton"),
+ VOLUMEUPBUTTON("volumeupbutton"),
+ ACTIVATED("activated")
+}
+
+private external val device: Device
-private var intDevice: Device? = null
+/**
+ * Cordova device information object.
+ */
+var cordovaDevice: Device? = null
+ private set
/**
* Add listeners for 'deviceready' Cordova event.
*/
fun addDeviceReadyListener(listener: (Device) -> Unit) {
- document.addEventListener("deviceready", {
+ document.addEventListener(CordovaEvent.DEVICEREADY.type, {
listener(device)
}, false)
}
@@ -77,28 +96,43 @@ fun addDeviceReadyListener(listener: (Device) -> Unit) {
* Add listeners for 'pause' Cordova event.
*/
fun addPauseListener(listener: () -> Unit) {
- document.addEventListener("pause", {
- listener()
- }, false)
+ addDeviceReadyListener {
+ document.addEventListener(CordovaEvent.PAUSE.type, {
+ listener()
+ }, false)
+ }
}
/**
* Add listeners for 'resume' Cordova event.
*/
fun addResumeListener(listener: (ResumeEvent) -> Unit) {
- document.addEventListener("resume", { e ->
- @Suppress("UnsafeCastFromDynamic")
- listener(e.asDynamic())
- }, false)
+ addDeviceReadyListener {
+ document.addEventListener(CordovaEvent.RESUME.type, { e ->
+ @Suppress("UnsafeCastFromDynamic")
+ listener(e.asDynamic())
+ }, false)
+ }
+}
+
+/**
+ * Add listeners for a Cordova events.
+ */
+fun addCordovaEventListener(event: CordovaEvent, listener: (Event) -> Unit) {
+ addDeviceReadyListener {
+ document.addEventListener(event.type, { e ->
+ listener(e)
+ }, false)
+ }
}
/**
* Suspending function to return device information object.
*/
suspend fun getDevice(): Device {
- return intDevice ?: suspendCoroutine { continuation ->
+ return cordovaDevice ?: suspendCoroutine { continuation ->
addDeviceReadyListener {
- intDevice = device
+ cordovaDevice = device
continuation.resume(device)
}
}