https://github.com/rxswiftcommunity/rxbatterymanager
A Reactive BatteryManager in Swift for iOS
https://github.com/rxswiftcommunity/rxbatterymanager
battery-information battery-level battery-monitor battery-status rx rxcocoa rxswift
Last synced: 4 months ago
JSON representation
A Reactive BatteryManager in Swift for iOS
- Host: GitHub
- URL: https://github.com/rxswiftcommunity/rxbatterymanager
- Owner: RxSwiftCommunity
- License: mit
- Created: 2020-05-25T06:44:01.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-01T17:18:40.000Z (over 4 years ago)
- Last Synced: 2025-06-01T10:32:03.638Z (4 months ago)
- Topics: battery-information, battery-level, battery-monitor, battery-status, rx, rxcocoa, rxswift
- Language: Swift
- Homepage:
- Size: 49.8 KB
- Stars: 24
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A Reactive BatteryManager in Swift
[](https://github.com/mustafagunes/RxBatteryManager)
[](https://github.com/mustafagunes/RxBatteryManager)
[](https://github.com/ReactiveX/RxSwift)
[](https://swift.org/package-manager)
[](https://cocoapods.org)
[](https://github.com/Carthage/Carthage)
[](https://github.com/mustafagunes/RxBatteryManager)
[](https://github.com/mustafagunes/RxBatteryManager/blob/master/LICENSE)## Motivation
I needed a battery manager when developing my apps with Rx. And I started to write the manager. With the battery manager, you can stream data from your battery in a reactive manner. For example, by subscribing to the critical battery status of your battery, you can improve the user experience and reduce battery usage by changing the dark/light modes of your application. I wanted to share this library I developed with the community. My biggest motivation was that the community was very helpful and friendly. I hope useful.## ๐ Requirements
* iOS 9.0+
* Xcode 11+
* Swift 5.1+
* RxSwift 5.1.1+## โ๏ธ Installation
### Swift Package Manager (requires Xcode 11)
Add package into Project settings -> Swift Packages### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html)
Add RxBatteryManager dependency to your Podfile
```
# Podfile
use_frameworks!# replace YOUR_TARGET_NAME with yours
target 'YOUR_TARGET_NAME' do
pod 'RxBatteryManager', '~> 1.0'
end
```
and run
```
$ pod install
```## ๐จโ๐ป Usage
```swift
import RxBatteryManager
```#### Singleton RxBatteryManager
```swift
let battery = Battery.monitor
```#### Init Library in AppDelegate
```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Monitoring Battery
Battery.monitor.start()
return true
}
```#### *level* - float returns
```swift
battery.level
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] level in
guard let self = self else { return }
print(level)
}).disposed(by: disposeBag)
```#### *state* - UIDevice.Enum returns in BatteryState type
```swift
battery.state
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] state in
guard let self = self else { return }
switch state {
case .unknown:
print("unknown")
case .unplugged:
print("unplugged")
case .charging:
print("charging")
case .full:
print("full")
@unknown default:
fatalError()
}
}).disposed(by: disposeBag)
```#### *isLowPowerMode* - bool returns
```swift
battery.isLowPowerMode
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] isLowPowerMode in
guard let self = self else { return }
print(isLowPowerMode)
}).disposed(by: disposeBag)
```#### *isLowLevel* - bool returns
```swift
battery.isLowLevel
.observeOn(MainScheduler.instance)
.distinctUntilChanged()
.subscribe(onNext: { [weak self] isLowLevel in
guard let self = self else { return }
self.isLowBatteryLabel.text = "\(isLowLevel)"
}).disposed(by: disposeBag)
```#### *isCriticalLevel* - bool returns
```swift
battery.isCriticalLevel
.observeOn(MainScheduler.instance)
.distinctUntilChanged()
.subscribe(onNext: { [weak self] isCriticalLevel in
guard let self = self else { return }
self.isCriticalBatteryLabel.text = "\(isCriticalLevel)"
}).disposed(by: disposeBag)
```**Note: I recommend you subscribe to Main Thread**
## ๐ฎโโ๏ธLicense
RxBatteryManager is available under the [MIT](https://github.com/mustafagunes/RxBatteryManager/blob/master/LICENSE) license. See the LICENSE file for more info.
Copyright (c) RxSwiftCommunity