RDXVM is another MVVM implementation inspired by Redux and ReactorKit.
Example
To run the example project, clone the repo, and run pod install from the Example directory first.
Requirements
Swift 5
iOS 9
Depencencies
RxCocoa (~> 6.0)
RxRelay (~> 6.0)
RxSwift (~> 6.0)
Installation
RDXVM is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'RDXVM'
Usage
ViewModel needs custom types for action, mutation, event, and state to subclass and create an instance.
enum Action {
case add(Int)
case subtract(Int)
}
enum Mutation {
case add(Int)
case calculating(Bool)
}
enum Event {
case notSupported
}
struct State {
var sum = 0
var calculating = false
}
You can subclass ViewModel with these types and must override react(action:state:) and reduce(mutation:state:) methods.
In react(action:state:, you should return an observable of Reaction:
a mutation to mutate state as the result of an action
an event to notify some event to view
an error that is explicit/implicit thrown
an action that is mapped from another action.
class CalcViewModel: ViewModel<Action, Mutation, State, Event> {
init(state: State = State()) {
super.init(state: state)
}
override func react(action: Action, state: State) -> Observable<Reaction> {
switch action {
case .add(let num):
return .of(.mutation(.calculating(true)),
.mutation(.add(num)),
.mutation(.calculating(false)))
case .subtract:
return .just(.event(.notSupported))
}
}
override func reduce(mutation: Mutation, state: inout State) {
switch mutation {
case let .add(let num):
state.sum += num
case let .calculating(let calculating):
state.calculating = calculating
}
}
}
let vm = CalcViewModel<Action, Mutation, Event, State>()
Send actions to ViewModel and get outputs(event, error, state) from ViewModel.
RDXVM
RDXVM is another MVVM implementation inspired by Redux and ReactorKit.
Example
To run the example project, clone the repo, and run
pod installfrom the Example directory first.Requirements
Depencencies
Installation
RDXVM is available through CocoaPods. To install it, simply add the following line to your Podfile:
Usage
ViewModel needs custom types for action, mutation, event, and state to subclass and create an instance.
You can subclass ViewModel with these types and must override
react(action:state:)andreduce(mutation:state:)methods. Inreact(action:state:, you should return an observable of Reaction:Send actions to ViewModel and get outputs(event, error, state) from ViewModel.
You can get current value of the state or property of the state.