flu

FluxxKit

Unidirectional data flow for reactive programming in iOS.

Showing:

Popularity

Downloads/wk

0

GitHub Stars

42

Maintenance

Last Commit

3yrs ago

Contributors

0

Package

Dependencies

0

License

MIT

Categories

Readme

FluxxKit

CI Status Swift 4.0 Carthage compatible Version License Platform

Overview

Unidirectional data flow for reactive programming in iOS. Flux and Reactive Programming.

FluxxKit is a porting facebook's flux implementation in Swift.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

More complicated real world example like below is here.

GIF

Getting Started

  1. State

    import FluxxKit
    import RxSwift
    
    final class ViewModel: StateType {
      var count = Variable<Int>(0)
    }
    
  2. Action

    extension ViewModel {
      enum Action: ActionType {
        case plus
        case minus
      }
    }
    
  3. Reducer

    extension ViewModel {
      final class Reducer: FluxxKit.Reducer<ViewModel, Action> {
        override func reduce(state: ViewModel, action: Action) {
    
          switch action {
          case .plus:
            state.count.value = state.count + 1
          case .minus:
            state.count.value = state.count - 1
          }
        }
      }
    }
    
  4. View

    Create store and register it to dispatcher, and bind store's state:

    import FluxxKit
    import RxSWift
    
    final class ViewController: UIViewController {
    
      @IBOutlet var counterLabel: UILabel!
      @IBOutlet var plusButton: UIButton!
      @IBOutlet var minusButton: UIButton!
      var store = Store<ViewModel, ViewModel.Action>(
        reducer: ViewModel.Reducer()
      )
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        Dispatcher.shared.register(store: self.store)
    
        store.state.count.asObservable().observeOn(MainScheduler.instance)
          .subscribe(onNext: { [weak self] count in
            self?.counterLabel.text = "\(count)"
          })
      }
    
      deinit {
        Dispatcher.shared.unregister(identifier: self.store.identifier)
      }
    }
    

    Dispatch action with UI action:

    @IBAction
    func onTouchPlusButton(sender: Any) {
      Dispatcher.shared.dispatch(action: ViewModel.Action.plus)
    }
    
    @IBAction
    func onTouchMinusButton(sender: Any) {
      Dispatcher.shared.dispatch(action: ViewModel.Action.minus)
    }
    

Architecture

(👻 nice diagram here 👻)

FLUX for Reactive Programming

FluxxKit would not emit any event when state change like flux. Instead, we have RxSwift, ReactiveSwift, ReactiveKit or something else. All the stateful things could be implemented as Observable or Stream, and ViewController could bind and react to them.

Flux

View -> Action -> Dispatcher -> (Middleware) -> Store -> Reducer -> Observable
  • When a user interacts with a View(Controller), it propagates an Action
  • through a central Dispatcher,
  • to the various Stores that hold the application's data,
  • state transition occurs in some Store that could responds to dispatched Action,
  • which will emit new items to Observable property in these Store.

Reactive Programming

Observable ---> View
  • ViewController subscribes Store's Observable properties,
  • and react to it.

Requirements

TargetVersion
iOS=> 8.0
Swift=> 4.0

Installation

FluxxKit is available through CocoaPods or Carthage.

CocoaPods

pod "FluxxKit"

Carthage

github "keitaoouchi/FluxxKit"

for detail, please follow the Carthage Instruction

Author

keitaoouchi, keita.oouchi@gmail.com

License

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial