Skip to content

Commit

Permalink
feat: Battery Status API (closes #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkruse committed Feb 2, 2025
1 parent 3cada2c commit 03d40d8
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 1 deletion.
135 changes: 135 additions & 0 deletions AppSrc/BatteryStatusDemo.wo
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
Use cWebView.pkg
Use cWebPanel.pkg
Use cWebLabel.pkg
Use cWebHtmlBox.pkg
Use cLogger.pkg

Use cBatteryStatusAPI.pkg
Use cWebCheckBox.pkg

Object oBatteryStatusDemo is a cWebView
Set peWebViewStyle to wvsDrillDown
Set peViewType to vtUndefined
Set pbShowCaption to False

Set psCaption to "Battery Status Demo"

Set piMaxWidth to 1024
Set piColumnCount to 6
Set pbServerOnShow to True

Object oBatteryStatusAPI is a cBatteryStatusAPI
Procedure OnChargingChange Boolean bCharging
If (bCharging) Begin
Send Log of oLogger "Device is charging"
End
Else Begin
Send Log of oLogger "Device is not charging"
End
End_Procedure

Procedure OnLevelChange Number nLevel
Send Log of oLogger (SFormat("Battery level is %1%", nLevel * 100))
End_Procedure

Procedure OnChargingTimeChange Integer iRemainingSeconds
If (iRemainingSeconds > -1) Begin
Send Log of oLogger (SFormat("Time to fully charged is %1 seconds", iRemainingSeconds))
End
Else Begin
Send Log of oLogger "Not currently charging"
End
End_Procedure

Procedure OnDischargingTimeChange Integer iRemainingSeconds
If (iRemainingSeconds > -1) Begin
Send Log of oLogger (SFormat("Time to fully discharged is %1 seconds", iRemainingSeconds))
End
Else Begin
Send Log of oLogger "Not currently discharging"
End
End_Procedure
End_Object

Object oWebMainPanel is a cWebPanel
Set piColumnCount to 12
WebSetResponsive piColumnCount rmMobile to 6

Object oWebLabel is a cWebLabel
Set piColumnSpan to 0
Set psCaption to "Supported?"
End_Object

Object oWebHtmlBox is a cWebHtmlBox
Set piColumnSpan to 0
Set psHtml to '<div class="as-label"><a href="https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API#browser_compatibility" target="_blank">See browser compatibility</a></div>'
End_Object

Object oWebLabel2 is a cWebLabel
Set psCaption to "The Battery Status API lets you receive events about changes in the battery status of the device. Note that the 'charging time' and 'discharging time' events do not seem to provide useful information."
Set piColumnSpan to 0
End_Object

Object oWebCheckbox1 is a cWebCheckbox
Set piColumnSpan to 3
Set psCaption to "Charging status"
Set pbServerOnChange to True

Procedure OnChange String sNewValue String sOldValue
WebSet pbServerOnChargingChange of oBatteryStatusAPI to (sNewValue = "1")
End_Procedure
End_Object

Object oWebCheckbox2 is a cWebCheckbox
Set piColumnSpan to 3
Set psCaption to "Level status"
Set pbServerOnChange to True
Set piColumnIndex to 3

Procedure OnChange String sNewValue String sOldValue
WebSet pbServerOnLevelChange of oBatteryStatusAPI to (sNewValue = "1")
End_Procedure
End_Object

Object oWebCheckbox3 is a cWebCheckbox
Set piColumnSpan to 3
Set psCaption to "Charging time status"
Set pbServerOnChange to True
Set piColumnIndex to 6

Procedure OnChange String sNewValue String sOldValue
WebSet pbServerOnChargingTimeChange of oBatteryStatusAPI to (sNewValue = "1")
End_Procedure
End_Object

Object oWebCheckbox4 is a cWebCheckbox
Set piColumnSpan to 3
Set psCaption to "Discharging time status"
Set pbServerOnChange to True
Set piColumnIndex to 9

Procedure OnChange String sNewValue String sOldValue
WebSet pbServerOnDischargingTimeChange of oBatteryStatusAPI to (sNewValue = "1")
End_Procedure
End_Object

Object oLogger is a cLogger
End_Object
End_Object

Procedure OnShow
Boolean bIsSupported

Forward Send OnShow

WebGet pbIsSupported of oBatteryStatusAPI to bIsSupported
If (bIsSupported) Begin
WebSet psCaption of oWebLabel to "BatteryStatus API is supported"
WebSet psTextColor of oWebLabel to "green"
End
Else Begin
WebSet psCaption of oWebLabel to "BatteryStatus API is NOT supported (try on Chrome, Edge or Opera)"
WebSet psTextColor of oWebLabel to "red"
End
End_Procedure
End_Object
18 changes: 18 additions & 0 deletions AppSrc/Dashboard.wo
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ Object oDashboard is a cWebView
WebSetResponsive piColumnCount rmTabletPortrait to 2
WebSetResponsive piColumnCount rmMobile to 1

Object oTileBatteryStatus is a cWebHtmlBox
Set pbServerOnClick to True
Set psCSSClass to "Tile"
Set psHtml to """
<div class="WebCon_Sizer" data-ServerOnClick="openview">
<div Class="Tile_Title">Battery Status API</div>
<div class="Tile_Subtitle">info about battery status</div>
<span class="material-icons-outlined">https</span>
</div>
"""

WebRegisterPath ntNavigateForward oBatteryStatusDemo

Procedure OnClick
Send NavigatePath
End_Procedure
End_Object

Object oTileBroadcastChannel is a cWebHtmlBox
Set pbServerOnClick to True
Set psCSSClass to "Tile"
Expand Down
10 changes: 10 additions & 0 deletions AppSrc/WebApp.src
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ Object oWebApp is a cWebApp
End_Procedure
End_Object

Object oWebMenuItemBatteryStatus is a cWebMenuItem
Set psCaption to "Battery Status Demo"
WebRegisterPath ntNavigateBegin oBatteryStatusDemo

Procedure OnClick
Send NavigatePath
End_Procedure
End_Object

Object oWebMenuItemBroadcastChannel is a cWebMenuItem
Set psCaption to "Broadcast Channel Demo"
WebRegisterPath ntNavigateBegin oBroadcastChannelDemo
Expand Down Expand Up @@ -285,6 +294,7 @@ Object oWebApp is a cWebApp
Use ScreenOrientationDemo.wo
Use WebStorageDemo.wo
Use MediaStreamRecordingDemo.wo
Use BatteryStatusDemo.wo
Set phoDefaultView to oDashboard

End_Object
Expand Down
60 changes: 60 additions & 0 deletions AppSrc/cBatteryStatusAPI.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Use cWebObject.pkg

Class cBatteryStatusAPI is a cWebObject

Procedure Construct_Object
Forward Send Construct_Object

{ WebProperty=Client DesignTime=False }
Property Boolean pbIsSupported False

{ WebProperty=Client }
Property Boolean pbServerOnChargingChange False
{ WebProperty=Client }
Property String psClientOnChargingChange ""
{ WebProperty=Client }
Property Boolean pbServerOnLevelChange False
{ WebProperty=Client }
Property String psClientOnLevelChange ""
{ WebProperty=Client }
Property Boolean pbServerOnChargingTimeChange False
{ WebProperty=Client }
Property String psClientOnChargingTimeChange ""
{ WebProperty=Client }
Property Boolean pbServerOnDischargingTimeChange False
{ WebProperty=Client }
Property String psClientOnDischargingTimeChange ""

Set psJSClass to "WebAPIs.BatteryStatus"
End_Procedure

Procedure End_Construct_Object
Forward Send End_Construct_Object

WebPublishProcedure OnChargingChange
WebPublishProcedure OnLevelChange
WebPublishProcedure OnChargingTimeChange
WebPublishProcedure OnDischargingTimeChange
End_Procedure

{ MethodType=Event }
Procedure OnChargingChange Boolean bCharging
// Reflects whether device is currently charging / connected to external power supply
End_Procedure

{ MethodType=Event }
Procedure OnLevelChange Number nLevel
// Reflects the current battery charge level from 0.0 to 1.0
End_Procedure

{ MethodType=Event }
Procedure OnChargingTimeChange Integer iRemainingSeconds
// Reflects time left to fully charged in seconds, or -1 if not charging
End_Procedure

{ MethodType=Event }
Procedure OnDischargingTimeChange Integer iRemainingSeconds
// Reflects time left to fully discharged in seconds, or -1 if not discharging
End_Procedure

End_Class
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DataFlex Custom Components for Web APIs.

Contains DataFlex implementations of:

* [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)
* [Broadcast Channel API](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)
* [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)
* [Contact Picker API](https://developer.mozilla.org/en-US/docs/Web/API/Contact_Picker_API)
Expand Down Expand Up @@ -52,4 +53,4 @@ If you're not sure, or you explicitly support older browsers, including core-js

If you *do* use core-js, please [support that project](https://github.com/zloirock/core-js/blob/master/docs/2023-02-14-so-whats-next.md)!

Including core-js will **not** implement API's that are not natively supported in a given browser. Always check the API pages linked above for browser support, and always check the `pbIsSupported` web property of each component at runtime before using it.
Including core-js will **not** implement API's that are not natively supported in a given browser. Always check the API pages linked above for browser support, and always check the `pbIsSupported` web property of each component at runtime before using it.
80 changes: 80 additions & 0 deletions src/battery-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
export default class BatteryStatus extends df.WebObject {
#battery;

constructor(sName, oParent) {
super(sName, oParent);
this.prop(df.tBool, 'pbIsSupported', false);
this.event('OnChargingChange');
this.event('OnLevelChange');
this.event('OnChargingTimeChange');
this.event('OnDischargingTimeChange');
}

async create(tDef) {
super.create(tDef);
this.set('pbIsSupported', 'getBattery' in navigator);
if (this.pbIsSupported) {
this.#battery = await navigator.getBattery();

this.#battery.addEventListener('chargingchange', () => {
this.#updateChargeInfo();
});
this.#updateChargeInfo();

this.#battery.addEventListener('levelchange', () => {
this.#updateLevelInfo();
});
this.#updateLevelInfo();

this.#battery.addEventListener('chargingtimechange', () => {
this.#updateChargingInfo();
});
this.#updateChargingInfo();

