SwiftRex-LoggerMiddleware 0.2.0

SwiftRex-LoggerMiddleware 0.2.0

维护者:Luiz Barbosa.



  • Luiz Barbosa

SwiftRex Middlewares

一组实用且可重用中间件,可快速启动您的应用程序。

Logger Middleware

将此中间件添加到您的链中,并将所有事件和动作记录到控制台,以及 reducer 之前和之后的状态,以及每个操作的耗时。建议将其作为链中的第一个组件,以便获得更精确的时间。

安装

对于 RxSwift 用户

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

对于 ReactiveSwift 用户

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

使用方法

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())
    }
}

网络可达性中间件

将此中间件添加到您的链中,以监控网络可达性。

安装

对于 RxSwift 用户

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

对于 ReactiveSwift 用户

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

用法

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
    }
}