Latest 0.2.0
Homepage https://github.com/SwiftRex/Middlewares
License Apache 2.0
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 3.0, requires ARC
Frameworks Foundation
Authors

SwiftRex Middlewares

A collection of useful and reusable middlewares to quickly bootstrap your app.

Logger Middleware

Plug this middleware in your chain and log all events and actions to the console, as well as the state before and after the reducers and the elapsed time of every operation. It’s recommended to be the first in the chain, so you can have more accurate times.

Installation

For RxSwift users:

pod 'RxSwift'
pod 'SwiftRex/UsingRxSwift'
pod 'SwiftRex-LoggerMiddleware/UsingRxSwift'

For ReactiveSwift users:

pod 'ReactiveSwift'
pod 'SwiftRex/UsingReactiveSwift'
pod 'SwiftRex-LoggerMiddleware/UsingReactiveSwift'

Usage

import Foundation
import SwiftRex
import SwiftRex_LoggerMiddleware

public let mainReducer: () -> Reducer<MainState> = {
    // ....
}

public let mainMiddleware: () -> ComposedMiddleware<MainState> = {
    LoggerMiddleware()
    // <> RouterMiddleware().lift(.navigation)
    // <> DirectLineMiddleware()
    // <> ReachabilityMiddleware().lift(.networkState)
    // <> ...
}

extension MainStore {
    public convenience init() {
        self.init(initialState: .init(),
                  reducer: mainReducer(),
                  middleware: mainMiddleware())
    }
}

Reachability Middleware

Plug this middleware in your chain to monitor network reachability.

Installation

For RxSwift users:

pod 'RxSwift'
pod 'SwiftRex/UsingRxSwift'
pod 'SwiftRex-ReachabilityMiddleware/UsingRxSwift'

For ReactiveSwift users:

pod 'ReactiveSwift'
pod 'SwiftRex/UsingReactiveSwift'
pod 'SwiftRex-ReachabilityMiddleware/UsingReactiveSwift'

Usage

import Foundation
import Reachability
import SwiftRex
import SwiftRex_ReachabilityMiddleware

public struct MainState: Equatable, Codable {
    // 1 - Add the property to hold reachability state anywhere on your main state tree
    public var networkState: Reachability.Connection = .none
    // ... public var ...

    public init() { }
}

public let mainReducer: () -> Reducer<MainState> = {
    // 2 - Add reachability reducer in any position of your reducer chain
    // Please notice that we lift the reducer for the property of type Reachability.Connection
    // to a reducer of type MainState by providing the KeyPath to the property.
    Reducer.reachability.lift(.networkState)
    // <> ...
}

public let mainMiddleware: () -> ComposedMiddleware<MainState> = {
    // 3 - Add reachability middleware in any position of your middleware chain
    // Please notice that we lift the middleware for the property of type Reachability.Connection
    // to a middleware of type MainState by providing the KeyPath to the property.
    ReachabilityMiddleware().lift(.networkState)
    // <> ...
}

extension MainStore {
    public convenience init() {
        self.init(initialState: .init(),
                  reducer: mainReducer(),
                  middleware: mainMiddleware())
    }
}

final class MyViewController: UIViewController {
    private let stateProvider = Environment.current.stateProvider()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupBindings()
    }

    private func setupBindings() {
        // 4 - You can now observe that property
        stateProvider
            .map { $0.networkState }
            .distinctUntilChanged()
            .asDriver(onErrorJustReturn: .none)
            .drive(onNext: { connection in
                switch connection {
                case .none:
                    showNoConnectionRibbon()
                case .wifi, .cellular:
                    hideNoConnectionRibbon()
                }
            }).disposed(by: disposeBag)
    }

    private func showNoConnectionRibbon() {
        // TODO: Implement
    }

    private func hideNoConnectionRibbon() {
        // TODO: Implement
    }
}

Latest podspec

{
    "name": "SwiftRex-LoggerMiddleware",
    "version": "0.2.0",
    "summary": "SwiftRex Logger Middleware",
    "description": "Logs all events, actions and state mutations",
    "homepage": "https://github.com/SwiftRex/Middlewares",
    "license": {
        "type": "Apache 2.0",
        "file": "LICENSE"
    },
    "authors": {
        "Luiz Barbosa": "[email protected]"
    },
    "source": {
        "git": "https://github.com/swiftrex/Middlewares.git",
        "tag": "v0.2.0"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "3.0"
    },
    "frameworks": "Foundation",
    "default_subspecs": "UsingRxSwift",
    "subspecs": [
        {
            "name": "UsingRxSwift",
            "dependencies": {
                "SwiftRex/UsingRxSwift": []
            },
            "source_files": "Sources/LoggerMiddleware/**/*.swift"
        },
        {
            "name": "UsingReactiveSwift",
            "dependencies": {
                "SwiftRex/UsingReactiveSwift": []
            },
            "source_files": "Sources/LoggerMiddleware/**/*.swift"
        }
    ]
}

Pin It on Pinterest

Share This