Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/geekaurora/SwiftUIRedux
🚀Comprehensive Redux library for SwiftUI, ensures State consistency across Stores with type-safe pub/sub pattern.
https://github.com/geekaurora/SwiftUIRedux
redux swift swiftui
Last synced: about 2 months ago
JSON representation
🚀Comprehensive Redux library for SwiftUI, ensures State consistency across Stores with type-safe pub/sub pattern.
- Host: GitHub
- URL: https://github.com/geekaurora/SwiftUIRedux
- Owner: geekaurora
- License: mit
- Created: 2020-04-06T10:15:11.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-02T18:58:55.000Z (7 months ago)
- Last Synced: 2024-08-03T09:02:32.795Z (5 months ago)
- Topics: redux, swift, swiftui
- Language: Swift
- Homepage:
- Size: 216 KB
- Stars: 22
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-about-SwiftUI - SwiftUIRedux - safe pub/sub pattern. (🌎 by the community)
README
# SwiftUIRedux
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
![Swift Version](https://img.shields.io/badge/swift-5.1-orange.svg)
[![License](https://img.shields.io/cocoapods/l/ReactiveListViewKit.svg?style=flat)](http://cocoapods.org/pods/ReactiveListViewKit)### Comprehensive Redux library for SwiftUI.
* Keep `State` consistent across `Stores` by type safe `pub/sub` pattern with `Reducers` of `RootStore`.
* Waterfall `Action` propagation flow from root to `State` subtree.
### Unidirectional Data Flow
* **RootStore/Dispatcher:** Propagates domained actions* **Reducer:** Subscribes to `RootStore` and reduces `Action` to generate new sub state
* **Action:** Action driven - more loosely coupled pattern than `Delegation` pattern
* **State:**
* Reduces `Action` and outputs new `State`
* Waterfall reduce flow: propagates `Action` to children nodes via `State` tree
### Declarative/Stateful/Immutable/Predictable
* Efficient tree diff algorithm, no more imperative manual update code.### Usage Example
***FeedListView***
```swift
struct FeedListView: View {
@ObservedObject var state = FeedListState()
var body: some View {
List {
ForEach(state.feeds, id: \.diffId) { feed in
FeedCell(feed: feed)
}
}
}
}```
***FeedListState***
```swift
public class FeedListState: ReduxReducer, ObservableObject {
@Published var feeds: [Feed] = []
public override func reduce(action: ReduxActionProtocol) {
// Propagates `action` to the substate tree.
// Setting `self.feeds` with new feeds triggers list UI reloading
// and SwiftUI will diff efficiently based on list identifier.
feeds = feeds.map { $0.reduce(action: action) }
}
```***Dispatch FeedLikeAction***
```swift
dispatch(action: FeedLikeAction(feed: feed))
```***Reduce FeedLikeAction***
```swift
public struct Feed {
public var diffId = UUID()
public let feedId: Int
public let title: String
public var isLiked: Bool { didSet { diffId = UUID() } }
}extension Feed: ReduxStateProtocol {
@discardableResult
public func reduce(action: ReduxActionProtocol) -> Self {
// Makes the deep copy of self.
var feedCopy = self
switch action {
case let action as FeedLikeAction:
// Reduces the `FeedLikeAction`.
if feedId == action.feed.feedId {
feedCopy.isLiked = !action.feed.isLiked
}
default: break
}
return feedCopy
}
}
```