Git Product home page Git Product logo

rxbatterymanager's Introduction

A Reactive BatteryManager in Swift

Build Status Swift RxSwift SPM Compatible CocoaPods Compatible Carthage Compatible Platform 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

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

import RxBatteryManager

Singleton RxBatteryManager

let battery = Battery.monitor

Init Library in AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
    // Monitoring Battery
    Battery.monitor.start()
    
    return true
}

level - float returns

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

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

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

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

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 license. See the LICENSE file for more info. Copyright (c) RxSwiftCommunity

rxbatterymanager's People

Contributors

mustafagunes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.