Juanpe / Counter

Counter is powerful and multipurpose counter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Version Platform Twitter: @JuanpeCatalan License

Counter is powerful and multipurpose counter.

Features 🚀

  • Easy to use
  • Increment/Decrement
  • You decide the start value
  • Sum several values in a single call
  • Use your own objects
  • Set Milestones (alarms)
  • Automatic counter

Supported OS & SDK Versions 📋

  • iOS 9.0+
  • Swift 3

🗂 Table of Contents

📲 Installation

Using CocoaPods

Edit your Podfile and specify the dependency:

pod "Counter"

Manually 💪🏼

If you prefer, you can integrate Counter into your project manually. Simply add the Counter.swift source file directly into your project.

🐒 How to use

Import Counter in proper place.

import Counter

Now, you only need to create a Counter instance and start to play 🙂

let counter = Counter() // 0
counter.increment()     // 1

You can specify how many units you want increment the counter

counter.increment(2)    // 3

Also you can decrement the count

counter.decrement()     // 2

And of course you can reset the counter

counter.reset()         // 0

Other powerful feature is that you can sum various values at the same time

counter.sum(countables: 2, 3, -1)  // 4

🤓 Advanced

Can use your own objects to increment the counter. Only need implement Countable protocol:

protocol Countable {
    var deltaValue: Int { get }
}

Example:

struct Person {
    var age: Int
}

extension Person: Countable{
    var deltaValue: Int {
        return self.age
    }
}

let dad = Person(age: 45)
let mum = Person(age: 40)
let son = Person(age: 25)

let ageCounter = Counter()
ageCounter.increment(dad)
ageCounter.increment(mum)
ageCounter.increment(son)

print(ageCounter.currentValue) // 110

// Also you can use `sum` method or static method

ageCounter.sum(countables:dad, mum, son) // 110

let ages = Counter.sum(countables:dad, mum, son) // 110

🔔 Milestones

Sometimes you need to know when a counter has reached or will reach a value. With Counter will be a piece of cake. You only need to add milestones and conform to CounterDelegate protocol. Then, your counter will notify you before reaching and when it reaches each previously defined milestone.

protocol CounterDelegate{
    func counter(_ counter: Counter, willReachValue value: Int)
    func counter(_ counter: Counter, hasReachedValue value: Int)
    func counter(_ counter: Counter, didChangeValue value: Int)
}

To add a new milestone:

counter.add(milestone: 3)

⏱ AutomaticCounter

End counting manually

let automaticCounter = AutomaticCounter(startIn: 0) // takes default parameters (interval: 1, autoIncrement: 1)
automaticCounter.delegate = self
automaticCounter.automaticDelegate = self
automaticCounter.startCounting()

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    automaticCounter.endCounting()
}

/* output
counter(_:didChangeValue:) => 1
counter(_:didChangeValue:) => 2
counter(_:didChangeValue:) => 3
counter(_:didFinishCounting:) => 3
*/

End counting at value

let automaticCounter = AutomaticCounter(startIn: 0, interval: 0.5, autoIncrement: 1)
automaticCounter.delegate = self
automaticCounter.automaticDelegate = self
automaticCounter.startCounting(endingAt: 10)

/* output
counter(_:didChangeValue:) => 1
counter(_:didChangeValue:) => 2
counter(_:didChangeValue:) => 3
counter(_:didChangeValue:) => 4
counter(_:didChangeValue:) => 5
counter(_:didChangeValue:) => 6
counter(_:didChangeValue:) => 7
counter(_:didChangeValue:) => 8
counter(_:didChangeValue:) => 9
counter(_:didChangeValue:) => 10
counter(_:didFinishCounting:) => 10
*/

End counting after time interval

let automaticCounter = AutomaticCounter(startIn: 0, interval: 1)
automaticCounter.delegate = self
automaticCounter.automaticDelegate = self
automaticCounter.startCounting(endingAfter: 3)

/* output
counter(_:didChangeValue:) => 1
counter(_:didChangeValue:) => 2
counter(_:didChangeValue:) => 3
counter(_:didFinishCounting:) => 3
*/

❤️ Contributed

This is an open source project, so feel free to contribute. How?

  • Open an issue.
  • Send feedback via email.
  • Propose your own fixes, suggestions and open a pull request with the changes.

See all contributors

👨🏻‍💻 Author

  • Juanpe Catalán alt text

👮🏻 License

MIT License

Copyright (c) 2017 Juanpe Catalán

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Counter is powerful and multipurpose counter

License:MIT License


Languages

Language:Swift 100.0%