Skip to content

Commit

Permalink
feat: add battery module
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Downs committed Mar 25, 2022
1 parent 39bc323 commit 13a0791
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
@@ -1,3 +1,4 @@
import { Battery } from './modules/battery';
import { FileStorage } from './modules/fileStorage';
import { LocalStorage } from './modules/localStorage';
import { Volume } from './modules/volume';
Expand All @@ -6,4 +7,5 @@ export class KaiOS {
static Volume = Volume;
static LocalStorage = LocalStorage;
static FileStorage = FileStorage;
static Battery = Battery;
}
1 change: 1 addition & 0 deletions src/models/Battery.ts
Expand Up @@ -3,6 +3,7 @@ export type Battery = {
level: number;
temperature: number;
onlevelchange: () => void;
onchargingchange: () => void;
addEventListener: (event: string, cb: () => void) => void;
removeEventListener: (event: string, cb: () => void) => void;
};
1 change: 1 addition & 0 deletions src/models/CallbackFn.ts
@@ -0,0 +1 @@
export type CallbackFn<T = never> = ((data?: T) => void) | null;
1 change: 1 addition & 0 deletions src/models/index.ts
@@ -1,4 +1,5 @@
export * from './Battery';
export * from './CallbackFn';
export * from './Connection';
export * from './DomApplication';
export * from './FileSearchResult';
Expand Down
42 changes: 42 additions & 0 deletions src/modules/battery.ts
@@ -0,0 +1,42 @@
import { CallbackFn } from '../models';
import { Navigator } from './navigator';

type Options = {
subscribeToChanges: boolean;
};

export class Battery {
charging: boolean = false;
level: number = 0;
temperature: number = 0;

constructor(options?: Partial<Options>) {
const opts: Options = {
subscribeToChanges: true,
};

this.updateStatus();

Navigator.navigator.battery.onlevelchange = () => {
if (opts.subscribeToChanges) {
this.updateStatus();
}
this.onLevelChange?.(Navigator.navigator.battery.level);
};
Navigator.navigator.battery.onchargingchange = () => {
if (opts.subscribeToChanges) {
this.updateStatus();
}
this.onChargingChange?.(Navigator.navigator.battery.charging);
};
}

onLevelChange: CallbackFn<number> = null;
onChargingChange: CallbackFn<boolean> = null;

private updateStatus() {
this.charging = Navigator.navigator.battery.charging;
this.level = Navigator.navigator.battery.level;
this.temperature = Navigator.navigator.battery.temperature;
}
}

0 comments on commit 13a0791

Please sign in to comment.