Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ScopeState to ViewState #37

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct MyReducer: ReducerProtocol {
}
}

@ScopeState
@ViewState
struct MyView: View {
@State var counter = 0

Expand Down Expand Up @@ -105,7 +105,7 @@ struct MyReducer: ReducerProtocol {
}
}

@ScopeState
@ViewState
struct MyView: View {
@State var counter = 0

Expand Down Expand Up @@ -168,7 +168,7 @@ struct MyReducer: ReducerProtocol {
}
}

@ScopeState
@ViewState
struct MyView: View {
@State var email: String = ""
@State var password: String = ""
Expand Down Expand Up @@ -196,7 +196,7 @@ If you want to send the Action of the child Reducer to the parent Reducer, use p
This is the sample code.

```Swift
@ScopeState
@ViewState
struct ParentView: View {
let store: Store<ParentReducer> = Store(reducer: ParentReducer())

Expand All @@ -220,7 +220,7 @@ struct ParentReducer: ReducerProtocol {
}
}

@ScopeState
@ViewState
struct ChildView: View, ActionSendable {
let store: Store<ChildReducer> = Store(reducer: ChildReducer())

Expand Down Expand Up @@ -248,12 +248,12 @@ struct ChildReducer: ReducerProtocol {
### Testing
You can write a test like this.
```Swift
let testStore = TestView().testStore(states: .init())
let testStore = TestView().testStore(viewState: .init())
await testStore.send(.increment) {
$0.count = 1
}

let testStore = TestView().testStore(states: .init())
let testStore = TestView().testStore(viewState: .init())
await testStore.send(.send)
await testStore.receive(.increment) {
$0.count = 1
Expand Down
2 changes: 1 addition & 1 deletion Sources/SimplexArchitecture/ActionSendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
/// A protocol for send actions to a store.
public protocol ActionSendable<Reducer> {
associatedtype Reducer: ReducerProtocol<Self>
associatedtype States: StatesProtocol
associatedtype ViewState: ViewStateProtocol

/// The store to which actions will be sent.
var store: Store<Reducer> { get }
Expand Down
6 changes: 3 additions & 3 deletions Sources/SimplexArchitecture/Internal/ActionTransition.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

/// ``ActionTransition`` represents a transition between states in a reducer. It captures the previous and next states, the associated side effect,effect context, and the action triggering the transition.
/// ``ActionTransition`` represents a transition between viewState in a reducer. It captures the previous and next viewState, the associated side effect,effect context, and the action triggering the transition.
struct ActionTransition<Reducer: ReducerProtocol> {
/// Represents a state. It includes the target state and the reducer state.
struct State {
let state: Reducer.Target.States?
let state: Reducer.Target.ViewState?
let reducerState: Reducer.ReducerState?
}

Expand Down Expand Up @@ -61,7 +61,7 @@ struct ActionTransition<Reducer: ReducerProtocol> {
) -> StateContainer<Reducer.Target> {
.init(
target,
states: state.state,
viewState: state.state,
reducerState: state.reducerState
)
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/SimplexArchitecture/Macros.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// Macro to create States structure by extracting properties to which property wrappers such as @State, @Binding @Published, etc. are applied.
/// Macro to create ViewState structure by extracting properties to which property wrappers such as @State, @Binding @Published, etc. are applied.
///
/// It is conformed to the `ActionSendable` protocol by the `ScopeState` macro.
/// It is conformed to the `ActionSendable` protocol by the `ViewState` macro.
///
/// Here is a example code.
/// ```
/// @ScopeState
/// @ViewState
/// struct MyView: View {
/// let store: Store<MyReducer>
///
Expand Down Expand Up @@ -40,7 +40,7 @@
/// ```
/// Here is a sample code if you want to use ReducerState.
/// ```
/// @ScopeState
/// @ViewState
/// struct MyView: View {
/// let store: Store<MyReducer>
///
Expand Down Expand Up @@ -74,7 +74,7 @@
/// }
/// }
/// ```
@attached(member, names: named(States))
@attached(member, names: named(ViewState))
@attached(extension, conformances: ActionSendable)
public macro ScopeState() =
#externalMacro(module: "SimplexArchitectureMacrosPlugin", type: "ScopeState")
public macro ViewState() =
#externalMacro(module: "SimplexArchitectureMacrosPlugin", type: "ViewStateMacro")
4 changes: 2 additions & 2 deletions Sources/SimplexArchitecture/Reducer/Pullbackable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
///
/// Here is a sample code.
/// ```
/// @ScopeState
/// @ViewState
/// struct ParentView: View {
/// let store: Store<ParentReducer> = Store(reducer: ParentReducer())
///
Expand All @@ -29,7 +29,7 @@ import Foundation
/// }
/// }
///
/// @ScopeState
/// @ViewState
/// struct ChildView: View, ActionSendable {
/// let store: Store<ChildReducer> = Store(reducer: ChildReducer())
///
Expand Down
6 changes: 3 additions & 3 deletions Sources/SimplexArchitecture/Reducer/ReducerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// }
/// }
///
/// @ScopeState
/// @ViewState
/// struct MyView: View {
/// @State var counter = 0
/// let store = Store(reducer: MyReducer())
Expand Down Expand Up @@ -64,7 +64,7 @@
/// }
/// }
///
/// @ScopeState
/// @ViewState
/// struct MyView: View {
/// @State var counter = 0
///
Expand All @@ -89,7 +89,7 @@
/// ```
///
public protocol ReducerProtocol<Target> {
/// Target for the Reducer to change state, which must conform to ActionSendable and is automatically conformed to by the StoreBuilder or ScopeState macros
/// Target for the Reducer to change state, which must conform to ActionSendable and is automatically conformed to by the StoreBuilder or ViewState macros
associatedtype Target: ActionSendable<Self>
/// State used by Reducer. Since the View is not update when the value of ReducerState is changed, it is used for the purpose of improving performance, etc.
/// The default is Never.
Expand Down
18 changes: 9 additions & 9 deletions Sources/SimplexArchitecture/StateContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ public final class StateContainer<Target: ActionSendable> {

var _reducerState: Target.Reducer.ReducerState?
var entity: Target
@TestOnly var states: Target.States?
@TestOnly var viewState: Target.ViewState?

init(
_ entity: consuming Target,
states: Target.States? = nil,
viewState: Target.ViewState? = nil,
reducerState: consuming Target.Reducer.ReducerState? = nil
) {
self.entity = entity
self.states = states
self.viewState = viewState
self._reducerState = reducerState
}

public subscript<U>(dynamicMember keyPath: WritableKeyPath<Target.States, U>) -> U {
public subscript<U>(dynamicMember keyPath: WritableKeyPath<Target.ViewState, U>) -> U {
_read {
guard !_XCTIsTesting else {
yield states![keyPath: keyPath]
yield viewState![keyPath: keyPath]
return
}
if let viewKeyPath = Target.States.keyPathMap[keyPath] as? WritableKeyPath<Target, U> {
if let viewKeyPath = Target.ViewState.keyPathMap[keyPath] as? WritableKeyPath<Target, U> {
yield entity[keyPath: viewKeyPath]
} else {
fatalError()
}
}
_modify {
guard !_XCTIsTesting else {
yield &states![keyPath: keyPath]
yield &viewState![keyPath: keyPath]
return
}
if let viewKeyPath = Target.States.keyPathMap[keyPath] as? WritableKeyPath<Target, U> {
if let viewKeyPath = Target.ViewState.keyPathMap[keyPath] as? WritableKeyPath<Target, U> {
yield &entity[keyPath: viewKeyPath]
} else {
fatalError()
Expand All @@ -49,6 +49,6 @@ public final class StateContainer<Target: ActionSendable> {
}

func copy() -> Self {
Self(entity, states: states, reducerState: _reducerState)
Self(entity, viewState: viewState, reducerState: _reducerState)
}
}
4 changes: 2 additions & 2 deletions Sources/SimplexArchitecture/Store/Store+send.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ extension Store {
sideEffect = reduce(container, action)
sentFromEffectActions.append(
ActionTransition(
previous: .init(state: before.states, reducerState: before._reducerState),
next: .init(state: container.states, reducerState: before._reducerState),
previous: .init(state: before.viewState, reducerState: before._reducerState),
next: .init(state: container.viewState, reducerState: before._reducerState),
effect: sideEffect,
effectContext: effectContext,
for: action
Expand Down
8 changes: 4 additions & 4 deletions Sources/SimplexArchitecture/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ public final class Store<Reducer: ReducerProtocol> {

public func getContainer(
for target: Reducer.Target,
states: Reducer.Target.States? = nil
viewState: Reducer.Target.ViewState? = nil
) -> StateContainer<Reducer.Target> {
if let container {
container
} else {
StateContainer(target, states: states, reducerState: initialReducerState?())
StateContainer(target, viewState: viewState, reducerState: initialReducerState?())
}
}

@inlinable
@discardableResult
public func setContainerIfNeeded(
for target: Reducer.Target,
states: Reducer.Target.States? = nil
viewState: Reducer.Target.ViewState? = nil
) -> StateContainer<Reducer.Target> {
if let container {
return container
} else {
let container = getContainer(for: target, states: states)
let container = getContainer(for: target, viewState: viewState)
self.container = container
return container
}
Expand Down
Loading