GithubHelp home page GithubHelp logo

tchigher / swift-unidirectional-flow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mecid/swift-unidirectional-flow

0.0 0.0 0.0 33 KB

Unidirectional flow implemented using the latest Swift Generics and Swift Concurrency features.

License: MIT License

Swift 100.00%

swift-unidirectional-flow's Introduction

swift-unidirectional-flow

Unidirectional flow implemented using the latest Swift Generics and Swift Concurrency features.

struct SearchState: Equatable {
    var repos: [Repo] = []
    var isLoading = false
}

enum SearchAction: Equatable {
    case search(query: String)
    case setResults(repos: [Repo])
}

struct SearchReducer: Reducer {
    func reduce(oldState: SearchState, with action: SearchAction) -> SearchState {
        var state = oldState
        
        switch action {
        case .search:
            state.isLoading = true
        case let .setResults(repos):
            state.repos = repos
            state.isLoading = false
        }
        
        return state
    }
}

struct SearchDependencies {
    var search: (String) async throws -> SearchResponse
}

struct SearchMiddleware: Middleware {
    func process(
        state: SearchState,
        with action: SearchAction,
        using dependencies: SearchDependencies
    ) async -> SearchAction? {
        switch action {
        case let .search(query):
            let results = try? await dependencies.search(query)
            return .setResults(repos: results?.items ?? [])
        default:
            return nil
        }
    }
}

typealias SearchStore = Store<SearchState, SearchAction, SearchDependencies>

struct SearchContainerView: View {
    @StateObject private var store = SearchStore(
        initialState: .init(),
        reducer: SearchReducer(),
        dependencies: .production,
        middlewares: [SearchMiddleware()]
    )
    @State private var query = ""
    
    var body: some View {
        List(store.state.repos) { repo in
            VStack(alignment: .leading) {
                Text(repo.name)
                    .font(.headline)
                
                if let description = repo.description {
                    Text(description)
                }
            }
        }
        .redacted(reason: store.state.isLoading ? .placeholder : [])
        .searchable(text: $query)
        .task(id: query) {
            await store.send(.search(query: query))
        }
        .navigationTitle("Github Search")
    }
}

To learn more about Unidirectional Flow in Swift, take a look at my dedicated post.

swift-unidirectional-flow's People

Contributors

mecid avatar djtech42 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.