Skip to content
No description, website, or topics provided.
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LoggerMiddleware.xcodeproj
Middlewares.xcworkspace
Pods
ReachabilityMiddleware.xcodeproj
Sources
.gitignore
.ruby-version
.slather.yml
.sourcery.yml
.swift-version
.swiftlint.yml
.travis.yml
CODE_OF_CONDUCT.md
LICENSE
Podfile
Podfile.lock
README.md
SwiftRex-LoggerMiddleware.podspec
SwiftRex-ReachabilityMiddleware.podspec

README.md

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
    }
}
You can’t perform that action at this time.