Verge is a state management library for iOS (UIKit / SwiftUI).Mostly it's based on Flux architecture.Flux architecture is so beautiful and simplified thinking.Although, we need to do several tuning to bring it into a real product.In fact, Flux needs to consider about computing resources.
Verge contains several ideas to do this from Web technologies such as Redux, Vuex, and Recoil.They have very useful techniques to be successful in real-world productions based on the core-concept of Flux.
Verge can be setting it up quickly, and tune performance up when we need it.Verge automatically tune-up as possible and shows us what makes performance badly while development from Xcode's documentation.
A way to create a ViewModel
final class MyViewModel: StoreComponentType {
struct State {
var name: String = ""
var count: Int = 0
}
let store: DefaultStore = .init(initialState: .init())
func myAction() {
commit {
$0.name = "Hello, Verge"
}
}
func increment() {
commit {
$0.count += 1
}
}
}
A way to create a customized store
struct MyState {
var name: String = ""
var count: Int = 0
}
final class MyStore: Store<MyState, Never> {
func myAction() {
commit {
$0.name = "Hello, Verge"
}
}
func increment() {
commit {
$0.count += 1
}
}
}
struct MyView: View {
let store: MyViewModel
var body: some View {
StateReader(store).content { state in
Text(state.name)
Button(action: {
self.store.myAction()
}) {
Text("Action")
}
}
}
}
final class MyViewController: UIViewController {
let viewModel: MyViewModel
...
var cancellable: VergeAnyCancellable?
init(viewModel: MyViewModel) {
self.viewModel = viewModel
self.cancellable = viewModel.sinkState { [weak self] state in
self?.update(state: state)
}
}
private func update(state: Changes<MyStore.State>) {
state.ifChanged(\\.name) { (name) in
nameLabel.text = name
}
state.ifChanged(\\.count) { (age) in
countLabel.text = age.description
}
...
}
}