Last active
July 27, 2019 17:52
-
-
Save larryonoff/c3c4cc7f8cb27c1165d8bf1635f713b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ReadinessSummaryReducerFactory.swift | |
| // RocketHealthKitUI | |
| // | |
| // Created by Ilya Laryionau on 24/07/2019. | |
| // Copyright © 2019 RocketBody. All rights reserved. | |
| // | |
| enum ReadinessSummaryAction: ActionProtocol { | |
| case willAppear | |
| case willDisappear | |
| case updateReadiness(_: Readiness?) | |
| case updateReadinessVariant(_: ReadinessSummaryVariant) | |
| } | |
| final class ReadinessSummaryRemoteConfigMiddleware: Middleware< | |
| ReadinessSummaryAction, | |
| ReadinessSummaryState | |
| > { | |
| private let remoteConfig: RemoteConfig = .shared | |
| private let remoteConfigDisposable: SerialDisposable = SerialDisposable() | |
| override func on( | |
| _ getState: @escaping () -> ReadinessSummaryState, | |
| action: ReadinessSummaryAction, | |
| next: @escaping (ReadinessSummaryAction) -> Void | |
| ) { | |
| switch action { | |
| case .willAppear: | |
| remoteConfigDisposable.inner = remoteConfig | |
| .observer() | |
| .skipError() | |
| .producer | |
| .prefix(value: remoteConfig) | |
| .map { $0.readinessSummaryVariant } | |
| .skipRepeats() | |
| .startWithValues { remoteVariant in | |
| let variant = ReadinessSummaryVariant(remoteVariant) | |
| next(ReadinessSummaryAction.updateReadinessVariant(variant)) | |
| } | |
| case .willDisappear: | |
| remoteConfigDisposable.inner = nil | |
| default: | |
| break | |
| } | |
| } | |
| } | |
| enum ReadinessSummaryReducer { | |
| static func reducer() -> Reducer< | |
| ReadinessSummaryAction, | |
| ReadinessSummaryState | |
| > { | |
| return Reducer<ReadinessSummaryAction, ReadinessSummaryState> { state, action in | |
| switch action { | |
| case let .updateReadiness(readiness): | |
| var newState = state | |
| newState.readiness = readiness | |
| return newState | |
| case let .updateReadinessVariant(variant): | |
| var newState = state | |
| newState.readinessSummaryVariant = variant | |
| return newState | |
| case .willAppear, | |
| .willDisappear: | |
| return state | |
| } | |
| } | |
| } | |
| } | |
| typealias ReadinessSummaryStore = | |
| Store<ReadinessSummaryAction, ReadinessSummaryState> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment