-
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.
* docs update for Docc * updated docs * updated docs * docs update * updated docs * updated docs * updated docs
- Loading branch information
1 parent
3184362
commit 06473d9
Showing
18 changed files
with
707 additions
and
462 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ version: 1 | |
builder: | ||
configs: | ||
- documentation_targets: [Puredux] | ||
|
||
scheme: Puredux |
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
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,12 @@ | ||
# Archive | ||
|
||
Documentation archive | ||
|
||
## Overview | ||
|
||
Achived pieces of documentation for the old versions. | ||
|
||
## Topics | ||
|
||
- <doc:Documentation-v1.x> | ||
|
File renamed without changes.
183 changes: 183 additions & 0 deletions
183
Sources/Puredux/Documentation.docc/Articles/GettingStarted.md
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,183 @@ | ||
# Getting Started | ||
|
||
## Installation | ||
|
||
### Swift Package Manager | ||
|
||
|
||
Puredux is available through Swift Package Manager. To install it in Xcode 11.0 or later: | ||
|
||
Select File > Swift Packages > Add Package Dependency... | ||
Enter the Puredux repository URL: | ||
|
||
``` | ||
https://github.com/KazaiMazai/Puredux | ||
``` | ||
|
||
## Basics | ||
|
||
At its core, Puredux follows a predictable state management pattern that consists of the following key components: | ||
|
||
- State: A type that represents the entire application state or a portion of it. | ||
- Actions: Events that describe possible changes in the system, which lead to state mutations. | ||
- Reducer: A function that dictates how state changes in response to specific actions. | ||
- Store: The central hub where: | ||
- Initial state and reducers are defined. | ||
- Actions are dispatched to trigger state changes. | ||
- New state values are propagated to any observers or views. | ||
|
||
|
||
|
||
```text | ||
+-----------------------------------------+ | ||
| Store | | ||
| | | ||
| +-----------+ +-------------------+ | | ||
| | Reducer |<--| Current State | | | ||
New State | +-----------+ +-------------------+ | Actions | ||
<-----------+ | A |<----------+ | ||
| | | | | A | ||
| | V | | | | ||
| | +-------------------+ | | | | ||
| | | New State |------------+ | | | ||
| | +-------------------+ | | | ||
| | | | | ||
| +-----------------------------------------+ | | ||
| | | ||
| | | ||
| | | ||
| +----------------+ +---+----+ | | ||
V Observer | | Async Work | | | | ||
+---------->| Side Effects |--------------->| Action |--------->| | ||
| | | Result | | | | ||
| +----------------+ +----+---+ | | ||
| | | ||
| +----------------+ +---+----+ | | ||
V Observer | | User | | | | ||
+---------->| UI |------------------>| Action |------>+ | ||
| | Interactions | | | ||
+----------------+ +----+---+ | ||
``` | ||
|
||
## Store Definitions | ||
|
||
Let's break down a typical store setup using Puredux. | ||
|
||
**1. Define the Action Protocol**: | ||
|
||
Actions in Puredux follow a protocol that ensures they can be handled uniformly. | ||
|
||
```swift | ||
protocol Action { | ||
// Define specific actions in your app by conforming to this protocol | ||
} | ||
``` | ||
|
||
**2. Define the AppState**: | ||
|
||
The application’s state can be represented by a struct, which will store the data relevant to your app. | ||
The reduce method defines how the state will change in response to an action. | ||
|
||
|
||
```swift | ||
struct AppState { | ||
// Define your app's state properties here | ||
|
||
mutating func reduce(_ action: Action) { | ||
// Logic for how the state should update when an action is dispatched | ||
} | ||
} | ||
``` | ||
**3. Define the Store and Inject it**: | ||
|
||
Using the root AppState, we create a store that integrates actions and the state. | ||
|
||
The store’s role is to manage actions and apply the reducer function whenever an action is dispatched. | ||
|
||
```swift | ||
|
||
extension Injected { | ||
@InjectEntry var root = StateStore<AppState, Action>(AppState()) { state, action in | ||
state.reduce(action) | ||
} | ||
} | ||
|
||
``` | ||
|
||
## SwiftUI Bindings | ||
|
||
Puredux can seamlessly integrate with SwiftUI to manage both global app state and local states. | ||
|
||
```swift | ||
|
||
// View might need a local state that we don't need to share with the whole app | ||
|
||
struct ViewState { | ||
// ... | ||
|
||
mutating func reduce(_ action: Action) { | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
In your SwiftUI VIew, you can combine the app's root store with local view-specific states. | ||
This allows the view to respond dynamically to both app-level and view-level state changes. | ||
|
||
```swift | ||
struct ContentView: View { | ||
// Combine the root store with local state | ||
@State var store: StoreOf(\.root).with(ViewState()) { state, action in | ||
state.reduce(action) | ||
} | ||
|
||
@State var viewState: (AppState, ViewState)? | ||
|
||
var body: some View { | ||
MyView(viewState) | ||
.subscribe(store) { viewState = $0 } | ||
.onAppear { | ||
dispatch(SomeAction()) | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
## UIKit Bindings | ||
|
||
Puredux also supports UIKit, offering a similar approach for handling state and actions within UIViewController. | ||
|
||
```swift | ||
|
||
struct MyScreenState { | ||
// ... | ||
|
||
mutating func reduce(_ action: Action) { | ||
// ... | ||
} | ||
} | ||
|
||
class MyViewController: ViewController { | ||
var store: StoreOf(\.root).with(MyScreenState()) { state, action in | ||
state.reduce(action) | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
subscribe(store) { [weak self] in | ||
// Handle updates with the derived props | ||
self?.updateUI(with: $0) | ||
} | ||
} | ||
|
||
private func updateUI(with state: (AppState, MyScreenState)) { | ||
// Update UI elements with the new view state | ||
} | ||
} | ||
|
||
``` | ||
|
17 changes: 17 additions & 0 deletions
17
Sources/Puredux/Documentation.docc/Articles/MigrationGuides.md
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,17 @@ | ||
# Migration guides | ||
|
||
Learn how to upgrade your application to the newest version of Puredux. | ||
|
||
## Overview | ||
|
||
|
||
> Important: Before following any particular migration guide be sure you have followed all the | ||
> preceding migration guides. | ||
## Topics | ||
|
||
- <doc:Migrating-to-2.0.x> | ||
- <doc:Migrating-to-1.9.x> | ||
- <doc:Migrating-to-1.3.x> | ||
- <doc:Migrating-to-1.2.x> | ||
- <doc:Migrating-to-1.1.x> |
Oops, something went wrong.