VmRx.swift: Easy way to use MVVM on iOS
VmRx is an iOS framework inspired by Airbnb MvRx for Android.
This is what it looks like:
// State
struct TodoListState: State {
var todos: Async<[Todo]> = .uninitialized
}
// ViewModel
class TodoListViewModel: ViewModel<TodoListState> {
private let api: Api
init(api: Api) {
self.api = api
super.init(initialState: TodoListState())
}
func refreshTodos() {
api
.fetchTodos()
.execute(with: self) { (state, result) in
state.copy(withChanges: { $0.todos = result })
}
}
}
// Controller
class TodoListController: UIViewController {
var viewModel: ViewModel<TodoListState> = TodoListViewModel(api: Api.shared)
override func viewDidLoad() {
super.viewDidLoad()
viewModel.subscribe { [weak self] in self?.useState($0) }
viewModel.refreshTodos()
}
func useState(_ state: TodoListState) {
print(state)
}
}
Installation
CocoaPods
Just add to your project's Podfile
:
pod "RxSwift"
pod "VmRx"
Carthage
github "viniciussoares/VmRx.swift" ~> 1.0
Manually
To manual install this library you should get the Sources/*.swift
imported into your project, alongside RxSwift.
Requirements
VmRx requires Swift 4.2 and dedicated version of RxSwift (5.0.0).