SwiftMVI 是一个 swift 的开源库,包含可遵循协议和扩展,它提供了在 SwiftUI 中使用独立的 UseCases
构建 MVI 架构的能力。
SwiftMVI 提供以下特性
Reducibles
和 Reducers
构建多种变体。主要目标是一组协议,用于以最小的努力归档结构化数据流。但是,如果您熟悉 MVI 架构,与其他 MVI 实现相比,存在一些重要的差异
Processing
使用 state()
方法来改变实际状态,并且 reducer 始终返回 Void,如果您必须处理 effect,请使用 effect()
方法。ObservableObject
或 statePublisher
进行更新。bind
方法连接到 Feature
,并且当您的 feature 遵循 EventReducer
时,Feature
可以充当 Publisher
。您可以使用 Swift Package Manager 通过在您的 Package.swift 文件中添加以下依赖项或直接在 Xcode 中添加来集成该库
.package(url: "https://github.com/xtro/SwiftMVI.git", .upToNextMajor(from: "0.2.0"))
Feature(特性)
class CounterFeature: ReducibleState, IntentReducer, Processing {
var state: Int
var statePublisher: StatePublisher
init(state: Int = 0) {
self.state = state
self.statePublisher = .init(state)
}
enum Intent {
case increment
case decrement
}
func reduce(intent: Intent) {
switch intent {
case .increment:
state {
$0 + 1
}
case .decrement:
state {
$0 - 1
}
}
}
}
View(视图)
struct CounterView: FeatureView {
let feature: CounterFeature
func body(_ newState: Int) -> some View {
VStack {
HStack {
Button("−") { feature(.decrement) }
Text("\(newState)")
Button("+") { feature(.increment) }
}
}
}
}
SwiftMVI 是一个 MIT 许可的开源项目,其持续开发完全由出色的支持者的支持成为可能。如果您想加入他们,请考虑赞助此开发。
欢迎提交 Pull Request。对于重大更改,请先打开一个 issue 讨论您想要更改的内容。
此库根据 MIT 许可证发布。有关详细信息,请参阅 LICENSE。