RedUxable
@MainActor
public protocol RedUxable
A protocol to help guide structuring a SwiftUI view to use RedUx.
The use of this protocol isn’t required in order to use RedUx. It can be used as a guide on how to setup RedUx.
An example of this may look like:
import RedUx
import SwiftUI
struct RootScreen: View, RedUxable {
typealias LocalState = AppState
typealias LocalEvent = AppEvent
typealias LocalEnvironment = AppEnvironment
let store: LocalStore
@StateObject var viewModel: LocalViewModel
// MARK: Initialization
init(store: LocalStore, viewModel: LocalViewModel) {
self.store = store
self._viewModel = .init(wrappedValue: viewModel)
}
// MARK: Body
var body: some View {
VStack(alignment: .center) {
Text(verbatim: .init(self.viewModel.count))
.font(.largeTitle)
HStack {
Button("Decrement") {
self.viewModel.send(.decrement)
}
.buttonStyle(.bordered)
Button("Increment") {
self.viewModel.send(.increment)
}
.buttonStyle(.bordered)
Button("Delayed increment") {
self.viewModel.send(.incrementWithDelay)
}
.buttonStyle(.bordered)
}
}
}
}
// MARK: Preview
struct RootScreen_Previews: PreviewProvider {
static var previews: some View {
RootScreen.mock(
state: .init(
count: 0
),
environment: .mock
)
}
}
-
The local state type.
Declaration
Swift
associatedtype LocalState : Equatable
-
The local event type.
Declaration
Swift
associatedtype LocalEvent
-
The local state type.
Declaration
Swift
associatedtype LocalEnvironment
-
The local store.
Declaration
Swift
typealias LocalStore = Store<LocalState, LocalEvent, LocalEnvironment>
-
The local view model
Declaration
Swift
typealias LocalViewModel = ViewModel<LocalState, LocalEvent>
-
make(store:
Default implementation) Create a “live” RedUxable view with a store and view model.
Default Implementation
Create a “live” RedUxable view with a store.
Declaration
Swift
static func make(store: LocalStore) -> Self
Parameters
store
The store.
Return Value
A view.
-
mock(state:
Default implementationenvironment: ) Create a “mock” RedUxable view.
Default Implementation
Create a “mock” RedUxable view.
This function will create a store that uses the state and environment object passed and an
empty
reducer.Declaration
Swift
static func mock( state: LocalState, environment: LocalEnvironment ) -> Self
Parameters
state
The state.
environment
The environment.
Return Value
A view.
-
The store
Declaration
Swift
var store: LocalStore { get }
-
The view model
Declaration
Swift
var viewModel: LocalViewModel { get }
-
Initialize a new RedUxable view.
Declaration
Swift
init(store: LocalStore, viewModel: LocalViewModel)