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
// TODO