Skip to content

Commit 046486a

Browse files
authored
[battery_plus] Implement getBatteryState (#361)
* [battery_plus] Implement getBatteryState * Hold a reference to EventChannel
1 parent a80cbef commit 046486a

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

packages/battery_plus/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.1.0
2+
3+
* Update battery_plus to 2.1.3.
4+
* Update battery_plus_platform_interface to 1.2.1.
5+
* Support `Battery.batteryState`.
6+
* Update the example app.
7+
* Code refactoring.
8+
19
## 1.0.3
210

311
* Update battery_plus to 2.0.2.

packages/battery_plus/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This package is not an _endorsed_ implementation of `battery_plus`. Therefore, y
1010

1111
```yaml
1212
dependencies:
13-
battery_plus: ^2.0.2
14-
battery_plus_tizen: ^1.0.3
13+
battery_plus: ^2.1.3
14+
battery_plus_tizen: ^1.1.0
1515
```
1616
1717
Then you can import `battery_plus` in your Dart code:
@@ -26,6 +26,7 @@ For detailed usage, see https://pub.dev/packages/battery_plus#usage.
2626

2727
- [x] `Battery.batteryLevel`
2828
- [ ] `Battery.isInBatterySaveMode`
29+
- [x] `Battery.batteryState`
2930
- [x] `Battery.onBatteryStateChanged`
3031

3132
## Supported devices

packages/battery_plus/example/lib/main.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ class _MyHomePageState extends State<MyHomePage> {
4646
@override
4747
void initState() {
4848
super.initState();
49+
_battery.batteryState.then(_updateBatteryState);
4950
_batteryStateSubscription =
50-
_battery.onBatteryStateChanged.listen((BatteryState state) {
51-
setState(() {
52-
_batteryState = state;
53-
});
51+
_battery.onBatteryStateChanged.listen(_updateBatteryState);
52+
}
53+
54+
void _updateBatteryState(BatteryState state) {
55+
if (_batteryState == state) return;
56+
setState(() {
57+
_batteryState = state;
5458
});
5559
}
5660

packages/battery_plus/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Demonstrates how to use the battery_plus plugin.
33
publish_to: "none"
44

55
dependencies:
6-
battery_plus: ^2.0.2
6+
battery_plus: ^2.1.3
77
battery_plus_tizen:
88
path: ../
99
flutter:

packages/battery_plus/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: battery_plus_tizen
22
description: Tizen implementation of the battery_plus plugin
33
homepage: https://github.com/flutter-tizen/plugins
44
repository: https://github.com/flutter-tizen/plugins/tree/master/packages/battery_plus
5-
version: 1.0.3
5+
version: 1.1.0
66

77
flutter:
88
plugin:
@@ -12,7 +12,7 @@ flutter:
1212
fileName: battery_plus_tizen_plugin.h
1313

1414
dependencies:
15-
battery_plus_platform_interface: ^1.1.1
15+
battery_plus_platform_interface: ^1.2.1
1616
flutter:
1717
sdk: flutter
1818

packages/battery_plus/tizen/src/battery_plus_tizen_plugin.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ class BatteryPlusTizenPlugin : public flutter::Plugin {
9494
plugin_pointer->HandleMethodCall(call, std::move(result));
9595
});
9696

97-
auto event_channel = std::make_unique<FlEventChannel>(
97+
plugin->event_channel_ = std::make_unique<FlEventChannel>(
9898
registrar->messenger(), "dev.fluttercommunity.plus/charging",
9999
&flutter::StandardMethodCodec::GetInstance());
100-
event_channel->SetStreamHandler(
100+
plugin->event_channel_->SetStreamHandler(
101101
std::make_unique<BatteryStatusStreamHandler>());
102102

103103
registrar->AddPlugin(std::move(plugin));
@@ -121,10 +121,21 @@ class BatteryPlusTizenPlugin : public flutter::Plugin {
121121
result->Error(std::to_string(battery.GetLastError()),
122122
battery.GetLastErrorString());
123123
}
124+
} else if (method_name == "getBatteryState") {
125+
DeviceBattery battery;
126+
BatteryStatus status = battery.GetStatus();
127+
if (status != BatteryStatus::kError) {
128+
result->Success(flutter::EncodableValue(BatteryStatusToString(status)));
129+
} else {
130+
result->Error(std::to_string(battery.GetLastError()),
131+
battery.GetLastErrorString());
132+
}
124133
} else {
125134
result->NotImplemented();
126135
}
127136
}
137+
138+
std::unique_ptr<FlEventChannel> event_channel_;
128139
};
129140

130141
} // namespace

0 commit comments

Comments
 (0)