Coder Social home page Coder Social logo

observable.swift's Introduction

Observable.Swift

UPDATE: Now you can add as much switches as you need, check the example project.

With this small piece of code (Observable.swift) you will be able to add reactivity programming to your app.
Simple, useful and easy to use.

var n = Observable(1)
var c: Int = 0

n.valueDidChange = { [unowned self] in
    c = n.value // 2 then 3
}

n.value = 2
c // 2
n.value = 3
c // 3

// we can create and combine signals with Observable<T> properties

ObservableSwitch:
With the ObservableSwitch you are able to combine signals and run some code depending on the status result.
You can specify the bool state you are expecting to run the closure (available: .AnyState, .OnlyTrue, .OnlyFalse).
Bellow we:

  • enable/disable a UIButton based on the length of the username and password fields.
  • check for strong passwords (length > 10)
  • check for the master password (.OnlyTrue)
// signin enabler switch
let obSwitch = ObservableSwitch(.AnyState)
obSwitch.action = { [unowned self] (status: Bool) -> () in
    self.signin.alpha   = (status) ? 1 : 0.5
    self.signin.enabled = status
}

// strong passwords switch
let obSwitchGreatPassword = ObservableSwitch(.AnyState)
obSwitchGreatPassword.action = { [unowned self] (status: Bool) -> () in
    self.signin.backgroundColor = (status) ? UIColor.green : UIColor.blue
}

// master password switch
let obSwitchMasterPassword = ObservableSwitch(.OnlyTrue)
obSwitchMasterPassword.action = { [unowned self] (status: Bool) -> () in
    let alertController = UIAlertController(title: "I know!", message: "It rocks!!!", preferredStyle: .alert)
    let action = UIAlertAction(title: "=)", style: .default, handler: nil)
    alertController.addAction(action)
    
    self.present(alertController, animated: true, completion: nil)
}

// adding the signals to the ObservableTextField properties
self.username.addSignal({ [unowned self] in self.username.count >= 4 }, toSwitch: obSwitch)
self.password.addSignal({ [unowned self] in self.password.count >= 4 }, toSwitch: obSwitch)
self.password.addSignal({ [unowned self] in self.password.count > 10 }, toSwitch: obSwitchGreatPassword)        
self.password.addSignal({ [unowned self] in self.password.text == "swift rocks" }, toSwitch: obSwitchMasterPassword)

Alt text

ObservableTextField:

self.textField.textDidChange = { [unowned self] (text: String) -> () in
	self.label.text = "Hi \(text)!"
}

Alt text

Observable:

// you can create signals for Observable<T> properties in case you need to observe/manage
// multiple properties/states

self.word.valueDidChange = { [unowned self] in
    self.label.text = self.word.value
    self.label.textColor = self.getRandomColor()
}

Alt text

License

All this code is released under the MIT license.

observable.swift's People

Watchers

Shahzad Majeed 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.