Latest 0.2.1
Homepage https://github.com/iamchiwon/Swiflux
License MIT
Platforms ios 8.0
Dependencies RxSwift
Authors

CI Status
Version
License
Platform

Intro

What is Flux?

Flux is the application architecture that Facebook uses for building client-side web applications.

Core of Flux

  • Unidirectional flow
  • View – Store – Action – Dispatcher

Swiflux

Minimal library for swift that according to flux concept.

Installation

Swiflux is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "Swiflux"

Requirements

  • RxSwift 3.x (or higher)
  • Swift 3.x (or higher)

Example

1. Import library

import Swiflux

2. Write Store class

i. extends StoreBase

class UserStore: StoreBase

ii. Override some (optional)

override func buildDefaultState() {
}
override func bindEvents() {
}

iii. Subscribe Action and change state

  • state : a data that store has. changing of state will be broadcast to views who subscribe.

    class UserStore: StoreBase {
    override func buildDefaultState() {
        //default state value
        setState(for: "greeting", state: "")
    }
    override func bindEvents() {
        //subscribe action event "app:greeting"
        Dispatcher.shared().on("app:greeting") { props in
            guard let props = props else { return }
    
            let firstName = props["firstName"] as! String
            let lastName = props["lastName"] as! String
    
            self.buildGreetMessage(first: firstName, last: lastName)
        }
    }
    
    //BUISINESS LOGIC
    func buildGreetMessage(first: String, last: String) {
        var greet = "Hi~ there!"
    
        if first.isEmpty && !last.isEmpty {
            greet = "Hello, (last)."
        } else if !first.isEmpty && last.isEmpty {
            greet = "Hi, (first)."
        } else if !first.isEmpty && !last.isEmpty {
            greet = "Hello, (first) (last)!"
        }
    
        //change state, and notify to View
        self.setState(for: "greeting", state: greet)
    }
    }

3. Bind Store with View

let userStore = UserStore()

//bind state data via UserStore
userStore.on("greeting") { greet in
    let greet = greet as! String
    self.greetingLabel.text = greet
}

4. Send Action events to Dispatcher

//Send Action Event
Dispatcher.shared().send("app:greeting", props: [
    "firstName": firstName as AnyObject,
    "lastName": lastName as AnyObject
])

Refer Example source code

Author

Ryan Song<[email protected]>

License

Swiflux is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Swiflux",
    "version": "0.2.1",
    "summary": "Minimal flux library for swift",
    "description": "Minimal flux library for swift",
    "homepage": "https://github.com/iamchiwon/Swiflux",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "iamchiwon": "[email protected]"
    },
    "source": {
        "git": "https://github.com/iamchiwon/Swiflux.git",
        "tag": "0.2.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Swiflux/Classes/**/*",
    "dependencies": {
        "RxSwift": []
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This