Skip to content

Commit

Permalink
feat(battery_plus)!: Introduce connected_not_charging state on MacOS (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuberen authored Dec 1, 2023
1 parent 6595e03 commit 78f44bf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C80D4294CF70F00263BE5 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
//
// BatteryPlusChargingHandler.swift
// battery_plus
//
// Created by Neevash Ramdial on 08/09/2020.
//
import Foundation
import FlutterMacOS

Expand Down Expand Up @@ -64,26 +58,28 @@ class BatteryPlusChargingHandler: NSObject, FlutterStreamHandler {
let powerSourceSnapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(powerSourceSnapshot).takeRetainedValue() as [CFTypeRef]

// Desktops do not have battery sources and are always charging.
// When no sources available it is highly likely a desktop, thus, connected_not_charging.
if sources.isEmpty {
return "charging"
return "connected_not_charging"
}

let description = IOPSGetPowerSourceDescription(powerSourceSnapshot, sources[0]).takeUnretainedValue() as! [String: AnyObject]

if let currentCapacity = description[kIOPSCurrentCapacityKey] as? Int {
if currentCapacity >= 95 {
return "full"
if let isFullyCharged = description[kIOPSIsChargedKey] as? Bool {
if isFullyCharged {
return "full"
}
}

// Returns nil when battery is discharging
if let isCharging = (description[kIOPSPowerSourceStateKey] as? String) {
if isCharging == kIOPSACPowerValue {
let isConnected = (description[kIOPSPowerSourceStateKey] as? String)

if let isCharging = (description[kIOPSIsChargingKey] as? Bool) {
if isCharging {
return "charging"
} else if isCharging == kIOPSBatteryPowerValue {
return "discharging"
} else if isConnected == kIOPSACPowerValue {
return "connected_not_charging"
} else {
return "unknown"
return "discharging"
}
}
return "UNAVAILABLE"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/// Indicates the current battery state.
enum BatteryState {
/// The battery is completely full of energy.
/// The battery is fully charged.
full,

/// The battery is currently storing energy.
/// The battery is currently charging.
charging,

/// Device is connected to external power source, but not charging the battery.
/// Usually happens when device has charge limit enabled and this limit is reached.
/// Available on MacOS and Android platforms only.
connectedNotCharging,

/// The battery is currently losing energy.
discharging,

/// The battery is not charging.
notCharging,

/// The state of the battery is unknown.
unknown
unknown;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ BatteryState parseBatteryState(String state) {
return BatteryState.charging;
case 'discharging':
return BatteryState.discharging;
case 'not_charging':
return BatteryState.notCharging;
case 'connected_not_charging':
return BatteryState.connectedNotCharging;
case 'unknown':
return BatteryState.unknown;
default:
Expand Down

0 comments on commit 78f44bf

Please sign in to comment.