-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from SwiftRex/LiftToCollection
Lift To Collection
- Loading branch information
Showing
2 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Sources/BridgeMiddleware/BridgeMiddleware+LiftToCollection.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Foundation | ||
import SwiftRex | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
extension BridgeMiddleware where StateType: Identifiable { | ||
public func liftToCollection<GlobalAction, GlobalState, CollectionState: MutableCollection>( | ||
inputAction actionMap: @escaping (GlobalAction) -> ElementIDAction<StateType.ID, InputActionType>?, | ||
outputAction outputMap: @escaping (ElementIDAction<StateType.ID, OutputActionType>) -> GlobalAction, | ||
stateCollection: @escaping (GlobalState) -> CollectionState | ||
) -> BridgeMiddleware<GlobalAction, GlobalAction, GlobalState> where CollectionState.Element == StateType { | ||
let mw = BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>() | ||
self.bridges.forEach { bridge in | ||
mw.bridges.append( | ||
BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>.Bridge( | ||
actionTransformation: { (globalInputAction: GlobalAction) -> GlobalAction? in | ||
guard let localElementIDInputAction = actionMap(globalInputAction), | ||
let localOutputAction = bridge.actionTransformation(localElementIDInputAction.action) | ||
else { return nil } | ||
|
||
return outputMap(.init(id: localElementIDInputAction.id, action: localOutputAction)) | ||
}, | ||
statePredicate: { (getState: GetState<GlobalState>, action: GlobalAction) -> Bool in | ||
guard let itemAction = actionMap(action), | ||
let itemState = stateCollection(getState()).first(where: { $0.id == itemAction.id }) | ||
else { return false } | ||
|
||
return bridge.statePredicate({ itemState }, itemAction.action) | ||
}, | ||
bridgedAtSource: bridge.bridgedAtSource | ||
) | ||
) | ||
} | ||
return mw | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
extension BridgeMiddleware where StateType: Identifiable, InputActionType == OutputActionType { | ||
public func liftToCollection<GlobalAction, GlobalState, CollectionState: MutableCollection>( | ||
action actionMap: WritableKeyPath<GlobalAction, ElementIDAction<StateType.ID, InputActionType>?>, | ||
stateCollection: KeyPath<GlobalState, CollectionState> | ||
) -> BridgeMiddleware<GlobalAction, GlobalAction, GlobalState> where CollectionState.Element == StateType { | ||
let mw = BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>() | ||
self.bridges.forEach { bridge in | ||
mw.bridges.append( | ||
BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>.Bridge( | ||
actionTransformation: { (globalInputAction: GlobalAction) -> GlobalAction? in | ||
guard let localElementIDInputAction = globalInputAction[keyPath: actionMap], | ||
let localOutputAction = bridge.actionTransformation(localElementIDInputAction.action) | ||
else { return nil } | ||
|
||
var newAction = globalInputAction | ||
newAction[keyPath: actionMap] = .init(id: localElementIDInputAction.id, action: localOutputAction) | ||
return newAction | ||
}, | ||
statePredicate: { (getState: GetState<GlobalState>, action: GlobalAction) -> Bool in | ||
guard let itemAction = action[keyPath: actionMap], | ||
let itemState = getState()[keyPath: stateCollection].first(where: { $0.id == itemAction.id }) | ||
else { return false } | ||
|
||
return bridge.statePredicate({ itemState }, itemAction.action) | ||
}, | ||
bridgedAtSource: bridge.bridgedAtSource | ||
) | ||
) | ||
} | ||
return mw | ||
} | ||
} |
This file contains 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