this.#battery.addEventListener('dischargingtimechange', () => {
this.#updateDischargingInfo();
});
this.#updateDischargingInfo();
}
}

#updateChargeInfo() {
this.fire('OnChargingChange', [df.fromBool(this.#battery.charging)]);
}

#updateLevelInfo() {
this.fire('OnLevelChange', [this.#battery.level]);
}

#updateChargingInfo() {
this.fire('OnChargingTimeChange', [this.#battery.chargingTime === Infinity ? -1 : this.#battery.chargingTime]);
}

#updateDischargingInfo() {
this.fire('OnDischargingTimeChange', [this.#battery.dischargingTime === Infinity ? -1 : this.#battery.dischargingTime]);
}

set_pbServerOnChargingChange(val) {
if (val) {
setTimeout(() => this.#updateChargeInfo());
}
}

set_pbServerOnLevelChange(val) {
if (val) {
setTimeout(() => this.#updateLevelInfo());
}
}

set_pbServerOnChargingTimeChange(val) {
if (val) {
setTimeout(() => this.#updateChargingInfo());
}
}

set_pbServerOnDischargingTimeChange(val) {
if (val) {
setTimeout(() => this.#updateDischargingInfo());
}
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BatteryStatus from "./battery-status";
import BroadcastChannel from './broadcast-channel';
import Clipboard from './clipboard';
import ContactPicker from './contact-picker';
Expand Down Expand Up @@ -29,6 +30,7 @@ global.WebAPIs = {
AbsoluteOrientationSensor,
AccelerometerSensor,
AmbientLightSensor,
BatteryStatus,
BroadcastChannel,
Clipboard,
ContactPicker,
Expand Down

0 comments on commit 03d40d8

Please sign in to comment.