Giter Club home page Giter Club logo

swiftredux's Introduction

Maintainability

SwiftRedux

SwiftRedux is a Swift implementation of the Redux state container. It relies on the same concepts and provides familiar Hooks through property wrappers.

Installation

SwiftRedux doesn't contain any external dependencies.

The only supported installation option is through Swift Package Manager:

Swift Package Manager

If you use Swift Package Manager, simply add SwiftRedux as a dependency of your package in Package.swift:

.package(url: "https://github.com/lucaslimapoa/SwiftRedux.git", from: "0.2.0")

Prerequisites

SwiftRedux relies on SwiftUI and Combine, so iOS 13 or later is required for it to work.

Usage

With SwiftRedux, the state of the app is kept inside a Store object. The Store is responsible for managing the state, updating the state through the use of reducers and handling logic, which can be handled my a middleware, for instance.

For starters, let's create a simple Counter app.

First, define a state for our app.

struct AppState: Equatable {
  var counter = 0
}

The AppState will hold the state for the whole app. Since this is a simple Counter app, the state will contain a simple count field. Now, we need a way to update the state. With SwiftRedux, the state can only be updated from Reducers. Reducers are objects that receive a dispatched action and the current state, the state is then updated based on the received action. Action are directly dispatched to the Store.

Let's create actions increasing and decreasing the counter now, so that our reducer can use it:

enum CounterAction {
  case increase
  case decrease
}

Now the reducer for the handling such actions can be created:

struct AppReducer: Reducer {
    func reduce(state: inout AppState, action: CounterAction) {
        switch action {
        case .increase:
            state.counter += 1
        case .decrease:
            state.counter -= 1
        }
    }
}

It's important to notice that the Reducer is sync, no async actions should be created or dispatched in the reducers.

Now that we have the AppState, CounterAction and AppReducer we can proceed and create a store:

let store = Store(
  initialState: AppState(),
  reducer: AppReducer()
)

Now all we have to do is to pass our store to the initial SwiftUI view, so the store can be put in the SwiftUI Environment and the Hooks (property wrappers) can be used.

ContentView()
  .store(store)

SwiftRedux provides a few Hooks so that the Store can be accessed. @Dispatch and @SelectState. The SwiftUI view would then look like this:

struct ContentView: View {
    @Dispatch<CounterAction> private var dispatch
    @SelectState(\AppState.counter) private var counter

    var body: some View {
        VStack {
            Stepper("Number of products", onIncrement: {
                dispatch(action: .increase)
            }, onDecrement: {
                dispatch(action: .decrease)
            })

            Text("The number of products is \(counter)")
        }
    }
}

Every time an action is dispatched and the state changes, the SwiftUI view will be updated and the counter property will have the new value from the store.

Advanced Usage

For looking into more advanced usage, please refer to the MovieDB-SwiftRedux app.

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.