battery.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. export class BatteryStatusService {
  18. // Attributes
  19. private batteryStatus: threema.BatteryStatus = null;
  20. /**
  21. * Update the battery status.
  22. */
  23. public setStatus(batteryStatus: threema.BatteryStatus): void {
  24. this.batteryStatus = batteryStatus;
  25. }
  26. /**
  27. * Is battery status information available?
  28. */
  29. public get dataAvailable(): boolean {
  30. return this.batteryStatus !== null;
  31. }
  32. /**
  33. * Return the charge level in percent.
  34. */
  35. public get percent(): number {
  36. return this.batteryStatus.percent;
  37. }
  38. /**
  39. * Return whether the battery is currently charging.
  40. */
  41. public get isCharging(): boolean {
  42. return this.batteryStatus.isCharging;
  43. }
  44. public toString(): string {
  45. if (this.batteryStatus === null) {
  46. return 'No data';
  47. }
  48. return this.percent + '%, ' + (this.isCharging ? 'charging' : 'discharging');
  49. }
  50. }