Skip to content

AsyncReactor is a reactive architecural pattern to organize your Swift code.

License

Notifications You must be signed in to change notification settings

diamirio/AsyncReactor

Repository files navigation

AsyncReactor Example

DIAMIR Logo

iOS Swift

AsyncReactor is a reactive architecural pattern to organize your Swift code.

Table of Contents

Usage

General

AsyncReactor is a reactive architecural pattern. The main component is an AsyncReactor. This AsyncReactor holds the State as well as the Actions.

class RepositorySearchReactor: AsyncReactor {
    enum Action {
        ...
    }
    
    struct State {
        ...
    }
    
    @Published
    private(set) var state: State

    func action(_ action: Action) async { 
        ...
    }
}

Integration

The AsyncReactor is provided to the child view as an EnvironmentObject. This is set by the ReactorView.

ReactorView(RepositorySearchReactor()) {
    RepositorySearchView()
}

Now the reactor can simply be used in the SwiftUI view as follows.

struct RepositorySearchView: View {

    @EnvironmentObject
    private var reactor: RepositorySearchReactor

    var body: some View { 
        ... 
    }
}

State updates

Whenever the State in the reactor changes, the view will updated accordingly. It is also possibel to bind the State.

var body: some View { 
    Text(reactor.state.name)
}

Actions

Actions are used to trigger a behaviour in our reactor.

var body: some View { 
    Button("Click me") {
        reactor.action(.buttonClick)
    }
}

Action Bindings

Action bindings enable us to bind values of the state in our view.

struct RepositorySearchView: View {

    ...

    @ActionBinding(RepositorySearchReactor.self, 
                   keyPath: \.hidePrivate,
                   action: .onHidePrivateToggle)
    private var hidePrivate: Bool

    var body: some View { 
        Toggle("Hide Private Repos", isOn: $hidePrivate)
    }
}

class RepositorySearchReactor: AsyncReactor {
  
    ...

    func action(_ action: Action) async {
        switch action {
        case .onHidePrivateToggle:
            state.hidePrivate.toggle()

        ...

        }
    }
}

Example

An example can be found in the Example folder.

License

MIT License

Copyright (c) 2023 DIAMIR Holding

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

AsyncReactor is a reactive architecural pattern to organize your Swift code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages